Skip to content

Commit 26d3103

Browse files
committed
ast: Add new Kind enums for more precise downcasting
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.
1 parent 43369f2 commit 26d3103

11 files changed

+229
-55
lines changed

gcc/rust/ast/rust-ast.h

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,13 @@ namespace AST {
7070
class ASTVisitor;
7171
using AttrVec = std::vector<Attribute>;
7272

73-
// The available kinds of AST Nodes
74-
enum class Kind
75-
{
76-
UNKNOWN,
77-
MODULE,
78-
MACRO_RULES_DEFINITION,
79-
MACRO_INVOCATION,
80-
IDENTIFIER,
81-
};
82-
8373
class Visitable
8474
{
8575
public:
8676
virtual ~Visitable () = default;
8777
virtual void accept_vis (ASTVisitor &vis) = 0;
8878
};
8979

90-
// Abstract base class for all AST elements
91-
class Node : public Visitable
92-
{
93-
public:
94-
/**
95-
* Get the kind of Node this is. This is used to differentiate various AST
96-
* elements with very little overhead when extracting the derived type
97-
* through static casting is not necessary.
98-
*/
99-
// FIXME: Mark this as `= 0` in the future to make sure every node
100-
// implements it
101-
virtual Kind get_ast_kind () const { return Kind::UNKNOWN; }
102-
};
103-
10480
// Delimiter types - used in macros and whatever.
10581
enum DelimType
10682
{
@@ -1092,7 +1068,7 @@ class MetaListNameValueStr;
10921068
/* Base statement abstract class. Note that most "statements" are not allowed
10931069
* in top-level module scope - only a subclass of statements called "items"
10941070
* are. */
1095-
class Stmt : public Node
1071+
class Stmt : public Visitable
10961072
{
10971073
public:
10981074
enum class Kind
@@ -1141,6 +1117,28 @@ class Stmt : public Node
11411117
class Item : public Stmt
11421118
{
11431119
public:
1120+
enum class Kind
1121+
{
1122+
MacroRulesDefinition,
1123+
MacroInvocation,
1124+
Module,
1125+
ExternCrate,
1126+
UseDeclaration,
1127+
Function,
1128+
TypeAlias,
1129+
Struct,
1130+
EnumItem,
1131+
Enum,
1132+
Union,
1133+
ConstantItem,
1134+
StaticItem,
1135+
Trait,
1136+
Impl,
1137+
ExternBlock,
1138+
};
1139+
1140+
virtual Kind get_item_kind () const = 0;
1141+
11441142
// Unique pointer custom clone function
11451143
std::unique_ptr<Item> clone_item () const
11461144
{
@@ -1221,14 +1219,54 @@ class VisItem : public Item
12211219
{
12221220
return outer_attrs;
12231221
}
1222+
1223+
virtual Item::Kind get_item_kind () const override = 0;
12241224
};
1225+
12251226
// forward decl of ExprWithoutBlock
12261227
class ExprWithoutBlock;
12271228

12281229
// Base expression AST node - abstract
1229-
class Expr : public Node
1230+
class Expr : public Visitable
12301231
{
12311232
public:
1233+
enum class Kind
1234+
{
1235+
PathInExpression,
1236+
QualifiedPathInExpression,
1237+
Literal,
1238+
Operator,
1239+
Grouped,
1240+
Array,
1241+
ArrayIndex,
1242+
Tuple,
1243+
TupleIndex,
1244+
Struct,
1245+
Call,
1246+
MethodCall,
1247+
FieldAccess,
1248+
Closure,
1249+
Block,
1250+
Continue,
1251+
Break,
1252+
Range,
1253+
Box,
1254+
Return,
1255+
UnsafeBlock,
1256+
Loop,
1257+
If,
1258+
IfLet,
1259+
Match,
1260+
Await,
1261+
AsyncBlock,
1262+
InlineAsm,
1263+
Identifier,
1264+
FormatArgs,
1265+
MacroInvocation,
1266+
};
1267+
1268+
virtual Kind get_expr_kind () const = 0;
1269+
12321270
// Unique pointer custom clone function
12331271
std::unique_ptr<Expr> clone_expr () const
12341272
{
@@ -1343,7 +1381,7 @@ class IdentifierExpr : public ExprWithoutBlock
13431381
outer_attrs = std::move (new_attrs);
13441382
}
13451383

1346-
Kind get_ast_kind () const override { return Kind::IDENTIFIER; }
1384+
Expr::Kind get_expr_kind () const override { return Expr::Kind::Identifier; }
13471385

13481386
protected:
13491387
// Clone method implementation
@@ -1410,7 +1448,7 @@ class Pattern : public Visitable
14101448
class TraitBound;
14111449

14121450
// Base class for types as represented in AST - abstract
1413-
class Type : public Node
1451+
class Type : public Visitable
14141452
{
14151453
public:
14161454
// Unique pointer custom clone function

gcc/rust/ast/rust-builtin-ast-nodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class FormatArgs : public Expr
202202
const FormatArguments &get_arguments () const { return arguments; }
203203
virtual location_t get_locus () const override;
204204

205+
Expr::Kind get_expr_kind () const override { return Expr::Kind::FormatArgs; }
206+
205207
private:
206208
location_t loc;
207209
// FIXME: This probably needs to be a separate type - it is one in rustc's

0 commit comments

Comments
 (0)