From 367edf757aff609b72de48732113ea756d878f52 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 15 Jun 2017 11:16:10 -0700 Subject: 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. --- source/slang/syntax.h | 61 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 23 deletions(-) (limited to 'source/slang/syntax.h') diff --git a/source/slang/syntax.h b/source/slang/syntax.h index ac2bcac87..642f4e99f 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -1453,8 +1453,26 @@ namespace Slang SLANG_DECLARE_DECL_REF(StructField) }; + // An `AggTypeDeclBase` captures the shared functionality + // between true aggregate type declarations and extension + // declarations: + // + // - Both can container members (they are `ContainerDecl`s) + // - Both can have declared bases + // - Both expose a `this` variable in their body + // + class AggTypeDeclBase : public ContainerDecl + { + public: + }; + + struct AggTypeDeclBaseRef : ContainerDeclRef + { + SLANG_DECLARE_DECL_REF(AggTypeDeclBase); + }; + // An extension to apply to an existing type - class ExtensionDecl : public ContainerDecl + class ExtensionDecl : public AggTypeDeclBase { public: TypeExp targetType; @@ -1466,7 +1484,7 @@ namespace Slang virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - struct ExtensionDeclRef : ContainerDeclRef + struct ExtensionDeclRef : AggTypeDeclBaseRef { SLANG_DECLARE_DECL_REF(ExtensionDecl); @@ -1474,9 +1492,10 @@ namespace Slang }; // Declaration of a type that represents some sort of aggregate - class AggTypeDecl : public ContainerDecl + class AggTypeDecl : public AggTypeDeclBase { public: + // extensions that might apply to this declaration ExtensionDecl* candidateExtensions = nullptr; FilteredMemberList GetFields() @@ -1505,7 +1524,7 @@ namespace Slang } }; - struct AggTypeDeclRef : public ContainerDeclRef + struct AggTypeDeclRef : public AggTypeDeclBaseRef { SLANG_DECLARE_DECL_REF(AggTypeDecl); @@ -1538,43 +1557,41 @@ namespace Slang FilteredMemberRefList GetFields() const { return GetMembersOfType(); } }; - // A trait which other types can conform to - class TraitDecl : public AggTypeDecl + // An interface which other types can conform to + class InterfaceDecl : public AggTypeDecl { public: - List bases; - virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - struct TraitDeclRef : public AggTypeDeclRef + struct InterfaceDeclRef : public AggTypeDeclRef { - SLANG_DECLARE_DECL_REF(TraitDecl); + SLANG_DECLARE_DECL_REF(InterfaceDecl); }; - // A declaration that states that the enclosing type supports a given trait + // A kind of pseudo-member that represents an explicit + // or implicit inheritance relationship. // - // TODO: this same construct might be used for represent other inheritance-like cases - class TraitConformanceDecl : public Decl + class InheritanceDecl : public Decl { public: // The type expression as written TypeExp base; - // The trait that we found we conform to... - TraitDeclRef traitDeclRef; - virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - struct TraitConformanceDeclRef : public DeclRef + struct InheritanceDeclRef : public DeclRef { - SLANG_DECLARE_DECL_REF(TraitConformanceDecl); + SLANG_DECLARE_DECL_REF(InheritanceDecl); - TraitDeclRef GetTraitDeclRef() { return Substitute(GetDecl()->traitDeclRef).As(); } + RefPtr getBaseType() { return Substitute(GetDecl()->base.type); } }; + // TODO: may eventually need sub-classes for explicit/direct vs. implicit/indirect inheritance + + // A declaration that represents a simple (non-aggregate) type class SimpleTypeDecl : public Decl { @@ -2744,11 +2761,9 @@ namespace Slang virtual void visitAccessorDecl(AccessorDecl* decl) = 0; - virtual void VisitTraitDecl(TraitDecl* /*decl*/) - {} + virtual void visitInterfaceDecl(InterfaceDecl* /*decl*/) = 0; - virtual void VisitTraitConformanceDecl(TraitConformanceDecl* /*decl*/) - {} + virtual void visitInheritanceDecl(InheritanceDecl* /*decl*/) = 0; virtual RefPtr VisitSharedTypeExpr(SharedTypeExpr* typeExpr) { -- cgit v1.2.3