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