summaryrefslogtreecommitdiffstats
path: root/source/slang/syntax.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-06-12 14:59:13 -0700
committerGitHub <noreply@github.com>2018-06-12 14:59:13 -0700
commit167d8579870db18756c234755b197e4ded930b0e (patch)
tree942f07efe9572699a62df80cadb136a1789780a6 /source/slang/syntax.cpp
parent7852a2bf0ef5aad0f4f318507c300352c25199f2 (diff)
Initial support for enum declarations (#599)
Slang `enum` declarations will always be scoped, e.g.: ```hlsl enum Color { Red, Green = 2, Blue, } Color c = Color.Red; // Not just `Red` ``` A user can write `enum class` as a placebo for now (to ease sharing of headers with C++). Slang does not currently support the `::` operator for static member lookup, so it must be `Color.Green` and not `Color::Green`. Support for `::` as an alternate syntax could be added later if there is strong user demand. An `enum` type can have a declared "tag type" using syntax like C++ `enum class`: ```hlsl enum MyThings : uint { First = 0, // ... } ``` The `enum` cases will store their values using that type. An `enum` that doesn't declare a tag type will use the type `int` by default. Enum cases are assigned values just like in C/C++: cases can have explicit values, but otherwise default to one more than the previous case, or zero for the first case. All `enum` types will automatically conform to a standard-library `interface` called `__EnumType`, which is used so that basic operators like equality testing can be defined generically for all `enum` types. This change only adds one operator at first (the `==` comparison), but other should be added later. An `enum` case needs to be explicitly converted to an integer where needed (e.g., `int(Color.Red)`). This is implemented by having the main integer types (`int` and `uint`) support built-in initializers that can work for *any* `enum` type (or rather, anything conforming to `__EnumType`). Eventually these will be restricted so that an `enum` type can only be converted to its associated tag type. IR code generation completely eliminates `enum` types and their cases. The `enum` type will be replaced with its tag type, and the cases will be replaced with the tag values. Currently this could leave some mess in the IR where cast operations are applied between values that actually have the same type.
Diffstat (limited to 'source/slang/syntax.cpp')
-rw-r--r--source/slang/syntax.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp
index 74c817b92..db0410cbd 100644
--- a/source/slang/syntax.cpp
+++ b/source/slang/syntax.cpp
@@ -286,6 +286,13 @@ void Type::accept(IValVisitor* visitor, void* extra)
return DeclRefType::Create(this, makeDeclRef<Decl>(stringTypeDecl));
}
+ Type* Session::getEnumTypeType()
+ {
+ auto enumTypeTypeDecl = findMagicDecl(this, "EnumTypeType");
+ return DeclRefType::Create(this, makeDeclRef<Decl>(enumTypeTypeDecl));
+ }
+
+
RefPtr<PtrType> Session::getPtrType(
RefPtr<Type> valueType)
{
@@ -1618,7 +1625,7 @@ void Type::accept(IValVisitor* visitor, void* extra)
newSubst->paramDecl = appGlobalGenericSubst->paramDecl;
newSubst->actualType = appGlobalGenericSubst->actualType;
newSubst->constraintArgs = appGlobalGenericSubst->constraintArgs;
-
+
*link = newSubst;
link = &newSubst->outer;
}
@@ -1834,7 +1841,7 @@ void Type::accept(IValVisitor* visitor, void* extra)
&diff);
if (!diff)
- return *this;
+ return *this;
*ioDiff += diff;
@@ -2413,7 +2420,7 @@ void Type::accept(IValVisitor* visitor, void* extra)
// TODO: need to print out substitutions too!
return name->text;
}
-
+
bool SubstitutionSet::Equals(SubstitutionSet substSet) const
{
if(!substitutions || !substSet.substitutions)