From 42f1cff5c1471e6bc3988a9810c20b8bcc1c84dd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 30 Oct 2017 08:54:09 -0700 Subject: Support explicit `this` expressions This is the first step towards supporting traditional object-oriented method definitions; the second step will be to allow `this` expressions to be implicit. - Add a test case using explicit `this`, and expected output - Update parsing logic for expressions so that it handled identifiers similarly to the declaration and statement logic: first try to parse using a syntax declaration looked up in the curent scope, and otherwise fall back to the ordinary `VarExpr` case. * As long as I'm making that change: switch `true` and `false` to be parsed via the callback mechanism rather than be special-cased. * This change will also help out if we ever wanted to add `super`/`base` expressions, `new`, `sizeof`/`alignof` or any other expression keywords. - Add a `ThisExpr` node and register a parser callback for it. - Add semantic checks for `ThisExpr`: basically just look upwards through scopes until we find either an aggregate type declaration or an `extension` declaration, and then use that as the type of the expression. - TODO: eventually we need to guard against a `this` expression inside of a `static` member. - The IR generation logic already handled creation of `this` parameters in function signatures; the missing piece was to register the appropriate parameter in the context, so that we can use it as the lowering of a `this` expression. --- source/slang/check.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index a42edc331..9b707d218 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -5970,6 +5970,11 @@ namespace Slang // We need to fix that. auto type = typeType->type; + if (type->As()) + { + return CreateErrorExpr(expr); + } + LookupResult lookupResult = lookUpMember( getSession(), this, @@ -5988,6 +5993,10 @@ namespace Slang expr->BaseExpression, expr->loc); } + else if (baseType->As()) + { + return CreateErrorExpr(expr); + } else { LookupResult lookupResult = lookUpMember( @@ -6093,6 +6102,54 @@ namespace Slang decl->SetCheckState(DeclCheckState::Checked); } + + // Perform semantic checking of an object-oriented `this` + // expression. + RefPtr 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()) + { + 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()) + { + 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( -- cgit v1.2.3 From 11f44241ffef478560eaba79af330c16f0bc8d69 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 30 Oct 2017 10:03:52 -0700 Subject: Allow for implicit `this` expressions. - When peforming ordinary lookup, if the container declaration for a scope is an aggregate type or `extension` decl, then use a "breadcrumb" to make sure that we use a `this` expression as the base of any resulting declaration reference - Add a test case for implicit `this` usage - Update constrained generic test case to use implicit `this` for member reference, as was originally intended --- source/slang/check.cpp | 34 ++++++++++++++ source/slang/lookup.cpp | 53 ++++++++++------------ source/slang/syntax.h | 6 +++ tests/compute/generics-constrained.slang | 4 +- .../generics-constrained.slang.expected.txt | 6 +-- tests/compute/implicit-this-expr.slang | 34 ++++++++++++++ .../compute/implicit-this-expr.slang.expected.txt | 4 ++ 7 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 tests/compute/implicit-this-expr.slang create mode 100644 tests/compute/implicit-this-expr.slang.expected.txt (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 9b707d218..0c35c4bf4 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -196,6 +196,16 @@ namespace Slang return derefExpr; } + RefPtr createImplicitThisMemberExpr( + Type* type, + SourceLoc loc) + { + RefPtr expr = new ThisExpr(); + expr->type = type; + expr->loc = loc; + return expr; + } + RefPtr ConstructLookupResultExpr( LookupResultItem const& item, RefPtr 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()) + { + bb = createImplicitThisMemberExpr( + GetTargetType(extensionDeclRef), + loc); + } + else + { + auto type = DeclRefType::Create(getSession(), breadcrumb->declRef); + bb = createImplicitThisMemberExpr( + type, + loc); + } + } + break; + default: SLANG_UNREACHABLE("all cases handle"); } 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 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(containerDecl->ParentDecl)) + DeclRef containerDeclRef = + DeclRef(containerDecl, nullptr).As(); + + 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()) { - subst = new Substitutions(); - subst->genericDecl = parentGenericDecl; - - for( auto pp : parentGenericDecl->Members ) - { - if( auto genericTypeParam = pp.As() ) - { - subst->args.Add(DeclRefType::Create( - session, - DeclRef(genericTypeParam.Ptr(), nullptr))); - } - else if( auto genericValParam = pp.As() ) - { - subst->args.Add(new GenericParamIntVal(DeclRef(genericValParam.Ptr(), nullptr))); - } - } + breadcrumb.kind = LookupResultItem::Breadcrumb::Kind::This; + breadcrumb.declRef = aggTypeDeclRef; + breadcrumb.prev = nullptr; + + breadcrumbs = &breadcrumb; } -#endif - DeclRef containerRef = DeclRef(containerDecl, subst).As(); + // 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/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/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 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 -- cgit v1.2.3