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 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (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"); } -- cgit v1.2.3