1 //===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements parsing of C++ templates.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/Parse/ParseDiagnostic.h"
17 #include "clang/Parse/Parser.h"
18 #include "clang/Parse/RAIIObjectsForParser.h"
19 #include "clang/Sema/DeclSpec.h"
20 #include "clang/Sema/ParsedTemplate.h"
21 #include "clang/Sema/Scope.h"
22 #include "llvm/Support/TimeProfiler.h"
23 using namespace clang;
24
25 /// Re-enter a possible template scope, creating as many template parameter
26 /// scopes as necessary.
27 /// \return The number of template parameter scopes entered.
ReenterTemplateScopes(MultiParseScope & S,Decl * D)28 unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) {
29 return Actions.ActOnReenterTemplateScope(D, [&] {
30 S.Enter(Scope::TemplateParamScope);
31 return Actions.getCurScope();
32 });
33 }
34
35 /// Parse a template declaration, explicit instantiation, or
36 /// explicit specialization.
ParseDeclarationStartingWithTemplate(DeclaratorContext Context,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)37 Decl *Parser::ParseDeclarationStartingWithTemplate(
38 DeclaratorContext Context, SourceLocation &DeclEnd,
39 ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
40 ObjCDeclContextSwitch ObjCDC(*this);
41
42 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
43 return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(),
44 DeclEnd, AccessAttrs, AS);
45 }
46 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs,
47 AS);
48 }
49
50 /// Parse a template declaration or an explicit specialization.
51 ///
52 /// Template declarations include one or more template parameter lists
53 /// and either the function or class template declaration. Explicit
54 /// specializations contain one or more 'template < >' prefixes
55 /// followed by a (possibly templated) declaration. Since the
56 /// syntactic form of both features is nearly identical, we parse all
57 /// of the template headers together and let semantic analysis sort
58 /// the declarations from the explicit specializations.
59 ///
60 /// template-declaration: [C++ temp]
61 /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
62 ///
63 /// template-declaration: [C++2a]
64 /// template-head declaration
65 /// template-head concept-definition
66 ///
67 /// TODO: requires-clause
68 /// template-head: [C++2a]
69 /// 'template' '<' template-parameter-list '>'
70 /// requires-clause[opt]
71 ///
72 /// explicit-specialization: [ C++ temp.expl.spec]
73 /// 'template' '<' '>' declaration
ParseTemplateDeclarationOrSpecialization(DeclaratorContext Context,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)74 Decl *Parser::ParseTemplateDeclarationOrSpecialization(
75 DeclaratorContext Context, SourceLocation &DeclEnd,
76 ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
77 assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
78 "Token does not start a template declaration.");
79
80 MultiParseScope TemplateParamScopes(*this);
81
82 // Tell the action that names should be checked in the context of
83 // the declaration to come.
84 ParsingDeclRAIIObject
85 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
86
87 // Parse multiple levels of template headers within this template
88 // parameter scope, e.g.,
89 //
90 // template<typename T>
91 // template<typename U>
92 // class A<T>::B { ... };
93 //
94 // We parse multiple levels non-recursively so that we can build a
95 // single data structure containing all of the template parameter
96 // lists to easily differentiate between the case above and:
97 //
98 // template<typename T>
99 // class A {
100 // template<typename U> class B;
101 // };
102 //
103 // In the first case, the action for declaring A<T>::B receives
104 // both template parameter lists. In the second case, the action for
105 // defining A<T>::B receives just the inner template parameter list
106 // (and retrieves the outer template parameter list from its
107 // context).
108 bool isSpecialization = true;
109 bool LastParamListWasEmpty = false;
110 TemplateParameterLists ParamLists;
111 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
112
113 do {
114 // Consume the 'export', if any.
115 SourceLocation ExportLoc;
116 TryConsumeToken(tok::kw_export, ExportLoc);
117
118 // Consume the 'template', which should be here.
119 SourceLocation TemplateLoc;
120 if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
121 Diag(Tok.getLocation(), diag::err_expected_template);
122 return nullptr;
123 }
124
125 // Parse the '<' template-parameter-list '>'
126 SourceLocation LAngleLoc, RAngleLoc;
127 SmallVector<NamedDecl*, 4> TemplateParams;
128 if (ParseTemplateParameters(TemplateParamScopes,
129 CurTemplateDepthTracker.getDepth(),
130 TemplateParams, LAngleLoc, RAngleLoc)) {
131 // Skip until the semi-colon or a '}'.
132 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
133 TryConsumeToken(tok::semi);
134 return nullptr;
135 }
136
137 ExprResult OptionalRequiresClauseConstraintER;
138 if (!TemplateParams.empty()) {
139 isSpecialization = false;
140 ++CurTemplateDepthTracker;
141
142 if (TryConsumeToken(tok::kw_requires)) {
143 OptionalRequiresClauseConstraintER =
144 Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression(
145 /*IsTrailingRequiresClause=*/false));
146 if (!OptionalRequiresClauseConstraintER.isUsable()) {
147 // Skip until the semi-colon or a '}'.
148 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
149 TryConsumeToken(tok::semi);
150 return nullptr;
151 }
152 }
153 } else {
154 LastParamListWasEmpty = true;
155 }
156
157 ParamLists.push_back(Actions.ActOnTemplateParameterList(
158 CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc,
159 TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get()));
160 } while (Tok.isOneOf(tok::kw_export, tok::kw_template));
161
162 // Parse the actual template declaration.
163 if (Tok.is(tok::kw_concept))
164 return ParseConceptDefinition(
165 ParsedTemplateInfo(&ParamLists, isSpecialization,
166 LastParamListWasEmpty),
167 DeclEnd);
168
169 return ParseSingleDeclarationAfterTemplate(
170 Context,
171 ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty),
172 ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
173 }
174
175 /// Parse a single declaration that declares a template,
176 /// template specialization, or explicit instantiation of a template.
177 ///
178 /// \param DeclEnd will receive the source location of the last token
179 /// within this declaration.
180 ///
181 /// \param AS the access specifier associated with this
182 /// declaration. Will be AS_none for namespace-scope declarations.
183 ///
184 /// \returns the new declaration.
ParseSingleDeclarationAfterTemplate(DeclaratorContext Context,const ParsedTemplateInfo & TemplateInfo,ParsingDeclRAIIObject & DiagsFromTParams,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)185 Decl *Parser::ParseSingleDeclarationAfterTemplate(
186 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
187 ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd,
188 ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
189 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
190 "Template information required");
191
192 if (Tok.is(tok::kw_static_assert)) {
193 // A static_assert declaration may not be templated.
194 Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
195 << TemplateInfo.getSourceRange();
196 // Parse the static_assert declaration to improve error recovery.
197 return ParseStaticAssertDeclaration(DeclEnd);
198 }
199
200 if (Context == DeclaratorContext::Member) {
201 // We are parsing a member template.
202 ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
203 &DiagsFromTParams);
204 return nullptr;
205 }
206
207 ParsedAttributesWithRange prefixAttrs(AttrFactory);
208 MaybeParseCXX11Attributes(prefixAttrs);
209
210 if (Tok.is(tok::kw_using)) {
211 auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
212 prefixAttrs);
213 if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl())
214 return nullptr;
215 return usingDeclPtr.get().getSingleDecl();
216 }
217
218 // Parse the declaration specifiers, stealing any diagnostics from
219 // the template parameters.
220 ParsingDeclSpec DS(*this, &DiagsFromTParams);
221
222 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
223 getDeclSpecContextFromDeclaratorContext(Context));
224
225 if (Tok.is(tok::semi)) {
226 ProhibitAttributes(prefixAttrs);
227 DeclEnd = ConsumeToken();
228 RecordDecl *AnonRecord = nullptr;
229 Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
230 getCurScope(), AS, DS,
231 TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
232 : MultiTemplateParamsArg(),
233 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation,
234 AnonRecord);
235 assert(!AnonRecord &&
236 "Anonymous unions/structs should not be valid with template");
237 DS.complete(Decl);
238 return Decl;
239 }
240
241 // Move the attributes from the prefix into the DS.
242 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
243 ProhibitAttributes(prefixAttrs);
244 else
245 DS.takeAttributesFrom(prefixAttrs);
246
247 // Parse the declarator.
248 ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context);
249 if (TemplateInfo.TemplateParams)
250 DeclaratorInfo.setTemplateParameterLists(*TemplateInfo.TemplateParams);
251 ParseDeclarator(DeclaratorInfo);
252 // Error parsing the declarator?
253 if (!DeclaratorInfo.hasName()) {
254 // If so, skip until the semi-colon or a }.
255 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
256 if (Tok.is(tok::semi))
257 ConsumeToken();
258 return nullptr;
259 }
260
261 llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() {
262 return std::string(DeclaratorInfo.getIdentifier() != nullptr
263 ? DeclaratorInfo.getIdentifier()->getName()
264 : "<unknown>");
265 });
266
267 LateParsedAttrList LateParsedAttrs(true);
268 if (DeclaratorInfo.isFunctionDeclarator()) {
269 if (Tok.is(tok::kw_requires))
270 ParseTrailingRequiresClause(DeclaratorInfo);
271
272 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
273 }
274
275 if (DeclaratorInfo.isFunctionDeclarator() &&
276 isStartOfFunctionDefinition(DeclaratorInfo)) {
277
278 // Function definitions are only allowed at file scope and in C++ classes.
279 // The C++ inline method definition case is handled elsewhere, so we only
280 // need to handle the file scope definition case.
281 if (Context != DeclaratorContext::File) {
282 Diag(Tok, diag::err_function_definition_not_allowed);
283 SkipMalformedDecl();
284 return nullptr;
285 }
286
287 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
288 // Recover by ignoring the 'typedef'. This was probably supposed to be
289 // the 'typename' keyword, which we should have already suggested adding
290 // if it's appropriate.
291 Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
292 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
293 DS.ClearStorageClassSpecs();
294 }
295
296 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
297 if (DeclaratorInfo.getName().getKind() !=
298 UnqualifiedIdKind::IK_TemplateId) {
299 // If the declarator-id is not a template-id, issue a diagnostic and
300 // recover by ignoring the 'template' keyword.
301 Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
302 return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
303 &LateParsedAttrs);
304 } else {
305 SourceLocation LAngleLoc
306 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
307 Diag(DeclaratorInfo.getIdentifierLoc(),
308 diag::err_explicit_instantiation_with_definition)
309 << SourceRange(TemplateInfo.TemplateLoc)
310 << FixItHint::CreateInsertion(LAngleLoc, "<>");
311
312 // Recover as if it were an explicit specialization.
313 TemplateParameterLists FakedParamLists;
314 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
315 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
316 LAngleLoc, nullptr));
317
318 return ParseFunctionDefinition(
319 DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
320 /*isSpecialization=*/true,
321 /*lastParameterListWasEmpty=*/true),
322 &LateParsedAttrs);
323 }
324 }
325 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
326 &LateParsedAttrs);
327 }
328
329 // Parse this declaration.
330 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
331 TemplateInfo);
332
333 if (Tok.is(tok::comma)) {
334 Diag(Tok, diag::err_multiple_template_declarators)
335 << (int)TemplateInfo.Kind;
336 SkipUntil(tok::semi);
337 return ThisDecl;
338 }
339
340 // Eat the semi colon after the declaration.
341 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
342 if (LateParsedAttrs.size() > 0)
343 ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
344 DeclaratorInfo.complete(ThisDecl);
345 return ThisDecl;
346 }
347
348 /// \brief Parse a single declaration that declares a concept.
349 ///
350 /// \param DeclEnd will receive the source location of the last token
351 /// within this declaration.
352 ///
353 /// \returns the new declaration.
354 Decl *
ParseConceptDefinition(const ParsedTemplateInfo & TemplateInfo,SourceLocation & DeclEnd)355 Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo,
356 SourceLocation &DeclEnd) {
357 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
358 "Template information required");
359 assert(Tok.is(tok::kw_concept) &&
360 "ParseConceptDefinition must be called when at a 'concept' keyword");
361
362 ConsumeToken(); // Consume 'concept'
363
364 SourceLocation BoolKWLoc;
365 if (TryConsumeToken(tok::kw_bool, BoolKWLoc))
366 Diag(Tok.getLocation(), diag::ext_concept_legacy_bool_keyword) <<
367 FixItHint::CreateRemoval(SourceLocation(BoolKWLoc));
368
369 DiagnoseAndSkipCXX11Attributes();
370
371 CXXScopeSpec SS;
372 if (ParseOptionalCXXScopeSpecifier(
373 SS, /*ObjectType=*/nullptr,
374 /*ObjectHadErrors=*/false, /*EnteringContext=*/false,
375 /*MayBePseudoDestructor=*/nullptr,
376 /*IsTypename=*/false, /*LastII=*/nullptr, /*OnlyNamespace=*/true) ||
377 SS.isInvalid()) {
378 SkipUntil(tok::semi);
379 return nullptr;
380 }
381
382 if (SS.isNotEmpty())
383 Diag(SS.getBeginLoc(),
384 diag::err_concept_definition_not_identifier);
385
386 UnqualifiedId Result;
387 if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr,
388 /*ObjectHadErrors=*/false, /*EnteringContext=*/false,
389 /*AllowDestructorName=*/false,
390 /*AllowConstructorName=*/false,
391 /*AllowDeductionGuide=*/false,
392 /*TemplateKWLoc=*/nullptr, Result)) {
393 SkipUntil(tok::semi);
394 return nullptr;
395 }
396
397 if (Result.getKind() != UnqualifiedIdKind::IK_Identifier) {
398 Diag(Result.getBeginLoc(), diag::err_concept_definition_not_identifier);
399 SkipUntil(tok::semi);
400 return nullptr;
401 }
402
403 IdentifierInfo *Id = Result.Identifier;
404 SourceLocation IdLoc = Result.getBeginLoc();
405
406 DiagnoseAndSkipCXX11Attributes();
407
408 if (!TryConsumeToken(tok::equal)) {
409 Diag(Tok.getLocation(), diag::err_expected) << tok::equal;
410 SkipUntil(tok::semi);
411 return nullptr;
412 }
413
414 ExprResult ConstraintExprResult =
415 Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression());
416 if (ConstraintExprResult.isInvalid()) {
417 SkipUntil(tok::semi);
418 return nullptr;
419 }
420
421 DeclEnd = Tok.getLocation();
422 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
423 Expr *ConstraintExpr = ConstraintExprResult.get();
424 return Actions.ActOnConceptDefinition(getCurScope(),
425 *TemplateInfo.TemplateParams,
426 Id, IdLoc, ConstraintExpr);
427 }
428
429 /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
430 /// angle brackets. Depth is the depth of this template-parameter-list, which
431 /// is the number of template headers directly enclosing this template header.
432 /// TemplateParams is the current list of template parameters we're building.
433 /// The template parameter we parse will be added to this list. LAngleLoc and
434 /// RAngleLoc will receive the positions of the '<' and '>', respectively,
435 /// that enclose this template parameter list.
436 ///
437 /// \returns true if an error occurred, false otherwise.
ParseTemplateParameters(MultiParseScope & TemplateScopes,unsigned Depth,SmallVectorImpl<NamedDecl * > & TemplateParams,SourceLocation & LAngleLoc,SourceLocation & RAngleLoc)438 bool Parser::ParseTemplateParameters(
439 MultiParseScope &TemplateScopes, unsigned Depth,
440 SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc,
441 SourceLocation &RAngleLoc) {
442 // Get the template parameter list.
443 if (!TryConsumeToken(tok::less, LAngleLoc)) {
444 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
445 return true;
446 }
447
448 // Try to parse the template parameter list.
449 bool Failed = false;
450 // FIXME: Missing greatergreatergreater support.
451 if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) {
452 TemplateScopes.Enter(Scope::TemplateParamScope);
453 Failed = ParseTemplateParameterList(Depth, TemplateParams);
454 }
455
456 if (Tok.is(tok::greatergreater)) {
457 // No diagnostic required here: a template-parameter-list can only be
458 // followed by a declaration or, for a template template parameter, the
459 // 'class' keyword. Therefore, the second '>' will be diagnosed later.
460 // This matters for elegant diagnosis of:
461 // template<template<typename>> struct S;
462 Tok.setKind(tok::greater);
463 RAngleLoc = Tok.getLocation();
464 Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
465 } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
466 Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
467 return true;
468 }
469 return false;
470 }
471
472 /// ParseTemplateParameterList - Parse a template parameter list. If
473 /// the parsing fails badly (i.e., closing bracket was left out), this
474 /// will try to put the token stream in a reasonable position (closing
475 /// a statement, etc.) and return false.
476 ///
477 /// template-parameter-list: [C++ temp]
478 /// template-parameter
479 /// template-parameter-list ',' template-parameter
480 bool
ParseTemplateParameterList(const unsigned Depth,SmallVectorImpl<NamedDecl * > & TemplateParams)481 Parser::ParseTemplateParameterList(const unsigned Depth,
482 SmallVectorImpl<NamedDecl*> &TemplateParams) {
483 while (1) {
484
485 if (NamedDecl *TmpParam
486 = ParseTemplateParameter(Depth, TemplateParams.size())) {
487 TemplateParams.push_back(TmpParam);
488 } else {
489 // If we failed to parse a template parameter, skip until we find
490 // a comma or closing brace.
491 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
492 StopAtSemi | StopBeforeMatch);
493 }
494
495 // Did we find a comma or the end of the template parameter list?
496 if (Tok.is(tok::comma)) {
497 ConsumeToken();
498 } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
499 // Don't consume this... that's done by template parser.
500 break;
501 } else {
502 // Somebody probably forgot to close the template. Skip ahead and
503 // try to get out of the expression. This error is currently
504 // subsumed by whatever goes on in ParseTemplateParameter.
505 Diag(Tok.getLocation(), diag::err_expected_comma_greater);
506 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
507 StopAtSemi | StopBeforeMatch);
508 return false;
509 }
510 }
511 return true;
512 }
513
514 /// Determine whether the parser is at the start of a template
515 /// type parameter.
isStartOfTemplateTypeParameter()516 Parser::TPResult Parser::isStartOfTemplateTypeParameter() {
517 if (Tok.is(tok::kw_class)) {
518 // "class" may be the start of an elaborated-type-specifier or a
519 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
520 switch (NextToken().getKind()) {
521 case tok::equal:
522 case tok::comma:
523 case tok::greater:
524 case tok::greatergreater:
525 case tok::ellipsis:
526 return TPResult::True;
527
528 case tok::identifier:
529 // This may be either a type-parameter or an elaborated-type-specifier.
530 // We have to look further.
531 break;
532
533 default:
534 return TPResult::False;
535 }
536
537 switch (GetLookAheadToken(2).getKind()) {
538 case tok::equal:
539 case tok::comma:
540 case tok::greater:
541 case tok::greatergreater:
542 return TPResult::True;
543
544 default:
545 return TPResult::False;
546 }
547 }
548
549 if (TryAnnotateTypeConstraint())
550 return TPResult::Error;
551
552 if (isTypeConstraintAnnotation() &&
553 // Next token might be 'auto' or 'decltype', indicating that this
554 // type-constraint is in fact part of a placeholder-type-specifier of a
555 // non-type template parameter.
556 !GetLookAheadToken(Tok.is(tok::annot_cxxscope) ? 2 : 1)
557 .isOneOf(tok::kw_auto, tok::kw_decltype))
558 return TPResult::True;
559
560 // 'typedef' is a reasonably-common typo/thinko for 'typename', and is
561 // ill-formed otherwise.
562 if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef))
563 return TPResult::False;
564
565 // C++ [temp.param]p2:
566 // There is no semantic difference between class and typename in a
567 // template-parameter. typename followed by an unqualified-id
568 // names a template type parameter. typename followed by a
569 // qualified-id denotes the type in a non-type
570 // parameter-declaration.
571 Token Next = NextToken();
572
573 // If we have an identifier, skip over it.
574 if (Next.getKind() == tok::identifier)
575 Next = GetLookAheadToken(2);
576
577 switch (Next.getKind()) {
578 case tok::equal:
579 case tok::comma:
580 case tok::greater:
581 case tok::greatergreater:
582 case tok::ellipsis:
583 return TPResult::True;
584
585 case tok::kw_typename:
586 case tok::kw_typedef:
587 case tok::kw_class:
588 // These indicate that a comma was missed after a type parameter, not that
589 // we have found a non-type parameter.
590 return TPResult::True;
591
592 default:
593 return TPResult::False;
594 }
595 }
596
597 /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
598 ///
599 /// template-parameter: [C++ temp.param]
600 /// type-parameter
601 /// parameter-declaration
602 ///
603 /// type-parameter: (See below)
604 /// type-parameter-key ...[opt] identifier[opt]
605 /// type-parameter-key identifier[opt] = type-id
606 /// (C++2a) type-constraint ...[opt] identifier[opt]
607 /// (C++2a) type-constraint identifier[opt] = type-id
608 /// 'template' '<' template-parameter-list '>' type-parameter-key
609 /// ...[opt] identifier[opt]
610 /// 'template' '<' template-parameter-list '>' type-parameter-key
611 /// identifier[opt] '=' id-expression
612 ///
613 /// type-parameter-key:
614 /// class
615 /// typename
616 ///
ParseTemplateParameter(unsigned Depth,unsigned Position)617 NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
618
619 switch (isStartOfTemplateTypeParameter()) {
620 case TPResult::True:
621 // Is there just a typo in the input code? ('typedef' instead of
622 // 'typename')
623 if (Tok.is(tok::kw_typedef)) {
624 Diag(Tok.getLocation(), diag::err_expected_template_parameter);
625
626 Diag(Tok.getLocation(), diag::note_meant_to_use_typename)
627 << FixItHint::CreateReplacement(CharSourceRange::getCharRange(
628 Tok.getLocation(),
629 Tok.getEndLoc()),
630 "typename");
631
632 Tok.setKind(tok::kw_typename);
633 }
634
635 return ParseTypeParameter(Depth, Position);
636 case TPResult::False:
637 break;
638
639 case TPResult::Error: {
640 // We return an invalid parameter as opposed to null to avoid having bogus
641 // diagnostics about an empty template parameter list.
642 // FIXME: Fix ParseTemplateParameterList to better handle nullptr results
643 // from here.
644 // Return a NTTP as if there was an error in a scope specifier, the user
645 // probably meant to write the type of a NTTP.
646 DeclSpec DS(getAttrFactory());
647 DS.SetTypeSpecError();
648 Declarator D(DS, DeclaratorContext::TemplateParam);
649 D.SetIdentifier(nullptr, Tok.getLocation());
650 D.setInvalidType(true);
651 NamedDecl *ErrorParam = Actions.ActOnNonTypeTemplateParameter(
652 getCurScope(), D, Depth, Position, /*EqualLoc=*/SourceLocation(),
653 /*DefaultArg=*/nullptr);
654 ErrorParam->setInvalidDecl(true);
655 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
656 StopAtSemi | StopBeforeMatch);
657 return ErrorParam;
658 }
659
660 case TPResult::Ambiguous:
661 llvm_unreachable("template param classification can't be ambiguous");
662 }
663
664 if (Tok.is(tok::kw_template))
665 return ParseTemplateTemplateParameter(Depth, Position);
666
667 // If it's none of the above, then it must be a parameter declaration.
668 // NOTE: This will pick up errors in the closure of the template parameter
669 // list (e.g., template < ; Check here to implement >> style closures.
670 return ParseNonTypeTemplateParameter(Depth, Position);
671 }
672
673 /// Check whether the current token is a template-id annotation denoting a
674 /// type-constraint.
isTypeConstraintAnnotation()675 bool Parser::isTypeConstraintAnnotation() {
676 const Token &T = Tok.is(tok::annot_cxxscope) ? NextToken() : Tok;
677 if (T.isNot(tok::annot_template_id))
678 return false;
679 const auto *ExistingAnnot =
680 static_cast<TemplateIdAnnotation *>(T.getAnnotationValue());
681 return ExistingAnnot->Kind == TNK_Concept_template;
682 }
683
684 /// Try parsing a type-constraint at the current location.
685 ///
686 /// type-constraint:
687 /// nested-name-specifier[opt] concept-name
688 /// nested-name-specifier[opt] concept-name
689 /// '<' template-argument-list[opt] '>'[opt]
690 ///
691 /// \returns true if an error occurred, and false otherwise.
TryAnnotateTypeConstraint()692 bool Parser::TryAnnotateTypeConstraint() {
693 if (!getLangOpts().CPlusPlus20)
694 return false;
695 CXXScopeSpec SS;
696 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
697 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
698 /*ObjectHadErrors=*/false,
699 /*EnteringContext=*/false,
700 /*MayBePseudoDestructor=*/nullptr,
701 // If this is not a type-constraint, then
702 // this scope-spec is part of the typename
703 // of a non-type template parameter
704 /*IsTypename=*/true, /*LastII=*/nullptr,
705 // We won't find concepts in
706 // non-namespaces anyway, so might as well
707 // parse this correctly for possible type
708 // names.
709 /*OnlyNamespace=*/false))
710 return true;
711
712 if (Tok.is(tok::identifier)) {
713 UnqualifiedId PossibleConceptName;
714 PossibleConceptName.setIdentifier(Tok.getIdentifierInfo(),
715 Tok.getLocation());
716
717 TemplateTy PossibleConcept;
718 bool MemberOfUnknownSpecialization = false;
719 auto TNK = Actions.isTemplateName(getCurScope(), SS,
720 /*hasTemplateKeyword=*/false,
721 PossibleConceptName,
722 /*ObjectType=*/ParsedType(),
723 /*EnteringContext=*/false,
724 PossibleConcept,
725 MemberOfUnknownSpecialization,
726 /*Disambiguation=*/true);
727 if (MemberOfUnknownSpecialization || !PossibleConcept ||
728 TNK != TNK_Concept_template) {
729 if (SS.isNotEmpty())
730 AnnotateScopeToken(SS, !WasScopeAnnotation);
731 return false;
732 }
733
734 // At this point we're sure we're dealing with a constrained parameter. It
735 // may or may not have a template parameter list following the concept
736 // name.
737 if (AnnotateTemplateIdToken(PossibleConcept, TNK, SS,
738 /*TemplateKWLoc=*/SourceLocation(),
739 PossibleConceptName,
740 /*AllowTypeAnnotation=*/false,
741 /*TypeConstraint=*/true))
742 return true;
743 }
744
745 if (SS.isNotEmpty())
746 AnnotateScopeToken(SS, !WasScopeAnnotation);
747 return false;
748 }
749
750 /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
751 /// Other kinds of template parameters are parsed in
752 /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
753 ///
754 /// type-parameter: [C++ temp.param]
755 /// 'class' ...[opt][C++0x] identifier[opt]
756 /// 'class' identifier[opt] '=' type-id
757 /// 'typename' ...[opt][C++0x] identifier[opt]
758 /// 'typename' identifier[opt] '=' type-id
ParseTypeParameter(unsigned Depth,unsigned Position)759 NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
760 assert((Tok.isOneOf(tok::kw_class, tok::kw_typename) ||
761 isTypeConstraintAnnotation()) &&
762 "A type-parameter starts with 'class', 'typename' or a "
763 "type-constraint");
764
765 CXXScopeSpec TypeConstraintSS;
766 TemplateIdAnnotation *TypeConstraint = nullptr;
767 bool TypenameKeyword = false;
768 SourceLocation KeyLoc;
769 ParseOptionalCXXScopeSpecifier(TypeConstraintSS, /*ObjectType=*/nullptr,
770 /*ObjectHadErrors=*/false,
771 /*EnteringContext*/ false);
772 if (Tok.is(tok::annot_template_id)) {
773 // Consume the 'type-constraint'.
774 TypeConstraint =
775 static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
776 assert(TypeConstraint->Kind == TNK_Concept_template &&
777 "stray non-concept template-id annotation");
778 KeyLoc = ConsumeAnnotationToken();
779 } else {
780 assert(TypeConstraintSS.isEmpty() &&
781 "expected type constraint after scope specifier");
782
783 // Consume the 'class' or 'typename' keyword.
784 TypenameKeyword = Tok.is(tok::kw_typename);
785 KeyLoc = ConsumeToken();
786 }
787
788 // Grab the ellipsis (if given).
789 SourceLocation EllipsisLoc;
790 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
791 Diag(EllipsisLoc,
792 getLangOpts().CPlusPlus11
793 ? diag::warn_cxx98_compat_variadic_templates
794 : diag::ext_variadic_templates);
795 }
796
797 // Grab the template parameter name (if given)
798 SourceLocation NameLoc = Tok.getLocation();
799 IdentifierInfo *ParamName = nullptr;
800 if (Tok.is(tok::identifier)) {
801 ParamName = Tok.getIdentifierInfo();
802 ConsumeToken();
803 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
804 tok::greatergreater)) {
805 // Unnamed template parameter. Don't have to do anything here, just
806 // don't consume this token.
807 } else {
808 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
809 return nullptr;
810 }
811
812 // Recover from misplaced ellipsis.
813 bool AlreadyHasEllipsis = EllipsisLoc.isValid();
814 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
815 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
816
817 // Grab a default argument (if available).
818 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
819 // we introduce the type parameter into the local scope.
820 SourceLocation EqualLoc;
821 ParsedType DefaultArg;
822 if (TryConsumeToken(tok::equal, EqualLoc))
823 DefaultArg =
824 ParseTypeName(/*Range=*/nullptr, DeclaratorContext::TemplateTypeArg)
825 .get();
826
827 NamedDecl *NewDecl = Actions.ActOnTypeParameter(getCurScope(),
828 TypenameKeyword, EllipsisLoc,
829 KeyLoc, ParamName, NameLoc,
830 Depth, Position, EqualLoc,
831 DefaultArg,
832 TypeConstraint != nullptr);
833
834 if (TypeConstraint) {
835 Actions.ActOnTypeConstraint(TypeConstraintSS, TypeConstraint,
836 cast<TemplateTypeParmDecl>(NewDecl),
837 EllipsisLoc);
838 }
839
840 return NewDecl;
841 }
842
843 /// ParseTemplateTemplateParameter - Handle the parsing of template
844 /// template parameters.
845 ///
846 /// type-parameter: [C++ temp.param]
847 /// 'template' '<' template-parameter-list '>' type-parameter-key
848 /// ...[opt] identifier[opt]
849 /// 'template' '<' template-parameter-list '>' type-parameter-key
850 /// identifier[opt] = id-expression
851 /// type-parameter-key:
852 /// 'class'
853 /// 'typename' [C++1z]
854 NamedDecl *
ParseTemplateTemplateParameter(unsigned Depth,unsigned Position)855 Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
856 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
857
858 // Handle the template <...> part.
859 SourceLocation TemplateLoc = ConsumeToken();
860 SmallVector<NamedDecl*,8> TemplateParams;
861 SourceLocation LAngleLoc, RAngleLoc;
862 {
863 MultiParseScope TemplateParmScope(*this);
864 if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams,
865 LAngleLoc, RAngleLoc)) {
866 return nullptr;
867 }
868 }
869
870 // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
871 // Generate a meaningful error if the user forgot to put class before the
872 // identifier, comma, or greater. Provide a fixit if the identifier, comma,
873 // or greater appear immediately or after 'struct'. In the latter case,
874 // replace the keyword with 'class'.
875 if (!TryConsumeToken(tok::kw_class)) {
876 bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
877 const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
878 if (Tok.is(tok::kw_typename)) {
879 Diag(Tok.getLocation(),
880 getLangOpts().CPlusPlus17
881 ? diag::warn_cxx14_compat_template_template_param_typename
882 : diag::ext_template_template_param_typename)
883 << (!getLangOpts().CPlusPlus17
884 ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
885 : FixItHint());
886 } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
887 tok::greatergreater, tok::ellipsis)) {
888 Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
889 << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
890 : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
891 } else
892 Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
893
894 if (Replace)
895 ConsumeToken();
896 }
897
898 // Parse the ellipsis, if given.
899 SourceLocation EllipsisLoc;
900 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
901 Diag(EllipsisLoc,
902 getLangOpts().CPlusPlus11
903 ? diag::warn_cxx98_compat_variadic_templates
904 : diag::ext_variadic_templates);
905
906 // Get the identifier, if given.
907 SourceLocation NameLoc = Tok.getLocation();
908 IdentifierInfo *ParamName = nullptr;
909 if (Tok.is(tok::identifier)) {
910 ParamName = Tok.getIdentifierInfo();
911 ConsumeToken();
912 } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
913 tok::greatergreater)) {
914 // Unnamed template parameter. Don't have to do anything here, just
915 // don't consume this token.
916 } else {
917 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
918 return nullptr;
919 }
920
921 // Recover from misplaced ellipsis.
922 bool AlreadyHasEllipsis = EllipsisLoc.isValid();
923 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
924 DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
925
926 TemplateParameterList *ParamList =
927 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
928 TemplateLoc, LAngleLoc,
929 TemplateParams,
930 RAngleLoc, nullptr);
931
932 // Grab a default argument (if available).
933 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
934 // we introduce the template parameter into the local scope.
935 SourceLocation EqualLoc;
936 ParsedTemplateArgument DefaultArg;
937 if (TryConsumeToken(tok::equal, EqualLoc)) {
938 DefaultArg = ParseTemplateTemplateArgument();
939 if (DefaultArg.isInvalid()) {
940 Diag(Tok.getLocation(),
941 diag::err_default_template_template_parameter_not_template);
942 SkipUntil(tok::comma, tok::greater, tok::greatergreater,
943 StopAtSemi | StopBeforeMatch);
944 }
945 }
946
947 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
948 ParamList, EllipsisLoc,
949 ParamName, NameLoc, Depth,
950 Position, EqualLoc, DefaultArg);
951 }
952
953 /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
954 /// template parameters (e.g., in "template<int Size> class array;").
955 ///
956 /// template-parameter:
957 /// ...
958 /// parameter-declaration
959 NamedDecl *
ParseNonTypeTemplateParameter(unsigned Depth,unsigned Position)960 Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
961 // Parse the declaration-specifiers (i.e., the type).
962 // FIXME: The type should probably be restricted in some way... Not all
963 // declarators (parts of declarators?) are accepted for parameters.
964 DeclSpec DS(AttrFactory);
965 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
966 DeclSpecContext::DSC_template_param);
967
968 // Parse this as a typename.
969 Declarator ParamDecl(DS, DeclaratorContext::TemplateParam);
970 ParseDeclarator(ParamDecl);
971 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
972 Diag(Tok.getLocation(), diag::err_expected_template_parameter);
973 return nullptr;
974 }
975
976 // Recover from misplaced ellipsis.
977 SourceLocation EllipsisLoc;
978 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
979 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
980
981 // If there is a default value, parse it.
982 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
983 // we introduce the template parameter into the local scope.
984 SourceLocation EqualLoc;
985 ExprResult DefaultArg;
986 if (TryConsumeToken(tok::equal, EqualLoc)) {
987 // C++ [temp.param]p15:
988 // When parsing a default template-argument for a non-type
989 // template-parameter, the first non-nested > is taken as the
990 // end of the template-parameter-list rather than a greater-than
991 // operator.
992 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
993 EnterExpressionEvaluationContext ConstantEvaluated(
994 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
995
996 DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
997 if (DefaultArg.isInvalid())
998 SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
999 }
1000
1001 // Create the parameter.
1002 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
1003 Depth, Position, EqualLoc,
1004 DefaultArg.get());
1005 }
1006
DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,SourceLocation CorrectLoc,bool AlreadyHasEllipsis,bool IdentifierHasName)1007 void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
1008 SourceLocation CorrectLoc,
1009 bool AlreadyHasEllipsis,
1010 bool IdentifierHasName) {
1011 FixItHint Insertion;
1012 if (!AlreadyHasEllipsis)
1013 Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
1014 Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
1015 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
1016 << !IdentifierHasName;
1017 }
1018
DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,Declarator & D)1019 void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
1020 Declarator &D) {
1021 assert(EllipsisLoc.isValid());
1022 bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
1023 if (!AlreadyHasEllipsis)
1024 D.setEllipsisLoc(EllipsisLoc);
1025 DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
1026 AlreadyHasEllipsis, D.hasName());
1027 }
1028
1029 /// Parses a '>' at the end of a template list.
1030 ///
1031 /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
1032 /// to determine if these tokens were supposed to be a '>' followed by
1033 /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
1034 ///
1035 /// \param RAngleLoc the location of the consumed '>'.
1036 ///
1037 /// \param ConsumeLastToken if true, the '>' is consumed.
1038 ///
1039 /// \param ObjCGenericList if true, this is the '>' closing an Objective-C
1040 /// type parameter or type argument list, rather than a C++ template parameter
1041 /// or argument list.
1042 ///
1043 /// \returns true, if current token does not start with '>', false otherwise.
ParseGreaterThanInTemplateList(SourceLocation LAngleLoc,SourceLocation & RAngleLoc,bool ConsumeLastToken,bool ObjCGenericList)1044 bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc,
1045 SourceLocation &RAngleLoc,
1046 bool ConsumeLastToken,
1047 bool ObjCGenericList) {
1048 // What will be left once we've consumed the '>'.
1049 tok::TokenKind RemainingToken;
1050 const char *ReplacementStr = "> >";
1051 bool MergeWithNextToken = false;
1052
1053 switch (Tok.getKind()) {
1054 default:
1055 Diag(getEndOfPreviousToken(), diag::err_expected) << tok::greater;
1056 Diag(LAngleLoc, diag::note_matching) << tok::less;
1057 return true;
1058
1059 case tok::greater:
1060 // Determine the location of the '>' token. Only consume this token
1061 // if the caller asked us to.
1062 RAngleLoc = Tok.getLocation();
1063 if (ConsumeLastToken)
1064 ConsumeToken();
1065 return false;
1066
1067 case tok::greatergreater:
1068 RemainingToken = tok::greater;
1069 break;
1070
1071 case tok::greatergreatergreater:
1072 RemainingToken = tok::greatergreater;
1073 break;
1074
1075 case tok::greaterequal:
1076 RemainingToken = tok::equal;
1077 ReplacementStr = "> =";
1078
1079 // Join two adjacent '=' tokens into one, for cases like:
1080 // void (*p)() = f<int>;
1081 // return f<int>==p;
1082 if (NextToken().is(tok::equal) &&
1083 areTokensAdjacent(Tok, NextToken())) {
1084 RemainingToken = tok::equalequal;
1085 MergeWithNextToken = true;
1086 }
1087 break;
1088
1089 case tok::greatergreaterequal:
1090 RemainingToken = tok::greaterequal;
1091 break;
1092 }
1093
1094 // This template-id is terminated by a token that starts with a '>'.
1095 // Outside C++11 and Objective-C, this is now error recovery.
1096 //
1097 // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we
1098 // extend that treatment to also apply to the '>>>' token.
1099 //
1100 // Objective-C allows this in its type parameter / argument lists.
1101
1102 SourceLocation TokBeforeGreaterLoc = PrevTokLocation;
1103 SourceLocation TokLoc = Tok.getLocation();
1104 Token Next = NextToken();
1105
1106 // Whether splitting the current token after the '>' would undesirably result
1107 // in the remaining token pasting with the token after it. This excludes the
1108 // MergeWithNextToken cases, which we've already handled.
1109 bool PreventMergeWithNextToken =
1110 (RemainingToken == tok::greater ||
1111 RemainingToken == tok::greatergreater) &&
1112 (Next.isOneOf(tok::greater, tok::greatergreater,
1113 tok::greatergreatergreater, tok::equal, tok::greaterequal,
1114 tok::greatergreaterequal, tok::equalequal)) &&
1115 areTokensAdjacent(Tok, Next);
1116
1117 // Diagnose this situation as appropriate.
1118 if (!ObjCGenericList) {
1119 // The source range of the replaced token(s).
1120 CharSourceRange ReplacementRange = CharSourceRange::getCharRange(
1121 TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(),
1122 getLangOpts()));
1123
1124 // A hint to put a space between the '>>'s. In order to make the hint as
1125 // clear as possible, we include the characters either side of the space in
1126 // the replacement, rather than just inserting a space at SecondCharLoc.
1127 FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
1128 ReplacementStr);
1129
1130 // A hint to put another space after the token, if it would otherwise be
1131 // lexed differently.
1132 FixItHint Hint2;
1133 if (PreventMergeWithNextToken)
1134 Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
1135
1136 unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
1137 if (getLangOpts().CPlusPlus11 &&
1138 (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
1139 DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
1140 else if (Tok.is(tok::greaterequal))
1141 DiagId = diag::err_right_angle_bracket_equal_needs_space;
1142 Diag(TokLoc, DiagId) << Hint1 << Hint2;
1143 }
1144
1145 // Find the "length" of the resulting '>' token. This is not always 1, as it
1146 // can contain escaped newlines.
1147 unsigned GreaterLength = Lexer::getTokenPrefixLength(
1148 TokLoc, 1, PP.getSourceManager(), getLangOpts());
1149
1150 // Annotate the source buffer to indicate that we split the token after the
1151 // '>'. This allows us to properly find the end of, and extract the spelling
1152 // of, the '>' token later.
1153 RAngleLoc = PP.SplitToken(TokLoc, GreaterLength);
1154
1155 // Strip the initial '>' from the token.
1156 bool CachingTokens = PP.IsPreviousCachedToken(Tok);
1157
1158 Token Greater = Tok;
1159 Greater.setLocation(RAngleLoc);
1160 Greater.setKind(tok::greater);
1161 Greater.setLength(GreaterLength);
1162
1163 unsigned OldLength = Tok.getLength();
1164 if (MergeWithNextToken) {
1165 ConsumeToken();
1166 OldLength += Tok.getLength();
1167 }
1168
1169 Tok.setKind(RemainingToken);
1170 Tok.setLength(OldLength - GreaterLength);
1171
1172 // Split the second token if lexing it normally would lex a different token
1173 // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>').
1174 SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength);
1175 if (PreventMergeWithNextToken)
1176 AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength());
1177 Tok.setLocation(AfterGreaterLoc);
1178
1179 // Update the token cache to match what we just did if necessary.
1180 if (CachingTokens) {
1181 // If the previous cached token is being merged, delete it.
1182 if (MergeWithNextToken)
1183 PP.ReplacePreviousCachedToken({});
1184
1185 if (ConsumeLastToken)
1186 PP.ReplacePreviousCachedToken({Greater, Tok});
1187 else
1188 PP.ReplacePreviousCachedToken({Greater});
1189 }
1190
1191 if (ConsumeLastToken) {
1192 PrevTokLocation = RAngleLoc;
1193 } else {
1194 PrevTokLocation = TokBeforeGreaterLoc;
1195 PP.EnterToken(Tok, /*IsReinject=*/true);
1196 Tok = Greater;
1197 }
1198
1199 return false;
1200 }
1201
1202
1203 /// Parses a template-id that after the template name has
1204 /// already been parsed.
1205 ///
1206 /// This routine takes care of parsing the enclosed template argument
1207 /// list ('<' template-parameter-list [opt] '>') and placing the
1208 /// results into a form that can be transferred to semantic analysis.
1209 ///
1210 /// \param ConsumeLastToken if true, then we will consume the last
1211 /// token that forms the template-id. Otherwise, we will leave the
1212 /// last token in the stream (e.g., so that it can be replaced with an
1213 /// annotation token).
1214 bool
ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,SourceLocation & LAngleLoc,TemplateArgList & TemplateArgs,SourceLocation & RAngleLoc)1215 Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,
1216 SourceLocation &LAngleLoc,
1217 TemplateArgList &TemplateArgs,
1218 SourceLocation &RAngleLoc) {
1219 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
1220
1221 // Consume the '<'.
1222 LAngleLoc = ConsumeToken();
1223
1224 // Parse the optional template-argument-list.
1225 bool Invalid = false;
1226 {
1227 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
1228 if (!Tok.isOneOf(tok::greater, tok::greatergreater,
1229 tok::greatergreatergreater, tok::greaterequal,
1230 tok::greatergreaterequal))
1231 Invalid = ParseTemplateArgumentList(TemplateArgs);
1232
1233 if (Invalid) {
1234 // Try to find the closing '>'.
1235 if (getLangOpts().CPlusPlus11)
1236 SkipUntil(tok::greater, tok::greatergreater,
1237 tok::greatergreatergreater, StopAtSemi | StopBeforeMatch);
1238 else
1239 SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
1240 }
1241 }
1242
1243 return ParseGreaterThanInTemplateList(LAngleLoc, RAngleLoc, ConsumeLastToken,
1244 /*ObjCGenericList=*/false) ||
1245 Invalid;
1246 }
1247
1248 /// Replace the tokens that form a simple-template-id with an
1249 /// annotation token containing the complete template-id.
1250 ///
1251 /// The first token in the stream must be the name of a template that
1252 /// is followed by a '<'. This routine will parse the complete
1253 /// simple-template-id and replace the tokens with a single annotation
1254 /// token with one of two different kinds: if the template-id names a
1255 /// type (and \p AllowTypeAnnotation is true), the annotation token is
1256 /// a type annotation that includes the optional nested-name-specifier
1257 /// (\p SS). Otherwise, the annotation token is a template-id
1258 /// annotation that does not include the optional
1259 /// nested-name-specifier.
1260 ///
1261 /// \param Template the declaration of the template named by the first
1262 /// token (an identifier), as returned from \c Action::isTemplateName().
1263 ///
1264 /// \param TNK the kind of template that \p Template
1265 /// refers to, as returned from \c Action::isTemplateName().
1266 ///
1267 /// \param SS if non-NULL, the nested-name-specifier that precedes
1268 /// this template name.
1269 ///
1270 /// \param TemplateKWLoc if valid, specifies that this template-id
1271 /// annotation was preceded by the 'template' keyword and gives the
1272 /// location of that keyword. If invalid (the default), then this
1273 /// template-id was not preceded by a 'template' keyword.
1274 ///
1275 /// \param AllowTypeAnnotation if true (the default), then a
1276 /// simple-template-id that refers to a class template, template
1277 /// template parameter, or other template that produces a type will be
1278 /// replaced with a type annotation token. Otherwise, the
1279 /// simple-template-id is always replaced with a template-id
1280 /// annotation token.
1281 ///
1282 /// \param TypeConstraint if true, then this is actually a type-constraint,
1283 /// meaning that the template argument list can be omitted (and the template in
1284 /// question must be a concept).
1285 ///
1286 /// If an unrecoverable parse error occurs and no annotation token can be
1287 /// formed, this function returns true.
1288 ///
AnnotateTemplateIdToken(TemplateTy Template,TemplateNameKind TNK,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,UnqualifiedId & TemplateName,bool AllowTypeAnnotation,bool TypeConstraint)1289 bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
1290 CXXScopeSpec &SS,
1291 SourceLocation TemplateKWLoc,
1292 UnqualifiedId &TemplateName,
1293 bool AllowTypeAnnotation,
1294 bool TypeConstraint) {
1295 assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
1296 assert((Tok.is(tok::less) || TypeConstraint) &&
1297 "Parser isn't at the beginning of a template-id");
1298 assert(!(TypeConstraint && AllowTypeAnnotation) && "type-constraint can't be "
1299 "a type annotation");
1300 assert((!TypeConstraint || TNK == TNK_Concept_template) && "type-constraint "
1301 "must accompany a concept name");
1302 assert((Template || TNK == TNK_Non_template) && "missing template name");
1303
1304 // Consume the template-name.
1305 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
1306
1307 // Parse the enclosed template argument list.
1308 SourceLocation LAngleLoc, RAngleLoc;
1309 TemplateArgList TemplateArgs;
1310 bool ArgsInvalid = false;
1311 if (!TypeConstraint || Tok.is(tok::less)) {
1312 ArgsInvalid = ParseTemplateIdAfterTemplateName(false, LAngleLoc,
1313 TemplateArgs, RAngleLoc);
1314 // If we couldn't recover from invalid arguments, don't form an annotation
1315 // token -- we don't know how much to annotate.
1316 // FIXME: This can lead to duplicate diagnostics if we retry parsing this
1317 // template-id in another context. Try to annotate anyway?
1318 if (RAngleLoc.isInvalid())
1319 return true;
1320 }
1321
1322 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
1323
1324 // Build the annotation token.
1325 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
1326 TypeResult Type = ArgsInvalid
1327 ? TypeError()
1328 : Actions.ActOnTemplateIdType(
1329 getCurScope(), SS, TemplateKWLoc, Template,
1330 TemplateName.Identifier, TemplateNameLoc,
1331 LAngleLoc, TemplateArgsPtr, RAngleLoc);
1332
1333 Tok.setKind(tok::annot_typename);
1334 setTypeAnnotation(Tok, Type);
1335 if (SS.isNotEmpty())
1336 Tok.setLocation(SS.getBeginLoc());
1337 else if (TemplateKWLoc.isValid())
1338 Tok.setLocation(TemplateKWLoc);
1339 else
1340 Tok.setLocation(TemplateNameLoc);
1341 } else {
1342 // Build a template-id annotation token that can be processed
1343 // later.
1344 Tok.setKind(tok::annot_template_id);
1345
1346 IdentifierInfo *TemplateII =
1347 TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
1348 ? TemplateName.Identifier
1349 : nullptr;
1350
1351 OverloadedOperatorKind OpKind =
1352 TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
1353 ? OO_None
1354 : TemplateName.OperatorFunctionId.Operator;
1355
1356 TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create(
1357 TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK,
1358 LAngleLoc, RAngleLoc, TemplateArgs, ArgsInvalid, TemplateIds);
1359
1360 Tok.setAnnotationValue(TemplateId);
1361 if (TemplateKWLoc.isValid())
1362 Tok.setLocation(TemplateKWLoc);
1363 else
1364 Tok.setLocation(TemplateNameLoc);
1365 }
1366
1367 // Common fields for the annotation token
1368 Tok.setAnnotationEndLoc(RAngleLoc);
1369
1370 // In case the tokens were cached, have Preprocessor replace them with the
1371 // annotation token.
1372 PP.AnnotateCachedTokens(Tok);
1373 return false;
1374 }
1375
1376 /// Replaces a template-id annotation token with a type
1377 /// annotation token.
1378 ///
1379 /// If there was a failure when forming the type from the template-id,
1380 /// a type annotation token will still be created, but will have a
1381 /// NULL type pointer to signify an error.
1382 ///
1383 /// \param SS The scope specifier appearing before the template-id, if any.
1384 ///
1385 /// \param IsClassName Is this template-id appearing in a context where we
1386 /// know it names a class, such as in an elaborated-type-specifier or
1387 /// base-specifier? ('typename' and 'template' are unneeded and disallowed
1388 /// in those contexts.)
AnnotateTemplateIdTokenAsType(CXXScopeSpec & SS,bool IsClassName)1389 void Parser::AnnotateTemplateIdTokenAsType(CXXScopeSpec &SS,
1390 bool IsClassName) {
1391 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
1392
1393 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1394 assert(TemplateId->mightBeType() &&
1395 "Only works for type and dependent templates");
1396
1397 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1398 TemplateId->NumArgs);
1399
1400 TypeResult Type =
1401 TemplateId->isInvalid()
1402 ? TypeError()
1403 : Actions.ActOnTemplateIdType(
1404 getCurScope(), SS, TemplateId->TemplateKWLoc,
1405 TemplateId->Template, TemplateId->Name,
1406 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
1407 TemplateArgsPtr, TemplateId->RAngleLoc,
1408 /*IsCtorOrDtorName*/ false, IsClassName);
1409 // Create the new "type" annotation token.
1410 Tok.setKind(tok::annot_typename);
1411 setTypeAnnotation(Tok, Type);
1412 if (SS.isNotEmpty()) // it was a C++ qualified type name.
1413 Tok.setLocation(SS.getBeginLoc());
1414 // End location stays the same
1415
1416 // Replace the template-id annotation token, and possible the scope-specifier
1417 // that precedes it, with the typename annotation token.
1418 PP.AnnotateCachedTokens(Tok);
1419 }
1420
1421 /// Determine whether the given token can end a template argument.
isEndOfTemplateArgument(Token Tok)1422 static bool isEndOfTemplateArgument(Token Tok) {
1423 // FIXME: Handle '>>>'.
1424 return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater,
1425 tok::greatergreatergreater);
1426 }
1427
1428 /// Parse a C++ template template argument.
ParseTemplateTemplateArgument()1429 ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
1430 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
1431 !Tok.is(tok::annot_cxxscope))
1432 return ParsedTemplateArgument();
1433
1434 // C++0x [temp.arg.template]p1:
1435 // A template-argument for a template template-parameter shall be the name
1436 // of a class template or an alias template, expressed as id-expression.
1437 //
1438 // We parse an id-expression that refers to a class template or alias
1439 // template. The grammar we parse is:
1440 //
1441 // nested-name-specifier[opt] template[opt] identifier ...[opt]
1442 //
1443 // followed by a token that terminates a template argument, such as ',',
1444 // '>', or (in some cases) '>>'.
1445 CXXScopeSpec SS; // nested-name-specifier, if present
1446 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1447 /*ObjectHadErrors=*/false,
1448 /*EnteringContext=*/false);
1449
1450 ParsedTemplateArgument Result;
1451 SourceLocation EllipsisLoc;
1452 if (SS.isSet() && Tok.is(tok::kw_template)) {
1453 // Parse the optional 'template' keyword following the
1454 // nested-name-specifier.
1455 SourceLocation TemplateKWLoc = ConsumeToken();
1456
1457 if (Tok.is(tok::identifier)) {
1458 // We appear to have a dependent template name.
1459 UnqualifiedId Name;
1460 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1461 ConsumeToken(); // the identifier
1462
1463 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1464
1465 // If the next token signals the end of a template argument, then we have
1466 // a (possibly-dependent) template name that could be a template template
1467 // argument.
1468 TemplateTy Template;
1469 if (isEndOfTemplateArgument(Tok) &&
1470 Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Name,
1471 /*ObjectType=*/nullptr,
1472 /*EnteringContext=*/false, Template))
1473 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1474 }
1475 } else if (Tok.is(tok::identifier)) {
1476 // We may have a (non-dependent) template name.
1477 TemplateTy Template;
1478 UnqualifiedId Name;
1479 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1480 ConsumeToken(); // the identifier
1481
1482 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1483
1484 if (isEndOfTemplateArgument(Tok)) {
1485 bool MemberOfUnknownSpecialization;
1486 TemplateNameKind TNK = Actions.isTemplateName(
1487 getCurScope(), SS,
1488 /*hasTemplateKeyword=*/false, Name,
1489 /*ObjectType=*/nullptr,
1490 /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization);
1491 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
1492 // We have an id-expression that refers to a class template or
1493 // (C++0x) alias template.
1494 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1495 }
1496 }
1497 }
1498
1499 // If this is a pack expansion, build it as such.
1500 if (EllipsisLoc.isValid() && !Result.isInvalid())
1501 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
1502
1503 return Result;
1504 }
1505
1506 /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
1507 ///
1508 /// template-argument: [C++ 14.2]
1509 /// constant-expression
1510 /// type-id
1511 /// id-expression
ParseTemplateArgument()1512 ParsedTemplateArgument Parser::ParseTemplateArgument() {
1513 // C++ [temp.arg]p2:
1514 // In a template-argument, an ambiguity between a type-id and an
1515 // expression is resolved to a type-id, regardless of the form of
1516 // the corresponding template-parameter.
1517 //
1518 // Therefore, we initially try to parse a type-id - and isCXXTypeId might look
1519 // up and annotate an identifier as an id-expression during disambiguation,
1520 // so enter the appropriate context for a constant expression template
1521 // argument before trying to disambiguate.
1522
1523 EnterExpressionEvaluationContext EnterConstantEvaluated(
1524 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
1525 /*LambdaContextDecl=*/nullptr,
1526 /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument);
1527 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
1528 TypeResult TypeArg = ParseTypeName(
1529 /*Range=*/nullptr, DeclaratorContext::TemplateArg);
1530 return Actions.ActOnTemplateTypeArgument(TypeArg);
1531 }
1532
1533 // Try to parse a template template argument.
1534 {
1535 TentativeParsingAction TPA(*this);
1536
1537 ParsedTemplateArgument TemplateTemplateArgument
1538 = ParseTemplateTemplateArgument();
1539 if (!TemplateTemplateArgument.isInvalid()) {
1540 TPA.Commit();
1541 return TemplateTemplateArgument;
1542 }
1543
1544 // Revert this tentative parse to parse a non-type template argument.
1545 TPA.Revert();
1546 }
1547
1548 // Parse a non-type template argument.
1549 SourceLocation Loc = Tok.getLocation();
1550 ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast);
1551 if (ExprArg.isInvalid() || !ExprArg.get()) {
1552 return ParsedTemplateArgument();
1553 }
1554
1555 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
1556 ExprArg.get(), Loc);
1557 }
1558
1559 /// ParseTemplateArgumentList - Parse a C++ template-argument-list
1560 /// (C++ [temp.names]). Returns true if there was an error.
1561 ///
1562 /// template-argument-list: [C++ 14.2]
1563 /// template-argument
1564 /// template-argument-list ',' template-argument
1565 bool
ParseTemplateArgumentList(TemplateArgList & TemplateArgs)1566 Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
1567
1568 ColonProtectionRAIIObject ColonProtection(*this, false);
1569
1570 do {
1571 ParsedTemplateArgument Arg = ParseTemplateArgument();
1572 SourceLocation EllipsisLoc;
1573 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
1574 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
1575
1576 if (Arg.isInvalid())
1577 return true;
1578
1579 // Save this template argument.
1580 TemplateArgs.push_back(Arg);
1581
1582 // If the next token is a comma, consume it and keep reading
1583 // arguments.
1584 } while (TryConsumeToken(tok::comma));
1585
1586 return false;
1587 }
1588
1589 /// Parse a C++ explicit template instantiation
1590 /// (C++ [temp.explicit]).
1591 ///
1592 /// explicit-instantiation:
1593 /// 'extern' [opt] 'template' declaration
1594 ///
1595 /// Note that the 'extern' is a GNU extension and C++11 feature.
ParseExplicitInstantiation(DeclaratorContext Context,SourceLocation ExternLoc,SourceLocation TemplateLoc,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)1596 Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context,
1597 SourceLocation ExternLoc,
1598 SourceLocation TemplateLoc,
1599 SourceLocation &DeclEnd,
1600 ParsedAttributes &AccessAttrs,
1601 AccessSpecifier AS) {
1602 // This isn't really required here.
1603 ParsingDeclRAIIObject
1604 ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
1605
1606 return ParseSingleDeclarationAfterTemplate(
1607 Context, ParsedTemplateInfo(ExternLoc, TemplateLoc),
1608 ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
1609 }
1610
getSourceRange() const1611 SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
1612 if (TemplateParams)
1613 return getTemplateParamsRange(TemplateParams->data(),
1614 TemplateParams->size());
1615
1616 SourceRange R(TemplateLoc);
1617 if (ExternLoc.isValid())
1618 R.setBegin(ExternLoc);
1619 return R;
1620 }
1621
LateTemplateParserCallback(void * P,LateParsedTemplate & LPT)1622 void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
1623 ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
1624 }
1625
1626 /// Late parse a C++ function template in Microsoft mode.
ParseLateTemplatedFuncDef(LateParsedTemplate & LPT)1627 void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
1628 if (!LPT.D)
1629 return;
1630
1631 // Destroy TemplateIdAnnotations when we're done, if possible.
1632 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
1633
1634 // Get the FunctionDecl.
1635 FunctionDecl *FunD = LPT.D->getAsFunction();
1636 // Track template parameter depth.
1637 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1638
1639 // To restore the context after late parsing.
1640 Sema::ContextRAII GlobalSavedContext(
1641 Actions, Actions.Context.getTranslationUnitDecl());
1642
1643 MultiParseScope Scopes(*this);
1644
1645 // Get the list of DeclContexts to reenter.
1646 SmallVector<DeclContext*, 4> DeclContextsToReenter;
1647 for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit();
1648 DC = DC->getLexicalParent())
1649 DeclContextsToReenter.push_back(DC);
1650
1651 // Reenter scopes from outermost to innermost.
1652 for (DeclContext *DC : reverse(DeclContextsToReenter)) {
1653 CurTemplateDepthTracker.addDepth(
1654 ReenterTemplateScopes(Scopes, cast<Decl>(DC)));
1655 Scopes.Enter(Scope::DeclScope);
1656 // We'll reenter the function context itself below.
1657 if (DC != FunD)
1658 Actions.PushDeclContext(Actions.getCurScope(), DC);
1659 }
1660
1661 assert(!LPT.Toks.empty() && "Empty body!");
1662
1663 // Append the current token at the end of the new token stream so that it
1664 // doesn't get lost.
1665 LPT.Toks.push_back(Tok);
1666 PP.EnterTokenStream(LPT.Toks, true, /*IsReinject*/true);
1667
1668 // Consume the previously pushed token.
1669 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1670 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
1671 "Inline method not starting with '{', ':' or 'try'");
1672
1673 // Parse the method body. Function body parsing code is similar enough
1674 // to be re-used for method bodies as well.
1675 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
1676 Scope::CompoundStmtScope);
1677
1678 // Recreate the containing function DeclContext.
1679 Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent());
1680
1681 Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
1682
1683 if (Tok.is(tok::kw_try)) {
1684 ParseFunctionTryBlock(LPT.D, FnScope);
1685 } else {
1686 if (Tok.is(tok::colon))
1687 ParseConstructorInitializer(LPT.D);
1688 else
1689 Actions.ActOnDefaultCtorInitializers(LPT.D);
1690
1691 if (Tok.is(tok::l_brace)) {
1692 assert((!isa<FunctionTemplateDecl>(LPT.D) ||
1693 cast<FunctionTemplateDecl>(LPT.D)
1694 ->getTemplateParameters()
1695 ->getDepth() == TemplateParameterDepth - 1) &&
1696 "TemplateParameterDepth should be greater than the depth of "
1697 "current template being instantiated!");
1698 ParseFunctionStatementBody(LPT.D, FnScope);
1699 Actions.UnmarkAsLateParsedTemplate(FunD);
1700 } else
1701 Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
1702 }
1703 }
1704
1705 /// Lex a delayed template function for late parsing.
LexTemplateFunctionForLateParsing(CachedTokens & Toks)1706 void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1707 tok::TokenKind kind = Tok.getKind();
1708 if (!ConsumeAndStoreFunctionPrologue(Toks)) {
1709 // Consume everything up to (and including) the matching right brace.
1710 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1711 }
1712
1713 // If we're in a function-try-block, we need to store all the catch blocks.
1714 if (kind == tok::kw_try) {
1715 while (Tok.is(tok::kw_catch)) {
1716 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1717 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1718 }
1719 }
1720 }
1721
1722 /// We've parsed something that could plausibly be intended to be a template
1723 /// name (\p LHS) followed by a '<' token, and the following code can't possibly
1724 /// be an expression. Determine if this is likely to be a template-id and if so,
1725 /// diagnose it.
diagnoseUnknownTemplateId(ExprResult LHS,SourceLocation Less)1726 bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) {
1727 TentativeParsingAction TPA(*this);
1728 // FIXME: We could look at the token sequence in a lot more detail here.
1729 if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater,
1730 StopAtSemi | StopBeforeMatch)) {
1731 TPA.Commit();
1732
1733 SourceLocation Greater;
1734 ParseGreaterThanInTemplateList(Less, Greater, true, false);
1735 Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS,
1736 Less, Greater);
1737 return true;
1738 }
1739
1740 // There's no matching '>' token, this probably isn't supposed to be
1741 // interpreted as a template-id. Parse it as an (ill-formed) comparison.
1742 TPA.Revert();
1743 return false;
1744 }
1745
checkPotentialAngleBracket(ExprResult & PotentialTemplateName)1746 void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) {
1747 assert(Tok.is(tok::less) && "not at a potential angle bracket");
1748
1749 bool DependentTemplateName = false;
1750 if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName,
1751 DependentTemplateName))
1752 return;
1753
1754 // OK, this might be a name that the user intended to be parsed as a
1755 // template-name, followed by a '<' token. Check for some easy cases.
1756
1757 // If we have potential_template<>, then it's supposed to be a template-name.
1758 if (NextToken().is(tok::greater) ||
1759 (getLangOpts().CPlusPlus11 &&
1760 NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) {
1761 SourceLocation Less = ConsumeToken();
1762 SourceLocation Greater;
1763 ParseGreaterThanInTemplateList(Less, Greater, true, false);
1764 Actions.diagnoseExprIntendedAsTemplateName(
1765 getCurScope(), PotentialTemplateName, Less, Greater);
1766 // FIXME: Perform error recovery.
1767 PotentialTemplateName = ExprError();
1768 return;
1769 }
1770
1771 // If we have 'potential_template<type-id', assume it's supposed to be a
1772 // template-name if there's a matching '>' later on.
1773 {
1774 // FIXME: Avoid the tentative parse when NextToken() can't begin a type.
1775 TentativeParsingAction TPA(*this);
1776 SourceLocation Less = ConsumeToken();
1777 if (isTypeIdUnambiguously() &&
1778 diagnoseUnknownTemplateId(PotentialTemplateName, Less)) {
1779 TPA.Commit();
1780 // FIXME: Perform error recovery.
1781 PotentialTemplateName = ExprError();
1782 return;
1783 }
1784 TPA.Revert();
1785 }
1786
1787 // Otherwise, remember that we saw this in case we see a potentially-matching
1788 // '>' token later on.
1789 AngleBracketTracker::Priority Priority =
1790 (DependentTemplateName ? AngleBracketTracker::DependentName
1791 : AngleBracketTracker::PotentialTypo) |
1792 (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess
1793 : AngleBracketTracker::NoSpaceBeforeLess);
1794 AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(),
1795 Priority);
1796 }
1797
checkPotentialAngleBracketDelimiter(const AngleBracketTracker::Loc & LAngle,const Token & OpToken)1798 bool Parser::checkPotentialAngleBracketDelimiter(
1799 const AngleBracketTracker::Loc &LAngle, const Token &OpToken) {
1800 // If a comma in an expression context is followed by a type that can be a
1801 // template argument and cannot be an expression, then this is ill-formed,
1802 // but might be intended to be part of a template-id.
1803 if (OpToken.is(tok::comma) && isTypeIdUnambiguously() &&
1804 diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) {
1805 AngleBrackets.clear(*this);
1806 return true;
1807 }
1808
1809 // If a context that looks like a template-id is followed by '()', then
1810 // this is ill-formed, but might be intended to be a template-id
1811 // followed by '()'.
1812 if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) &&
1813 NextToken().is(tok::r_paren)) {
1814 Actions.diagnoseExprIntendedAsTemplateName(
1815 getCurScope(), LAngle.TemplateName, LAngle.LessLoc,
1816 OpToken.getLocation());
1817 AngleBrackets.clear(*this);
1818 return true;
1819 }
1820
1821 // After a '>' (etc), we're no longer potentially in a construct that's
1822 // intended to be treated as a template-id.
1823 if (OpToken.is(tok::greater) ||
1824 (getLangOpts().CPlusPlus11 &&
1825 OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater)))
1826 AngleBrackets.clear(*this);
1827 return false;
1828 }
1829