diff options
| author | Yong He <yonghe@outlook.com> | 2017-10-30 19:31:52 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-30 19:31:52 -0400 |
| commit | 832d9c708891b10145c6648d893b04ca4a0b879a (patch) | |
| tree | e1f44fc27bf80d94de5ac0e866c7409b2adcec22 | |
| parent | c24c173101c2c124401af77d8c513a23efac3b7e (diff) | |
| parent | 3ffdf610d05a9318731bd7237da530c3d312a9a9 (diff) | |
Merge pull request #235 from tfoleyNV/explicit-this-expr
Support `this` expressions (explicit and implicit)
| -rw-r--r-- | source/slang/check.cpp | 91 | ||||
| -rw-r--r-- | source/slang/diagnostic-defs.h | 13 | ||||
| -rw-r--r-- | source/slang/emit.cpp | 11 | ||||
| -rw-r--r-- | source/slang/expr-defs.h | 7 | ||||
| -rw-r--r-- | source/slang/lookup.cpp | 53 | ||||
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 30 | ||||
| -rw-r--r-- | source/slang/parser.cpp | 57 | ||||
| -rw-r--r-- | source/slang/syntax.h | 6 | ||||
| -rw-r--r-- | tests/compute/explicit-this-expr.slang | 34 | ||||
| -rw-r--r-- | tests/compute/explicit-this-expr.slang.expected.txt | 4 | ||||
| -rw-r--r-- | tests/compute/generics-constrained.slang | 4 | ||||
| -rw-r--r-- | tests/compute/generics-constrained.slang.expected.txt | 6 | ||||
| -rw-r--r-- | tests/compute/implicit-this-expr.slang | 34 | ||||
| -rw-r--r-- | tests/compute/implicit-this-expr.slang.expected.txt | 4 |
14 files changed, 302 insertions, 52 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp index a42edc331..0c35c4bf4 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -196,6 +196,16 @@ namespace Slang return derefExpr; } + RefPtr<Expr> createImplicitThisMemberExpr( + Type* type, + SourceLoc loc) + { + RefPtr<ThisExpr> expr = new ThisExpr(); + expr->type = type; + expr->loc = loc; + return expr; + } + RefPtr<Expr> ConstructLookupResultExpr( LookupResultItem const& item, RefPtr<Expr> baseExpr, @@ -228,6 +238,30 @@ namespace Slang } break; + case LookupResultItem::Breadcrumb::Kind::This: + { + // We expect a `this` to always come + // at the start of a chain. + SLANG_ASSERT(bb == nullptr); + + // The member was looked up via a `this` expression, + // so we need to create one here. + if (auto extensionDeclRef = breadcrumb->declRef.As<ExtensionDecl>()) + { + bb = createImplicitThisMemberExpr( + GetTargetType(extensionDeclRef), + loc); + } + else + { + auto type = DeclRefType::Create(getSession(), breadcrumb->declRef); + bb = createImplicitThisMemberExpr( + type, + loc); + } + } + break; + default: SLANG_UNREACHABLE("all cases handle"); } @@ -5970,6 +6004,11 @@ namespace Slang // We need to fix that. auto type = typeType->type; + if (type->As<ErrorType>()) + { + return CreateErrorExpr(expr); + } + LookupResult lookupResult = lookUpMember( getSession(), this, @@ -5988,6 +6027,10 @@ namespace Slang expr->BaseExpression, expr->loc); } + else if (baseType->As<ErrorType>()) + { + return CreateErrorExpr(expr); + } else { LookupResult lookupResult = lookUpMember( @@ -6093,6 +6136,54 @@ namespace Slang decl->SetCheckState(DeclCheckState::Checked); } + + // Perform semantic checking of an object-oriented `this` + // expression. + RefPtr<Expr> visitThisExpr(ThisExpr* expr) + { + // We will do an upwards search starting in the current + // scope, looking for a surrounding type (or `extension`) + // declaration that could be the referrant of the expression. + auto scope = expr->scope; + while (scope) + { + auto containerDecl = scope->containerDecl; + if (auto aggTypeDecl = containerDecl->As<AggTypeDecl>()) + { + EnsureDecl(aggTypeDecl); + + // Okay, we are using `this` in the context of an + // aggregate type, so the expression should be + // of the corresponding type. + expr->type = DeclRefType::Create( + getSession(), + makeDeclRef(aggTypeDecl)); + return expr; + } + else if (auto extensionDecl = containerDecl->As<ExtensionDecl>()) + { + EnsureDecl(extensionDecl); + + // When `this` is used in the context of an `extension` + // declaration, then it should refer to an instance of + // the type being extended. + // + // TODO: There is potentially a small gotcha here that + // lookup through such a `this` expression should probably + // prioritize members declared in the current extension + // if there are multiple extensions in scope that add + // members with the same name... + // + expr->type = QualType(extensionDecl->targetType.type); + return expr; + } + + scope = scope->parent; + } + + getSink()->diagnose(expr, Diagnostics::thisExpressionOutsideOfTypeDecl); + return CreateErrorExpr(expr); + } }; bool isPrimaryDecl( diff --git a/source/slang/diagnostic-defs.h b/source/slang/diagnostic-defs.h index 36e68c853..cca1c4869 100644 --- a/source/slang/diagnostic-defs.h +++ b/source/slang/diagnostic-defs.h @@ -308,15 +308,16 @@ DIAGNOSTIC(39999, Error, tooManyArguments, "too many arguments to call (got $0, DIAGNOSTIC(39999, Error, invalidIntegerLiteralSuffix, "invalid suffix '$0' on integer literal") DIAGNOSTIC(39999, Error, invalidFloatingPOintLiteralSuffix, "invalid suffix '$0' on floating-point literal") -DIAGNOSTIC(39999, Error, conflictingExplicitBindingsForParameter, "conflicting explicit bindings for parameter '$0'") -DIAGNOSTIC(39999, Warning, parameterBindingsOverlap, "explicit binding for parameter '$0' overlaps with parameter '$1'") +DIAGNOSTIC(39999, Error, conflictingExplicitBindingsForParameter, "conflicting explicit bindings for parameter '$0'") +DIAGNOSTIC(39999, Warning, parameterBindingsOverlap, "explicit binding for parameter '$0' overlaps with parameter '$1'") -DIAGNOSTIC(38000, Error, entryPointFunctionNotFound, "no function found matching entry point name '$0'") -DIAGNOSTIC(38001, Error, ambiguousEntryPoint, "more than one function matches entry point name '$0'") -DIAGNOSTIC(38002, Note, entryPointCandidate, "see candidate declaration for entry point '$0'") -DIAGNOSTIC(38003, Error, entryPointSymbolNotAFunction, "entry point '$0' must be declared as a function") +DIAGNOSTIC(38000, Error, entryPointFunctionNotFound, "no function found matching entry point name '$0'") +DIAGNOSTIC(38001, Error, ambiguousEntryPoint, "more than one function matches entry point name '$0'") +DIAGNOSTIC(38002, Note, entryPointCandidate, "see candidate declaration for entry point '$0'") +DIAGNOSTIC(38003, Error, entryPointSymbolNotAFunction, "entry point '$0' must be declared as a function") DIAGNOSTIC(38100, Error, typeDoesntImplementInterfaceRequirement, "type '$0' does not provide required interface member '$1'") +DIAGNOSTIC(38101, Error, thisExpressionOutsideOfTypeDecl, "'this' expression can only be used in members of an aggregate type") // // 4xxxx - IL code generation. diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index d433d5e2f..2e8ab58d4 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -2395,6 +2395,17 @@ struct EmitVisitor if(needClose) Emit(")"); } + void visitThisExpr(ThisExpr* expr, ExprEmitArg const& arg) + { + auto prec = kEOp_Atomic; + auto outerPrec = arg.outerPrec; + bool needClose = MaybeEmitParens(outerPrec, prec); + + Emit("this"); + + if(needClose) Emit(")"); + } + void visitSwizzleExpr(SwizzleExpr* swizExpr, ExprEmitArg const& arg) { auto prec = kEOp_Postfix; diff --git a/source/slang/expr-defs.h b/source/slang/expr-defs.h index 81e8d9275..e3c16a674 100644 --- a/source/slang/expr-defs.h +++ b/source/slang/expr-defs.h @@ -148,3 +148,10 @@ END_SYNTAX_CLASS() SYNTAX_CLASS(ParenExpr, Expr) SYNTAX_FIELD(RefPtr<Expr>, base); END_SYNTAX_CLASS() + +// An object-oriented `this` expression, used to +// refer to the current instance of an enclosing type. +SYNTAX_CLASS(ThisExpr, Expr) + FIELD(RefPtr<Scope>, scope); +END_SYNTAX_CLASS() + diff --git a/source/slang/lookup.cpp b/source/slang/lookup.cpp index cf51ae720..c0cb657c4 100644 --- a/source/slang/lookup.cpp +++ b/source/slang/lookup.cpp @@ -319,40 +319,35 @@ void DoLookupImpl( if(!containerDecl) continue; - // If the container is a generic, then we need to instantiate it - // at the parameters themselves, so provide a fully-resolved - // declaration reference for lookup. - RefPtr<Substitutions> subst = nullptr; -#if 1 - // Actually, the above rationale seems bogus. If we are looking - // up from "inside" a generic declaration, we don't want to - // get its members pre-specialized, right? -#else - if(auto parentGenericDecl = dynamic_cast<GenericDecl*>(containerDecl->ParentDecl)) + DeclRef<ContainerDecl> containerDeclRef = + DeclRef<Decl>(containerDecl, nullptr).As<ContainerDecl>(); + + BreadcrumbInfo breadcrumb; + BreadcrumbInfo* breadcrumbs = nullptr; + + // Depending on the kind of container we are looking into, + // we may need to insert something like a `this` expression + // to resolve the lookup result. + // + // Note: We are checking for `AggTypeDeclBase` here, and not + // just `AggTypeDecl`, because we want to catch `extension` + // declarations as well. + // + if (auto aggTypeDeclRef = containerDeclRef.As<AggTypeDeclBase>()) { - subst = new Substitutions(); - subst->genericDecl = parentGenericDecl; - - for( auto pp : parentGenericDecl->Members ) - { - if( auto genericTypeParam = pp.As<GenericTypeParamDecl>() ) - { - subst->args.Add(DeclRefType::Create( - session, - DeclRef<GenericTypeParamDecl>(genericTypeParam.Ptr(), nullptr))); - } - else if( auto genericValParam = pp.As<GenericValueParamDecl>() ) - { - subst->args.Add(new GenericParamIntVal(DeclRef<GenericValueParamDecl>(genericValParam.Ptr(), nullptr))); - } - } + breadcrumb.kind = LookupResultItem::Breadcrumb::Kind::This; + breadcrumb.declRef = aggTypeDeclRef; + breadcrumb.prev = nullptr; + + breadcrumbs = &breadcrumb; } -#endif - DeclRef<ContainerDecl> containerRef = DeclRef<Decl>(containerDecl, subst).As<ContainerDecl>(); + // Now perform "local" lookup in the context of the container, + // as if we were looking up a member directly. + // DoLocalLookupImpl( session, - name, containerRef, request, result, nullptr); + name, containerDeclRef, request, result, breadcrumbs); } if (result.isValid()) diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 2f0ea810e..93b84ad31 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -288,6 +288,15 @@ struct IRGenContext IRBuilder* irBuilder; + // The value to use for any `this` expressions + // that appear in the current context. + // + // TODO: If we ever allow nesting of (non-static) + // types, then we may need to support references + // to an "outer `this`", and this representation + // might be insufficient. + LoweredValInfo thisVal; + Session* getSession() { return shared->compileRequest->mSession; @@ -1008,6 +1017,11 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo> return subscriptValue(type, baseVal, indexVal); } + LoweredValInfo visitThisExpr(ThisExpr* expr) + { + return context->thisVal; + } + LoweredValInfo visitMemberExpr(MemberExpr* expr) { auto loweredType = lowerType(context, expr->type); @@ -2360,9 +2374,18 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> }; struct ParameterInfo { + // This AST-level type of the parameter Type* type; + + // The direction (`in` vs `out` vs `in out`) ParameterDirection direction; + + // The variable/parameter declaration for + // this parameter (if any) VarDeclBase* decl; + + // Is this the representation of a `this` parameter? + bool isThisParam = false; }; // // We need a way to compute the appropriate `ParameterDirection` for a @@ -2399,6 +2422,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> info.type = paramDecl->getType(); info.decl = paramDecl; info.direction = getParameterDirection(paramDecl); + info.isThisParam = false; return info; } // @@ -2492,6 +2516,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> info.type = type; info.decl = nullptr; info.direction = direction; + info.isThisParam = true; ioParameterLists->params.Add(info); } @@ -2808,6 +2833,11 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> DeclRef<VarDeclBase> paramDeclRef = makeDeclRef(paramDecl); subContext->shared->declValues.Add(paramDeclRef, paramVal); } + + if (paramInfo.isThisParam) + { + subContext->thisVal = paramVal; + } } lowerStmt(subContext, decl->Body); diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 3a8c5b362..322f403e6 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -3334,6 +3334,32 @@ namespace Slang static RefPtr<Expr> parsePrefixExpr(Parser* parser); + // Parse OOP `this` expression syntax + static RefPtr<RefObject> parseThisExpr(Parser* parser, void* /*userData*/) + { + RefPtr<ThisExpr> expr = new ThisExpr(); + expr->scope = parser->currentScope; + return expr; + } + + static RefPtr<Expr> parseBoolLitExpr(Parser* parser, bool value) + { + RefPtr<ConstantExpr> constExpr = new ConstantExpr(); + constExpr->ConstType = ConstantExpr::ConstantType::Bool; + constExpr->integerValue = value ? 1 : 0; + return constExpr; + } + + static RefPtr<RefObject> parseTrueExpr(Parser* parser, void* /*userData*/) + { + return parseBoolLitExpr(parser, true); + } + + static RefPtr<RefObject> parseFalseExpr(Parser* parser, void* /*userData*/) + { + return parseBoolLitExpr(parser, false); + } + static RefPtr<Expr> parseAtomicExpr(Parser* parser) { switch( peekTokenType(parser) ) @@ -3577,19 +3603,18 @@ namespace Slang case TokenType::Identifier: { - // TODO(tfoley): Need a name-lookup step here to resolve - // syntactic keywords in expression context. + // We will perform name lookup here so that we can find syntax + // keywords registered for use as expressions. + Token nameToken = peekToken(parser); - if (parser->LookAheadToken("true") || parser->LookAheadToken("false")) + RefPtr<Expr> parsedExpr; + if (tryParseUsingSyntaxDecl<Expr>(parser, &parsedExpr)) { - RefPtr<ConstantExpr> constExpr = new ConstantExpr(); - auto token = parser->tokenReader.AdvanceToken(); - constExpr->token = token; - parser->FillPosition(constExpr.Ptr()); - constExpr->ConstType = ConstantExpr::ConstantType::Bool; - constExpr->integerValue = token.Content == "true" ? 1 : 0; - - return constExpr; + if (!parsedExpr->loc.isValid()) + { + parsedExpr->loc = nameToken.loc; + } + return parsedExpr; } // Default behavior is just to create a name expression @@ -4090,6 +4115,16 @@ namespace Slang #undef MODIFIER + // Add syntax for expression keywords + #define EXPR(KEYWORD, CALLBACK) \ + addBuiltinSyntax<Expr>(session, scope, #KEYWORD, &CALLBACK) + + EXPR(this, parseThisExpr); + EXPR(true, parseTrueExpr); + EXPR(false, parseFalseExpr); + + #undef EXPR + return moduleDecl; } diff --git a/source/slang/syntax.h b/source/slang/syntax.h index d32c4de15..0b5a38631 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -859,6 +859,12 @@ namespace Slang // lookup was able to find a member through type `U` // instead. Constraint, + + // The lookup process considered a member of an + // enclosing type as being in scope, so that any + // reference to that member needs to use a `this` + // expression as appropriate. + This, }; // The kind of lookup step that was performed diff --git a/tests/compute/explicit-this-expr.slang b/tests/compute/explicit-this-expr.slang new file mode 100644 index 000000000..7bd8dff99 --- /dev/null +++ b/tests/compute/explicit-this-expr.slang @@ -0,0 +1,34 @@ +//TEST(smoke,compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +// Access fields of a `struct` type from within a "method" by +// using an explicit `this` expression. + +struct A +{ + float x; + + float addWith(float y) + { + return this.x + y; + } +}; + +RWStructuredBuffer<float> outputBuffer : register(u0); + + +float test(float inVal) +{ + A a; + a.x = inVal; + return a.addWith(inVal*inVal); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + float inVal = float(tid); + float outVal = test(inVal); + outputBuffer[tid] = outVal; +}
\ No newline at end of file diff --git a/tests/compute/explicit-this-expr.slang.expected.txt b/tests/compute/explicit-this-expr.slang.expected.txt new file mode 100644 index 000000000..f73cfe6c3 --- /dev/null +++ b/tests/compute/explicit-this-expr.slang.expected.txt @@ -0,0 +1,4 @@ +0 +40000000 +40C00000 +41400000 diff --git a/tests/compute/generics-constrained.slang b/tests/compute/generics-constrained.slang index 669674376..c8ab71bfa 100644 --- a/tests/compute/generics-constrained.slang +++ b/tests/compute/generics-constrained.slang @@ -17,9 +17,7 @@ struct A : Helper // TODO: we should be able to reference a member variable here, // but the front-end isn't handling references through `this` // properly yet. -// return a; - - return 1.0f; + return a; } }; diff --git a/tests/compute/generics-constrained.slang.expected.txt b/tests/compute/generics-constrained.slang.expected.txt index cc5e55ab6..ae6b9920b 100644 --- a/tests/compute/generics-constrained.slang.expected.txt +++ b/tests/compute/generics-constrained.slang.expected.txt @@ -1,4 +1,4 @@ +0 3F800000 -3F800000 -3F800000 -3F800000 +40000000 +40400000 diff --git a/tests/compute/implicit-this-expr.slang b/tests/compute/implicit-this-expr.slang new file mode 100644 index 000000000..339c5fb6a --- /dev/null +++ b/tests/compute/implicit-this-expr.slang @@ -0,0 +1,34 @@ +//TEST(smoke,compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +// Access fields of a `struct` type from within a "method" by +// using an implicit `this` expression. + +struct A +{ + float x; + + float addWith(float y) + { + return x + y; + } +}; + +RWStructuredBuffer<float> outputBuffer : register(u0); + + +float test(float inVal) +{ + A a; + a.x = inVal; + return a.addWith(inVal*inVal); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + float inVal = float(tid); + float outVal = test(inVal); + outputBuffer[tid] = outVal; +}
\ No newline at end of file diff --git a/tests/compute/implicit-this-expr.slang.expected.txt b/tests/compute/implicit-this-expr.slang.expected.txt new file mode 100644 index 000000000..f73cfe6c3 --- /dev/null +++ b/tests/compute/implicit-this-expr.slang.expected.txt @@ -0,0 +1,4 @@ +0 +40000000 +40C00000 +41400000 |
