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/parser.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/parser.cpp')
| -rw-r--r-- | source/slang/parser.cpp | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 3a8c5b362..322f403e6 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -3334,6 +3334,32 @@ namespace Slang static RefPtr<Expr> parsePrefixExpr(Parser* parser); + // Parse OOP `this` expression syntax + static RefPtr<RefObject> parseThisExpr(Parser* parser, void* /*userData*/) + { + RefPtr<ThisExpr> expr = new ThisExpr(); + expr->scope = parser->currentScope; + return expr; + } + + static RefPtr<Expr> parseBoolLitExpr(Parser* parser, bool value) + { + RefPtr<ConstantExpr> constExpr = new ConstantExpr(); + constExpr->ConstType = ConstantExpr::ConstantType::Bool; + constExpr->integerValue = value ? 1 : 0; + return constExpr; + } + + static RefPtr<RefObject> parseTrueExpr(Parser* parser, void* /*userData*/) + { + return parseBoolLitExpr(parser, true); + } + + static RefPtr<RefObject> parseFalseExpr(Parser* parser, void* /*userData*/) + { + return parseBoolLitExpr(parser, false); + } + static RefPtr<Expr> parseAtomicExpr(Parser* parser) { switch( peekTokenType(parser) ) @@ -3577,19 +3603,18 @@ namespace Slang case TokenType::Identifier: { - // TODO(tfoley): Need a name-lookup step here to resolve - // syntactic keywords in expression context. + // We will perform name lookup here so that we can find syntax + // keywords registered for use as expressions. + Token nameToken = peekToken(parser); - if (parser->LookAheadToken("true") || parser->LookAheadToken("false")) + RefPtr<Expr> parsedExpr; + if (tryParseUsingSyntaxDecl<Expr>(parser, &parsedExpr)) { - RefPtr<ConstantExpr> constExpr = new ConstantExpr(); - auto token = parser->tokenReader.AdvanceToken(); - constExpr->token = token; - parser->FillPosition(constExpr.Ptr()); - constExpr->ConstType = ConstantExpr::ConstantType::Bool; - constExpr->integerValue = token.Content == "true" ? 1 : 0; - - return constExpr; + if (!parsedExpr->loc.isValid()) + { + parsedExpr->loc = nameToken.loc; + } + return parsedExpr; } // Default behavior is just to create a name expression @@ -4090,6 +4115,16 @@ namespace Slang #undef MODIFIER + // Add syntax for expression keywords + #define EXPR(KEYWORD, CALLBACK) \ + addBuiltinSyntax<Expr>(session, scope, #KEYWORD, &CALLBACK) + + EXPR(this, parseThisExpr); + EXPR(true, parseTrueExpr); + EXPR(false, parseFalseExpr); + + #undef EXPR + return moduleDecl; } |
