diff --git a/packages/cxx-frontend/src/AST.ts b/packages/cxx-frontend/src/AST.ts index 43f45077..f7301b81 100644 --- a/packages/cxx-frontend/src/AST.ts +++ b/packages/cxx-frontend/src/AST.ts @@ -3831,19 +3831,12 @@ export class MemberExpressionAST extends ExpressionAST { return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser); } - /** - * Returns the location of the template token in this node - */ - getTemplateToken(): Token | undefined { - return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser); - } - /** * Returns the memberId of this node */ getMemberId(): IdExpressionAST | undefined { return AST.from( - cxx.getASTSlot(this.getHandle(), 3), + cxx.getASTSlot(this.getHandle(), 2), this.parser, ); } @@ -3852,7 +3845,7 @@ export class MemberExpressionAST extends ExpressionAST { * Returns the accessOp attribute of this node */ getAccessOp(): TokenKind { - return cxx.getASTSlot(this.getHandle(), 4); + return cxx.getASTSlot(this.getHandle(), 3); } } diff --git a/src/parser/cxx/ast.cc b/src/parser/cxx/ast.cc index a82bee0c..b226b3aa 100644 --- a/src/parser/cxx/ast.cc +++ b/src/parser/cxx/ast.cc @@ -1103,14 +1103,12 @@ auto SubscriptExpressionAST::lastSourceLocation() -> SourceLocation { auto MemberExpressionAST::firstSourceLocation() -> SourceLocation { if (auto loc = cxx::firstSourceLocation(baseExpression)) return loc; if (auto loc = cxx::firstSourceLocation(accessLoc)) return loc; - if (auto loc = cxx::firstSourceLocation(templateLoc)) return loc; if (auto loc = cxx::firstSourceLocation(memberId)) return loc; return {}; } auto MemberExpressionAST::lastSourceLocation() -> SourceLocation { if (auto loc = cxx::lastSourceLocation(memberId)) return loc; - if (auto loc = cxx::lastSourceLocation(templateLoc)) return loc; if (auto loc = cxx::lastSourceLocation(accessLoc)) return loc; if (auto loc = cxx::lastSourceLocation(baseExpression)) return loc; return {}; diff --git a/src/parser/cxx/ast.fbs b/src/parser/cxx/ast.fbs index 7f0ad8d9..f3e79c4c 100644 --- a/src/parser/cxx/ast.fbs +++ b/src/parser/cxx/ast.fbs @@ -1097,7 +1097,6 @@ table MemberExpression /* ExpressionAST */ { member_id: IdExpression; access_op: uint32; access_loc: SourceLocation; - template_loc: SourceLocation; } table PostIncrExpression /* ExpressionAST */ { diff --git a/src/parser/cxx/ast.h b/src/parser/cxx/ast.h index fe4074a1..42a6bf27 100644 --- a/src/parser/cxx/ast.h +++ b/src/parser/cxx/ast.h @@ -1451,7 +1451,6 @@ class MemberExpressionAST final : public ExpressionAST { ExpressionAST* baseExpression = nullptr; SourceLocation accessLoc; - SourceLocation templateLoc; IdExpressionAST* memberId = nullptr; TokenKind accessOp = TokenKind::T_EOF_SYMBOL; diff --git a/src/parser/cxx/ast_cloner.cc b/src/parser/cxx/ast_cloner.cc index 64bd5f51..53b35959 100644 --- a/src/parser/cxx/ast_cloner.cc +++ b/src/parser/cxx/ast_cloner.cc @@ -1444,8 +1444,6 @@ void ASTCloner::visit(MemberExpressionAST* ast) { copy->accessLoc = ast->accessLoc; - copy->templateLoc = ast->templateLoc; - copy->memberId = accept(ast->memberId); copy->accessOp = ast->accessOp; diff --git a/src/parser/cxx/ast_encoder.cc b/src/parser/cxx/ast_encoder.cc index 958d5ba1..c31454e3 100644 --- a/src/parser/cxx/ast_encoder.cc +++ b/src/parser/cxx/ast_encoder.cc @@ -2200,8 +2200,6 @@ void ASTEncoder::visit(MemberExpressionAST* ast) { auto accessLoc = encodeSourceLocation(ast->accessLoc); - auto templateLoc = encodeSourceLocation(ast->templateLoc); - const auto memberId = accept(ast->memberId); io::MemberExpression::Builder builder{fbb_}; @@ -2209,7 +2207,6 @@ void ASTEncoder::visit(MemberExpressionAST* ast) { builder.add_base_expression_type( static_cast(baseExpressionType)); builder.add_access_loc(accessLoc.o); - builder.add_template_loc(templateLoc.o); builder.add_member_id(memberId.o); builder.add_access_op(static_cast(ast->accessOp)); diff --git a/src/parser/cxx/ast_slot.cc b/src/parser/cxx/ast_slot.cc index cef1afc4..eaaee845 100644 --- a/src/parser/cxx/ast_slot.cc +++ b/src/parser/cxx/ast_slot.cc @@ -2140,24 +2140,19 @@ void ASTSlot::visit(MemberExpressionAST* ast) { slotKind_ = ASTSlotKind::kToken; slotNameIndex_ = SlotNameIndex{0}; break; - case 2: // templateLoc - value_ = ast->templateLoc.index(); - slotKind_ = ASTSlotKind::kToken; - slotNameIndex_ = SlotNameIndex{190}; - break; - case 3: // memberId + case 2: // memberId value_ = reinterpret_cast(ast->memberId); slotKind_ = ASTSlotKind::kNode; slotNameIndex_ = SlotNameIndex{129}; break; - case 4: // accessOp + case 3: // accessOp value_ = intptr_t(ast->accessOp); slotKind_ = ASTSlotKind::kIntAttribute; slotNameIndex_ = SlotNameIndex{1}; break; } // switch - slotCount_ = 5; + slotCount_ = 4; } void ASTSlot::visit(PostIncrExpressionAST* ast) { diff --git a/src/parser/cxx/parser.cc b/src/parser/cxx/parser.cc index 1c4084ea..479ff692 100644 --- a/src/parser/cxx/parser.cc +++ b/src/parser/cxx/parser.cc @@ -653,8 +653,8 @@ auto Parser::parse_id_expression(IdExpressionAST*& yyast, bool inRequiresClause) const auto isTemplateIntroduced = match(TokenKind::T_TEMPLATE, templateLoc); UnqualifiedIdAST* unqualifiedId = nullptr; - if (!parse_unqualified_id(unqualifiedId, - /*isTemplateIntroduced*/ false, inRequiresClause)) + if (!parse_unqualified_id(unqualifiedId, isTemplateIntroduced, + inRequiresClause)) return false; lookahead.commit(); @@ -671,10 +671,11 @@ auto Parser::parse_id_expression(IdExpressionAST*& yyast, bool inRequiresClause) } auto Parser::parse_maybe_template_id(UnqualifiedIdAST*& yyast, + bool isTemplateIntroduced, bool inRequiresClause) -> bool { LookaheadParser lookahead{this}; - auto template_id = parse_template_id(yyast); + auto template_id = parse_template_id(yyast, isTemplateIntroduced); if (!template_id) return false; @@ -722,7 +723,8 @@ auto Parser::parse_unqualified_id(UnqualifiedIdAST*& yyast, bool inRequiresClause) -> bool { const auto start = currentLocation(); - if (parse_maybe_template_id(yyast, inRequiresClause)) return true; + if (parse_maybe_template_id(yyast, isTemplateIntroduced, inRequiresClause)) + return true; rewind(start); @@ -1590,8 +1592,6 @@ auto Parser::parse_member_expression(ExpressionAST*& yyast) -> bool { yyast = ast; - // match(TokenKind::T_TEMPLATE, ast->templateLoc); - if (!parse_id_expression(ast->memberId)) parse_error("expected a member name"); @@ -8349,7 +8349,8 @@ auto Parser::parse_function_operator_template_id( return true; } -auto Parser::parse_template_id(UnqualifiedIdAST*& yyast) -> bool { +auto Parser::parse_template_id(UnqualifiedIdAST*& yyast, + bool isTemplateIntroduced) -> bool { if (LiteralOperatorTemplateIdAST* templateName = nullptr; parse_literal_operator_template_id(templateName)) { yyast = templateName; @@ -8359,7 +8360,7 @@ auto Parser::parse_template_id(UnqualifiedIdAST*& yyast) -> bool { yyast = templateName; return true; } else if (SimpleTemplateIdAST* templateName = nullptr; - parse_simple_template_id(templateName)) { + parse_simple_template_id(templateName, isTemplateIntroduced)) { yyast = templateName; return true; } else { diff --git a/src/parser/cxx/parser.h b/src/parser/cxx/parser.h index 7c6f97cd..30ee58a2 100644 --- a/src/parser/cxx/parser.h +++ b/src/parser/cxx/parser.h @@ -128,8 +128,8 @@ class Parser final { [[nodiscard]] auto parse_id_expression(IdExpressionAST*& yyast, bool inRequiresClause = false) -> bool; [[nodiscard]] auto parse_maybe_template_id(UnqualifiedIdAST*& yyast, - bool inRequiresClause = false) - -> bool; + bool isTemplateIntroduced, + bool inRequiresClause) -> bool; [[nodiscard]] auto parse_unqualified_id(UnqualifiedIdAST*& yyast, bool isTemplateIntroduced, bool inRequiresClause) -> bool; @@ -601,7 +601,8 @@ class Parser final { LiteralOperatorTemplateIdAST*& yyast) -> bool; [[nodiscard]] auto parse_function_operator_template_id( OperatorFunctionTemplateIdAST*& yyast) -> bool; - [[nodiscard]] auto parse_template_id(UnqualifiedIdAST*& yyast) -> bool; + [[nodiscard]] auto parse_template_id(UnqualifiedIdAST*& yyast, + bool isTemplateIntroduced) -> bool; [[nodiscard]] auto parse_template_argument_list( List*& yyast) -> bool; [[nodiscard]] auto parse_template_argument(TemplateArgumentAST*& yyast) diff --git a/tests/unit_tests/ast/template_member_expression_01.cc b/tests/unit_tests/ast/template_member_expression_01.cc new file mode 100644 index 00000000..abef9512 --- /dev/null +++ b/tests/unit_tests/ast/template_member_expression_01.cc @@ -0,0 +1,114 @@ +// RUN: %cxx -verify -ast-dump %s | %filecheck %s --match-full-lines + +struct Allocator { + template + auto Allocate(int) -> T * { + return nullptr; + } +}; + +auto copy(Allocator &A) -> void * { return A.template Allocate(128); } +// clang-format off +// CHECK:translation-unit +// CHECK-NEXT: declaration-list +// CHECK-NEXT: simple-declaration +// CHECK-NEXT: decl-specifier-list +// CHECK-NEXT: class-specifier +// CHECK-NEXT: class-key: struct +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: Allocator +// CHECK-NEXT: declaration-list +// CHECK-NEXT: template-declaration +// CHECK-NEXT: template-parameter-list +// CHECK-NEXT: typename-type-parameter +// CHECK-NEXT: identifier: T +// CHECK-NEXT: declaration: function-definition +// CHECK-NEXT: decl-specifier-list +// CHECK-NEXT: auto-type-specifier +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: core-declarator: id-declarator +// CHECK-NEXT: declarator-id: id-expression +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: Allocate +// CHECK-NEXT: declarator-chunk-list +// CHECK-NEXT: function-declarator-chunk +// CHECK-NEXT: parameters-and-qualifiers: parameters-and-qualifiers +// CHECK-NEXT: parameter-declaration-clause: parameter-declaration-clause +// CHECK-NEXT: parameter-declaration-list +// CHECK-NEXT: parameter-declaration +// CHECK-NEXT: type-specifier-list +// CHECK-NEXT: integral-type-specifier +// CHECK-NEXT: specifier: int +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: trailing-return-type: trailing-return-type +// CHECK-NEXT: type-id: type-id +// CHECK-NEXT: type-specifier-list +// CHECK-NEXT: named-type-specifier +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: T +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: ptr-op-list +// CHECK-NEXT: pointer-operator +// CHECK-NEXT: function-body: compound-statement-function-body +// CHECK-NEXT: statement: compound-statement +// CHECK-NEXT: statement-list +// CHECK-NEXT: return-statement +// CHECK-NEXT: expression: nullptr-literal-expression +// CHECK-NEXT: literal: nullptr +// CHECK-NEXT: function-definition +// CHECK-NEXT: decl-specifier-list +// CHECK-NEXT: auto-type-specifier +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: core-declarator: id-declarator +// CHECK-NEXT: declarator-id: id-expression +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: copy +// CHECK-NEXT: declarator-chunk-list +// CHECK-NEXT: function-declarator-chunk +// CHECK-NEXT: parameters-and-qualifiers: parameters-and-qualifiers +// CHECK-NEXT: parameter-declaration-clause: parameter-declaration-clause +// CHECK-NEXT: parameter-declaration-list +// CHECK-NEXT: parameter-declaration +// CHECK-NEXT: type-specifier-list +// CHECK-NEXT: named-type-specifier +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: Allocator +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: ptr-op-list +// CHECK-NEXT: reference-operator +// CHECK-NEXT: ref-op: & +// CHECK-NEXT: core-declarator: id-declarator +// CHECK-NEXT: declarator-id: id-expression +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: A +// CHECK-NEXT: trailing-return-type: trailing-return-type +// CHECK-NEXT: type-id: type-id +// CHECK-NEXT: type-specifier-list +// CHECK-NEXT: void-type-specifier +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: ptr-op-list +// CHECK-NEXT: pointer-operator +// CHECK-NEXT: function-body: compound-statement-function-body +// CHECK-NEXT: statement: compound-statement +// CHECK-NEXT: statement-list +// CHECK-NEXT: return-statement +// CHECK-NEXT: expression: call-expression +// CHECK-NEXT: base-expression: member-expression +// CHECK-NEXT: access-op: . +// CHECK-NEXT: base-expression: id-expression +// CHECK-NEXT: unqualified-id: name-id +// CHECK-NEXT: identifier: A +// CHECK-NEXT: member-id: id-expression +// CHECK-NEXT: is-template-introduced: true +// CHECK-NEXT: unqualified-id: simple-template-id +// CHECK-NEXT: identifier: Allocate +// CHECK-NEXT: template-argument-list +// CHECK-NEXT: type-template-argument +// CHECK-NEXT: type-id: type-id +// CHECK-NEXT: type-specifier-list +// CHECK-NEXT: integral-type-specifier +// CHECK-NEXT: specifier: char +// CHECK-NEXT: declarator: declarator +// CHECK-NEXT: expression-list +// CHECK-NEXT: int-literal-expression +// CHECK-NEXT: literal: 128