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