Skip to content

Commit 14b99be

Browse files
committed
ast: Add base Node class with get_ast_kind() function
This adds a new base class common to all abstract base classes of the AST: We can use it to store information shared by all nodes, such as the newly introduced `AST::Kind` which helps in differentiating nodes. We could also consider using it to store location info, since all AST nodes probably need it.
1 parent 2dfc196 commit 14b99be

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

gcc/rust/ast/rust-ast.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ namespace AST {
3636
class ASTVisitor;
3737
using AttrVec = std::vector<Attribute>;
3838

39+
// The available kinds of AST Nodes
40+
enum Kind
41+
{
42+
UNKNOWN,
43+
MACRO_RULES_DEFINITION,
44+
};
45+
46+
// Abstract base class for all AST elements
47+
class Node
48+
{
49+
public:
50+
/**
51+
* Get the kind of Node this is. This is used to differentiate various AST
52+
* elements with very little overhead when extracting the derived type through
53+
* static casting is not necessary.
54+
*/
55+
// FIXME: Mark this as `= 0` in the future to make sure every node implements
56+
// it
57+
virtual Kind get_ast_kind () const { return Kind::UNKNOWN; }
58+
};
59+
3960
// Delimiter types - used in macros and whatever.
4061
enum DelimType
4162
{
@@ -813,7 +834,7 @@ class MetaListNameValueStr;
813834

814835
/* Base statement abstract class. Note that most "statements" are not allowed in
815836
* top-level module scope - only a subclass of statements called "items" are. */
816-
class Stmt
837+
class Stmt : public Node
817838
{
818839
public:
819840
// Unique pointer custom clone function

gcc/rust/ast/rust-macro.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ class MacroRulesDefinition : public MacroItem
441441
is_builtin_rule = true;
442442
}
443443

444+
Kind get_ast_kind () const override { return Kind::MACRO_RULES_DEFINITION; }
445+
444446
protected:
445447
/* Use covariance to implement clone function as returning this object rather
446448
* than base */

0 commit comments

Comments
 (0)