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