summaryrefslogtreecommitdiffstats
path: root/source/slang/parser.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2017-12-27 21:51:56 -0500
committerYong He <yonghe@outlook.com>2017-12-27 21:51:56 -0500
commiteaf3d840b16a8646ab4545487679c869eef500b7 (patch)
tree3ae6f64ec51a2714ac06310723a1c320e91e5974 /source/slang/parser.cpp
parentd55b56bc804f25d8390f1dc6b09ff9116ffcaf29 (diff)
Using a visitor to systematically replace lookup scopes of generic function's return type expression.
fixes #336 Add a `ReplaceScopeVisitor` to replace the scopes of the return type expression tree to use the generic decl's scope instead of module's scope after parser has determined the decl is a generic function header decl.
Diffstat (limited to 'source/slang/parser.cpp')
-rw-r--r--source/slang/parser.cpp40
1 files changed, 38 insertions, 2 deletions
diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp
index 35cc96b5c..662b75a2c 100644
--- a/source/slang/parser.cpp
+++ b/source/slang/parser.cpp
@@ -4,6 +4,7 @@
#include "compiler.h"
#include "lookup.h"
+#include "visitor.h"
namespace Slang
{
@@ -1099,6 +1100,40 @@ namespace Slang
}
}
+ // systematically replace all scopes in an expression tree
+ class ReplaceScopeVisitor : public ExprVisitor<ReplaceScopeVisitor>
+ {
+ public:
+ RefPtr<Scope> scope;
+ void visitDeclRefExpr(DeclRefExpr* expr)
+ {
+ expr->scope = scope;
+ }
+ void visitGenericAppExpr(GenericAppExpr * expr)
+ {
+ expr->FunctionExpr->accept(this, nullptr);
+ for (auto arg : expr->Arguments)
+ arg->accept(this, nullptr);
+ }
+ void visitIndexExpr(IndexExpr * expr)
+ {
+ expr->BaseExpression->accept(this, nullptr);
+ expr->IndexExpression->accept(this, nullptr);
+ }
+ void visitMemberExpr(MemberExpr * expr)
+ {
+ expr->BaseExpression->accept(this, nullptr);
+ expr->scope = scope;
+ }
+ void visitStaticMemberExpr(StaticMemberExpr * expr)
+ {
+ expr->BaseExpression->accept(this, nullptr);
+ expr->scope = scope;
+ }
+ void visitExpr(Expr* /*expr*/)
+ {}
+ };
+
static RefPtr<Decl> ParseFuncDeclHeader(
Parser* parser,
DeclaratorInfo const& declaratorInfo,
@@ -1114,8 +1149,9 @@ namespace Slang
// if return type is a DeclRef type, we need to update its scope to use this function decl's scope
// so that LookUp can find the generic type parameters declared after the function name
- if (auto declRefRetType = declaratorInfo.typeSpec.As<DeclRefExpr>())
- declRefRetType->scope = parser->currentScope;
+ ReplaceScopeVisitor replaceScopeVisitor;
+ replaceScopeVisitor.scope = parser->currentScope;
+ declaratorInfo.typeSpec->accept(&replaceScopeVisitor, nullptr);
decl->ReturnType = TypeExp(declaratorInfo.typeSpec);
auto parseFuncDeclHeaderInner = [&](GenericDecl *)