Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: Add new Kind enums for more precise downcasting #3320

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 66 additions & 28 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,13 @@ namespace AST {
class ASTVisitor;
using AttrVec = std::vector<Attribute>;

// The available kinds of AST Nodes
enum class Kind
{
UNKNOWN,
MODULE,
MACRO_RULES_DEFINITION,
MACRO_INVOCATION,
IDENTIFIER,
};

class Visitable
{
public:
virtual ~Visitable () = default;
virtual void accept_vis (ASTVisitor &vis) = 0;
};

// Abstract base class for all AST elements
class Node : public Visitable
{
public:
/**
* Get the kind of Node this is. This is used to differentiate various AST
* elements with very little overhead when extracting the derived type
* through static casting is not necessary.
*/
// FIXME: Mark this as `= 0` in the future to make sure every node
// implements it
virtual Kind get_ast_kind () const { return Kind::UNKNOWN; }
};

// Delimiter types - used in macros and whatever.
enum DelimType
{
Expand Down Expand Up @@ -1092,7 +1068,7 @@ class MetaListNameValueStr;
/* Base statement abstract class. Note that most "statements" are not allowed
* in top-level module scope - only a subclass of statements called "items"
* are. */
class Stmt : public Node
class Stmt : public Visitable
{
public:
enum class Kind
Expand Down Expand Up @@ -1141,6 +1117,28 @@ class Stmt : public Node
class Item : public Stmt
{
public:
enum class Kind
{
MacroRulesDefinition,
MacroInvocation,
Module,
ExternCrate,
UseDeclaration,
Function,
TypeAlias,
Struct,
EnumItem,
Enum,
Union,
ConstantItem,
StaticItem,
Trait,
Impl,
ExternBlock,
};

virtual Kind get_item_kind () const = 0;

// Unique pointer custom clone function
std::unique_ptr<Item> clone_item () const
{
Expand Down Expand Up @@ -1221,14 +1219,54 @@ class VisItem : public Item
{
return outer_attrs;
}

virtual Item::Kind get_item_kind () const override = 0;
};

// forward decl of ExprWithoutBlock
class ExprWithoutBlock;

// Base expression AST node - abstract
class Expr : public Node
class Expr : public Visitable
{
public:
enum class Kind
{
PathInExpression,
QualifiedPathInExpression,
Literal,
Operator,
Grouped,
Array,
ArrayIndex,
Tuple,
TupleIndex,
Struct,
Call,
MethodCall,
FieldAccess,
Closure,
Block,
Continue,
Break,
Range,
Box,
Return,
UnsafeBlock,
Loop,
If,
IfLet,
Match,
Await,
AsyncBlock,
InlineAsm,
Identifier,
FormatArgs,
MacroInvocation,
};

virtual Kind get_expr_kind () const = 0;

// Unique pointer custom clone function
std::unique_ptr<Expr> clone_expr () const
{
Expand Down Expand Up @@ -1343,7 +1381,7 @@ class IdentifierExpr : public ExprWithoutBlock
outer_attrs = std::move (new_attrs);
}

Kind get_ast_kind () const override { return Kind::IDENTIFIER; }
Expr::Kind get_expr_kind () const override { return Expr::Kind::Identifier; }

protected:
// Clone method implementation
Expand Down Expand Up @@ -1410,7 +1448,7 @@ class Pattern : public Visitable
class TraitBound;

// Base class for types as represented in AST - abstract
class Type : public Node
class Type : public Visitable
{
public:
// Unique pointer custom clone function
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-builtin-ast-nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class FormatArgs : public Expr
const FormatArguments &get_arguments () const { return arguments; }
virtual location_t get_locus () const override;

Expr::Kind get_expr_kind () const override { return Expr::Kind::FormatArgs; }

private:
location_t loc;
// FIXME: This probably needs to be a separate type - it is one in rustc's
Expand Down
Loading
Loading