summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-15 11:16:10 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-15 12:42:52 -0700
commit367edf757aff609b72de48732113ea756d878f52 (patch)
tree493fd248f57444120588d4d0979d391875d7f5bb /tests
parent3491d3578c7fa3e88e7c16c394ec64238c636f04 (diff)
Add basic support for `interface` declarations
- Add a test case for `interface` declarations and the exected implicit type conversion rules around them - Rename exising "trait" declaration kind to "interface" - There was already basic syntax for `__trait` declarations, and a bunch of related machinery. - Not all of it worked as needed, but it was clearly a start at solving the problem - Change `InterfaceConformanceDecl` to a more general `InheritanceDecl` that covers inheritance from any type expression (leave it to other code to validate the cases that should be allowed) - Instead of keeping a raw `bases` array on interface/trait declarations, turn all inheritance clauses into `IheritanceDecl` members - Add support for inheritance clause on `struct` types - Remove the `__conforms` syntax only used in the stdlib, in favor of conentional `: Base` style syntax already in place for aggregate types - Make sure that the parser pushes a new scope around he member declarations of an aggregate type, so that lookup in member functions will correctly find members of the enclosing type - In `TryCoerceImpl`, allow a type that conforms to an interface to be implicitly conveted to the corresponding interface type. This leaves out a lot of major functionality: - There is no validation that a type provides all the members it is supposed to as part of fulfilling a claimed interface conformance - The lookup process needs to deal with inherited members at some point. - We can avoid this for now if we don't allow inheritance for concrete types - When it comes time to handle it, it *might* be possible to implement by considering an `InheritanceDecl` to be, conceptually, a member of the inherited type, with a `__transparent` modifier - The lookup rules member functions do *not* deal with a lot of stuff: - There is no `this` expression right now - The semantic checker does not rewrite `foo` to `this.foo`, so downstream stages aren't going to get things in a clean format - There is no handling of mutability currently - The right answer there is probably to make member functions on `struct` types non-mutating by default, and add a qualifier to opt in to mutability. I believe this is actually what the OOP syntax in HLSL did way back when. - There is no handling of `static` members, and thus no checking to make sure that non-static members aren't referenced in static functions - None of this affects down-stream code generation right now, so it probably won't actually produce anything valid. - This is where we start needing a suitable IR to use for lowering, to manage the complexity.
Diffstat (limited to 'tests')
-rw-r--r--tests/front-end/interface.slang65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/front-end/interface.slang b/tests/front-end/interface.slang
new file mode 100644
index 000000000..754addf61
--- /dev/null
+++ b/tests/front-end/interface.slang
@@ -0,0 +1,65 @@
+//TEST:SIMPLE:
+
+// Confirm that basic `interface` syntax stuff type-checks.
+
+// The example here is adapted from examples in Matt Pharr's
+// chapter in GPU Gems: "An Introduction to Shader Interaces"
+
+struct LightSample
+{
+ float3 C; // radiance
+ float3 L; // direction
+};
+
+interface Light
+{
+ LightSample illuminate(float3 P_world);
+}
+
+struct PointLight : Light
+{
+ float3 Plight_world;
+ float3 C;
+
+ LightSample illuminate(float3 P_world)
+ {
+ float3 delta = Plight_world - P_world;
+ float3 L = normalize(delta);
+ float distance = length(delta);
+
+ LightSample result;
+ result.L = L;
+ result.C = C * (1 / (distance*distance));
+ return result;
+ }
+};
+
+// using the concrete type directly
+float3 A( float3 P_world, PointLight light )
+{
+ return light.illuminate(P_world).L;
+}
+
+// using the abstract interface type
+float3 B( float3 P_world, Light light )
+{
+ return light.illuminate(P_world).L;
+}
+
+//
+float3 Test(float3 P_world, PointLight pointLight, Light light)
+{
+ // dconcrete type expected, concrete type provided
+ float3 a = A(P_world, pointLight);
+
+ // abstract type expected, abstract type provided
+ float3 b = B(P_world, light);
+
+ // abstract type expected, concrete type provided
+ float3 c = B(P_world, pointLight);
+
+ // The remaining case (passing `Light` where `PointLight` is expected)
+ // should be an error, so we want a distinct test for that.
+
+ return a + b + c;
+} \ No newline at end of file