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