From 56b44cbf582fac32e31601fd2a7ae1d6cb8f71b2 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 7 Jul 2017 09:57:26 -0700 Subject: Fix up visitor approach. The existing code used a catch-all `visit()` method, and then relied on overloading to find the right version (allowing fallback to a `visit()` method taking a base-class parameter). This approach works, but has some big down-sides: - When browsing the code, you have a bunch of identically-named methods, and it can be hard to find the one you want. - It is impossible to use inheritance to implement fallback for `visit()` methods, because *any* method in the derived class with that name hides *all* methods with the same name in a base class This change makes the `visit()` methods use the name of the corresponding syntax class, and then has visitors inherit the fallback methods they need from the base visitor template class. --- source/slang/lower.cpp | 111 +++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 55 deletions(-) (limited to 'source/slang/lower.cpp') diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index f9bf97107..8bc9619f3 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -76,6 +76,7 @@ struct StructuralTransformVisitorBase } }; +#if 0 template struct StructuralTransformStmtVisitor : StructuralTransformVisitorBase @@ -107,6 +108,7 @@ struct StructuralTransformStmtVisitor #include "object-meta-end.h" }; +#endif template RefPtr structuralTransform( @@ -130,7 +132,7 @@ struct StructuralTransformExprVisitor #define SYNTAX_CLASS(NAME, BASE, ...) \ - RefPtr visit(NAME* obj) { \ + RefPtr visit##NAME(NAME* obj) { \ RefPtr result = new NAME(*obj); \ transformFields(result, obj); \ return result; \ @@ -216,8 +218,7 @@ struct LoweringVisitor : ExprVisitor> , StmtVisitor , DeclVisitor> - , TypeVisitor> - , ValVisitor> + , ValVisitor, RefPtr> { // SharedLoweringContext* shared; @@ -247,12 +248,12 @@ struct LoweringVisitor return ValVisitor::dispatch(val); } - RefPtr visit(GenericParamIntVal* val) + RefPtr visitGenericParamIntVal(GenericParamIntVal* val) { return new GenericParamIntVal(translateDeclRef(DeclRef(val->declRef)).As()); } - RefPtr visit(ConstantIntVal* val) + RefPtr visitConstantIntVal(ConstantIntVal* val) { return val; } @@ -276,40 +277,40 @@ struct LoweringVisitor return result; } - RefPtr visit(ErrorType* type) + RefPtr visitErrorType(ErrorType* type) { return type; } - RefPtr visit(OverloadGroupType* type) + RefPtr visitOverloadGroupType(OverloadGroupType* type) { return type; } - RefPtr visit(InitializerListType* type) + RefPtr visitInitializerListType(InitializerListType* type) { return type; } - RefPtr visit(GenericDeclRefType* type) + RefPtr visitGenericDeclRefType(GenericDeclRefType* type) { return new GenericDeclRefType(translateDeclRef(DeclRef(type->declRef)).As()); } - RefPtr visit(FuncType* type) + RefPtr visitFuncType(FuncType* type) { RefPtr loweredType = new FuncType(); loweredType->declRef = translateDeclRef(DeclRef(type->declRef)).As(); return loweredType; } - RefPtr visit(DeclRefType* type) + RefPtr visitDeclRefType(DeclRefType* type) { auto loweredDeclRef = translateDeclRef(type->declRef); return DeclRefType::Create(loweredDeclRef); } - RefPtr visit(NamedExpressionType* type) + RefPtr visitNamedExpressionType(NamedExpressionType* type) { if (shared->target == CodeGenTarget::GLSL) { @@ -320,12 +321,12 @@ struct LoweringVisitor return new NamedExpressionType(translateDeclRef(DeclRef(type->declRef)).As()); } - RefPtr visit(TypeType* type) + RefPtr visitTypeType(TypeType* type) { return new TypeType(lowerType(type->type)); } - RefPtr visit(ArrayExpressionType* type) + RefPtr visitArrayExpressionType(ArrayExpressionType* type) { RefPtr loweredType = new ArrayExpressionType(); loweredType->BaseType = lowerType(type->BaseType); @@ -350,7 +351,7 @@ struct LoweringVisitor } // catch-all - RefPtr visit( + RefPtr visitExpressionSyntaxNode( ExpressionSyntaxNode* expr) { return structuralTransform(expr, this); @@ -404,7 +405,7 @@ struct LoweringVisitor return result; } - RefPtr visit( + RefPtr visitVarExpressionSyntaxNode( VarExpressionSyntaxNode* expr) { // If the expression didn't get resolved, we can leave it as-is @@ -428,7 +429,7 @@ struct LoweringVisitor return loweredExpr; } - RefPtr visit( + RefPtr visitMemberExpressionSyntaxNode( MemberExpressionSyntaxNode* expr) { auto loweredBase = lowerExpr(expr->BaseExpression); @@ -521,7 +522,7 @@ struct LoweringVisitor StmtVisitor::dispatch(stmt); } - RefPtr visit(ScopeDecl* decl) + RefPtr visitScopeDecl(ScopeDecl* decl) { RefPtr loweredDecl = new ScopeDecl(); lowerDeclCommon(loweredDecl, decl); @@ -594,7 +595,7 @@ struct LoweringVisitor addStmt(stmt); } - void visit(BlockStmt* stmt) + void visitBlockStmt(BlockStmt* stmt) { RefPtr loweredStmt = new BlockStmt(); lowerScopeStmtFields(loweredStmt, stmt); @@ -606,7 +607,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(SeqStmt* stmt) + void visitSeqStmt(SeqStmt* stmt) { for( auto ss : stmt->stmts ) { @@ -614,12 +615,12 @@ struct LoweringVisitor } } - void visit(ExpressionStatementSyntaxNode* stmt) + void visitExpressionStatementSyntaxNode(ExpressionStatementSyntaxNode* stmt) { addExprStmt(lowerExpr(stmt->Expression)); } - void visit(VarDeclrStatementSyntaxNode* stmt) + void visitVarDeclrStatementSyntaxNode(VarDeclrStatementSyntaxNode* stmt) { DeclVisitor::dispatch(stmt->decl); } @@ -651,42 +652,42 @@ struct LoweringVisitor loweredStmt->parentStmt = translateStmtRef(originalStmt->parentStmt); } - void visit(ContinueStatementSyntaxNode* stmt) + void visitContinueStatementSyntaxNode(ContinueStatementSyntaxNode* stmt) { RefPtr loweredStmt = new ContinueStatementSyntaxNode(); lowerChildStmtFields(loweredStmt, stmt); addStmt(loweredStmt); } - void visit(BreakStatementSyntaxNode* stmt) + void visitBreakStatementSyntaxNode(BreakStatementSyntaxNode* stmt) { RefPtr loweredStmt = new BreakStatementSyntaxNode(); lowerChildStmtFields(loweredStmt, stmt); addStmt(loweredStmt); } - void visit(DefaultStmt* stmt) + void visitDefaultStmt(DefaultStmt* stmt) { RefPtr loweredStmt = new DefaultStmt(); lowerChildStmtFields(loweredStmt, stmt); addStmt(loweredStmt); } - void visit(DiscardStatementSyntaxNode* stmt) + void visitDiscardStatementSyntaxNode(DiscardStatementSyntaxNode* stmt) { RefPtr loweredStmt = new DiscardStatementSyntaxNode(); lowerStmtFields(loweredStmt, stmt); addStmt(loweredStmt); } - void visit(EmptyStatementSyntaxNode* stmt) + void visitEmptyStatementSyntaxNode(EmptyStatementSyntaxNode* stmt) { RefPtr loweredStmt = new EmptyStatementSyntaxNode(); lowerStmtFields(loweredStmt, stmt); addStmt(loweredStmt); } - void visit(UnparsedStmt* stmt) + void visitUnparsedStmt(UnparsedStmt* stmt) { RefPtr loweredStmt = new UnparsedStmt(); lowerStmtFields(loweredStmt, stmt); @@ -696,7 +697,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(CaseStmt* stmt) + void visitCaseStmt(CaseStmt* stmt) { RefPtr loweredStmt = new CaseStmt(); lowerChildStmtFields(loweredStmt, stmt); @@ -706,7 +707,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(IfStatementSyntaxNode* stmt) + void visitIfStatementSyntaxNode(IfStatementSyntaxNode* stmt) { RefPtr loweredStmt = new IfStatementSyntaxNode(); lowerStmtFields(loweredStmt, stmt); @@ -718,7 +719,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(SwitchStmt* stmt) + void visitSwitchStmt(SwitchStmt* stmt) { RefPtr loweredStmt = new SwitchStmt(); lowerScopeStmtFields(loweredStmt, stmt); @@ -732,7 +733,7 @@ struct LoweringVisitor } - void visit(ForStatementSyntaxNode* stmt) + void visitForStatementSyntaxNode(ForStatementSyntaxNode* stmt) { RefPtr loweredStmt = new ForStatementSyntaxNode(); lowerScopeStmtFields(loweredStmt, stmt); @@ -747,7 +748,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(WhileStatementSyntaxNode* stmt) + void visitWhileStatementSyntaxNode(WhileStatementSyntaxNode* stmt) { RefPtr loweredStmt = new WhileStatementSyntaxNode(); lowerScopeStmtFields(loweredStmt, stmt); @@ -760,7 +761,7 @@ struct LoweringVisitor addStmt(loweredStmt); } - void visit(DoWhileStatementSyntaxNode* stmt) + void visitDoWhileStatementSyntaxNode(DoWhileStatementSyntaxNode* stmt) { RefPtr loweredStmt = new DoWhileStatementSyntaxNode(); lowerScopeStmtFields(loweredStmt, stmt); @@ -805,7 +806,7 @@ struct LoweringVisitor assign(expr, createVarRef(expr->Position, varDecl)); } - void visit(ReturnStatementSyntaxNode* stmt) + void visitReturnStatementSyntaxNode(ReturnStatementSyntaxNode* stmt) { auto loweredStmt = new ReturnStatementSyntaxNode(); lowerStmtCommon(loweredStmt, stmt); @@ -1004,59 +1005,59 @@ struct LoweringVisitor // Catch-all - RefPtr visit(ModifierDecl*) + RefPtr visitModifierDecl(ModifierDecl*) { // should not occur in user code SLANG_UNEXPECTED("modifiers shouldn't occur in user code"); } - RefPtr visit(GenericValueParamDecl*) + RefPtr visitGenericValueParamDecl(GenericValueParamDecl*) { SLANG_UNEXPECTED("generics should be lowered to specialized decls"); } - RefPtr visit(GenericTypeParamDecl*) + RefPtr visitGenericTypeParamDecl(GenericTypeParamDecl*) { SLANG_UNEXPECTED("generics should be lowered to specialized decls"); } - RefPtr visit(GenericTypeConstraintDecl*) + RefPtr visitGenericTypeConstraintDecl(GenericTypeConstraintDecl*) { SLANG_UNEXPECTED("generics should be lowered to specialized decls"); } - RefPtr visit(GenericDecl*) + RefPtr visitGenericDecl(GenericDecl*) { SLANG_UNEXPECTED("generics should be lowered to specialized decls"); } - RefPtr visit(ProgramSyntaxNode*) + RefPtr visitProgramSyntaxNode(ProgramSyntaxNode*) { SLANG_UNEXPECTED("module decls should be lowered explicitly"); } - RefPtr visit(SubscriptDecl*) + RefPtr visitSubscriptDecl(SubscriptDecl*) { // We don't expect to find direct references to a subscript // declaration, but rather to the underlying accessors return nullptr; } - RefPtr visit(InheritanceDecl*) + RefPtr visitInheritanceDecl(InheritanceDecl*) { // We should deal with these explicitly, as part of lowering // the type that contains them. return nullptr; } - RefPtr visit(ExtensionDecl*) + RefPtr visitExtensionDecl(ExtensionDecl*) { // Extensions won't exist in the lowered code: their members // will turn into ordinary functions that get called explicitly return nullptr; } - RefPtr visit(TypeDefDecl* decl) + RefPtr visitTypeDefDecl(TypeDefDecl* decl) { RefPtr loweredDecl = new TypeDefDecl(); lowerDeclCommon(loweredDecl, decl); @@ -1067,7 +1068,7 @@ struct LoweringVisitor return loweredDecl; } - RefPtr visit(ImportDecl* decl) + RefPtr visitImportDecl(ImportDecl* decl) { // No need to translate things here if we are // in "full" mode, because we will selectively @@ -1086,7 +1087,7 @@ struct LoweringVisitor return nullptr; } - RefPtr visit(EmptyDecl* decl) + RefPtr visitEmptyDecl(EmptyDecl* decl) { // Empty declarations are really only useful in GLSL, // where they are used to hold metadata that doesn't @@ -1103,7 +1104,7 @@ struct LoweringVisitor return loweredDecl; } - RefPtr visit(AggTypeDecl* decl) + RefPtr visitAggTypeDecl(AggTypeDecl* decl) { // We want to lower any aggregate type declaration // to just a `struct` type that contains its fields. @@ -1145,7 +1146,7 @@ struct LoweringVisitor return loweredDecl; } - RefPtr visit( + RefPtr visitVariable( Variable* decl) { auto loweredDecl = lowerVarDeclCommon(new Variable(), decl); @@ -1173,13 +1174,13 @@ struct LoweringVisitor return loweredDecl; } - RefPtr visit( + RefPtr visitStructField( StructField* decl) { return lowerVarDeclCommon(new StructField(), decl); } - RefPtr visit( + RefPtr visitParameterSyntaxNode( ParameterSyntaxNode* decl) { return lowerVarDeclCommon(new ParameterSyntaxNode(), decl); @@ -1191,7 +1192,7 @@ struct LoweringVisitor } - RefPtr visit( + RefPtr visitDeclGroup( DeclGroup* group) { for (auto decl : group->decls) @@ -1201,7 +1202,7 @@ struct LoweringVisitor return nullptr; } - RefPtr visit( + RefPtr visitFunctionDeclBase( FunctionDeclBase* decl) { // TODO: need to generate a name @@ -1474,7 +1475,7 @@ struct LoweringVisitor RefPtr entryPointLayout) { // First, loer the entry-point function as an ordinary function: - auto loweredEntryPointFunc = visit(entryPointDecl); + auto loweredEntryPointFunc = visitFunctionDeclBase(entryPointDecl); // Now we will generate a `void main() { ... }` function to call the lowered code. RefPtr mainDecl = new FunctionSyntaxNode(); @@ -1672,7 +1673,7 @@ struct LoweringVisitor { // Default case: lower an entry point just like any other function default: - return visit(entryPointDecl); + return visitFunctionDeclBase(entryPointDecl); // For Slang->GLSL translation, we need to lower things from HLSL-style // declarations over to GLSL conventions -- cgit v1.2.3 From 4921bdbe0d75f1ad29fae18920678287919a2e29 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 6 Jul 2017 20:40:42 -0700 Subject: Eliminate `typedef`s when lowering to GLSL GLSL doesn't support `typedef` declarations. The lowering code already lowered any named types (references to `typedef`s) to their underlying definition when targetting GLSL. This changes makes sure that we also don't generate any lowered output for `typedef` declarations in the source program. --- source/slang/lower.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source/slang/lower.cpp') diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index 8bc9619f3..b90573495 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -1059,6 +1059,12 @@ struct LoweringVisitor RefPtr visitTypeDefDecl(TypeDefDecl* decl) { + if (shared->target == CodeGenTarget::GLSL) + { + // GLSL does not support `typedef`, so we will lower it out of existence here + return nullptr; + } + RefPtr loweredDecl = new TypeDefDecl(); lowerDeclCommon(loweredDecl, decl); -- cgit v1.2.3