aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-03-17 10:21:32 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2022-03-17 10:21:32 +0100
commit14b99bed0801da460db998f8ec65f31e992f0e52 (patch)
tree1a5c899063a5e769e76d278d44aa3c2ef9006c4c /gcc
parent2dfc19647774cb26a0f735bda8006068a40cfba0 (diff)
downloadgcc-14b99bed0801da460db998f8ec65f31e992f0e52.zip
gcc-14b99bed0801da460db998f8ec65f31e992f0e52.tar.gz
gcc-14b99bed0801da460db998f8ec65f31e992f0e52.tar.bz2
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast.h23
-rw-r--r--gcc/rust/ast/rust-macro.h2
2 files changed, 24 insertions, 1 deletions
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index d07501e..6587142 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -36,6 +36,27 @@ namespace AST {
class ASTVisitor;
using AttrVec = std::vector<Attribute>;
+// The available kinds of AST Nodes
+enum Kind
+{
+ UNKNOWN,
+ MACRO_RULES_DEFINITION,
+};
+
+// Abstract base class for all AST elements
+class Node
+{
+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
{
@@ -813,7 +834,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
+class Stmt : public Node
{
public:
// Unique pointer custom clone function
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index bf07dd0..4adf467 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -441,6 +441,8 @@ public:
is_builtin_rule = true;
}
+ Kind get_ast_kind () const override { return Kind::MACRO_RULES_DEFINITION; }
+
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */