diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-10-30 08:54:09 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-10-30 09:40:04 -0700 |
| commit | 42f1cff5c1471e6bc3988a9810c20b8bcc1c84dd (patch) | |
| tree | 95ce50d87fb26c7cc2f41e6afd518cfab6a91ef9 /source/slang/lower-to-ir.cpp | |
| parent | 4ab545bcd0716cc3f2da432a921c1f53fdce7925 (diff) | |
Support explicit `this` expressions
This is the first step towards supporting traditional object-oriented method definitions; the second step will be to allow `this` expressions to be implicit.
- Add a test case using explicit `this`, and expected output
- Update parsing logic for expressions so that it handled identifiers similarly to the declaration and statement logic: first try to parse using a syntax declaration looked up in the curent scope, and otherwise fall back to the ordinary `VarExpr` case.
* As long as I'm making that change: switch `true` and `false` to be parsed via the callback mechanism rather than be special-cased.
* This change will also help out if we ever wanted to add `super`/`base` expressions, `new`, `sizeof`/`alignof` or any other expression keywords.
- Add a `ThisExpr` node and register a parser callback for it.
- Add semantic checks for `ThisExpr`: basically just look upwards through scopes until we find either an aggregate type declaration or an `extension` declaration, and then use that as the type of the expression.
- TODO: eventually we need to guard against a `this` expression inside of a `static` member.
- The IR generation logic already handled creation of `this` parameters in function signatures; the missing piece was to register the appropriate parameter in the context, so that we can use it as the lowering of a `this` expression.
Diffstat (limited to 'source/slang/lower-to-ir.cpp')
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 2f0ea810e..93b84ad31 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -288,6 +288,15 @@ struct IRGenContext IRBuilder* irBuilder; + // The value to use for any `this` expressions + // that appear in the current context. + // + // TODO: If we ever allow nesting of (non-static) + // types, then we may need to support references + // to an "outer `this`", and this representation + // might be insufficient. + LoweredValInfo thisVal; + Session* getSession() { return shared->compileRequest->mSession; @@ -1008,6 +1017,11 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo> return subscriptValue(type, baseVal, indexVal); } + LoweredValInfo visitThisExpr(ThisExpr* expr) + { + return context->thisVal; + } + LoweredValInfo visitMemberExpr(MemberExpr* expr) { auto loweredType = lowerType(context, expr->type); @@ -2360,9 +2374,18 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> }; struct ParameterInfo { + // This AST-level type of the parameter Type* type; + + // The direction (`in` vs `out` vs `in out`) ParameterDirection direction; + + // The variable/parameter declaration for + // this parameter (if any) VarDeclBase* decl; + + // Is this the representation of a `this` parameter? + bool isThisParam = false; }; // // We need a way to compute the appropriate `ParameterDirection` for a @@ -2399,6 +2422,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> info.type = paramDecl->getType(); info.decl = paramDecl; info.direction = getParameterDirection(paramDecl); + info.isThisParam = false; return info; } // @@ -2492,6 +2516,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> info.type = type; info.decl = nullptr; info.direction = direction; + info.isThisParam = true; ioParameterLists->params.Add(info); } @@ -2808,6 +2833,11 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> DeclRef<VarDeclBase> paramDeclRef = makeDeclRef(paramDecl); subContext->shared->declValues.Add(paramDeclRef, paramVal); } + + if (paramInfo.isThisParam) + { + subContext->thisVal = paramVal; + } } lowerStmt(subContext, decl->Body); |
