summaryrefslogtreecommitdiffstats
path: root/source/slang/check.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/slang/check.cpp')
-rw-r--r--source/slang/check.cpp91
1 files changed, 91 insertions, 0 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(