summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/check.cpp34
-rw-r--r--source/slang/lookup.cpp53
-rw-r--r--source/slang/syntax.h6
-rw-r--r--tests/compute/generics-constrained.slang4
-rw-r--r--tests/compute/generics-constrained.slang.expected.txt6
-rw-r--r--tests/compute/implicit-this-expr.slang34
-rw-r--r--tests/compute/implicit-this-expr.slang.expected.txt4
7 files changed, 106 insertions, 35 deletions
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<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");
}
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<Substitutions> 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<GenericDecl*>(containerDecl->ParentDecl))
+ DeclRef<ContainerDecl> containerDeclRef =
+ DeclRef<Decl>(containerDecl, nullptr).As<ContainerDecl>();
+
+ 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<AggTypeDeclBase>())
{
- subst = new Substitutions();
- subst->genericDecl = parentGenericDecl;
-
- for( auto pp : parentGenericDecl->Members )
- {
- if( auto genericTypeParam = pp.As<GenericTypeParamDecl>() )
- {
- subst->args.Add(DeclRefType::Create(
- session,
- DeclRef<GenericTypeParamDecl>(genericTypeParam.Ptr(), nullptr)));
- }
- else if( auto genericValParam = pp.As<GenericValueParamDecl>() )
- {
- subst->args.Add(new GenericParamIntVal(DeclRef<GenericValueParamDecl>(genericValParam.Ptr(), nullptr)));
- }
- }
+ breadcrumb.kind = LookupResultItem::Breadcrumb::Kind::This;
+ breadcrumb.declRef = aggTypeDeclRef;
+ breadcrumb.prev = nullptr;
+
+ breadcrumbs = &breadcrumb;
}
-#endif
- DeclRef<ContainerDecl> containerRef = DeclRef<Decl>(containerDecl, subst).As<ContainerDecl>();
+ // 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<float> 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