diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2020-02-21 10:39:05 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-21 10:39:05 -0800 |
| commit | 830671c2210191f69ddc403cc12f5454bb55b0f0 (patch) | |
| tree | bc242d34920715c33c827f4071d1ca8a1994a6b6 /source/slang/slang-check-expr.cpp | |
| parent | 433ce869481b72ad44897dcc91d7038b03ba45e2 (diff) | |
Add surface syntax for "this type" (#1236)
Within the context of an aggregate type (or an `extension` of one), the programmer can use `this` to refer to the "current" instance of the surrounding type, but there is no easy way to utter the name of the type itself. This is especially relevant inside of an `interface`, where the type of `this` isn't actually the `interface` type, but rather a placeholder for the as-yet-unknown concrete type that will implement the interface.
This change adds a keyword `This` that works similarly to `this`, but names the current *type* instead of the current instance. It can be used to declare things like binary methods or factory functions in an interface:
```
interface IBasicMathType
{
This absoluteValue();
This sumWith(This left);
}
T doSomeMath<T:IBasicMathType>(T value)
{
return value.sumWith(value.absoluteValue());
}
```
The `This` type is consistent with the type named `Self` in Rust and Swift (where Rust/Swift use `self` instead of `this`). Other names could be considered (e.g., `ThisType`) if we find that users don't like the name in this change.
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
| -rw-r--r-- | source/slang/slang-check-expr.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index ebc3a3ef9..524bab4e4 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1677,6 +1677,12 @@ namespace Slang expr->type.IsLeftValue = true; } } + else if( auto typeOrExtensionDecl = as<AggTypeDeclBase>(containerDecl) ) + { + expr->type.type = calcThisType(makeDeclRef(typeOrExtensionDecl)); + return expr; + } +#if 0 else if (auto aggTypeDecl = as<AggTypeDecl>(containerDecl)) { ensureDecl(aggTypeDecl, DeclCheckState::CanUseAsType); @@ -1706,6 +1712,7 @@ namespace Slang expr->type.type = extensionDecl->targetType.type; return expr; } +#endif scope = scope->parent; } @@ -1713,4 +1720,27 @@ namespace Slang getSink()->diagnose(expr, Diagnostics::thisExpressionOutsideOfTypeDecl); return CreateErrorExpr(expr); } + + RefPtr<Expr> SemanticsExprVisitor::visitThisTypeExpr(ThisTypeExpr* expr) + { + auto scope = expr->scope; + while (scope) + { + auto containerDecl = scope->containerDecl; + if( auto typeOrExtensionDecl = as<AggTypeDeclBase>(containerDecl) ) + { + auto thisType = calcThisType(makeDeclRef(typeOrExtensionDecl)); + auto thisTypeType = getTypeType(thisType); + + expr->type.type = thisTypeType; + return expr; + } + + scope = scope->parent; + } + + getSink()->diagnose(expr, Diagnostics::thisTypeOutsideOfTypeDecl); + return CreateErrorExpr(expr); + } + } |
