ParseTemplate.cpp revision 199990
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/Parse/Parser.h"
15#include "clang/Parse/ParseDiagnostic.h"
16#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
18#include "clang/Parse/Template.h"
19using namespace clang;
20
21/// \brief Parse a template declaration, explicit instantiation, or
22/// explicit specialization.
23Parser::DeclPtrTy
24Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
25                                             SourceLocation &DeclEnd,
26                                             AccessSpecifier AS) {
27  if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
28    return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
29                                      DeclEnd);
30
31  return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
32}
33
34/// \brief RAII class that manages the template parameter depth.
35namespace {
36  class TemplateParameterDepthCounter {
37    unsigned &Depth;
38    unsigned AddedLevels;
39
40  public:
41    explicit TemplateParameterDepthCounter(unsigned &Depth)
42      : Depth(Depth), AddedLevels(0) { }
43
44    ~TemplateParameterDepthCounter() {
45      Depth -= AddedLevels;
46    }
47
48    void operator++() {
49      ++Depth;
50      ++AddedLevels;
51    }
52
53    operator unsigned() const { return Depth; }
54  };
55}
56
57/// \brief Parse a template declaration or an explicit specialization.
58///
59/// Template declarations include one or more template parameter lists
60/// and either the function or class template declaration. Explicit
61/// specializations contain one or more 'template < >' prefixes
62/// followed by a (possibly templated) declaration. Since the
63/// syntactic form of both features is nearly identical, we parse all
64/// of the template headers together and let semantic analysis sort
65/// the declarations from the explicit specializations.
66///
67///       template-declaration: [C++ temp]
68///         'export'[opt] 'template' '<' template-parameter-list '>' declaration
69///
70///       explicit-specialization: [ C++ temp.expl.spec]
71///         'template' '<' '>' declaration
72Parser::DeclPtrTy
73Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
74                                                 SourceLocation &DeclEnd,
75                                                 AccessSpecifier AS) {
76  assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
77         "Token does not start a template declaration.");
78
79  // Enter template-parameter scope.
80  ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
81
82  // Parse multiple levels of template headers within this template
83  // parameter scope, e.g.,
84  //
85  //   template<typename T>
86  //     template<typename U>
87  //       class A<T>::B { ... };
88  //
89  // We parse multiple levels non-recursively so that we can build a
90  // single data structure containing all of the template parameter
91  // lists to easily differentiate between the case above and:
92  //
93  //   template<typename T>
94  //   class A {
95  //     template<typename U> class B;
96  //   };
97  //
98  // In the first case, the action for declaring A<T>::B receives
99  // both template parameter lists. In the second case, the action for
100  // defining A<T>::B receives just the inner template parameter list
101  // (and retrieves the outer template parameter list from its
102  // context).
103  bool isSpecialization = true;
104  bool LastParamListWasEmpty = false;
105  TemplateParameterLists ParamLists;
106  TemplateParameterDepthCounter Depth(TemplateParameterDepth);
107  do {
108    // Consume the 'export', if any.
109    SourceLocation ExportLoc;
110    if (Tok.is(tok::kw_export)) {
111      ExportLoc = ConsumeToken();
112    }
113
114    // Consume the 'template', which should be here.
115    SourceLocation TemplateLoc;
116    if (Tok.is(tok::kw_template)) {
117      TemplateLoc = ConsumeToken();
118    } else {
119      Diag(Tok.getLocation(), diag::err_expected_template);
120      return DeclPtrTy();
121    }
122
123    // Parse the '<' template-parameter-list '>'
124    SourceLocation LAngleLoc, RAngleLoc;
125    TemplateParameterList TemplateParams;
126    if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
127                                RAngleLoc)) {
128      // Skip until the semi-colon or a }.
129      SkipUntil(tok::r_brace, true, true);
130      if (Tok.is(tok::semi))
131        ConsumeToken();
132      return DeclPtrTy();
133    }
134
135    ParamLists.push_back(
136      Actions.ActOnTemplateParameterList(Depth, ExportLoc,
137                                         TemplateLoc, LAngleLoc,
138                                         TemplateParams.data(),
139                                         TemplateParams.size(), RAngleLoc));
140
141    if (!TemplateParams.empty()) {
142      isSpecialization = false;
143      ++Depth;
144    } else {
145      LastParamListWasEmpty = true;
146    }
147  } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
148
149  // Parse the actual template declaration.
150  return ParseSingleDeclarationAfterTemplate(Context,
151                                             ParsedTemplateInfo(&ParamLists,
152                                                             isSpecialization,
153                                                         LastParamListWasEmpty),
154                                             DeclEnd, AS);
155}
156
157/// \brief Parse a single declaration that declares a template,
158/// template specialization, or explicit instantiation of a template.
159///
160/// \param TemplateParams if non-NULL, the template parameter lists
161/// that preceded this declaration. In this case, the declaration is a
162/// template declaration, out-of-line definition of a template, or an
163/// explicit template specialization. When NULL, the declaration is an
164/// explicit template instantiation.
165///
166/// \param TemplateLoc when TemplateParams is NULL, the location of
167/// the 'template' keyword that indicates that we have an explicit
168/// template instantiation.
169///
170/// \param DeclEnd will receive the source location of the last token
171/// within this declaration.
172///
173/// \param AS the access specifier associated with this
174/// declaration. Will be AS_none for namespace-scope declarations.
175///
176/// \returns the new declaration.
177Parser::DeclPtrTy
178Parser::ParseSingleDeclarationAfterTemplate(
179                                       unsigned Context,
180                                       const ParsedTemplateInfo &TemplateInfo,
181                                       SourceLocation &DeclEnd,
182                                       AccessSpecifier AS) {
183  assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
184         "Template information required");
185
186  if (Context == Declarator::MemberContext) {
187    // We are parsing a member template.
188    ParseCXXClassMemberDeclaration(AS, TemplateInfo);
189    return DeclPtrTy::make((void*)0);
190  }
191
192  // Parse the declaration specifiers.
193  ParsingDeclSpec DS(*this);
194
195  if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
196    DS.AddAttributes(ParseCXX0XAttributes().AttrList);
197
198  ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
199
200  if (Tok.is(tok::semi)) {
201    DeclEnd = ConsumeToken();
202    DeclPtrTy Decl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
203    DS.complete(Decl);
204    return Decl;
205  }
206
207  // Parse the declarator.
208  ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
209  ParseDeclarator(DeclaratorInfo);
210  // Error parsing the declarator?
211  if (!DeclaratorInfo.hasName()) {
212    // If so, skip until the semi-colon or a }.
213    SkipUntil(tok::r_brace, true, true);
214    if (Tok.is(tok::semi))
215      ConsumeToken();
216    return DeclPtrTy();
217  }
218
219  // If we have a declaration or declarator list, handle it.
220  if (isDeclarationAfterDeclarator()) {
221    // Parse this declaration.
222    DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
223                                                         TemplateInfo);
224
225    if (Tok.is(tok::comma)) {
226      Diag(Tok, diag::err_multiple_template_declarators)
227        << (int)TemplateInfo.Kind;
228      SkipUntil(tok::semi, true, false);
229      return ThisDecl;
230    }
231
232    // Eat the semi colon after the declaration.
233    ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
234    DS.complete(ThisDecl);
235    return ThisDecl;
236  }
237
238  if (DeclaratorInfo.isFunctionDeclarator() &&
239      isStartOfFunctionDefinition()) {
240    if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
241      Diag(Tok, diag::err_function_declared_typedef);
242
243      if (Tok.is(tok::l_brace)) {
244        // This recovery skips the entire function body. It would be nice
245        // to simply call ParseFunctionDefinition() below, however Sema
246        // assumes the declarator represents a function, not a typedef.
247        ConsumeBrace();
248        SkipUntil(tok::r_brace, true);
249      } else {
250        SkipUntil(tok::semi);
251      }
252      return DeclPtrTy();
253    }
254    return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
255  }
256
257  if (DeclaratorInfo.isFunctionDeclarator())
258    Diag(Tok, diag::err_expected_fn_body);
259  else
260    Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
261  SkipUntil(tok::semi);
262  return DeclPtrTy();
263}
264
265/// ParseTemplateParameters - Parses a template-parameter-list enclosed in
266/// angle brackets. Depth is the depth of this template-parameter-list, which
267/// is the number of template headers directly enclosing this template header.
268/// TemplateParams is the current list of template parameters we're building.
269/// The template parameter we parse will be added to this list. LAngleLoc and
270/// RAngleLoc will receive the positions of the '<' and '>', respectively,
271/// that enclose this template parameter list.
272///
273/// \returns true if an error occurred, false otherwise.
274bool Parser::ParseTemplateParameters(unsigned Depth,
275                                     TemplateParameterList &TemplateParams,
276                                     SourceLocation &LAngleLoc,
277                                     SourceLocation &RAngleLoc) {
278  // Get the template parameter list.
279  if (!Tok.is(tok::less)) {
280    Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
281    return true;
282  }
283  LAngleLoc = ConsumeToken();
284
285  // Try to parse the template parameter list.
286  if (Tok.is(tok::greater))
287    RAngleLoc = ConsumeToken();
288  else if (ParseTemplateParameterList(Depth, TemplateParams)) {
289    if (!Tok.is(tok::greater)) {
290      Diag(Tok.getLocation(), diag::err_expected_greater);
291      return true;
292    }
293    RAngleLoc = ConsumeToken();
294  }
295  return false;
296}
297
298/// ParseTemplateParameterList - Parse a template parameter list. If
299/// the parsing fails badly (i.e., closing bracket was left out), this
300/// will try to put the token stream in a reasonable position (closing
301/// a statement, etc.) and return false.
302///
303///       template-parameter-list:    [C++ temp]
304///         template-parameter
305///         template-parameter-list ',' template-parameter
306bool
307Parser::ParseTemplateParameterList(unsigned Depth,
308                                   TemplateParameterList &TemplateParams) {
309  while (1) {
310    if (DeclPtrTy TmpParam
311          = ParseTemplateParameter(Depth, TemplateParams.size())) {
312      TemplateParams.push_back(TmpParam);
313    } else {
314      // If we failed to parse a template parameter, skip until we find
315      // a comma or closing brace.
316      SkipUntil(tok::comma, tok::greater, true, true);
317    }
318
319    // Did we find a comma or the end of the template parmeter list?
320    if (Tok.is(tok::comma)) {
321      ConsumeToken();
322    } else if (Tok.is(tok::greater)) {
323      // Don't consume this... that's done by template parser.
324      break;
325    } else {
326      // Somebody probably forgot to close the template. Skip ahead and
327      // try to get out of the expression. This error is currently
328      // subsumed by whatever goes on in ParseTemplateParameter.
329      // TODO: This could match >>, and it would be nice to avoid those
330      // silly errors with template <vec<T>>.
331      // Diag(Tok.getLocation(), diag::err_expected_comma_greater);
332      SkipUntil(tok::greater, true, true);
333      return false;
334    }
335  }
336  return true;
337}
338
339/// \brief Determine whether the parser is at the start of a template
340/// type parameter.
341bool Parser::isStartOfTemplateTypeParameter() {
342  if (Tok.is(tok::kw_class))
343    return true;
344
345  if (Tok.isNot(tok::kw_typename))
346    return false;
347
348  // C++ [temp.param]p2:
349  //   There is no semantic difference between class and typename in a
350  //   template-parameter. typename followed by an unqualified-id
351  //   names a template type parameter. typename followed by a
352  //   qualified-id denotes the type in a non-type
353  //   parameter-declaration.
354  Token Next = NextToken();
355
356  // If we have an identifier, skip over it.
357  if (Next.getKind() == tok::identifier)
358    Next = GetLookAheadToken(2);
359
360  switch (Next.getKind()) {
361  case tok::equal:
362  case tok::comma:
363  case tok::greater:
364  case tok::greatergreater:
365  case tok::ellipsis:
366    return true;
367
368  default:
369    return false;
370  }
371}
372
373/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
374///
375///       template-parameter: [C++ temp.param]
376///         type-parameter
377///         parameter-declaration
378///
379///       type-parameter: (see below)
380///         'class' ...[opt][C++0x] identifier[opt]
381///         'class' identifier[opt] '=' type-id
382///         'typename' ...[opt][C++0x] identifier[opt]
383///         'typename' identifier[opt] '=' type-id
384///         'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt]
385///         'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
386Parser::DeclPtrTy
387Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
388  if (isStartOfTemplateTypeParameter())
389    return ParseTypeParameter(Depth, Position);
390
391  if (Tok.is(tok::kw_template))
392    return ParseTemplateTemplateParameter(Depth, Position);
393
394  // If it's none of the above, then it must be a parameter declaration.
395  // NOTE: This will pick up errors in the closure of the template parameter
396  // list (e.g., template < ; Check here to implement >> style closures.
397  return ParseNonTypeTemplateParameter(Depth, Position);
398}
399
400/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
401/// Other kinds of template parameters are parsed in
402/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
403///
404///       type-parameter:     [C++ temp.param]
405///         'class' ...[opt][C++0x] identifier[opt]
406///         'class' identifier[opt] '=' type-id
407///         'typename' ...[opt][C++0x] identifier[opt]
408///         'typename' identifier[opt] '=' type-id
409Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){
410  assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
411         "A type-parameter starts with 'class' or 'typename'");
412
413  // Consume the 'class' or 'typename' keyword.
414  bool TypenameKeyword = Tok.is(tok::kw_typename);
415  SourceLocation KeyLoc = ConsumeToken();
416
417  // Grab the ellipsis (if given).
418  bool Ellipsis = false;
419  SourceLocation EllipsisLoc;
420  if (Tok.is(tok::ellipsis)) {
421    Ellipsis = true;
422    EllipsisLoc = ConsumeToken();
423
424    if (!getLang().CPlusPlus0x)
425      Diag(EllipsisLoc, diag::err_variadic_templates);
426  }
427
428  // Grab the template parameter name (if given)
429  SourceLocation NameLoc;
430  IdentifierInfo* ParamName = 0;
431  if (Tok.is(tok::identifier)) {
432    ParamName = Tok.getIdentifierInfo();
433    NameLoc = ConsumeToken();
434  } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
435            Tok.is(tok::greater)) {
436    // Unnamed template parameter. Don't have to do anything here, just
437    // don't consume this token.
438  } else {
439    Diag(Tok.getLocation(), diag::err_expected_ident);
440    return DeclPtrTy();
441  }
442
443  DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword,
444                                                   Ellipsis, EllipsisLoc,
445                                                   KeyLoc, ParamName, NameLoc,
446                                                   Depth, Position);
447
448  // Grab a default type id (if given).
449  if (Tok.is(tok::equal)) {
450    SourceLocation EqualLoc = ConsumeToken();
451    SourceLocation DefaultLoc = Tok.getLocation();
452    TypeResult DefaultType = ParseTypeName();
453    if (!DefaultType.isInvalid())
454      Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc,
455                                        DefaultType.get());
456  }
457
458  return TypeParam;
459}
460
461/// ParseTemplateTemplateParameter - Handle the parsing of template
462/// template parameters.
463///
464///       type-parameter:    [C++ temp.param]
465///         'template' '<' template-parameter-list '>' 'class' identifier[opt]
466///         'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression
467Parser::DeclPtrTy
468Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
469  assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
470
471  // Handle the template <...> part.
472  SourceLocation TemplateLoc = ConsumeToken();
473  TemplateParameterList TemplateParams;
474  SourceLocation LAngleLoc, RAngleLoc;
475  {
476    ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
477    if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
478                               RAngleLoc)) {
479      return DeclPtrTy();
480    }
481  }
482
483  // Generate a meaningful error if the user forgot to put class before the
484  // identifier, comma, or greater.
485  if (!Tok.is(tok::kw_class)) {
486    Diag(Tok.getLocation(), diag::err_expected_class_before)
487      << PP.getSpelling(Tok);
488    return DeclPtrTy();
489  }
490  SourceLocation ClassLoc = ConsumeToken();
491
492  // Get the identifier, if given.
493  SourceLocation NameLoc;
494  IdentifierInfo* ParamName = 0;
495  if (Tok.is(tok::identifier)) {
496    ParamName = Tok.getIdentifierInfo();
497    NameLoc = ConsumeToken();
498  } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
499    // Unnamed template parameter. Don't have to do anything here, just
500    // don't consume this token.
501  } else {
502    Diag(Tok.getLocation(), diag::err_expected_ident);
503    return DeclPtrTy();
504  }
505
506  TemplateParamsTy *ParamList =
507    Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
508                                       TemplateLoc, LAngleLoc,
509                                       &TemplateParams[0],
510                                       TemplateParams.size(),
511                                       RAngleLoc);
512
513  Parser::DeclPtrTy Param
514    = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc,
515                                             ParamList, ParamName,
516                                             NameLoc, Depth, Position);
517
518  // Get the a default value, if given.
519  if (Tok.is(tok::equal)) {
520    SourceLocation EqualLoc = ConsumeToken();
521    ParsedTemplateArgument Default = ParseTemplateTemplateArgument();
522    if (Default.isInvalid()) {
523      Diag(Tok.getLocation(),
524           diag::err_default_template_template_parameter_not_template);
525      static tok::TokenKind EndToks[] = {
526        tok::comma, tok::greater, tok::greatergreater
527      };
528      SkipUntil(EndToks, 3, true, true);
529      return Param;
530    } else if (Param)
531      Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, Default);
532  }
533
534  return Param;
535}
536
537/// ParseNonTypeTemplateParameter - Handle the parsing of non-type
538/// template parameters (e.g., in "template<int Size> class array;").
539///
540///       template-parameter:
541///         ...
542///         parameter-declaration
543///
544/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(),
545/// but that didn't work out to well. Instead, this tries to recrate the basic
546/// parsing of parameter declarations, but tries to constrain it for template
547/// parameters.
548/// FIXME: We need to make a ParseParameterDeclaration that works for
549/// non-type template parameters and normal function parameters.
550Parser::DeclPtrTy
551Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
552  SourceLocation StartLoc = Tok.getLocation();
553
554  // Parse the declaration-specifiers (i.e., the type).
555  // FIXME: The type should probably be restricted in some way... Not all
556  // declarators (parts of declarators?) are accepted for parameters.
557  DeclSpec DS;
558  ParseDeclarationSpecifiers(DS);
559
560  // Parse this as a typename.
561  Declarator ParamDecl(DS, Declarator::TemplateParamContext);
562  ParseDeclarator(ParamDecl);
563  if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) {
564    // This probably shouldn't happen - and it's more of a Sema thing, but
565    // basically we didn't parse the type name because we couldn't associate
566    // it with an AST node. we should just skip to the comma or greater.
567    // TODO: This is currently a placeholder for some kind of Sema Error.
568    Diag(Tok.getLocation(), diag::err_parse_error);
569    SkipUntil(tok::comma, tok::greater, true, true);
570    return DeclPtrTy();
571  }
572
573  // Create the parameter.
574  DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl,
575                                                          Depth, Position);
576
577  // If there is a default value, parse it.
578  if (Tok.is(tok::equal)) {
579    SourceLocation EqualLoc = ConsumeToken();
580
581    // C++ [temp.param]p15:
582    //   When parsing a default template-argument for a non-type
583    //   template-parameter, the first non-nested > is taken as the
584    //   end of the template-parameter-list rather than a greater-than
585    //   operator.
586    GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
587
588    OwningExprResult DefaultArg = ParseAssignmentExpression();
589    if (DefaultArg.isInvalid())
590      SkipUntil(tok::comma, tok::greater, true, true);
591    else if (Param)
592      Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc,
593                                                   move(DefaultArg));
594  }
595
596  return Param;
597}
598
599/// \brief Parses a template-id that after the template name has
600/// already been parsed.
601///
602/// This routine takes care of parsing the enclosed template argument
603/// list ('<' template-parameter-list [opt] '>') and placing the
604/// results into a form that can be transferred to semantic analysis.
605///
606/// \param Template the template declaration produced by isTemplateName
607///
608/// \param TemplateNameLoc the source location of the template name
609///
610/// \param SS if non-NULL, the nested-name-specifier preceding the
611/// template name.
612///
613/// \param ConsumeLastToken if true, then we will consume the last
614/// token that forms the template-id. Otherwise, we will leave the
615/// last token in the stream (e.g., so that it can be replaced with an
616/// annotation token).
617bool
618Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
619                                         SourceLocation TemplateNameLoc,
620                                         const CXXScopeSpec *SS,
621                                         bool ConsumeLastToken,
622                                         SourceLocation &LAngleLoc,
623                                         TemplateArgList &TemplateArgs,
624                                         SourceLocation &RAngleLoc) {
625  assert(Tok.is(tok::less) && "Must have already parsed the template-name");
626
627  // Consume the '<'.
628  LAngleLoc = ConsumeToken();
629
630  // Parse the optional template-argument-list.
631  bool Invalid = false;
632  {
633    GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
634    if (Tok.isNot(tok::greater))
635      Invalid = ParseTemplateArgumentList(TemplateArgs);
636
637    if (Invalid) {
638      // Try to find the closing '>'.
639      SkipUntil(tok::greater, true, !ConsumeLastToken);
640
641      return true;
642    }
643  }
644
645  if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
646    return true;
647
648  // Determine the location of the '>' or '>>'. Only consume this
649  // token if the caller asked us to.
650  RAngleLoc = Tok.getLocation();
651
652  if (Tok.is(tok::greatergreater)) {
653    if (!getLang().CPlusPlus0x) {
654      const char *ReplaceStr = "> >";
655      if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
656        ReplaceStr = "> > ";
657
658      Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
659        << CodeModificationHint::CreateReplacement(
660                                 SourceRange(Tok.getLocation()), ReplaceStr);
661    }
662
663    Tok.setKind(tok::greater);
664    if (!ConsumeLastToken) {
665      // Since we're not supposed to consume the '>>' token, we need
666      // to insert a second '>' token after the first.
667      PP.EnterToken(Tok);
668    }
669  } else if (ConsumeLastToken)
670    ConsumeToken();
671
672  return false;
673}
674
675/// \brief Replace the tokens that form a simple-template-id with an
676/// annotation token containing the complete template-id.
677///
678/// The first token in the stream must be the name of a template that
679/// is followed by a '<'. This routine will parse the complete
680/// simple-template-id and replace the tokens with a single annotation
681/// token with one of two different kinds: if the template-id names a
682/// type (and \p AllowTypeAnnotation is true), the annotation token is
683/// a type annotation that includes the optional nested-name-specifier
684/// (\p SS). Otherwise, the annotation token is a template-id
685/// annotation that does not include the optional
686/// nested-name-specifier.
687///
688/// \param Template  the declaration of the template named by the first
689/// token (an identifier), as returned from \c Action::isTemplateName().
690///
691/// \param TemplateNameKind the kind of template that \p Template
692/// refers to, as returned from \c Action::isTemplateName().
693///
694/// \param SS if non-NULL, the nested-name-specifier that precedes
695/// this template name.
696///
697/// \param TemplateKWLoc if valid, specifies that this template-id
698/// annotation was preceded by the 'template' keyword and gives the
699/// location of that keyword. If invalid (the default), then this
700/// template-id was not preceded by a 'template' keyword.
701///
702/// \param AllowTypeAnnotation if true (the default), then a
703/// simple-template-id that refers to a class template, template
704/// template parameter, or other template that produces a type will be
705/// replaced with a type annotation token. Otherwise, the
706/// simple-template-id is always replaced with a template-id
707/// annotation token.
708///
709/// If an unrecoverable parse error occurs and no annotation token can be
710/// formed, this function returns true.
711///
712bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
713                                     const CXXScopeSpec *SS,
714                                     UnqualifiedId &TemplateName,
715                                     SourceLocation TemplateKWLoc,
716                                     bool AllowTypeAnnotation) {
717  assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
718  assert(Template && Tok.is(tok::less) &&
719         "Parser isn't at the beginning of a template-id");
720
721  // Consume the template-name.
722  SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
723
724  // Parse the enclosed template argument list.
725  SourceLocation LAngleLoc, RAngleLoc;
726  TemplateArgList TemplateArgs;
727  bool Invalid = ParseTemplateIdAfterTemplateName(Template,
728                                                  TemplateNameLoc,
729                                                  SS, false, LAngleLoc,
730                                                  TemplateArgs,
731                                                  RAngleLoc);
732
733  if (Invalid) {
734    // If we failed to parse the template ID but skipped ahead to a >, we're not
735    // going to be able to form a token annotation.  Eat the '>' if present.
736    if (Tok.is(tok::greater))
737      ConsumeToken();
738    return true;
739  }
740
741  ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
742                                     TemplateArgs.size());
743
744  // Build the annotation token.
745  if (TNK == TNK_Type_template && AllowTypeAnnotation) {
746    Action::TypeResult Type
747      = Actions.ActOnTemplateIdType(Template, TemplateNameLoc,
748                                    LAngleLoc, TemplateArgsPtr,
749                                    RAngleLoc);
750    if (Type.isInvalid()) {
751      // If we failed to parse the template ID but skipped ahead to a >, we're not
752      // going to be able to form a token annotation.  Eat the '>' if present.
753      if (Tok.is(tok::greater))
754        ConsumeToken();
755      return true;
756    }
757
758    Tok.setKind(tok::annot_typename);
759    Tok.setAnnotationValue(Type.get());
760    if (SS && SS->isNotEmpty())
761      Tok.setLocation(SS->getBeginLoc());
762    else if (TemplateKWLoc.isValid())
763      Tok.setLocation(TemplateKWLoc);
764    else
765      Tok.setLocation(TemplateNameLoc);
766  } else {
767    // Build a template-id annotation token that can be processed
768    // later.
769    Tok.setKind(tok::annot_template_id);
770    TemplateIdAnnotation *TemplateId
771      = TemplateIdAnnotation::Allocate(TemplateArgs.size());
772    TemplateId->TemplateNameLoc = TemplateNameLoc;
773    if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
774      TemplateId->Name = TemplateName.Identifier;
775      TemplateId->Operator = OO_None;
776    } else {
777      TemplateId->Name = 0;
778      TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
779    }
780    TemplateId->Template = Template.getAs<void*>();
781    TemplateId->Kind = TNK;
782    TemplateId->LAngleLoc = LAngleLoc;
783    TemplateId->RAngleLoc = RAngleLoc;
784    ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
785    for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
786      Args[Arg] = TemplateArgs[Arg];
787    Tok.setAnnotationValue(TemplateId);
788    if (TemplateKWLoc.isValid())
789      Tok.setLocation(TemplateKWLoc);
790    else
791      Tok.setLocation(TemplateNameLoc);
792
793    TemplateArgsPtr.release();
794  }
795
796  // Common fields for the annotation token
797  Tok.setAnnotationEndLoc(RAngleLoc);
798
799  // In case the tokens were cached, have Preprocessor replace them with the
800  // annotation token.
801  PP.AnnotateCachedTokens(Tok);
802  return false;
803}
804
805/// \brief Replaces a template-id annotation token with a type
806/// annotation token.
807///
808/// If there was a failure when forming the type from the template-id,
809/// a type annotation token will still be created, but will have a
810/// NULL type pointer to signify an error.
811void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) {
812  assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
813
814  TemplateIdAnnotation *TemplateId
815    = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
816  assert((TemplateId->Kind == TNK_Type_template ||
817          TemplateId->Kind == TNK_Dependent_template_name) &&
818         "Only works for type and dependent templates");
819
820  ASTTemplateArgsPtr TemplateArgsPtr(Actions,
821                                     TemplateId->getTemplateArgs(),
822                                     TemplateId->NumArgs);
823
824  Action::TypeResult Type
825    = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
826                                  TemplateId->TemplateNameLoc,
827                                  TemplateId->LAngleLoc,
828                                  TemplateArgsPtr,
829                                  TemplateId->RAngleLoc);
830  // Create the new "type" annotation token.
831  Tok.setKind(tok::annot_typename);
832  Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get());
833  if (SS && SS->isNotEmpty()) // it was a C++ qualified type name.
834    Tok.setLocation(SS->getBeginLoc());
835  Tok.setAnnotationEndLoc(TemplateId->TemplateNameLoc);
836
837  // Replace the template-id annotation token, and possible the scope-specifier
838  // that precedes it, with the typename annotation token.
839  PP.AnnotateCachedTokens(Tok);
840  TemplateId->Destroy();
841}
842
843/// \brief Determine whether the given token can end a template argument.
844static bool isEndOfTemplateArgument(Token Tok) {
845  return Tok.is(tok::comma) || Tok.is(tok::greater) ||
846         Tok.is(tok::greatergreater);
847}
848
849/// \brief Parse a C++ template template argument.
850ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
851  if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
852      !Tok.is(tok::annot_cxxscope))
853    return ParsedTemplateArgument();
854
855  // C++0x [temp.arg.template]p1:
856  //   A template-argument for a template template-parameter shall be the name
857  //   of a class template or a template alias, expressed as id-expression.
858  //
859  // We parse an id-expression that refers to a class template or template
860  // alias. The grammar we parse is:
861  //
862  //   nested-name-specifier[opt] template[opt] identifier
863  //
864  // followed by a token that terminates a template argument, such as ',',
865  // '>', or (in some cases) '>>'.
866  CXXScopeSpec SS; // nested-name-specifier, if present
867  ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
868                                 /*EnteringContext=*/false);
869
870  if (SS.isSet() && Tok.is(tok::kw_template)) {
871    // Parse the optional 'template' keyword following the
872    // nested-name-specifier.
873    SourceLocation TemplateLoc = ConsumeToken();
874
875    if (Tok.is(tok::identifier)) {
876      // We appear to have a dependent template name.
877      UnqualifiedId Name;
878      Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
879      ConsumeToken(); // the identifier
880
881      // If the next token signals the end of a template argument,
882      // then we have a dependent template name that could be a template
883      // template argument.
884      if (isEndOfTemplateArgument(Tok)) {
885        TemplateTy Template
886        = Actions.ActOnDependentTemplateName(TemplateLoc, SS, Name,
887                                             /*ObjectType=*/0,
888                                             /*EnteringContext=*/false);
889        if (Template.get())
890          return ParsedTemplateArgument(SS, Template, Name.StartLocation);
891      }
892    }
893  } else if (Tok.is(tok::identifier)) {
894    // We may have a (non-dependent) template name.
895    TemplateTy Template;
896    UnqualifiedId Name;
897    Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
898    ConsumeToken(); // the identifier
899
900    if (isEndOfTemplateArgument(Tok)) {
901      TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, Name,
902                                                    /*ObjectType=*/0,
903                                                    /*EnteringContext=*/false,
904                                                    Template);
905      if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
906        // We have an id-expression that refers to a class template or
907        // (C++0x) template alias.
908        return ParsedTemplateArgument(SS, Template, Name.StartLocation);
909      }
910    }
911  }
912
913  // We don't have a template template argument.
914  return ParsedTemplateArgument();
915}
916
917/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
918///
919///       template-argument: [C++ 14.2]
920///         constant-expression
921///         type-id
922///         id-expression
923ParsedTemplateArgument Parser::ParseTemplateArgument() {
924  // C++ [temp.arg]p2:
925  //   In a template-argument, an ambiguity between a type-id and an
926  //   expression is resolved to a type-id, regardless of the form of
927  //   the corresponding template-parameter.
928  //
929  // Therefore, we initially try to parse a type-id.
930  if (isCXXTypeId(TypeIdAsTemplateArgument)) {
931    SourceLocation Loc = Tok.getLocation();
932    TypeResult TypeArg = ParseTypeName();
933    if (TypeArg.isInvalid())
934      return ParsedTemplateArgument();
935
936    return ParsedTemplateArgument(ParsedTemplateArgument::Type, TypeArg.get(),
937                                  Loc);
938  }
939
940  // Try to parse a template template argument.
941  {
942    TentativeParsingAction TPA(*this);
943
944    ParsedTemplateArgument TemplateTemplateArgument
945      = ParseTemplateTemplateArgument();
946    if (!TemplateTemplateArgument.isInvalid()) {
947      TPA.Commit();
948      return TemplateTemplateArgument;
949    }
950
951    // Revert this tentative parse to parse a non-type template argument.
952    TPA.Revert();
953  }
954
955  // Parse a non-type template argument.
956  SourceLocation Loc = Tok.getLocation();
957  OwningExprResult ExprArg = ParseConstantExpression();
958  if (ExprArg.isInvalid() || !ExprArg.get())
959    return ParsedTemplateArgument();
960
961  return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
962                                ExprArg.release(), Loc);
963}
964
965/// ParseTemplateArgumentList - Parse a C++ template-argument-list
966/// (C++ [temp.names]). Returns true if there was an error.
967///
968///       template-argument-list: [C++ 14.2]
969///         template-argument
970///         template-argument-list ',' template-argument
971bool
972Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
973  while (true) {
974    ParsedTemplateArgument Arg = ParseTemplateArgument();
975    if (Arg.isInvalid()) {
976      SkipUntil(tok::comma, tok::greater, true, true);
977      return true;
978    }
979
980    // Save this template argument.
981    TemplateArgs.push_back(Arg);
982
983    // If the next token is a comma, consume it and keep reading
984    // arguments.
985    if (Tok.isNot(tok::comma)) break;
986
987    // Consume the comma.
988    ConsumeToken();
989  }
990
991  return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater);
992}
993
994/// \brief Parse a C++ explicit template instantiation
995/// (C++ [temp.explicit]).
996///
997///       explicit-instantiation:
998///         'extern' [opt] 'template' declaration
999///
1000/// Note that the 'extern' is a GNU extension and C++0x feature.
1001Parser::DeclPtrTy
1002Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1003                                   SourceLocation TemplateLoc,
1004                                   SourceLocation &DeclEnd) {
1005  return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
1006                                             ParsedTemplateInfo(ExternLoc,
1007                                                                TemplateLoc),
1008                                             DeclEnd, AS_none);
1009}
1010