summaryrefslogtreecommitdiffstats
path: root/source/slang/check.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-10-30 08:54:09 -0700
committerTim Foley <tfoley@nvidia.com>2017-10-30 09:40:04 -0700
commit42f1cff5c1471e6bc3988a9810c20b8bcc1c84dd (patch)
tree95ce50d87fb26c7cc2f41e6afd518cfab6a91ef9 /source/slang/check.cpp
parent4ab545bcd0716cc3f2da432a921c1f53fdce7925 (diff)
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.
Diffstat (limited to 'source/slang/check.cpp')
-rw-r--r--source/slang/check.cpp57
1 files changed, 57 insertions, 0 deletions
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<ErrorType>())
+ {
+ return CreateErrorExpr(expr);
+ }
+
LookupResult lookupResult = lookUpMember(
getSession(),
this,
@@ -5988,6 +5993,10 @@ namespace Slang
expr->BaseExpression,
expr->loc);
}
+ else if (baseType->As<ErrorType>())
+ {
+ 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<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(