Skip to content

Commit

Permalink
ast: Add new Kind enums for more precise downcasting
Browse files Browse the repository at this point in the history
This commit adds things like Item::Kind, Expr::Kind, etc, and implements the associated `get_*_kind` functions.
It also removes the more generic AST::Kind enum we were using, which was incomplete and painful to use.

gcc/rust/ChangeLog:

	* ast/rust-ast.h: Add new Kind enums, remove Node class.
	* ast/rust-builtin-ast-nodes.h: Use new Kind enums.
	* ast/rust-expr.h (class LoopLabel): Likewise.
	* ast/rust-item.h: Likewise.
	* ast/rust-macro.h: Likewise.
	* ast/rust-path.h: Likewise.
	* expand/rust-macro-builtins-helpers.cc: Likewise.
	* expand/rust-macro-builtins-utility.cc (MacroBuiltin::concat_handler): Likewise.
	(MacroBuiltin::stringify_handler): Likewise.
	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Likewise.
	* resolve/rust-early-name-resolver.cc: Likewise.
	* hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Likewise.
  • Loading branch information
CohenArthur committed Dec 22, 2024
1 parent b5c354d commit bb48ede
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 55 deletions.
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

0 comments on commit bb48ede

Please sign in to comment.