1 //===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements parsing of C++ templates.
12 //===----------------------------------------------------------------------===//
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"
20 #include "clang/AST/DeclTemplate.h"
21 #include "clang/AST/ASTConsumer.h"
22 using namespace clang;
24 /// \brief Parse a template declaration, explicit instantiation, or
25 /// explicit specialization.
27 Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
28 SourceLocation &DeclEnd,
30 AttributeList *AccessAttrs) {
31 ObjCDeclContextSwitch ObjCDC(*this);
33 if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
34 return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(),
37 return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS,
41 /// \brief RAII class that manages the template parameter depth.
43 class TemplateParameterDepthCounter {
48 explicit TemplateParameterDepthCounter(unsigned &Depth)
49 : Depth(Depth), AddedLevels(0) { }
51 ~TemplateParameterDepthCounter() {
60 operator unsigned() const { return Depth; }
64 /// \brief Parse a template declaration or an explicit specialization.
66 /// Template declarations include one or more template parameter lists
67 /// and either the function or class template declaration. Explicit
68 /// specializations contain one or more 'template < >' prefixes
69 /// followed by a (possibly templated) declaration. Since the
70 /// syntactic form of both features is nearly identical, we parse all
71 /// of the template headers together and let semantic analysis sort
72 /// the declarations from the explicit specializations.
74 /// template-declaration: [C++ temp]
75 /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
77 /// explicit-specialization: [ C++ temp.expl.spec]
78 /// 'template' '<' '>' declaration
80 Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
81 SourceLocation &DeclEnd,
83 AttributeList *AccessAttrs) {
84 assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
85 "Token does not start a template declaration.");
87 // Enter template-parameter scope.
88 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
90 // Tell the action that names should be checked in the context of
91 // the declaration to come.
92 ParsingDeclRAIIObject ParsingTemplateParams(*this);
94 // Parse multiple levels of template headers within this template
95 // parameter scope, e.g.,
97 // template<typename T>
98 // template<typename U>
99 // class A<T>::B { ... };
101 // We parse multiple levels non-recursively so that we can build a
102 // single data structure containing all of the template parameter
103 // lists to easily differentiate between the case above and:
105 // template<typename T>
107 // template<typename U> class B;
110 // In the first case, the action for declaring A<T>::B receives
111 // both template parameter lists. In the second case, the action for
112 // defining A<T>::B receives just the inner template parameter list
113 // (and retrieves the outer template parameter list from its
115 bool isSpecialization = true;
116 bool LastParamListWasEmpty = false;
117 TemplateParameterLists ParamLists;
118 TemplateParameterDepthCounter Depth(TemplateParameterDepth);
120 // Consume the 'export', if any.
121 SourceLocation ExportLoc;
122 if (Tok.is(tok::kw_export)) {
123 ExportLoc = ConsumeToken();
126 // Consume the 'template', which should be here.
127 SourceLocation TemplateLoc;
128 if (Tok.is(tok::kw_template)) {
129 TemplateLoc = ConsumeToken();
131 Diag(Tok.getLocation(), diag::err_expected_template);
135 // Parse the '<' template-parameter-list '>'
136 SourceLocation LAngleLoc, RAngleLoc;
137 SmallVector<Decl*, 4> TemplateParams;
138 if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc,
140 // Skip until the semi-colon or a }.
141 SkipUntil(tok::r_brace, true, true);
142 if (Tok.is(tok::semi))
147 ParamLists.push_back(
148 Actions.ActOnTemplateParameterList(Depth, ExportLoc,
149 TemplateLoc, LAngleLoc,
150 TemplateParams.data(),
151 TemplateParams.size(), RAngleLoc));
153 if (!TemplateParams.empty()) {
154 isSpecialization = false;
157 LastParamListWasEmpty = true;
159 } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
161 // Parse the actual template declaration.
162 return ParseSingleDeclarationAfterTemplate(Context,
163 ParsedTemplateInfo(&ParamLists,
165 LastParamListWasEmpty),
166 ParsingTemplateParams,
167 DeclEnd, AS, AccessAttrs);
170 /// \brief Parse a single declaration that declares a template,
171 /// template specialization, or explicit instantiation of a template.
173 /// \param TemplateParams if non-NULL, the template parameter lists
174 /// that preceded this declaration. In this case, the declaration is a
175 /// template declaration, out-of-line definition of a template, or an
176 /// explicit template specialization. When NULL, the declaration is an
177 /// explicit template instantiation.
179 /// \param TemplateLoc when TemplateParams is NULL, the location of
180 /// the 'template' keyword that indicates that we have an explicit
181 /// template instantiation.
183 /// \param DeclEnd will receive the source location of the last token
184 /// within this declaration.
186 /// \param AS the access specifier associated with this
187 /// declaration. Will be AS_none for namespace-scope declarations.
189 /// \returns the new declaration.
191 Parser::ParseSingleDeclarationAfterTemplate(
193 const ParsedTemplateInfo &TemplateInfo,
194 ParsingDeclRAIIObject &DiagsFromTParams,
195 SourceLocation &DeclEnd,
197 AttributeList *AccessAttrs) {
198 assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
199 "Template information required");
201 if (Context == Declarator::MemberContext) {
202 // We are parsing a member template.
203 ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
208 ParsedAttributesWithRange prefixAttrs(AttrFactory);
209 MaybeParseCXX0XAttributes(prefixAttrs);
211 if (Tok.is(tok::kw_using))
212 return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
215 // Parse the declaration specifiers, stealing the accumulated
216 // diagnostics from the template parameters.
217 ParsingDeclSpec DS(*this, &DiagsFromTParams);
219 DS.takeAttributesFrom(prefixAttrs);
221 ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
222 getDeclSpecContextFromDeclaratorContext(Context));
224 if (Tok.is(tok::semi)) {
225 DeclEnd = ConsumeToken();
226 Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
231 // Parse the declarator.
232 ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
233 ParseDeclarator(DeclaratorInfo);
234 // Error parsing the declarator?
235 if (!DeclaratorInfo.hasName()) {
236 // If so, skip until the semi-colon or a }.
237 SkipUntil(tok::r_brace, true, true);
238 if (Tok.is(tok::semi))
243 // If we have a declaration or declarator list, handle it.
244 if (isDeclarationAfterDeclarator()) {
245 // Parse this declaration.
246 Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
249 if (Tok.is(tok::comma)) {
250 Diag(Tok, diag::err_multiple_template_declarators)
251 << (int)TemplateInfo.Kind;
252 SkipUntil(tok::semi, true, false);
256 // Eat the semi colon after the declaration.
257 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
258 DeclaratorInfo.complete(ThisDecl);
262 if (DeclaratorInfo.isFunctionDeclarator() &&
263 isStartOfFunctionDefinition(DeclaratorInfo)) {
264 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
265 Diag(Tok, diag::err_function_declared_typedef);
267 if (Tok.is(tok::l_brace)) {
268 // This recovery skips the entire function body. It would be nice
269 // to simply call ParseFunctionDefinition() below, however Sema
270 // assumes the declarator represents a function, not a typedef.
272 SkipUntil(tok::r_brace, true);
274 SkipUntil(tok::semi);
278 return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo);
281 if (DeclaratorInfo.isFunctionDeclarator())
282 Diag(Tok, diag::err_expected_fn_body);
284 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
285 SkipUntil(tok::semi);
289 /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
290 /// angle brackets. Depth is the depth of this template-parameter-list, which
291 /// is the number of template headers directly enclosing this template header.
292 /// TemplateParams is the current list of template parameters we're building.
293 /// The template parameter we parse will be added to this list. LAngleLoc and
294 /// RAngleLoc will receive the positions of the '<' and '>', respectively,
295 /// that enclose this template parameter list.
297 /// \returns true if an error occurred, false otherwise.
298 bool Parser::ParseTemplateParameters(unsigned Depth,
299 SmallVectorImpl<Decl*> &TemplateParams,
300 SourceLocation &LAngleLoc,
301 SourceLocation &RAngleLoc) {
302 // Get the template parameter list.
303 if (!Tok.is(tok::less)) {
304 Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
307 LAngleLoc = ConsumeToken();
309 // Try to parse the template parameter list.
310 if (Tok.is(tok::greater))
311 RAngleLoc = ConsumeToken();
312 else if (ParseTemplateParameterList(Depth, TemplateParams)) {
313 if (!Tok.is(tok::greater)) {
314 Diag(Tok.getLocation(), diag::err_expected_greater);
317 RAngleLoc = ConsumeToken();
322 /// ParseTemplateParameterList - Parse a template parameter list. If
323 /// the parsing fails badly (i.e., closing bracket was left out), this
324 /// will try to put the token stream in a reasonable position (closing
325 /// a statement, etc.) and return false.
327 /// template-parameter-list: [C++ temp]
328 /// template-parameter
329 /// template-parameter-list ',' template-parameter
331 Parser::ParseTemplateParameterList(unsigned Depth,
332 SmallVectorImpl<Decl*> &TemplateParams) {
335 = ParseTemplateParameter(Depth, TemplateParams.size())) {
336 TemplateParams.push_back(TmpParam);
338 // If we failed to parse a template parameter, skip until we find
339 // a comma or closing brace.
340 SkipUntil(tok::comma, tok::greater, true, true);
343 // Did we find a comma or the end of the template parmeter list?
344 if (Tok.is(tok::comma)) {
346 } else if (Tok.is(tok::greater)) {
347 // Don't consume this... that's done by template parser.
350 // Somebody probably forgot to close the template. Skip ahead and
351 // try to get out of the expression. This error is currently
352 // subsumed by whatever goes on in ParseTemplateParameter.
353 // TODO: This could match >>, and it would be nice to avoid those
354 // silly errors with template <vec<T>>.
355 Diag(Tok.getLocation(), diag::err_expected_comma_greater);
356 SkipUntil(tok::greater, true, true);
363 /// \brief Determine whether the parser is at the start of a template
365 bool Parser::isStartOfTemplateTypeParameter() {
366 if (Tok.is(tok::kw_class)) {
367 // "class" may be the start of an elaborated-type-specifier or a
368 // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
369 switch (NextToken().getKind()) {
373 case tok::greatergreater:
377 case tok::identifier:
378 // This may be either a type-parameter or an elaborated-type-specifier.
379 // We have to look further.
386 switch (GetLookAheadToken(2).getKind()) {
390 case tok::greatergreater:
398 if (Tok.isNot(tok::kw_typename))
401 // C++ [temp.param]p2:
402 // There is no semantic difference between class and typename in a
403 // template-parameter. typename followed by an unqualified-id
404 // names a template type parameter. typename followed by a
405 // qualified-id denotes the type in a non-type
406 // parameter-declaration.
407 Token Next = NextToken();
409 // If we have an identifier, skip over it.
410 if (Next.getKind() == tok::identifier)
411 Next = GetLookAheadToken(2);
413 switch (Next.getKind()) {
417 case tok::greatergreater:
426 /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
428 /// template-parameter: [C++ temp.param]
430 /// parameter-declaration
432 /// type-parameter: (see below)
433 /// 'class' ...[opt] identifier[opt]
434 /// 'class' identifier[opt] '=' type-id
435 /// 'typename' ...[opt] identifier[opt]
436 /// 'typename' identifier[opt] '=' type-id
437 /// 'template' '<' template-parameter-list '>'
438 /// 'class' ...[opt] identifier[opt]
439 /// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
441 Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
442 if (isStartOfTemplateTypeParameter())
443 return ParseTypeParameter(Depth, Position);
445 if (Tok.is(tok::kw_template))
446 return ParseTemplateTemplateParameter(Depth, Position);
448 // If it's none of the above, then it must be a parameter declaration.
449 // NOTE: This will pick up errors in the closure of the template parameter
450 // list (e.g., template < ; Check here to implement >> style closures.
451 return ParseNonTypeTemplateParameter(Depth, Position);
454 /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
455 /// Other kinds of template parameters are parsed in
456 /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
458 /// type-parameter: [C++ temp.param]
459 /// 'class' ...[opt][C++0x] identifier[opt]
460 /// 'class' identifier[opt] '=' type-id
461 /// 'typename' ...[opt][C++0x] identifier[opt]
462 /// 'typename' identifier[opt] '=' type-id
463 Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
464 assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
465 "A type-parameter starts with 'class' or 'typename'");
467 // Consume the 'class' or 'typename' keyword.
468 bool TypenameKeyword = Tok.is(tok::kw_typename);
469 SourceLocation KeyLoc = ConsumeToken();
471 // Grab the ellipsis (if given).
472 bool Ellipsis = false;
473 SourceLocation EllipsisLoc;
474 if (Tok.is(tok::ellipsis)) {
476 EllipsisLoc = ConsumeToken();
479 getLang().CPlusPlus0x
480 ? diag::warn_cxx98_compat_variadic_templates
481 : diag::ext_variadic_templates);
484 // Grab the template parameter name (if given)
485 SourceLocation NameLoc;
486 IdentifierInfo* ParamName = 0;
487 if (Tok.is(tok::identifier)) {
488 ParamName = Tok.getIdentifierInfo();
489 NameLoc = ConsumeToken();
490 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
491 Tok.is(tok::greater)) {
492 // Unnamed template parameter. Don't have to do anything here, just
493 // don't consume this token.
495 Diag(Tok.getLocation(), diag::err_expected_ident);
499 // Grab a default argument (if available).
500 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
501 // we introduce the type parameter into the local scope.
502 SourceLocation EqualLoc;
503 ParsedType DefaultArg;
504 if (Tok.is(tok::equal)) {
505 EqualLoc = ConsumeToken();
506 DefaultArg = ParseTypeName().get();
509 return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis,
510 EllipsisLoc, KeyLoc, ParamName, NameLoc,
511 Depth, Position, EqualLoc, DefaultArg);
514 /// ParseTemplateTemplateParameter - Handle the parsing of template
515 /// template parameters.
517 /// type-parameter: [C++ temp.param]
518 /// 'template' '<' template-parameter-list '>' 'class'
519 /// ...[opt] identifier[opt]
520 /// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
523 Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
524 assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
526 // Handle the template <...> part.
527 SourceLocation TemplateLoc = ConsumeToken();
528 SmallVector<Decl*,8> TemplateParams;
529 SourceLocation LAngleLoc, RAngleLoc;
531 ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
532 if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
538 // Generate a meaningful error if the user forgot to put class before the
539 // identifier, comma, or greater.
540 if (!Tok.is(tok::kw_class)) {
541 Diag(Tok.getLocation(), diag::err_expected_class_before)
542 << PP.getSpelling(Tok);
547 // Parse the ellipsis, if given.
548 SourceLocation EllipsisLoc;
549 if (Tok.is(tok::ellipsis)) {
550 EllipsisLoc = ConsumeToken();
553 getLang().CPlusPlus0x
554 ? diag::warn_cxx98_compat_variadic_templates
555 : diag::ext_variadic_templates);
558 // Get the identifier, if given.
559 SourceLocation NameLoc;
560 IdentifierInfo* ParamName = 0;
561 if (Tok.is(tok::identifier)) {
562 ParamName = Tok.getIdentifierInfo();
563 NameLoc = ConsumeToken();
564 } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) {
565 // Unnamed template parameter. Don't have to do anything here, just
566 // don't consume this token.
568 Diag(Tok.getLocation(), diag::err_expected_ident);
572 TemplateParameterList *ParamList =
573 Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
574 TemplateLoc, LAngleLoc,
575 TemplateParams.data(),
576 TemplateParams.size(),
579 // Grab a default argument (if available).
580 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
581 // we introduce the template parameter into the local scope.
582 SourceLocation EqualLoc;
583 ParsedTemplateArgument DefaultArg;
584 if (Tok.is(tok::equal)) {
585 EqualLoc = ConsumeToken();
586 DefaultArg = ParseTemplateTemplateArgument();
587 if (DefaultArg.isInvalid()) {
588 Diag(Tok.getLocation(),
589 diag::err_default_template_template_parameter_not_template);
590 static const tok::TokenKind EndToks[] = {
591 tok::comma, tok::greater, tok::greatergreater
593 SkipUntil(EndToks, 3, true, true);
597 return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
598 ParamList, EllipsisLoc,
599 ParamName, NameLoc, Depth,
600 Position, EqualLoc, DefaultArg);
603 /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
604 /// template parameters (e.g., in "template<int Size> class array;").
606 /// template-parameter:
608 /// parameter-declaration
610 Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
611 // Parse the declaration-specifiers (i.e., the type).
612 // FIXME: The type should probably be restricted in some way... Not all
613 // declarators (parts of declarators?) are accepted for parameters.
614 DeclSpec DS(AttrFactory);
615 ParseDeclarationSpecifiers(DS);
617 // Parse this as a typename.
618 Declarator ParamDecl(DS, Declarator::TemplateParamContext);
619 ParseDeclarator(ParamDecl);
620 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
621 // This probably shouldn't happen - and it's more of a Sema thing, but
622 // basically we didn't parse the type name because we couldn't associate
623 // it with an AST node. we should just skip to the comma or greater.
624 // TODO: This is currently a placeholder for some kind of Sema Error.
625 Diag(Tok.getLocation(), diag::err_parse_error);
626 SkipUntil(tok::comma, tok::greater, true, true);
630 // If there is a default value, parse it.
631 // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
632 // we introduce the template parameter into the local scope.
633 SourceLocation EqualLoc;
634 ExprResult DefaultArg;
635 if (Tok.is(tok::equal)) {
636 EqualLoc = ConsumeToken();
638 // C++ [temp.param]p15:
639 // When parsing a default template-argument for a non-type
640 // template-parameter, the first non-nested > is taken as the
641 // end of the template-parameter-list rather than a greater-than
643 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
645 DefaultArg = ParseAssignmentExpression();
646 if (DefaultArg.isInvalid())
647 SkipUntil(tok::comma, tok::greater, true, true);
650 // Create the parameter.
651 return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
652 Depth, Position, EqualLoc,
656 /// \brief Parses a template-id that after the template name has
657 /// already been parsed.
659 /// This routine takes care of parsing the enclosed template argument
660 /// list ('<' template-parameter-list [opt] '>') and placing the
661 /// results into a form that can be transferred to semantic analysis.
663 /// \param Template the template declaration produced by isTemplateName
665 /// \param TemplateNameLoc the source location of the template name
667 /// \param SS if non-NULL, the nested-name-specifier preceding the
670 /// \param ConsumeLastToken if true, then we will consume the last
671 /// token that forms the template-id. Otherwise, we will leave the
672 /// last token in the stream (e.g., so that it can be replaced with an
673 /// annotation token).
675 Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
676 SourceLocation TemplateNameLoc,
677 const CXXScopeSpec &SS,
678 bool ConsumeLastToken,
679 SourceLocation &LAngleLoc,
680 TemplateArgList &TemplateArgs,
681 SourceLocation &RAngleLoc) {
682 assert(Tok.is(tok::less) && "Must have already parsed the template-name");
685 LAngleLoc = ConsumeToken();
687 // Parse the optional template-argument-list.
688 bool Invalid = false;
690 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
691 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
692 Invalid = ParseTemplateArgumentList(TemplateArgs);
695 // Try to find the closing '>'.
696 SkipUntil(tok::greater, true, !ConsumeLastToken);
702 if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) {
703 Diag(Tok.getLocation(), diag::err_expected_greater);
707 // Determine the location of the '>' or '>>'. Only consume this
708 // token if the caller asked us to.
709 RAngleLoc = Tok.getLocation();
711 if (Tok.is(tok::greatergreater)) {
712 if (!getLang().CPlusPlus0x) {
713 const char *ReplaceStr = "> >";
714 if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater))
717 Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space)
718 << FixItHint::CreateReplacement(
719 SourceRange(Tok.getLocation()), ReplaceStr);
722 Tok.setKind(tok::greater);
723 if (!ConsumeLastToken) {
724 // Since we're not supposed to consume the '>>' token, we need
725 // to insert a second '>' token after the first.
728 } else if (ConsumeLastToken)
734 /// \brief Replace the tokens that form a simple-template-id with an
735 /// annotation token containing the complete template-id.
737 /// The first token in the stream must be the name of a template that
738 /// is followed by a '<'. This routine will parse the complete
739 /// simple-template-id and replace the tokens with a single annotation
740 /// token with one of two different kinds: if the template-id names a
741 /// type (and \p AllowTypeAnnotation is true), the annotation token is
742 /// a type annotation that includes the optional nested-name-specifier
743 /// (\p SS). Otherwise, the annotation token is a template-id
744 /// annotation that does not include the optional
745 /// nested-name-specifier.
747 /// \param Template the declaration of the template named by the first
748 /// token (an identifier), as returned from \c Action::isTemplateName().
750 /// \param TemplateNameKind the kind of template that \p Template
751 /// refers to, as returned from \c Action::isTemplateName().
753 /// \param SS if non-NULL, the nested-name-specifier that precedes
754 /// this template name.
756 /// \param TemplateKWLoc if valid, specifies that this template-id
757 /// annotation was preceded by the 'template' keyword and gives the
758 /// location of that keyword. If invalid (the default), then this
759 /// template-id was not preceded by a 'template' keyword.
761 /// \param AllowTypeAnnotation if true (the default), then a
762 /// simple-template-id that refers to a class template, template
763 /// template parameter, or other template that produces a type will be
764 /// replaced with a type annotation token. Otherwise, the
765 /// simple-template-id is always replaced with a template-id
766 /// annotation token.
768 /// If an unrecoverable parse error occurs and no annotation token can be
769 /// formed, this function returns true.
771 bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
773 UnqualifiedId &TemplateName,
774 SourceLocation TemplateKWLoc,
775 bool AllowTypeAnnotation) {
776 assert(getLang().CPlusPlus && "Can only annotate template-ids in C++");
777 assert(Template && Tok.is(tok::less) &&
778 "Parser isn't at the beginning of a template-id");
780 // Consume the template-name.
781 SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
783 // Parse the enclosed template argument list.
784 SourceLocation LAngleLoc, RAngleLoc;
785 TemplateArgList TemplateArgs;
786 bool Invalid = ParseTemplateIdAfterTemplateName(Template,
788 SS, false, LAngleLoc,
793 // If we failed to parse the template ID but skipped ahead to a >, we're not
794 // going to be able to form a token annotation. Eat the '>' if present.
795 if (Tok.is(tok::greater))
800 ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
801 TemplateArgs.size());
803 // Build the annotation token.
804 if (TNK == TNK_Type_template && AllowTypeAnnotation) {
806 = Actions.ActOnTemplateIdType(SS,
807 Template, TemplateNameLoc,
808 LAngleLoc, TemplateArgsPtr,
810 if (Type.isInvalid()) {
811 // If we failed to parse the template ID but skipped ahead to a >, we're not
812 // going to be able to form a token annotation. Eat the '>' if present.
813 if (Tok.is(tok::greater))
818 Tok.setKind(tok::annot_typename);
819 setTypeAnnotation(Tok, Type.get());
821 Tok.setLocation(SS.getBeginLoc());
822 else if (TemplateKWLoc.isValid())
823 Tok.setLocation(TemplateKWLoc);
825 Tok.setLocation(TemplateNameLoc);
827 // Build a template-id annotation token that can be processed
829 Tok.setKind(tok::annot_template_id);
830 TemplateIdAnnotation *TemplateId
831 = TemplateIdAnnotation::Allocate(TemplateArgs.size());
832 TemplateId->TemplateNameLoc = TemplateNameLoc;
833 if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
834 TemplateId->Name = TemplateName.Identifier;
835 TemplateId->Operator = OO_None;
837 TemplateId->Name = 0;
838 TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
841 TemplateId->Template = Template;
842 TemplateId->Kind = TNK;
843 TemplateId->LAngleLoc = LAngleLoc;
844 TemplateId->RAngleLoc = RAngleLoc;
845 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
846 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
847 Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
848 Tok.setAnnotationValue(TemplateId);
849 if (TemplateKWLoc.isValid())
850 Tok.setLocation(TemplateKWLoc);
852 Tok.setLocation(TemplateNameLoc);
854 TemplateArgsPtr.release();
857 // Common fields for the annotation token
858 Tok.setAnnotationEndLoc(RAngleLoc);
860 // In case the tokens were cached, have Preprocessor replace them with the
862 PP.AnnotateCachedTokens(Tok);
866 /// \brief Replaces a template-id annotation token with a type
867 /// annotation token.
869 /// If there was a failure when forming the type from the template-id,
870 /// a type annotation token will still be created, but will have a
871 /// NULL type pointer to signify an error.
872 void Parser::AnnotateTemplateIdTokenAsType() {
873 assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
875 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
876 assert((TemplateId->Kind == TNK_Type_template ||
877 TemplateId->Kind == TNK_Dependent_template_name) &&
878 "Only works for type and dependent templates");
880 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
881 TemplateId->getTemplateArgs(),
882 TemplateId->NumArgs);
885 = Actions.ActOnTemplateIdType(TemplateId->SS,
886 TemplateId->Template,
887 TemplateId->TemplateNameLoc,
888 TemplateId->LAngleLoc,
890 TemplateId->RAngleLoc);
891 // Create the new "type" annotation token.
892 Tok.setKind(tok::annot_typename);
893 setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
894 if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
895 Tok.setLocation(TemplateId->SS.getBeginLoc());
896 // End location stays the same
898 // Replace the template-id annotation token, and possible the scope-specifier
899 // that precedes it, with the typename annotation token.
900 PP.AnnotateCachedTokens(Tok);
903 /// \brief Determine whether the given token can end a template argument.
904 static bool isEndOfTemplateArgument(Token Tok) {
905 return Tok.is(tok::comma) || Tok.is(tok::greater) ||
906 Tok.is(tok::greatergreater);
909 /// \brief Parse a C++ template template argument.
910 ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
911 if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
912 !Tok.is(tok::annot_cxxscope))
913 return ParsedTemplateArgument();
915 // C++0x [temp.arg.template]p1:
916 // A template-argument for a template template-parameter shall be the name
917 // of a class template or an alias template, expressed as id-expression.
919 // We parse an id-expression that refers to a class template or alias
920 // template. The grammar we parse is:
922 // nested-name-specifier[opt] template[opt] identifier ...[opt]
924 // followed by a token that terminates a template argument, such as ',',
925 // '>', or (in some cases) '>>'.
926 CXXScopeSpec SS; // nested-name-specifier, if present
927 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
928 /*EnteringContext=*/false);
930 ParsedTemplateArgument Result;
931 SourceLocation EllipsisLoc;
932 if (SS.isSet() && Tok.is(tok::kw_template)) {
933 // Parse the optional 'template' keyword following the
934 // nested-name-specifier.
935 SourceLocation TemplateLoc = ConsumeToken();
937 if (Tok.is(tok::identifier)) {
938 // We appear to have a dependent template name.
940 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
941 ConsumeToken(); // the identifier
943 // Parse the ellipsis.
944 if (Tok.is(tok::ellipsis))
945 EllipsisLoc = ConsumeToken();
947 // If the next token signals the end of a template argument,
948 // then we have a dependent template name that could be a template
949 // template argument.
951 if (isEndOfTemplateArgument(Tok) &&
952 Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc,
954 /*ObjectType=*/ ParsedType(),
955 /*EnteringContext=*/false,
957 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
959 } else if (Tok.is(tok::identifier)) {
960 // We may have a (non-dependent) template name.
963 Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
964 ConsumeToken(); // the identifier
966 // Parse the ellipsis.
967 if (Tok.is(tok::ellipsis))
968 EllipsisLoc = ConsumeToken();
970 if (isEndOfTemplateArgument(Tok)) {
971 bool MemberOfUnknownSpecialization;
972 TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
973 /*hasTemplateKeyword=*/false,
975 /*ObjectType=*/ ParsedType(),
976 /*EnteringContext=*/false,
978 MemberOfUnknownSpecialization);
979 if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
980 // We have an id-expression that refers to a class template or
981 // (C++0x) alias template.
982 Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
987 // If this is a pack expansion, build it as such.
988 if (EllipsisLoc.isValid() && !Result.isInvalid())
989 Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
994 /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
996 /// template-argument: [C++ 14.2]
997 /// constant-expression
1000 ParsedTemplateArgument Parser::ParseTemplateArgument() {
1001 // C++ [temp.arg]p2:
1002 // In a template-argument, an ambiguity between a type-id and an
1003 // expression is resolved to a type-id, regardless of the form of
1004 // the corresponding template-parameter.
1006 // Therefore, we initially try to parse a type-id.
1007 if (isCXXTypeId(TypeIdAsTemplateArgument)) {
1008 SourceLocation Loc = Tok.getLocation();
1009 TypeResult TypeArg = ParseTypeName(/*Range=*/0,
1010 Declarator::TemplateTypeArgContext);
1011 if (TypeArg.isInvalid())
1012 return ParsedTemplateArgument();
1014 return ParsedTemplateArgument(ParsedTemplateArgument::Type,
1015 TypeArg.get().getAsOpaquePtr(),
1019 // Try to parse a template template argument.
1021 TentativeParsingAction TPA(*this);
1023 ParsedTemplateArgument TemplateTemplateArgument
1024 = ParseTemplateTemplateArgument();
1025 if (!TemplateTemplateArgument.isInvalid()) {
1027 return TemplateTemplateArgument;
1030 // Revert this tentative parse to parse a non-type template argument.
1034 // Parse a non-type template argument.
1035 SourceLocation Loc = Tok.getLocation();
1036 ExprResult ExprArg = ParseConstantExpression();
1037 if (ExprArg.isInvalid() || !ExprArg.get())
1038 return ParsedTemplateArgument();
1040 return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
1041 ExprArg.release(), Loc);
1044 /// \brief Determine whether the current tokens can only be parsed as a
1045 /// template argument list (starting with the '<') and never as a '<'
1047 bool Parser::IsTemplateArgumentList(unsigned Skip) {
1048 struct AlwaysRevertAction : TentativeParsingAction {
1049 AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
1050 ~AlwaysRevertAction() { Revert(); }
1059 if (!Tok.is(tok::less))
1063 // An empty template argument list.
1064 if (Tok.is(tok::greater))
1067 // See whether we have declaration specifiers, which indicate a type.
1068 while (isCXXDeclarationSpecifier() == TPResult::True())
1071 // If we have a '>' or a ',' then this is a template argument list.
1072 return Tok.is(tok::greater) || Tok.is(tok::comma);
1075 /// ParseTemplateArgumentList - Parse a C++ template-argument-list
1076 /// (C++ [temp.names]). Returns true if there was an error.
1078 /// template-argument-list: [C++ 14.2]
1079 /// template-argument
1080 /// template-argument-list ',' template-argument
1082 Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
1084 ParsedTemplateArgument Arg = ParseTemplateArgument();
1085 if (Tok.is(tok::ellipsis)) {
1086 SourceLocation EllipsisLoc = ConsumeToken();
1087 Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
1090 if (Arg.isInvalid()) {
1091 SkipUntil(tok::comma, tok::greater, true, true);
1095 // Save this template argument.
1096 TemplateArgs.push_back(Arg);
1098 // If the next token is a comma, consume it and keep reading
1100 if (Tok.isNot(tok::comma)) break;
1102 // Consume the comma.
1109 /// \brief Parse a C++ explicit template instantiation
1110 /// (C++ [temp.explicit]).
1112 /// explicit-instantiation:
1113 /// 'extern' [opt] 'template' declaration
1115 /// Note that the 'extern' is a GNU extension and C++0x feature.
1116 Decl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
1117 SourceLocation TemplateLoc,
1118 SourceLocation &DeclEnd) {
1119 // This isn't really required here.
1120 ParsingDeclRAIIObject ParsingTemplateParams(*this);
1122 return ParseSingleDeclarationAfterTemplate(Declarator::FileContext,
1123 ParsedTemplateInfo(ExternLoc,
1125 ParsingTemplateParams,
1129 SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
1131 return getTemplateParamsRange(TemplateParams->data(),
1132 TemplateParams->size());
1134 SourceRange R(TemplateLoc);
1135 if (ExternLoc.isValid())
1136 R.setBegin(ExternLoc);
1140 void Parser::LateTemplateParserCallback(void *P, const FunctionDecl *FD) {
1141 ((Parser*)P)->LateTemplateParser(FD);
1145 void Parser::LateTemplateParser(const FunctionDecl *FD) {
1146 LateParsedTemplatedFunction *LPT = LateParsedTemplateMap[FD];
1148 ParseLateTemplatedFuncDef(*LPT);
1152 llvm_unreachable("Late templated function without associated lexed tokens");
1155 /// \brief Late parse a C++ function template in Microsoft mode.
1156 void Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) {
1160 // If this is a member template, introduce the template parameter scope.
1161 ParseScope TemplateScope(this, Scope::TemplateParamScope);
1163 // Get the FunctionDecl.
1164 FunctionDecl *FD = 0;
1165 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(LMT.D))
1166 FD = FunTmpl->getTemplatedDecl();
1168 FD = cast<FunctionDecl>(LMT.D);
1170 // Reinject the template parameters.
1171 SmallVector<ParseScope*, 4> TemplateParamScopeStack;
1172 DeclaratorDecl* Declarator = dyn_cast<DeclaratorDecl>(FD);
1173 if (Declarator && Declarator->getNumTemplateParameterLists() != 0) {
1174 Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator);
1175 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
1177 Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D);
1179 // Get the list of DeclContext to reenter.
1180 SmallVector<DeclContext*, 4> DeclContextToReenter;
1181 DeclContext *DD = FD->getLexicalParent();
1182 while (DD && DD->isRecord()) {
1183 DeclContextToReenter.push_back(DD);
1184 DD = DD->getLexicalParent();
1187 // Reenter template scopes from outmost to innermost.
1188 SmallVector<DeclContext*, 4>::reverse_iterator II =
1189 DeclContextToReenter.rbegin();
1190 for (; II != DeclContextToReenter.rend(); ++II) {
1191 if (ClassTemplatePartialSpecializationDecl* MD =
1192 dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(*II)) {
1193 TemplateParamScopeStack.push_back(new ParseScope(this,
1194 Scope::TemplateParamScope));
1195 Actions.ActOnReenterTemplateScope(getCurScope(), MD);
1196 } else if (CXXRecordDecl* MD = dyn_cast_or_null<CXXRecordDecl>(*II)) {
1197 TemplateParamScopeStack.push_back(new ParseScope(this,
1198 Scope::TemplateParamScope,
1199 MD->getDescribedClassTemplate() != 0 ));
1200 Actions.ActOnReenterTemplateScope(getCurScope(),
1201 MD->getDescribedClassTemplate());
1205 assert(!LMT.Toks.empty() && "Empty body!");
1207 // Append the current token at the end of the new token stream so that it
1208 // doesn't get lost.
1209 LMT.Toks.push_back(Tok);
1210 PP.EnterTokenStream(LMT.Toks.data(), LMT.Toks.size(), true, false);
1212 // Consume the previously pushed token.
1214 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
1215 && "Inline method not starting with '{', ':' or 'try'");
1217 // Parse the method body. Function body parsing code is similar enough
1218 // to be re-used for method bodies as well.
1219 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
1221 // Recreate the DeclContext.
1222 Sema::ContextRAII SavedContext(Actions, Actions.getContainingDC(FD));
1224 if (FunctionTemplateDecl *FunctionTemplate
1225 = dyn_cast_or_null<FunctionTemplateDecl>(LMT.D))
1226 Actions.ActOnStartOfFunctionDef(getCurScope(),
1227 FunctionTemplate->getTemplatedDecl());
1228 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(LMT.D))
1229 Actions.ActOnStartOfFunctionDef(getCurScope(), Function);
1232 if (Tok.is(tok::kw_try)) {
1233 ParseFunctionTryBlock(LMT.D, FnScope);
1235 if (Tok.is(tok::colon))
1236 ParseConstructorInitializer(LMT.D);
1238 Actions.ActOnDefaultCtorInitializers(LMT.D);
1240 if (Tok.is(tok::l_brace)) {
1241 ParseFunctionStatementBody(LMT.D, FnScope);
1242 Actions.MarkAsLateParsedTemplate(FD, false);
1244 Actions.ActOnFinishFunctionBody(LMT.D, 0);
1249 SmallVector<ParseScope*, 4>::reverse_iterator I =
1250 TemplateParamScopeStack.rbegin();
1251 for (; I != TemplateParamScopeStack.rend(); ++I)
1254 DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D);
1256 Actions.getASTConsumer().HandleTopLevelDecl(grp.get());
1259 /// \brief Lex a delayed template function for late parsing.
1260 void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1261 tok::TokenKind kind = Tok.getKind();
1262 if (!ConsumeAndStoreFunctionPrologue(Toks)) {
1263 // Consume everything up to (and including) the matching right brace.
1264 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1267 // If we're in a function-try-block, we need to store all the catch blocks.
1268 if (kind == tok::kw_try) {
1269 while (Tok.is(tok::kw_catch)) {
1270 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1271 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);