summaryrefslogtreecommitdiff
path: root/source/slang/decl-defs.h
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2019-01-22 14:57:25 -0800
committerGitHub <noreply@github.com>2019-01-22 14:57:25 -0800
commita08a3140716b89146bf0a22dc014c5470e90e910 (patch)
tree8033fe6e7bb47dce2a1bca3bb767e6ac7329afb9 /source/slang/decl-defs.h
parenta005007c2e9132788c92aa5a9c5fed2cb90f7841 (diff)
Clean up variable declaration class hierarchy (#787)
The AST class hierarchy for variable declarations had a few messy bits. First, there are more subclasses of `VarDeclBase` than seem strictly necessary; especially for stuff like `struct` member variable which use `StructField` even for `static` fields (which are effectively globals). Second, the AST node type for the "cases" within an `enum` was made a subclass of `VarDeclBase` for expediency, but this isn't really semantically accurate (and doesn't seem to be paying off much in deduplication of code). This change tries to address both of those problems. First, we replace the existing `Variable` and `StructField` cases with a single `VarDecl` case that covers globals, locals, and member variables. I haven't gone so far as to replace function parameters or generic value parameters, but that might be worth considering as a further clean-up. Second, we change `EnumCaseDecl` to inherit directly from `Decl` instead of `VarDeclBase` and add an explicit case for handling them where they were previously handled as if they were variable declarations (this was done by manually surveying all locations in the code that referenced `VarDeclBase`).
Diffstat (limited to 'source/slang/decl-defs.h')
-rw-r--r--source/slang/decl-defs.h38
1 files changed, 29 insertions, 9 deletions
diff --git a/source/slang/decl-defs.h b/source/slang/decl-defs.h
index 502412b4b..076db1188 100644
--- a/source/slang/decl-defs.h
+++ b/source/slang/decl-defs.h
@@ -33,7 +33,7 @@ ABSTRACT_SYNTAX_CLASS(ContainerDecl, Decl)
)
END_SYNTAX_CLASS()
-// Base class for all variable-like declarations
+// Base class for all variable declarations
ABSTRACT_SYNTAX_CLASS(VarDeclBase, Decl)
// type of the variable
@@ -47,9 +47,9 @@ ABSTRACT_SYNTAX_CLASS(VarDeclBase, Decl)
SYNTAX_FIELD(RefPtr<Expr>, initExpr)
END_SYNTAX_CLASS()
-
-// A field of a `struct` type
-SIMPLE_SYNTAX_CLASS(StructField, VarDeclBase)
+// Ordinary potentially-mutable variables (locals, globals, and member variables)
+SYNTAX_CLASS(VarDecl, VarDeclBase)
+END_SYNTAX_CLASS()
// An `AggTypeDeclBase` captures the shared functionality
// between true aggregate type declarations and extension
@@ -76,9 +76,9 @@ ABSTRACT_SYNTAX_CLASS(AggTypeDecl, AggTypeDeclBase)
RAW(
// extensions that might apply to this declaration
ExtensionDecl* candidateExtensions = nullptr;
- FilteredMemberList<StructField> GetFields()
+ FilteredMemberList<VarDecl> GetFields()
{
- return getMembersOfType<StructField>();
+ return getMembersOfType<VarDecl>();
}
)
END_SYNTAX_CLASS()
@@ -98,7 +98,28 @@ RAW(
)
END_SYNTAX_CLASS()
-SIMPLE_SYNTAX_CLASS(EnumCaseDecl, VarDeclBase)
+// A single case in an enum.
+//
+// E.g., in a declaration like:
+//
+// enum Color { Red = 0, Green, Blue };
+//
+// The `Red = 0` is the declaration of the `Red`
+// case, with `0` as an explicit expression for its
+// _tag value_.
+//
+SYNTAX_CLASS(EnumCaseDecl, Decl)
+
+ // type of the parent `enum`
+ SYNTAX_FIELD(TypeExp, type)
+
+ RAW(
+ Type* getType() { return type.type.Ptr(); }
+ )
+
+ // Tag value
+ SYNTAX_FIELD(RefPtr<Expr>, tagExpr)
+END_SYNTAX_CLASS()
// An interface which other types can conform to
SIMPLE_SYNTAX_CLASS(InterfaceDecl, AggTypeDecl)
@@ -156,6 +177,7 @@ END_SYNTAX_CLASS()
// A scope for local declarations (e.g., as part of a statement)
SIMPLE_SYNTAX_CLASS(ScopeDecl, ContainerDecl)
+// A function/initializer/subscript parameter (potentially mutable)
SIMPLE_SYNTAX_CLASS(ParamDecl, VarDeclBase)
// Base class for things that have parameter lists and can thus be applied to arguments ("called")
@@ -205,8 +227,6 @@ SIMPLE_SYNTAX_CLASS(RefAccessorDecl, AccessorDecl)
SIMPLE_SYNTAX_CLASS(FuncDecl, FunctionDeclBase)
-SIMPLE_SYNTAX_CLASS(Variable, VarDeclBase);
-
// A "module" of code (essentiately, a single translation unit)
// that provides a scope for some number of declarations.
SYNTAX_CLASS(ModuleDecl, ContainerDecl)