summaryrefslogtreecommitdiffstats
path: root/source/slang/syntax.h
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 /source/slang/syntax.h
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 'source/slang/syntax.h')
-rw-r--r--source/slang/syntax.h61
1 files changed, 38 insertions, 23 deletions
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<SyntaxNode> 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<StructField> 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<FieldDeclRef> GetFields() const { return GetMembersOfType<FieldDeclRef>(); }
};
- // 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<TypeExp> bases;
-
virtual RefPtr<SyntaxNode> 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<SyntaxNode> 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<TraitDeclRef>(); }
+ RefPtr<ExpressionType> 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<ExpressionSyntaxNode> VisitSharedTypeExpr(SharedTypeExpr* typeExpr)
{