clang  9.0.0svn
ParseTemplate.cpp
Go to the documentation of this file.
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"
16 #include "clang/Parse/Parser.h"
18 #include "clang/Sema/DeclSpec.h"
20 #include "clang/Sema/Scope.h"
21 #include "llvm/Support/TimeProfiler.h"
22 using namespace clang;
23 
24 /// Parse a template declaration, explicit instantiation, or
25 /// explicit specialization.
26 Decl *Parser::ParseDeclarationStartingWithTemplate(
27  DeclaratorContext Context, SourceLocation &DeclEnd,
28  ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
29  ObjCDeclContextSwitch ObjCDC(*this);
30 
31  if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
32  return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(),
33  DeclEnd, AccessAttrs, AS);
34  }
35  return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs,
36  AS);
37 }
38 
39 /// Parse a template declaration or an explicit specialization.
40 ///
41 /// Template declarations include one or more template parameter lists
42 /// and either the function or class template declaration. Explicit
43 /// specializations contain one or more 'template < >' prefixes
44 /// followed by a (possibly templated) declaration. Since the
45 /// syntactic form of both features is nearly identical, we parse all
46 /// of the template headers together and let semantic analysis sort
47 /// the declarations from the explicit specializations.
48 ///
49 /// template-declaration: [C++ temp]
50 /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
51 ///
52 /// explicit-specialization: [ C++ temp.expl.spec]
53 /// 'template' '<' '>' declaration
54 Decl *Parser::ParseTemplateDeclarationOrSpecialization(
55  DeclaratorContext Context, SourceLocation &DeclEnd,
56  ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
57  assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
58  "Token does not start a template declaration.");
59 
60  // Enter template-parameter scope.
61  ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
62 
63  // Tell the action that names should be checked in the context of
64  // the declaration to come.
66  ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
67 
68  // Parse multiple levels of template headers within this template
69  // parameter scope, e.g.,
70  //
71  // template<typename T>
72  // template<typename U>
73  // class A<T>::B { ... };
74  //
75  // We parse multiple levels non-recursively so that we can build a
76  // single data structure containing all of the template parameter
77  // lists to easily differentiate between the case above and:
78  //
79  // template<typename T>
80  // class A {
81  // template<typename U> class B;
82  // };
83  //
84  // In the first case, the action for declaring A<T>::B receives
85  // both template parameter lists. In the second case, the action for
86  // defining A<T>::B receives just the inner template parameter list
87  // (and retrieves the outer template parameter list from its
88  // context).
89  bool isSpecialization = true;
90  bool LastParamListWasEmpty = false;
91  TemplateParameterLists ParamLists;
92  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
93 
94  do {
95  // Consume the 'export', if any.
96  SourceLocation ExportLoc;
97  TryConsumeToken(tok::kw_export, ExportLoc);
98 
99  // Consume the 'template', which should be here.
100  SourceLocation TemplateLoc;
101  if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
102  Diag(Tok.getLocation(), diag::err_expected_template);
103  return nullptr;
104  }
105 
106  // Parse the '<' template-parameter-list '>'
107  SourceLocation LAngleLoc, RAngleLoc;
108  SmallVector<NamedDecl*, 4> TemplateParams;
109  if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(),
110  TemplateParams, LAngleLoc, RAngleLoc)) {
111  // Skip until the semi-colon or a '}'.
112  SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
113  TryConsumeToken(tok::semi);
114  return nullptr;
115  }
116 
117  ExprResult OptionalRequiresClauseConstraintER;
118  if (!TemplateParams.empty()) {
119  isSpecialization = false;
120  ++CurTemplateDepthTracker;
121 
122  if (TryConsumeToken(tok::kw_requires)) {
123  OptionalRequiresClauseConstraintER =
125  if (!OptionalRequiresClauseConstraintER.isUsable()) {
126  // Skip until the semi-colon or a '}'.
127  SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
128  TryConsumeToken(tok::semi);
129  return nullptr;
130  }
131  }
132  } else {
133  LastParamListWasEmpty = true;
134  }
135 
136  ParamLists.push_back(Actions.ActOnTemplateParameterList(
137  CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc,
138  TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get()));
139  } while (Tok.isOneOf(tok::kw_export, tok::kw_template));
140 
141  unsigned NewFlags = getCurScope()->getFlags() & ~Scope::TemplateParamScope;
142  ParseScopeFlags TemplateScopeFlags(this, NewFlags, isSpecialization);
143 
144  // Parse the actual template declaration.
145  return ParseSingleDeclarationAfterTemplate(
146  Context,
147  ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty),
148  ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
149 }
150 
151 /// Parse a single declaration that declares a template,
152 /// template specialization, or explicit instantiation of a template.
153 ///
154 /// \param DeclEnd will receive the source location of the last token
155 /// within this declaration.
156 ///
157 /// \param AS the access specifier associated with this
158 /// declaration. Will be AS_none for namespace-scope declarations.
159 ///
160 /// \returns the new declaration.
161 Decl *Parser::ParseSingleDeclarationAfterTemplate(
162  DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
163  ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd,
164  ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
165  assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
166  "Template information required");
167 
168  if (Tok.is(tok::kw_static_assert)) {
169  // A static_assert declaration may not be templated.
170  Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
171  << TemplateInfo.getSourceRange();
172  // Parse the static_assert declaration to improve error recovery.
173  return ParseStaticAssertDeclaration(DeclEnd);
174  }
175 
176  if (Context == DeclaratorContext::MemberContext) {
177  // We are parsing a member template.
178  ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
179  &DiagsFromTParams);
180  return nullptr;
181  }
182 
183  ParsedAttributesWithRange prefixAttrs(AttrFactory);
184  MaybeParseCXX11Attributes(prefixAttrs);
185 
186  if (Tok.is(tok::kw_using)) {
187  auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
188  prefixAttrs);
189  if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl())
190  return nullptr;
191  return usingDeclPtr.get().getSingleDecl();
192  }
193 
194  // Parse the declaration specifiers, stealing any diagnostics from
195  // the template parameters.
196  ParsingDeclSpec DS(*this, &DiagsFromTParams);
197 
198  ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
199  getDeclSpecContextFromDeclaratorContext(Context));
200 
201  if (Tok.is(tok::semi)) {
202  ProhibitAttributes(prefixAttrs);
203  DeclEnd = ConsumeToken();
204  RecordDecl *AnonRecord = nullptr;
206  getCurScope(), AS, DS,
207  TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
209  TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation,
210  AnonRecord);
211  assert(!AnonRecord &&
212  "Anonymous unions/structs should not be valid with template");
213  DS.complete(Decl);
214  return Decl;
215  }
216 
217  // Move the attributes from the prefix into the DS.
218  if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
219  ProhibitAttributes(prefixAttrs);
220  else
221  DS.takeAttributesFrom(prefixAttrs);
222 
223  // Parse the declarator.
224  ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context);
225  ParseDeclarator(DeclaratorInfo);
226  // Error parsing the declarator?
227  if (!DeclaratorInfo.hasName()) {
228  // If so, skip until the semi-colon or a }.
229  SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
230  if (Tok.is(tok::semi))
231  ConsumeToken();
232  return nullptr;
233  }
234 
235  llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() {
236  return DeclaratorInfo.getIdentifier() != nullptr
237  ? DeclaratorInfo.getIdentifier()->getName()
238  : "<unknown>";
239  });
240 
241  LateParsedAttrList LateParsedAttrs(true);
242  if (DeclaratorInfo.isFunctionDeclarator())
243  MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
244 
245  if (DeclaratorInfo.isFunctionDeclarator() &&
246  isStartOfFunctionDefinition(DeclaratorInfo)) {
247 
248  // Function definitions are only allowed at file scope and in C++ classes.
249  // The C++ inline method definition case is handled elsewhere, so we only
250  // need to handle the file scope definition case.
251  if (Context != DeclaratorContext::FileContext) {
252  Diag(Tok, diag::err_function_definition_not_allowed);
254  return nullptr;
255  }
256 
257  if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
258  // Recover by ignoring the 'typedef'. This was probably supposed to be
259  // the 'typename' keyword, which we should have already suggested adding
260  // if it's appropriate.
261  Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
262  << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
263  DS.ClearStorageClassSpecs();
264  }
265 
266  if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
267  if (DeclaratorInfo.getName().getKind() !=
269  // If the declarator-id is not a template-id, issue a diagnostic and
270  // recover by ignoring the 'template' keyword.
271  Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
272  return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
273  &LateParsedAttrs);
274  } else {
275  SourceLocation LAngleLoc
276  = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
277  Diag(DeclaratorInfo.getIdentifierLoc(),
278  diag::err_explicit_instantiation_with_definition)
279  << SourceRange(TemplateInfo.TemplateLoc)
280  << FixItHint::CreateInsertion(LAngleLoc, "<>");
281 
282  // Recover as if it were an explicit specialization.
283  TemplateParameterLists FakedParamLists;
284  FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
285  0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
286  LAngleLoc, nullptr));
287 
288  return ParseFunctionDefinition(
289  DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
290  /*isSpecialization=*/true,
291  /*LastParamListWasEmpty=*/true),
292  &LateParsedAttrs);
293  }
294  }
295  return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
296  &LateParsedAttrs);
297  }
298 
299  // Parse this declaration.
300  Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
301  TemplateInfo);
302 
303  if (Tok.is(tok::comma)) {
304  Diag(Tok, diag::err_multiple_template_declarators)
305  << (int)TemplateInfo.Kind;
306  SkipUntil(tok::semi);
307  return ThisDecl;
308  }
309 
310  // Eat the semi colon after the declaration.
311  ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
312  if (LateParsedAttrs.size() > 0)
313  ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
314  DeclaratorInfo.complete(ThisDecl);
315  return ThisDecl;
316 }
317 
318 /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
319 /// angle brackets. Depth is the depth of this template-parameter-list, which
320 /// is the number of template headers directly enclosing this template header.
321 /// TemplateParams is the current list of template parameters we're building.
322 /// The template parameter we parse will be added to this list. LAngleLoc and
323 /// RAngleLoc will receive the positions of the '<' and '>', respectively,
324 /// that enclose this template parameter list.
325 ///
326 /// \returns true if an error occurred, false otherwise.
327 bool Parser::ParseTemplateParameters(
328  unsigned Depth, SmallVectorImpl<NamedDecl *> &TemplateParams,
329  SourceLocation &LAngleLoc, SourceLocation &RAngleLoc) {
330  // Get the template parameter list.
331  if (!TryConsumeToken(tok::less, LAngleLoc)) {
332  Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
333  return true;
334  }
335 
336  // Try to parse the template parameter list.
337  bool Failed = false;
338  if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater))
339  Failed = ParseTemplateParameterList(Depth, TemplateParams);
340 
341  if (Tok.is(tok::greatergreater)) {
342  // No diagnostic required here: a template-parameter-list can only be
343  // followed by a declaration or, for a template template parameter, the
344  // 'class' keyword. Therefore, the second '>' will be diagnosed later.
345  // This matters for elegant diagnosis of:
346  // template<template<typename>> struct S;
347  Tok.setKind(tok::greater);
348  RAngleLoc = Tok.getLocation();
350  } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
351  Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
352  return true;
353  }
354  return false;
355 }
356 
357 /// ParseTemplateParameterList - Parse a template parameter list. If
358 /// the parsing fails badly (i.e., closing bracket was left out), this
359 /// will try to put the token stream in a reasonable position (closing
360 /// a statement, etc.) and return false.
361 ///
362 /// template-parameter-list: [C++ temp]
363 /// template-parameter
364 /// template-parameter-list ',' template-parameter
365 bool
366 Parser::ParseTemplateParameterList(const unsigned Depth,
367  SmallVectorImpl<NamedDecl*> &TemplateParams) {
368  while (1) {
369 
370  if (NamedDecl *TmpParam
371  = ParseTemplateParameter(Depth, TemplateParams.size())) {
372  TemplateParams.push_back(TmpParam);
373  } else {
374  // If we failed to parse a template parameter, skip until we find
375  // a comma or closing brace.
376  SkipUntil(tok::comma, tok::greater, tok::greatergreater,
378  }
379 
380  // Did we find a comma or the end of the template parameter list?
381  if (Tok.is(tok::comma)) {
382  ConsumeToken();
383  } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
384  // Don't consume this... that's done by template parser.
385  break;
386  } else {
387  // Somebody probably forgot to close the template. Skip ahead and
388  // try to get out of the expression. This error is currently
389  // subsumed by whatever goes on in ParseTemplateParameter.
390  Diag(Tok.getLocation(), diag::err_expected_comma_greater);
391  SkipUntil(tok::comma, tok::greater, tok::greatergreater,
393  return false;
394  }
395  }
396  return true;
397 }
398 
399 /// Determine whether the parser is at the start of a template
400 /// type parameter.
401 bool Parser::isStartOfTemplateTypeParameter() {
402  if (Tok.is(tok::kw_class)) {
403  // "class" may be the start of an elaborated-type-specifier or a
404  // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
405  switch (NextToken().getKind()) {
406  case tok::equal:
407  case tok::comma:
408  case tok::greater:
409  case tok::greatergreater:
410  case tok::ellipsis:
411  return true;
412 
413  case tok::identifier:
414  // This may be either a type-parameter or an elaborated-type-specifier.
415  // We have to look further.
416  break;
417 
418  default:
419  return false;
420  }
421 
422  switch (GetLookAheadToken(2).getKind()) {
423  case tok::equal:
424  case tok::comma:
425  case tok::greater:
426  case tok::greatergreater:
427  return true;
428 
429  default:
430  return false;
431  }
432  }
433 
434  // 'typedef' is a reasonably-common typo/thinko for 'typename', and is
435  // ill-formed otherwise.
436  if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef))
437  return false;
438 
439  // C++ [temp.param]p2:
440  // There is no semantic difference between class and typename in a
441  // template-parameter. typename followed by an unqualified-id
442  // names a template type parameter. typename followed by a
443  // qualified-id denotes the type in a non-type
444  // parameter-declaration.
445  Token Next = NextToken();
446 
447  // If we have an identifier, skip over it.
448  if (Next.getKind() == tok::identifier)
449  Next = GetLookAheadToken(2);
450 
451  switch (Next.getKind()) {
452  case tok::equal:
453  case tok::comma:
454  case tok::greater:
455  case tok::greatergreater:
456  case tok::ellipsis:
457  return true;
458 
459  case tok::kw_typename:
460  case tok::kw_typedef:
461  case tok::kw_class:
462  // These indicate that a comma was missed after a type parameter, not that
463  // we have found a non-type parameter.
464  return true;
465 
466  default:
467  return false;
468  }
469 }
470 
471 /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
472 ///
473 /// template-parameter: [C++ temp.param]
474 /// type-parameter
475 /// parameter-declaration
476 ///
477 /// type-parameter: (see below)
478 /// 'class' ...[opt] identifier[opt]
479 /// 'class' identifier[opt] '=' type-id
480 /// 'typename' ...[opt] identifier[opt]
481 /// 'typename' identifier[opt] '=' type-id
482 /// 'template' '<' template-parameter-list '>'
483 /// 'class' ...[opt] identifier[opt]
484 /// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
485 /// = id-expression
486 NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
487  if (isStartOfTemplateTypeParameter()) {
488  // Is there just a typo in the input code? ('typedef' instead of 'typename')
489  if (Tok.is(tok::kw_typedef)) {
490  Diag(Tok.getLocation(), diag::err_expected_template_parameter);
491 
492  Diag(Tok.getLocation(), diag::note_meant_to_use_typename)
494  Tok.getLocation(), Tok.getEndLoc()),
495  "typename");
496 
497  Tok.setKind(tok::kw_typename);
498  }
499 
500  return ParseTypeParameter(Depth, Position);
501  }
502 
503  if (Tok.is(tok::kw_template))
504  return ParseTemplateTemplateParameter(Depth, Position);
505 
506  // If it's none of the above, then it must be a parameter declaration.
507  // NOTE: This will pick up errors in the closure of the template parameter
508  // list (e.g., template < ; Check here to implement >> style closures.
509  return ParseNonTypeTemplateParameter(Depth, Position);
510 }
511 
512 /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
513 /// Other kinds of template parameters are parsed in
514 /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
515 ///
516 /// type-parameter: [C++ temp.param]
517 /// 'class' ...[opt][C++0x] identifier[opt]
518 /// 'class' identifier[opt] '=' type-id
519 /// 'typename' ...[opt][C++0x] identifier[opt]
520 /// 'typename' identifier[opt] '=' type-id
521 NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
522  assert(Tok.isOneOf(tok::kw_class, tok::kw_typename) &&
523  "A type-parameter starts with 'class' or 'typename'");
524 
525  // Consume the 'class' or 'typename' keyword.
526  bool TypenameKeyword = Tok.is(tok::kw_typename);
527  SourceLocation KeyLoc = ConsumeToken();
528 
529  // Grab the ellipsis (if given).
530  SourceLocation EllipsisLoc;
531  if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
532  Diag(EllipsisLoc,
534  ? diag::warn_cxx98_compat_variadic_templates
535  : diag::ext_variadic_templates);
536  }
537 
538  // Grab the template parameter name (if given)
539  SourceLocation NameLoc;
540  IdentifierInfo *ParamName = nullptr;
541  if (Tok.is(tok::identifier)) {
542  ParamName = Tok.getIdentifierInfo();
543  NameLoc = ConsumeToken();
544  } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
545  tok::greatergreater)) {
546  // Unnamed template parameter. Don't have to do anything here, just
547  // don't consume this token.
548  } else {
549  Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
550  return nullptr;
551  }
552 
553  // Recover from misplaced ellipsis.
554  bool AlreadyHasEllipsis = EllipsisLoc.isValid();
555  if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
556  DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
557 
558  // Grab a default argument (if available).
559  // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
560  // we introduce the type parameter into the local scope.
561  SourceLocation EqualLoc;
562  ParsedType DefaultArg;
563  if (TryConsumeToken(tok::equal, EqualLoc))
564  DefaultArg = ParseTypeName(/*Range=*/nullptr,
566 
567  return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc,
568  KeyLoc, ParamName, NameLoc, Depth, Position,
569  EqualLoc, DefaultArg);
570 }
571 
572 /// ParseTemplateTemplateParameter - Handle the parsing of template
573 /// template parameters.
574 ///
575 /// type-parameter: [C++ temp.param]
576 /// 'template' '<' template-parameter-list '>' type-parameter-key
577 /// ...[opt] identifier[opt]
578 /// 'template' '<' template-parameter-list '>' type-parameter-key
579 /// identifier[opt] = id-expression
580 /// type-parameter-key:
581 /// 'class'
582 /// 'typename' [C++1z]
583 NamedDecl *
584 Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
585  assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
586 
587  // Handle the template <...> part.
588  SourceLocation TemplateLoc = ConsumeToken();
589  SmallVector<NamedDecl*,8> TemplateParams;
590  SourceLocation LAngleLoc, RAngleLoc;
591  {
592  ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
593  if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
594  RAngleLoc)) {
595  return nullptr;
596  }
597  }
598 
599  // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
600  // Generate a meaningful error if the user forgot to put class before the
601  // identifier, comma, or greater. Provide a fixit if the identifier, comma,
602  // or greater appear immediately or after 'struct'. In the latter case,
603  // replace the keyword with 'class'.
604  if (!TryConsumeToken(tok::kw_class)) {
605  bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
606  const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
607  if (Tok.is(tok::kw_typename)) {
608  Diag(Tok.getLocation(),
609  getLangOpts().CPlusPlus17
610  ? diag::warn_cxx14_compat_template_template_param_typename
611  : diag::ext_template_template_param_typename)
612  << (!getLangOpts().CPlusPlus17
613  ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
614  : FixItHint());
615  } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
616  tok::greatergreater, tok::ellipsis)) {
617  Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
618  << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
619  : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
620  } else
621  Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
622 
623  if (Replace)
624  ConsumeToken();
625  }
626 
627  // Parse the ellipsis, if given.
628  SourceLocation EllipsisLoc;
629  if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
630  Diag(EllipsisLoc,
632  ? diag::warn_cxx98_compat_variadic_templates
633  : diag::ext_variadic_templates);
634 
635  // Get the identifier, if given.
636  SourceLocation NameLoc;
637  IdentifierInfo *ParamName = nullptr;
638  if (Tok.is(tok::identifier)) {
639  ParamName = Tok.getIdentifierInfo();
640  NameLoc = ConsumeToken();
641  } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
642  tok::greatergreater)) {
643  // Unnamed template parameter. Don't have to do anything here, just
644  // don't consume this token.
645  } else {
646  Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
647  return nullptr;
648  }
649 
650  // Recover from misplaced ellipsis.
651  bool AlreadyHasEllipsis = EllipsisLoc.isValid();
652  if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
653  DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
654 
655  TemplateParameterList *ParamList =
657  TemplateLoc, LAngleLoc,
658  TemplateParams,
659  RAngleLoc, nullptr);
660 
661  // Grab a default argument (if available).
662  // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
663  // we introduce the template parameter into the local scope.
664  SourceLocation EqualLoc;
665  ParsedTemplateArgument DefaultArg;
666  if (TryConsumeToken(tok::equal, EqualLoc)) {
667  DefaultArg = ParseTemplateTemplateArgument();
668  if (DefaultArg.isInvalid()) {
669  Diag(Tok.getLocation(),
670  diag::err_default_template_template_parameter_not_template);
671  SkipUntil(tok::comma, tok::greater, tok::greatergreater,
673  }
674  }
675 
676  return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
677  ParamList, EllipsisLoc,
678  ParamName, NameLoc, Depth,
679  Position, EqualLoc, DefaultArg);
680 }
681 
682 /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
683 /// template parameters (e.g., in "template<int Size> class array;").
684 ///
685 /// template-parameter:
686 /// ...
687 /// parameter-declaration
688 NamedDecl *
689 Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
690  // Parse the declaration-specifiers (i.e., the type).
691  // FIXME: The type should probably be restricted in some way... Not all
692  // declarators (parts of declarators?) are accepted for parameters.
693  DeclSpec DS(AttrFactory);
694  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
695  DeclSpecContext::DSC_template_param);
696 
697  // Parse this as a typename.
699  ParseDeclarator(ParamDecl);
700  if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
701  Diag(Tok.getLocation(), diag::err_expected_template_parameter);
702  return nullptr;
703  }
704 
705  // Recover from misplaced ellipsis.
706  SourceLocation EllipsisLoc;
707  if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
708  DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
709 
710  // If there is a default value, parse it.
711  // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
712  // we introduce the template parameter into the local scope.
713  SourceLocation EqualLoc;
714  ExprResult DefaultArg;
715  if (TryConsumeToken(tok::equal, EqualLoc)) {
716  // C++ [temp.param]p15:
717  // When parsing a default template-argument for a non-type
718  // template-parameter, the first non-nested > is taken as the
719  // end of the template-parameter-list rather than a greater-than
720  // operator.
721  GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
722  EnterExpressionEvaluationContext ConstantEvaluated(
724 
726  if (DefaultArg.isInvalid())
727  SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
728  }
729 
730  // Create the parameter.
731  return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
732  Depth, Position, EqualLoc,
733  DefaultArg.get());
734 }
735 
736 void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
737  SourceLocation CorrectLoc,
738  bool AlreadyHasEllipsis,
739  bool IdentifierHasName) {
740  FixItHint Insertion;
741  if (!AlreadyHasEllipsis)
742  Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
743  Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
744  << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
745  << !IdentifierHasName;
746 }
747 
748 void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
749  Declarator &D) {
750  assert(EllipsisLoc.isValid());
751  bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
752  if (!AlreadyHasEllipsis)
753  D.setEllipsisLoc(EllipsisLoc);
754  DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
755  AlreadyHasEllipsis, D.hasName());
756 }
757 
758 /// Parses a '>' at the end of a template list.
759 ///
760 /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
761 /// to determine if these tokens were supposed to be a '>' followed by
762 /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
763 ///
764 /// \param RAngleLoc the location of the consumed '>'.
765 ///
766 /// \param ConsumeLastToken if true, the '>' is consumed.
767 ///
768 /// \param ObjCGenericList if true, this is the '>' closing an Objective-C
769 /// type parameter or type argument list, rather than a C++ template parameter
770 /// or argument list.
771 ///
772 /// \returns true, if current token does not start with '>', false otherwise.
773 bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc,
774  bool ConsumeLastToken,
775  bool ObjCGenericList) {
776  // What will be left once we've consumed the '>'.
777  tok::TokenKind RemainingToken;
778  const char *ReplacementStr = "> >";
779  bool MergeWithNextToken = false;
780 
781  switch (Tok.getKind()) {
782  default:
783  Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
784  return true;
785 
786  case tok::greater:
787  // Determine the location of the '>' token. Only consume this token
788  // if the caller asked us to.
789  RAngleLoc = Tok.getLocation();
790  if (ConsumeLastToken)
791  ConsumeToken();
792  return false;
793 
794  case tok::greatergreater:
795  RemainingToken = tok::greater;
796  break;
797 
798  case tok::greatergreatergreater:
799  RemainingToken = tok::greatergreater;
800  break;
801 
802  case tok::greaterequal:
803  RemainingToken = tok::equal;
804  ReplacementStr = "> =";
805 
806  // Join two adjacent '=' tokens into one, for cases like:
807  // void (*p)() = f<int>;
808  // return f<int>==p;
809  if (NextToken().is(tok::equal) &&
810  areTokensAdjacent(Tok, NextToken())) {
811  RemainingToken = tok::equalequal;
812  MergeWithNextToken = true;
813  }
814  break;
815 
816  case tok::greatergreaterequal:
817  RemainingToken = tok::greaterequal;
818  break;
819  }
820 
821  // This template-id is terminated by a token that starts with a '>'.
822  // Outside C++11 and Objective-C, this is now error recovery.
823  //
824  // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we
825  // extend that treatment to also apply to the '>>>' token.
826  //
827  // Objective-C allows this in its type parameter / argument lists.
828 
829  SourceLocation TokBeforeGreaterLoc = PrevTokLocation;
830  SourceLocation TokLoc = Tok.getLocation();
831  Token Next = NextToken();
832 
833  // Whether splitting the current token after the '>' would undesirably result
834  // in the remaining token pasting with the token after it. This excludes the
835  // MergeWithNextToken cases, which we've already handled.
836  bool PreventMergeWithNextToken =
837  (RemainingToken == tok::greater ||
838  RemainingToken == tok::greatergreater) &&
839  (Next.isOneOf(tok::greater, tok::greatergreater,
840  tok::greatergreatergreater, tok::equal, tok::greaterequal,
841  tok::greatergreaterequal, tok::equalequal)) &&
842  areTokensAdjacent(Tok, Next);
843 
844  // Diagnose this situation as appropriate.
845  if (!ObjCGenericList) {
846  // The source range of the replaced token(s).
848  TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(),
849  getLangOpts()));
850 
851  // A hint to put a space between the '>>'s. In order to make the hint as
852  // clear as possible, we include the characters either side of the space in
853  // the replacement, rather than just inserting a space at SecondCharLoc.
854  FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
855  ReplacementStr);
856 
857  // A hint to put another space after the token, if it would otherwise be
858  // lexed differently.
859  FixItHint Hint2;
860  if (PreventMergeWithNextToken)
861  Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
862 
863  unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
864  if (getLangOpts().CPlusPlus11 &&
865  (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
866  DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
867  else if (Tok.is(tok::greaterequal))
868  DiagId = diag::err_right_angle_bracket_equal_needs_space;
869  Diag(TokLoc, DiagId) << Hint1 << Hint2;
870  }
871 
872  // Find the "length" of the resulting '>' token. This is not always 1, as it
873  // can contain escaped newlines.
874  unsigned GreaterLength = Lexer::getTokenPrefixLength(
875  TokLoc, 1, PP.getSourceManager(), getLangOpts());
876 
877  // Annotate the source buffer to indicate that we split the token after the
878  // '>'. This allows us to properly find the end of, and extract the spelling
879  // of, the '>' token later.
880  RAngleLoc = PP.SplitToken(TokLoc, GreaterLength);
881 
882  // Strip the initial '>' from the token.
883  bool CachingTokens = PP.IsPreviousCachedToken(Tok);
884 
885  Token Greater = Tok;
886  Greater.setLocation(RAngleLoc);
887  Greater.setKind(tok::greater);
888  Greater.setLength(GreaterLength);
889 
890  unsigned OldLength = Tok.getLength();
891  if (MergeWithNextToken) {
892  ConsumeToken();
893  OldLength += Tok.getLength();
894  }
895 
896  Tok.setKind(RemainingToken);
897  Tok.setLength(OldLength - GreaterLength);
898 
899  // Split the second token if lexing it normally would lex a different token
900  // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>').
901  SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength);
902  if (PreventMergeWithNextToken)
903  AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength());
904  Tok.setLocation(AfterGreaterLoc);
905 
906  // Update the token cache to match what we just did if necessary.
907  if (CachingTokens) {
908  // If the previous cached token is being merged, delete it.
909  if (MergeWithNextToken)
911 
912  if (ConsumeLastToken)
914  else
915  PP.ReplacePreviousCachedToken({Greater});
916  }
917 
918  if (ConsumeLastToken) {
919  PrevTokLocation = RAngleLoc;
920  } else {
921  PrevTokLocation = TokBeforeGreaterLoc;
922  PP.EnterToken(Tok, /*IsReinject=*/true);
923  Tok = Greater;
924  }
925 
926  return false;
927 }
928 
929 
930 /// Parses a template-id that after the template name has
931 /// already been parsed.
932 ///
933 /// This routine takes care of parsing the enclosed template argument
934 /// list ('<' template-parameter-list [opt] '>') and placing the
935 /// results into a form that can be transferred to semantic analysis.
936 ///
937 /// \param ConsumeLastToken if true, then we will consume the last
938 /// token that forms the template-id. Otherwise, we will leave the
939 /// last token in the stream (e.g., so that it can be replaced with an
940 /// annotation token).
941 bool
942 Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,
943  SourceLocation &LAngleLoc,
944  TemplateArgList &TemplateArgs,
945  SourceLocation &RAngleLoc) {
946  assert(Tok.is(tok::less) && "Must have already parsed the template-name");
947 
948  // Consume the '<'.
949  LAngleLoc = ConsumeToken();
950 
951  // Parse the optional template-argument-list.
952  bool Invalid = false;
953  {
954  GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
955  if (!Tok.isOneOf(tok::greater, tok::greatergreater,
956  tok::greatergreatergreater, tok::greaterequal,
957  tok::greatergreaterequal))
958  Invalid = ParseTemplateArgumentList(TemplateArgs);
959 
960  if (Invalid) {
961  // Try to find the closing '>'.
962  if (ConsumeLastToken)
963  SkipUntil(tok::greater, StopAtSemi);
964  else
965  SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
966  return true;
967  }
968  }
969 
970  return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken,
971  /*ObjCGenericList=*/false);
972 }
973 
974 /// Replace the tokens that form a simple-template-id with an
975 /// annotation token containing the complete template-id.
976 ///
977 /// The first token in the stream must be the name of a template that
978 /// is followed by a '<'. This routine will parse the complete
979 /// simple-template-id and replace the tokens with a single annotation
980 /// token with one of two different kinds: if the template-id names a
981 /// type (and \p AllowTypeAnnotation is true), the annotation token is
982 /// a type annotation that includes the optional nested-name-specifier
983 /// (\p SS). Otherwise, the annotation token is a template-id
984 /// annotation that does not include the optional
985 /// nested-name-specifier.
986 ///
987 /// \param Template the declaration of the template named by the first
988 /// token (an identifier), as returned from \c Action::isTemplateName().
989 ///
990 /// \param TNK the kind of template that \p Template
991 /// refers to, as returned from \c Action::isTemplateName().
992 ///
993 /// \param SS if non-NULL, the nested-name-specifier that precedes
994 /// this template name.
995 ///
996 /// \param TemplateKWLoc if valid, specifies that this template-id
997 /// annotation was preceded by the 'template' keyword and gives the
998 /// location of that keyword. If invalid (the default), then this
999 /// template-id was not preceded by a 'template' keyword.
1000 ///
1001 /// \param AllowTypeAnnotation if true (the default), then a
1002 /// simple-template-id that refers to a class template, template
1003 /// template parameter, or other template that produces a type will be
1004 /// replaced with a type annotation token. Otherwise, the
1005 /// simple-template-id is always replaced with a template-id
1006 /// annotation token.
1007 ///
1008 /// If an unrecoverable parse error occurs and no annotation token can be
1009 /// formed, this function returns true.
1010 ///
1011 bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
1012  CXXScopeSpec &SS,
1013  SourceLocation TemplateKWLoc,
1015  bool AllowTypeAnnotation) {
1016  assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
1017  assert(Template && Tok.is(tok::less) &&
1018  "Parser isn't at the beginning of a template-id");
1019 
1020  // Consume the template-name.
1021  SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
1022 
1023  // Parse the enclosed template argument list.
1024  SourceLocation LAngleLoc, RAngleLoc;
1025  TemplateArgList TemplateArgs;
1026  bool Invalid = ParseTemplateIdAfterTemplateName(false, LAngleLoc,
1027  TemplateArgs,
1028  RAngleLoc);
1029 
1030  if (Invalid) {
1031  // If we failed to parse the template ID but skipped ahead to a >, we're not
1032  // going to be able to form a token annotation. Eat the '>' if present.
1033  TryConsumeToken(tok::greater);
1034  // FIXME: Annotate the token stream so we don't produce the same errors
1035  // again if we're doing this annotation as part of a tentative parse.
1036  return true;
1037  }
1038 
1039  ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
1040 
1041  // Build the annotation token.
1042  if (TNK == TNK_Type_template && AllowTypeAnnotation) {
1044  getCurScope(), SS, TemplateKWLoc, Template, TemplateName.Identifier,
1045  TemplateNameLoc, LAngleLoc, TemplateArgsPtr, RAngleLoc);
1046  if (Type.isInvalid()) {
1047  // If we failed to parse the template ID but skipped ahead to a >, we're
1048  // not going to be able to form a token annotation. Eat the '>' if
1049  // present.
1050  TryConsumeToken(tok::greater);
1051  // FIXME: Annotate the token stream so we don't produce the same errors
1052  // again if we're doing this annotation as part of a tentative parse.
1053  return true;
1054  }
1055 
1056  Tok.setKind(tok::annot_typename);
1057  setTypeAnnotation(Tok, Type.get());
1058  if (SS.isNotEmpty())
1059  Tok.setLocation(SS.getBeginLoc());
1060  else if (TemplateKWLoc.isValid())
1061  Tok.setLocation(TemplateKWLoc);
1062  else
1063  Tok.setLocation(TemplateNameLoc);
1064  } else {
1065  // Build a template-id annotation token that can be processed
1066  // later.
1067  Tok.setKind(tok::annot_template_id);
1068 
1069  IdentifierInfo *TemplateII =
1070  TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
1071  ? TemplateName.Identifier
1072  : nullptr;
1073 
1074  OverloadedOperatorKind OpKind =
1075  TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
1076  ? OO_None
1077  : TemplateName.OperatorFunctionId.Operator;
1078 
1080  SS, TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK,
1081  LAngleLoc, RAngleLoc, TemplateArgs, TemplateIds);
1082 
1083  Tok.setAnnotationValue(TemplateId);
1084  if (TemplateKWLoc.isValid())
1085  Tok.setLocation(TemplateKWLoc);
1086  else
1087  Tok.setLocation(TemplateNameLoc);
1088  }
1089 
1090  // Common fields for the annotation token
1091  Tok.setAnnotationEndLoc(RAngleLoc);
1092 
1093  // In case the tokens were cached, have Preprocessor replace them with the
1094  // annotation token.
1095  PP.AnnotateCachedTokens(Tok);
1096  return false;
1097 }
1098 
1099 /// Replaces a template-id annotation token with a type
1100 /// annotation token.
1101 ///
1102 /// If there was a failure when forming the type from the template-id,
1103 /// a type annotation token will still be created, but will have a
1104 /// NULL type pointer to signify an error.
1105 ///
1106 /// \param IsClassName Is this template-id appearing in a context where we
1107 /// know it names a class, such as in an elaborated-type-specifier or
1108 /// base-specifier? ('typename' and 'template' are unneeded and disallowed
1109 /// in those contexts.)
1110 void Parser::AnnotateTemplateIdTokenAsType(bool IsClassName) {
1111  assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
1112 
1113  TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1114  assert((TemplateId->Kind == TNK_Type_template ||
1115  TemplateId->Kind == TNK_Dependent_template_name ||
1116  TemplateId->Kind == TNK_Undeclared_template) &&
1117  "Only works for type and dependent templates");
1118 
1119  ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1120  TemplateId->NumArgs);
1121 
1122  TypeResult Type
1123  = Actions.ActOnTemplateIdType(getCurScope(),
1124  TemplateId->SS,
1125  TemplateId->TemplateKWLoc,
1126  TemplateId->Template,
1127  TemplateId->Name,
1128  TemplateId->TemplateNameLoc,
1129  TemplateId->LAngleLoc,
1130  TemplateArgsPtr,
1131  TemplateId->RAngleLoc,
1132  /*IsCtorOrDtorName*/false,
1133  IsClassName);
1134  // Create the new "type" annotation token.
1135  Tok.setKind(tok::annot_typename);
1136  setTypeAnnotation(Tok, Type.isInvalid() ? nullptr : Type.get());
1137  if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
1138  Tok.setLocation(TemplateId->SS.getBeginLoc());
1139  // End location stays the same
1140 
1141  // Replace the template-id annotation token, and possible the scope-specifier
1142  // that precedes it, with the typename annotation token.
1143  PP.AnnotateCachedTokens(Tok);
1144 }
1145 
1146 /// Determine whether the given token can end a template argument.
1147 static bool isEndOfTemplateArgument(Token Tok) {
1148  return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater);
1149 }
1150 
1151 /// Parse a C++ template template argument.
1152 ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
1153  if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
1154  !Tok.is(tok::annot_cxxscope))
1155  return ParsedTemplateArgument();
1156 
1157  // C++0x [temp.arg.template]p1:
1158  // A template-argument for a template template-parameter shall be the name
1159  // of a class template or an alias template, expressed as id-expression.
1160  //
1161  // We parse an id-expression that refers to a class template or alias
1162  // template. The grammar we parse is:
1163  //
1164  // nested-name-specifier[opt] template[opt] identifier ...[opt]
1165  //
1166  // followed by a token that terminates a template argument, such as ',',
1167  // '>', or (in some cases) '>>'.
1168  CXXScopeSpec SS; // nested-name-specifier, if present
1169  ParseOptionalCXXScopeSpecifier(SS, nullptr,
1170  /*EnteringContext=*/false);
1171 
1173  SourceLocation EllipsisLoc;
1174  if (SS.isSet() && Tok.is(tok::kw_template)) {
1175  // Parse the optional 'template' keyword following the
1176  // nested-name-specifier.
1177  SourceLocation TemplateKWLoc = ConsumeToken();
1178 
1179  if (Tok.is(tok::identifier)) {
1180  // We appear to have a dependent template name.
1181  UnqualifiedId Name;
1182  Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1183  ConsumeToken(); // the identifier
1184 
1185  TryConsumeToken(tok::ellipsis, EllipsisLoc);
1186 
1187  // If the next token signals the end of a template argument,
1188  // then we have a dependent template name that could be a template
1189  // template argument.
1190  TemplateTy Template;
1191  if (isEndOfTemplateArgument(Tok) &&
1193  getCurScope(), SS, TemplateKWLoc, Name,
1194  /*ObjectType=*/nullptr,
1195  /*EnteringContext=*/false, Template))
1196  Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1197  }
1198  } else if (Tok.is(tok::identifier)) {
1199  // We may have a (non-dependent) template name.
1200  TemplateTy Template;
1201  UnqualifiedId Name;
1202  Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1203  ConsumeToken(); // the identifier
1204 
1205  TryConsumeToken(tok::ellipsis, EllipsisLoc);
1206 
1207  if (isEndOfTemplateArgument(Tok)) {
1208  bool MemberOfUnknownSpecialization;
1209  TemplateNameKind TNK = Actions.isTemplateName(
1210  getCurScope(), SS,
1211  /*hasTemplateKeyword=*/false, Name,
1212  /*ObjectType=*/nullptr,
1213  /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization);
1214  if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
1215  // We have an id-expression that refers to a class template or
1216  // (C++0x) alias template.
1217  Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1218  }
1219  }
1220  }
1221 
1222  // If this is a pack expansion, build it as such.
1223  if (EllipsisLoc.isValid() && !Result.isInvalid())
1224  Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
1225 
1226  return Result;
1227 }
1228 
1229 /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
1230 ///
1231 /// template-argument: [C++ 14.2]
1232 /// constant-expression
1233 /// type-id
1234 /// id-expression
1235 ParsedTemplateArgument Parser::ParseTemplateArgument() {
1236  // C++ [temp.arg]p2:
1237  // In a template-argument, an ambiguity between a type-id and an
1238  // expression is resolved to a type-id, regardless of the form of
1239  // the corresponding template-parameter.
1240  //
1241  // Therefore, we initially try to parse a type-id - and isCXXTypeId might look
1242  // up and annotate an identifier as an id-expression during disambiguation,
1243  // so enter the appropriate context for a constant expression template
1244  // argument before trying to disambiguate.
1245 
1246  EnterExpressionEvaluationContext EnterConstantEvaluated(
1248  /*LambdaContextDecl=*/nullptr,
1250  if (isCXXTypeId(TypeIdAsTemplateArgument)) {
1251  TypeResult TypeArg = ParseTypeName(
1252  /*Range=*/nullptr, DeclaratorContext::TemplateArgContext);
1253  return Actions.ActOnTemplateTypeArgument(TypeArg);
1254  }
1255 
1256  // Try to parse a template template argument.
1257  {
1258  TentativeParsingAction TPA(*this);
1259 
1260  ParsedTemplateArgument TemplateTemplateArgument
1261  = ParseTemplateTemplateArgument();
1262  if (!TemplateTemplateArgument.isInvalid()) {
1263  TPA.Commit();
1264  return TemplateTemplateArgument;
1265  }
1266 
1267  // Revert this tentative parse to parse a non-type template argument.
1268  TPA.Revert();
1269  }
1270 
1271  // Parse a non-type template argument.
1272  SourceLocation Loc = Tok.getLocation();
1274  if (ExprArg.isInvalid() || !ExprArg.get())
1275  return ParsedTemplateArgument();
1276 
1278  ExprArg.get(), Loc);
1279 }
1280 
1281 /// ParseTemplateArgumentList - Parse a C++ template-argument-list
1282 /// (C++ [temp.names]). Returns true if there was an error.
1283 ///
1284 /// template-argument-list: [C++ 14.2]
1285 /// template-argument
1286 /// template-argument-list ',' template-argument
1287 bool
1288 Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
1289 
1290  ColonProtectionRAIIObject ColonProtection(*this, false);
1291 
1292  do {
1293  ParsedTemplateArgument Arg = ParseTemplateArgument();
1294  SourceLocation EllipsisLoc;
1295  if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
1296  Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
1297 
1298  if (Arg.isInvalid()) {
1299  SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
1300  return true;
1301  }
1302 
1303  // Save this template argument.
1304  TemplateArgs.push_back(Arg);
1305 
1306  // If the next token is a comma, consume it and keep reading
1307  // arguments.
1308  } while (TryConsumeToken(tok::comma));
1309 
1310  return false;
1311 }
1312 
1313 /// Parse a C++ explicit template instantiation
1314 /// (C++ [temp.explicit]).
1315 ///
1316 /// explicit-instantiation:
1317 /// 'extern' [opt] 'template' declaration
1318 ///
1319 /// Note that the 'extern' is a GNU extension and C++11 feature.
1320 Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context,
1321  SourceLocation ExternLoc,
1322  SourceLocation TemplateLoc,
1323  SourceLocation &DeclEnd,
1324  ParsedAttributes &AccessAttrs,
1325  AccessSpecifier AS) {
1326  // This isn't really required here.
1328  ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
1329 
1330  return ParseSingleDeclarationAfterTemplate(
1331  Context, ParsedTemplateInfo(ExternLoc, TemplateLoc),
1332  ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
1333 }
1334 
1336  if (TemplateParams)
1337  return getTemplateParamsRange(TemplateParams->data(),
1338  TemplateParams->size());
1339 
1340  SourceRange R(TemplateLoc);
1341  if (ExternLoc.isValid())
1342  R.setBegin(ExternLoc);
1343  return R;
1344 }
1345 
1346 void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
1347  ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
1348 }
1349 
1350 /// Late parse a C++ function template in Microsoft mode.
1351 void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
1352  if (!LPT.D)
1353  return;
1354 
1355  // Get the FunctionDecl.
1356  FunctionDecl *FunD = LPT.D->getAsFunction();
1357  // Track template parameter depth.
1358  TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1359 
1360  // To restore the context after late parsing.
1361  Sema::ContextRAII GlobalSavedContext(
1362  Actions, Actions.Context.getTranslationUnitDecl());
1363 
1364  SmallVector<ParseScope*, 4> TemplateParamScopeStack;
1365 
1366  // Get the list of DeclContexts to reenter. For inline methods, we only want
1367  // to push the DeclContext of the outermost class. This matches the way the
1368  // parser normally parses bodies of inline methods when the outermost class is
1369  // complete.
1370  struct ContainingDC {
1371  ContainingDC(DeclContext *DC, bool ShouldPush) : Pair(DC, ShouldPush) {}
1372  llvm::PointerIntPair<DeclContext *, 1, bool> Pair;
1373  DeclContext *getDC() { return Pair.getPointer(); }
1374  bool shouldPushDC() { return Pair.getInt(); }
1375  };
1376  SmallVector<ContainingDC, 4> DeclContextsToReenter;
1377  DeclContext *DD = FunD;
1378  DeclContext *NextContaining = Actions.getContainingDC(DD);
1379  while (DD && !DD->isTranslationUnit()) {
1380  bool ShouldPush = DD == NextContaining;
1381  DeclContextsToReenter.push_back({DD, ShouldPush});
1382  if (ShouldPush)
1383  NextContaining = Actions.getContainingDC(DD);
1384  DD = DD->getLexicalParent();
1385  }
1386 
1387  // Reenter template scopes from outermost to innermost.
1388  for (ContainingDC CDC : reverse(DeclContextsToReenter)) {
1389  TemplateParamScopeStack.push_back(
1390  new ParseScope(this, Scope::TemplateParamScope));
1391  unsigned NumParamLists = Actions.ActOnReenterTemplateScope(
1392  getCurScope(), cast<Decl>(CDC.getDC()));
1393  CurTemplateDepthTracker.addDepth(NumParamLists);
1394  if (CDC.shouldPushDC()) {
1395  TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope));
1396  Actions.PushDeclContext(Actions.getCurScope(), CDC.getDC());
1397  }
1398  }
1399 
1400  assert(!LPT.Toks.empty() && "Empty body!");
1401 
1402  // Append the current token at the end of the new token stream so that it
1403  // doesn't get lost.
1404  LPT.Toks.push_back(Tok);
1405  PP.EnterTokenStream(LPT.Toks, true, /*IsReinject*/true);
1406 
1407  // Consume the previously pushed token.
1408  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1409  assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
1410  "Inline method not starting with '{', ':' or 'try'");
1411 
1412  // Parse the method body. Function body parsing code is similar enough
1413  // to be re-used for method bodies as well.
1414  ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
1416 
1417  // Recreate the containing function DeclContext.
1418  Sema::ContextRAII FunctionSavedContext(Actions,
1419  Actions.getContainingDC(FunD));
1420 
1421  Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
1422 
1423  if (Tok.is(tok::kw_try)) {
1424  ParseFunctionTryBlock(LPT.D, FnScope);
1425  } else {
1426  if (Tok.is(tok::colon))
1427  ParseConstructorInitializer(LPT.D);
1428  else
1429  Actions.ActOnDefaultCtorInitializers(LPT.D);
1430 
1431  if (Tok.is(tok::l_brace)) {
1432  assert((!isa<FunctionTemplateDecl>(LPT.D) ||
1433  cast<FunctionTemplateDecl>(LPT.D)
1434  ->getTemplateParameters()
1435  ->getDepth() == TemplateParameterDepth - 1) &&
1436  "TemplateParameterDepth should be greater than the depth of "
1437  "current template being instantiated!");
1438  ParseFunctionStatementBody(LPT.D, FnScope);
1439  Actions.UnmarkAsLateParsedTemplate(FunD);
1440  } else
1441  Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
1442  }
1443 
1444  // Exit scopes.
1445  FnScope.Exit();
1447  TemplateParamScopeStack.rbegin();
1448  for (; I != TemplateParamScopeStack.rend(); ++I)
1449  delete *I;
1450 }
1451 
1452 /// Lex a delayed template function for late parsing.
1453 void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1454  tok::TokenKind kind = Tok.getKind();
1455  if (!ConsumeAndStoreFunctionPrologue(Toks)) {
1456  // Consume everything up to (and including) the matching right brace.
1457  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1458  }
1459 
1460  // If we're in a function-try-block, we need to store all the catch blocks.
1461  if (kind == tok::kw_try) {
1462  while (Tok.is(tok::kw_catch)) {
1463  ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1464  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1465  }
1466  }
1467 }
1468 
1469 /// We've parsed something that could plausibly be intended to be a template
1470 /// name (\p LHS) followed by a '<' token, and the following code can't possibly
1471 /// be an expression. Determine if this is likely to be a template-id and if so,
1472 /// diagnose it.
1473 bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) {
1474  TentativeParsingAction TPA(*this);
1475  // FIXME: We could look at the token sequence in a lot more detail here.
1476  if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater,
1478  TPA.Commit();
1479 
1481  ParseGreaterThanInTemplateList(Greater, true, false);
1482  Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS,
1483  Less, Greater);
1484  return true;
1485  }
1486 
1487  // There's no matching '>' token, this probably isn't supposed to be
1488  // interpreted as a template-id. Parse it as an (ill-formed) comparison.
1489  TPA.Revert();
1490  return false;
1491 }
1492 
1493 void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) {
1494  assert(Tok.is(tok::less) && "not at a potential angle bracket");
1495 
1496  bool DependentTemplateName = false;
1497  if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName,
1498  DependentTemplateName))
1499  return;
1500 
1501  // OK, this might be a name that the user intended to be parsed as a
1502  // template-name, followed by a '<' token. Check for some easy cases.
1503 
1504  // If we have potential_template<>, then it's supposed to be a template-name.
1505  if (NextToken().is(tok::greater) ||
1506  (getLangOpts().CPlusPlus11 &&
1507  NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) {
1508  SourceLocation Less = ConsumeToken();
1510  ParseGreaterThanInTemplateList(Greater, true, false);
1511  Actions.diagnoseExprIntendedAsTemplateName(
1512  getCurScope(), PotentialTemplateName, Less, Greater);
1513  // FIXME: Perform error recovery.
1514  PotentialTemplateName = ExprError();
1515  return;
1516  }
1517 
1518  // If we have 'potential_template<type-id', assume it's supposed to be a
1519  // template-name if there's a matching '>' later on.
1520  {
1521  // FIXME: Avoid the tentative parse when NextToken() can't begin a type.
1522  TentativeParsingAction TPA(*this);
1523  SourceLocation Less = ConsumeToken();
1524  if (isTypeIdUnambiguously() &&
1525  diagnoseUnknownTemplateId(PotentialTemplateName, Less)) {
1526  TPA.Commit();
1527  // FIXME: Perform error recovery.
1528  PotentialTemplateName = ExprError();
1529  return;
1530  }
1531  TPA.Revert();
1532  }
1533 
1534  // Otherwise, remember that we saw this in case we see a potentially-matching
1535  // '>' token later on.
1536  AngleBracketTracker::Priority Priority =
1537  (DependentTemplateName ? AngleBracketTracker::DependentName
1538  : AngleBracketTracker::PotentialTypo) |
1539  (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess
1540  : AngleBracketTracker::NoSpaceBeforeLess);
1541  AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(),
1542  Priority);
1543 }
1544 
1545 bool Parser::checkPotentialAngleBracketDelimiter(
1546  const AngleBracketTracker::Loc &LAngle, const Token &OpToken) {
1547  // If a comma in an expression context is followed by a type that can be a
1548  // template argument and cannot be an expression, then this is ill-formed,
1549  // but might be intended to be part of a template-id.
1550  if (OpToken.is(tok::comma) && isTypeIdUnambiguously() &&
1551  diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) {
1552  AngleBrackets.clear(*this);
1553  return true;
1554  }
1555 
1556  // If a context that looks like a template-id is followed by '()', then
1557  // this is ill-formed, but might be intended to be a template-id
1558  // followed by '()'.
1559  if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) &&
1560  NextToken().is(tok::r_paren)) {
1561  Actions.diagnoseExprIntendedAsTemplateName(
1562  getCurScope(), LAngle.TemplateName, LAngle.LessLoc,
1563  OpToken.getLocation());
1564  AngleBrackets.clear(*this);
1565  return true;
1566  }
1567 
1568  // After a '>' (etc), we're no longer potentially in a construct that's
1569  // intended to be treated as a template-id.
1570  if (OpToken.is(tok::greater) ||
1571  (getLangOpts().CPlusPlus11 &&
1572  OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater)))
1573  AngleBrackets.clear(*this);
1574  return false;
1575 }
Defines the clang::ASTContext interface.
void ReplacePreviousCachedToken(ArrayRef< Token > NewToks)
Replace token in CachedLexPos - 1 in CachedTokens by the tokens in NewToks.
Definition: PPCaching.cpp:157
Represents a function declaration or definition.
Definition: Decl.h:1743
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
SourceRange getSourceRange() const LLVM_READONLY
Return the source range that covers this unqualified-id.
Definition: DeclSpec.h:1137
SourceLocation StartLocation
The location of the first token that describes this unqualified-id, which will be the location of the...
Definition: DeclSpec.h:995
IdentifierInfo * Identifier
When Kind == IK_Identifier, the parsed identifier, or when Kind == IK_UserLiteralId, the identifier suffix.
Definition: DeclSpec.h:965
TemplateNameKind isTemplateName(Scope *S, CXXScopeSpec &SS, bool hasTemplateKeyword, const UnqualifiedId &Name, ParsedType ObjectType, bool EnteringContext, TemplateTy &Template, bool &MemberOfUnknownSpecialization)
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:97
The name refers to a dependent template name:
Definition: TemplateKinds.h:46
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
RAII object used to inform the actions that we&#39;re currently parsing a declaration.
Defines the C++ template declaration subclasses.
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1414
This indicates that the scope corresponds to a function, which means that labels are set here...
Definition: Scope.h:47
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:105
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:57
static TemplateIdAnnotation * Create(CXXScopeSpec SS, SourceLocation TemplateKWLoc, SourceLocation TemplateNameLoc, IdentifierInfo *Name, OverloadedOperatorKind OperatorKind, ParsedTemplateTy OpaqueTemplateName, TemplateNameKind TemplateKind, SourceLocation LAngleLoc, SourceLocation RAngleLoc, ArrayRef< ParsedTemplateArgument > TemplateArgs, SmallVectorImpl< TemplateIdAnnotation *> &CleanupList)
Creates a new TemplateIdAnnotation with NumArgs arguments and appends it to List. ...
RAII object that enters a new expression evaluation context.
Definition: Sema.h:11131
Information about one declarator, including the parsed type information and the identifier.
Definition: DeclSpec.h:1768
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:67
CharSourceRange getSourceRange(const SourceRange &Range)
Returns the token CharSourceRange corresponding to Range.
Definition: FixIt.h:32
friend class ObjCDeclContextSwitch
Definition: Parser.h:61
ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and restores it when destroyed...
tok::TokenKind getKind() const
Definition: Token.h:92
bool SkipUntil(tok::TokenKind T, SkipUntilFlags Flags=static_cast< SkipUntilFlags >(0))
SkipUntil - Read tokens until we get to the specified token, then consume it (unless StopBeforeMatch ...
Definition: Parser.h:1089
Information about a template-id annotation token.
Represents a struct/union/class.
Definition: Decl.h:3598
const Token & NextToken()
NextToken - This peeks ahead one token and returns it without consuming it.
Definition: Parser.h:755
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
bool TryConsumeToken(tok::TokenKind Expected)
Definition: Parser.h:456
One of these records is kept for each identifier that is lexed.
Lookup for the name failed, but we&#39;re assuming it was a template name anyway.
Definition: TemplateKinds.h:50
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:442
OverloadedOperatorKind Operator
The kind of overloaded operator.
Definition: DeclSpec.h:948
struct OFI OperatorFunctionId
When Kind == IK_OperatorFunctionId, the overloaded operator that we parsed.
Definition: DeclSpec.h:969
CachedTokens Toks
Definition: Sema.h:11186
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
A non-type template parameter, stored as an expression.
void setKind(tok::TokenKind K)
Definition: Token.h:93
NamedDecl * ActOnTemplateTemplateParameter(Scope *S, SourceLocation TmpLoc, TemplateParameterList *Params, SourceLocation EllipsisLoc, IdentifierInfo *ParamName, SourceLocation ParamNameLoc, unsigned Depth, unsigned Position, SourceLocation EqualLoc, ParsedTemplateArgument DefaultArg)
ActOnTemplateTemplateParameter - Called when a C++ template template parameter (e.g.
Represents a C++ unqualified-id that has been parsed.
Definition: DeclSpec.h:937
PtrTy get() const
Definition: Ownership.h:170
ExprResult CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl=nullptr, llvm::function_ref< ExprResult(Expr *)> Filter=[](Expr *E) -> ExprResult { return E;})
Process any TypoExprs in the given Expr and its children, generating diagnostics as appropriate and r...
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
bool IsPreviousCachedToken(const Token &Tok) const
Whether Tok is the most recent token (CachedLexPos - 1) in CachedTokens.
Definition: PPCaching.cpp:139
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Characters, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token...
Definition: Lexer.h:348
Represents a C++ nested-name-specifier or a global scope specifier.
Definition: DeclSpec.h:63
SourceLocation ConsumeAnyToken(bool ConsumeCodeCompletionTok=false)
ConsumeAnyToken - Dispatch to the right Consume* method based on the current token type...
Definition: Parser.h:476
The current context is "potentially evaluated" in C++11 terms, but the expression is evaluated at com...
unsigned getFlags() const
getFlags - Return the flags for this scope.
Definition: Scope.h:220
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:319
A class for parsing a declarator.
TemplateParameterList * ActOnTemplateParameterList(unsigned Depth, SourceLocation ExportLoc, SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl *> Params, SourceLocation RAngleLoc, Expr *RequiresClause)
ActOnTemplateParameterList - Builds a TemplateParameterList, optionally constrained by RequiresClause...
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:1786
void setAnnotationValue(void *val)
Definition: Token.h:230
Decl * ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, DeclSpec &DS, RecordDecl *&AnonRecord)
ParsedFreeStandingDeclSpec - This method is invoked when a declspec with no declarator (e...
Definition: SemaDecl.cpp:4140
Represents a character-granular source range.
void AnnotateCachedTokens(const Token &Tok)
We notify the Preprocessor that if it is caching tokens (because backtrack is enabled) it should repl...
This file defines the classes used to store parsed information about declaration-specifiers and decla...
void SkipMalformedDecl()
SkipMalformedDecl - Read tokens until we get to some likely good stopping point for skipping past a s...
Definition: ParseDecl.cpp:1897
OpaquePtr< TemplateName > TemplateTy
Definition: Parser.h:420
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:126
static unsigned getTokenPrefixLength(SourceLocation TokStart, unsigned CharNo, const SourceManager &SM, const LangOptions &LangOpts)
Get the physical length (including trigraphs and escaped newlines) of the first Characters characters...
Definition: Lexer.cpp:710
SourceLocation getBeginLoc() const
Definition: DeclSpec.h:72
static bool isEndOfTemplateArgument(Token Tok)
Determine whether the given token can end a template argument.
Represents a C++ template name within the type system.
Definition: TemplateName.h:187
This is a compound statement scope.
Definition: Scope.h:130
int Depth
Definition: ASTDiff.cpp:190
UnqualifiedIdKind getKind() const
Determine what kind of name we have.
Definition: DeclSpec.h:1019
TemplateNameKind
Specifies the kind of template name that an identifier refers to.
Definition: TemplateKinds.h:20
DeclaratorContext
Definition: DeclSpec.h:1726
void setEllipsisLoc(SourceLocation EL)
Definition: DeclSpec.h:2459
bool isInvalid() const
Definition: Ownership.h:166
bool isUsable() const
Definition: Ownership.h:167
The result type of a method or function.
ParsedTemplateArgument ActOnPackExpansion(const ParsedTemplateArgument &Arg, SourceLocation EllipsisLoc)
Invoked when parsing a template argument followed by an ellipsis, which creates a pack expansion...
RAII object that makes &#39;>&#39; behave either as an operator or as the closing angle bracket for a templat...
const LangOptions & getLangOpts() const
Definition: Parser.h:403
static CharSourceRange getCharRange(SourceRange R)
SourceManager & getSourceManager() const
Definition: Preprocessor.h:907
A class for parsing a DeclSpec.
Stop skipping at semicolon.
Definition: Parser.h:1069
ActionResult - This structure is used while parsing/acting on expressions, stmts, etc...
Definition: Ownership.h:153
Represents the parsed form of a C++ template argument.
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:217
bool hasName() const
hasName - Whether this declarator has a name, which might be an identifier (accessible via getIdentif...
Definition: DeclSpec.h:2120
Encodes a location in the source.
void setLength(unsigned Len)
Definition: Token.h:135
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:310
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:179
void setAnnotationEndLoc(SourceLocation L)
Definition: Token.h:144
NamedDecl * ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, unsigned Depth, unsigned Position, SourceLocation EqualLoc, Expr *DefaultArg)
TemplateNameKind ActOnDependentTemplateName(Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, const UnqualifiedId &Name, ParsedType ObjectType, bool EnteringContext, TemplateTy &Template, bool AllowInjectedClassName=false)
Form a dependent template name.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
void EnterToken(const Token &Tok, bool IsReinject)
Enters a token in the token stream to be lexed next.
Scope * getCurScope() const
Definition: Parser.h:410
ExprResult ParseConstantExpressionInExprEvalContext(TypeCastState isTypeCast=NotTypeCast)
Definition: ParseExpr.cpp:200
TypeResult ActOnTemplateIdType(Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, TemplateTy Template, IdentifierInfo *TemplateII, SourceLocation TemplateIILoc, SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgs, SourceLocation RAngleLoc, bool IsCtorOrDtorName=false, bool IsClassName=false)
bool isNot(tok::TokenKind K) const
Definition: Token.h:98
SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length)
Split the first Length characters out of the token starting at TokLoc and return a location pointing ...
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:117
This is a scope that corresponds to the template parameters of a C++ template.
Definition: Scope.h:77
ExprResult ParseConstraintExpression()
Parse a constraint-expression.
Definition: ParseExpr.cpp:234
bool isInvalid() const
Determine whether the given template argument is invalid.
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:99
The name refers to a template whose specialization produces a type.
Definition: TemplateKinds.h:30
static const TST TST_unspecified
Definition: DeclSpec.h:272
unsigned getLength() const
Definition: Token.h:129
NamedDecl * ActOnTypeParameter(Scope *S, bool Typename, SourceLocation EllipsisLoc, SourceLocation KeyLoc, IdentifierInfo *ParamName, SourceLocation ParamNameLoc, unsigned Depth, unsigned Position, SourceLocation EqualLoc, ParsedType DefaultArg)
ActOnTypeParameter - Called when a C++ template type parameter (e.g., "typename T") has been parsed...
Not an overloaded operator.
Definition: OperatorKinds.h:22
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Definition: Parser.cpp:72
ParsedTemplateArgument ActOnTemplateTypeArgument(TypeResult ParsedType)
Convert a parsed type into a parsed template argument.
SourceRange getTemplateParamsRange(TemplateParameterList const *const *Params, unsigned NumParams)
Retrieves the range of the given template parameter lists.
ExprResult ParseAssignmentExpression(TypeCastState isTypeCast=NotTypeCast)
Parse an expr that doesn&#39;t include (top-level) commas.
Definition: ParseExpr.cpp:159
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:91
SmallVector< TemplateParameterList *, 4 > TemplateParameterLists
Definition: Parser.h:422
This is a scope that can contain a declaration.
Definition: Scope.h:59
SourceLocation getIdentifierLoc() const
Definition: DeclSpec.h:2136
bool isSet() const
Deprecated.
Definition: DeclSpec.h:209
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1004
Captures information about "declaration specifiers".
Definition: DeclSpec.h:228
SourceLocation ConsumeToken()
ConsumeToken - Consume the current &#39;peek token&#39; and lex the next one.
Definition: Parser.h:448
SourceLocation getEllipsisLoc() const
Definition: DeclSpec.h:2458
Decl * D
The template function declaration to be late parsed.
Definition: Sema.h:11188
bool isNotEmpty() const
A scope specifier is present, but may be valid or invalid.
Definition: DeclSpec.h:191
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:60
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:128
A template-id, e.g., f<int>.
ExprResult ExprError()
Definition: Ownership.h:279
Contains a late templated function.
Definition: Sema.h:11185
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:65
void setLocation(SourceLocation L)
Definition: Token.h:134
A trivial tuple used to represent a source range.
ASTContext & Context
Definition: Sema.h:374
This represents a decl that may have a name.
Definition: Decl.h:248
void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc)
Specify that this unqualified-id was parsed as an identifier.
Definition: DeclSpec.h:1026
bool isTranslationUnit() const
Definition: DeclBase.h:1845
SourceLocation getBegin() const
ParsedAttributes - A collection of parsed attributes.
Definition: ParsedAttr.h:906
TypeResult ParseTypeName(SourceRange *Range=nullptr, DeclaratorContext Context=DeclaratorContext::TypeNameContext, AccessSpecifier AS=AS_none, Decl **OwnedType=nullptr, ParsedAttributes *Attrs=nullptr)
ParseTypeName type-name: [C99 6.7.6] specifier-qualifier-list abstract-declarator[opt].
Definition: ParseDecl.cpp:42
Stop skipping at specified token, but don&#39;t skip the token itself.
Definition: Parser.h:1071
A RAII object to temporarily push a declaration context.
Definition: Sema.h:768
SourceLocation getEndLoc() const
Definition: Token.h:153