From a08a3140716b89146bf0a22dc014c5470e90e910 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 22 Jan 2019 14:57:25 -0800 Subject: 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`). --- source/slang/parser.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'source/slang/parser.cpp') diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 1afc8ebcf..a21d57b05 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -1322,17 +1322,17 @@ namespace Slang static RefPtr CreateVarDeclForContext( ContainerDecl* containerDecl ) { - if (dynamic_cast(containerDecl) || dynamic_cast(containerDecl)) - { - return new StructField(); - } - else if (dynamic_cast(containerDecl)) + if (dynamic_cast(containerDecl)) { + // Function parameters always use their dedicated syntax class. + // return new ParamDecl(); } else { - return new Variable(); + // Globals, locals, and member variables all use the same syntax class. + // + return new VarDecl(); } } @@ -2227,7 +2227,7 @@ namespace Slang // The first is a type declaration that holds all the members, while // the second is a variable declaration that uses the buffer type. RefPtr bufferDataTypeDecl = new StructDecl(); - RefPtr bufferVarDecl = new Variable(); + RefPtr bufferVarDecl = new VarDecl(); // Both declarations will have a location that points to the name parser->FillPosition(bufferDataTypeDecl.Ptr()); @@ -2395,7 +2395,7 @@ namespace Slang // The first is a type declaration that holds all the members, while // the second is a variable declaration that uses the buffer type. RefPtr blockDataTypeDecl = new StructDecl(); - RefPtr blockVarDecl = new Variable(); + RefPtr blockVarDecl = new VarDecl(); addModifier(blockDataTypeDecl, new ImplicitParameterGroupElementTypeModifier()); addModifier(blockVarDecl, new ImplicitParameterGroupVariableModifier()); @@ -3135,7 +3135,7 @@ namespace Slang if(AdvanceIf(parser, TokenType::OpAssign)) { - decl->initExpr = parser->ParseArgExpr(); + decl->tagExpr = parser->ParseArgExpr(); } return decl; @@ -3270,7 +3270,7 @@ namespace Slang parser->ReadToken(TokenType::LParent); NameLoc varNameAndLoc = expectIdentifier(parser); - RefPtr varDecl = new Variable(); + RefPtr varDecl = new VarDecl(); varDecl->nameAndLoc = varNameAndLoc; varDecl->loc = varNameAndLoc.loc; -- cgit v1.2.3