xref: /minix3/external/bsd/llvm/dist/clang/lib/Parse/ParseTemplate.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file implements parsing of C++ templates.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h"
15f4a2713aSLionel Sambuc #include "RAIIObjectsForParser.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
18f4a2713aSLionel Sambuc #include "clang/Parse/ParseDiagnostic.h"
19f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
20f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
22f4a2713aSLionel Sambuc using namespace clang;
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc /// \brief Parse a template declaration, explicit instantiation, or
25f4a2713aSLionel Sambuc /// explicit specialization.
26f4a2713aSLionel Sambuc Decl *
ParseDeclarationStartingWithTemplate(unsigned Context,SourceLocation & DeclEnd,AccessSpecifier AS,AttributeList * AccessAttrs)27f4a2713aSLionel Sambuc Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
28f4a2713aSLionel Sambuc                                              SourceLocation &DeclEnd,
29f4a2713aSLionel Sambuc                                              AccessSpecifier AS,
30f4a2713aSLionel Sambuc                                              AttributeList *AccessAttrs) {
31f4a2713aSLionel Sambuc   ObjCDeclContextSwitch ObjCDC(*this);
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
34f4a2713aSLionel Sambuc     return ParseExplicitInstantiation(Context,
35f4a2713aSLionel Sambuc                                       SourceLocation(), ConsumeToken(),
36f4a2713aSLionel Sambuc                                       DeclEnd, AS);
37f4a2713aSLionel Sambuc   }
38f4a2713aSLionel Sambuc   return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS,
39f4a2713aSLionel Sambuc                                                   AccessAttrs);
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc /// \brief Parse a template declaration or an explicit specialization.
45f4a2713aSLionel Sambuc ///
46f4a2713aSLionel Sambuc /// Template declarations include one or more template parameter lists
47f4a2713aSLionel Sambuc /// and either the function or class template declaration. Explicit
48f4a2713aSLionel Sambuc /// specializations contain one or more 'template < >' prefixes
49f4a2713aSLionel Sambuc /// followed by a (possibly templated) declaration. Since the
50f4a2713aSLionel Sambuc /// syntactic form of both features is nearly identical, we parse all
51f4a2713aSLionel Sambuc /// of the template headers together and let semantic analysis sort
52f4a2713aSLionel Sambuc /// the declarations from the explicit specializations.
53f4a2713aSLionel Sambuc ///
54f4a2713aSLionel Sambuc ///       template-declaration: [C++ temp]
55f4a2713aSLionel Sambuc ///         'export'[opt] 'template' '<' template-parameter-list '>' declaration
56f4a2713aSLionel Sambuc ///
57f4a2713aSLionel Sambuc ///       explicit-specialization: [ C++ temp.expl.spec]
58f4a2713aSLionel Sambuc ///         'template' '<' '>' declaration
59f4a2713aSLionel Sambuc Decl *
ParseTemplateDeclarationOrSpecialization(unsigned Context,SourceLocation & DeclEnd,AccessSpecifier AS,AttributeList * AccessAttrs)60f4a2713aSLionel Sambuc Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
61f4a2713aSLionel Sambuc                                                  SourceLocation &DeclEnd,
62f4a2713aSLionel Sambuc                                                  AccessSpecifier AS,
63f4a2713aSLionel Sambuc                                                  AttributeList *AccessAttrs) {
64f4a2713aSLionel Sambuc   assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) &&
65f4a2713aSLionel Sambuc          "Token does not start a template declaration.");
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   // Enter template-parameter scope.
68f4a2713aSLionel Sambuc   ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   // Tell the action that names should be checked in the context of
71f4a2713aSLionel Sambuc   // the declaration to come.
72f4a2713aSLionel Sambuc   ParsingDeclRAIIObject
73f4a2713aSLionel Sambuc     ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc   // Parse multiple levels of template headers within this template
76f4a2713aSLionel Sambuc   // parameter scope, e.g.,
77f4a2713aSLionel Sambuc   //
78f4a2713aSLionel Sambuc   //   template<typename T>
79f4a2713aSLionel Sambuc   //     template<typename U>
80f4a2713aSLionel Sambuc   //       class A<T>::B { ... };
81f4a2713aSLionel Sambuc   //
82f4a2713aSLionel Sambuc   // We parse multiple levels non-recursively so that we can build a
83f4a2713aSLionel Sambuc   // single data structure containing all of the template parameter
84f4a2713aSLionel Sambuc   // lists to easily differentiate between the case above and:
85f4a2713aSLionel Sambuc   //
86f4a2713aSLionel Sambuc   //   template<typename T>
87f4a2713aSLionel Sambuc   //   class A {
88f4a2713aSLionel Sambuc   //     template<typename U> class B;
89f4a2713aSLionel Sambuc   //   };
90f4a2713aSLionel Sambuc   //
91f4a2713aSLionel Sambuc   // In the first case, the action for declaring A<T>::B receives
92f4a2713aSLionel Sambuc   // both template parameter lists. In the second case, the action for
93f4a2713aSLionel Sambuc   // defining A<T>::B receives just the inner template parameter list
94f4a2713aSLionel Sambuc   // (and retrieves the outer template parameter list from its
95f4a2713aSLionel Sambuc   // context).
96f4a2713aSLionel Sambuc   bool isSpecialization = true;
97f4a2713aSLionel Sambuc   bool LastParamListWasEmpty = false;
98f4a2713aSLionel Sambuc   TemplateParameterLists ParamLists;
99f4a2713aSLionel Sambuc   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc   do {
102f4a2713aSLionel Sambuc     // Consume the 'export', if any.
103f4a2713aSLionel Sambuc     SourceLocation ExportLoc;
104*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::kw_export, ExportLoc);
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc     // Consume the 'template', which should be here.
107f4a2713aSLionel Sambuc     SourceLocation TemplateLoc;
108*0a6a1f1dSLionel Sambuc     if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
109f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_expected_template);
110*0a6a1f1dSLionel Sambuc       return nullptr;
111f4a2713aSLionel Sambuc     }
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc     // Parse the '<' template-parameter-list '>'
114f4a2713aSLionel Sambuc     SourceLocation LAngleLoc, RAngleLoc;
115f4a2713aSLionel Sambuc     SmallVector<Decl*, 4> TemplateParams;
116f4a2713aSLionel Sambuc     if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(),
117f4a2713aSLionel Sambuc                                 TemplateParams, LAngleLoc, RAngleLoc)) {
118f4a2713aSLionel Sambuc       // Skip until the semi-colon or a }.
119f4a2713aSLionel Sambuc       SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
120*0a6a1f1dSLionel Sambuc       TryConsumeToken(tok::semi);
121*0a6a1f1dSLionel Sambuc       return nullptr;
122f4a2713aSLionel Sambuc     }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc     ParamLists.push_back(
125f4a2713aSLionel Sambuc       Actions.ActOnTemplateParameterList(CurTemplateDepthTracker.getDepth(),
126f4a2713aSLionel Sambuc                                          ExportLoc,
127f4a2713aSLionel Sambuc                                          TemplateLoc, LAngleLoc,
128f4a2713aSLionel Sambuc                                          TemplateParams.data(),
129f4a2713aSLionel Sambuc                                          TemplateParams.size(), RAngleLoc));
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc     if (!TemplateParams.empty()) {
132f4a2713aSLionel Sambuc       isSpecialization = false;
133f4a2713aSLionel Sambuc       ++CurTemplateDepthTracker;
134f4a2713aSLionel Sambuc     } else {
135f4a2713aSLionel Sambuc       LastParamListWasEmpty = true;
136f4a2713aSLionel Sambuc     }
137f4a2713aSLionel Sambuc   } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template));
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc   // Parse the actual template declaration.
140f4a2713aSLionel Sambuc   return ParseSingleDeclarationAfterTemplate(Context,
141f4a2713aSLionel Sambuc                                              ParsedTemplateInfo(&ParamLists,
142f4a2713aSLionel Sambuc                                                              isSpecialization,
143f4a2713aSLionel Sambuc                                                          LastParamListWasEmpty),
144f4a2713aSLionel Sambuc                                              ParsingTemplateParams,
145f4a2713aSLionel Sambuc                                              DeclEnd, AS, AccessAttrs);
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc /// \brief Parse a single declaration that declares a template,
149f4a2713aSLionel Sambuc /// template specialization, or explicit instantiation of a template.
150f4a2713aSLionel Sambuc ///
151f4a2713aSLionel Sambuc /// \param DeclEnd will receive the source location of the last token
152f4a2713aSLionel Sambuc /// within this declaration.
153f4a2713aSLionel Sambuc ///
154f4a2713aSLionel Sambuc /// \param AS the access specifier associated with this
155f4a2713aSLionel Sambuc /// declaration. Will be AS_none for namespace-scope declarations.
156f4a2713aSLionel Sambuc ///
157f4a2713aSLionel Sambuc /// \returns the new declaration.
158f4a2713aSLionel Sambuc Decl *
ParseSingleDeclarationAfterTemplate(unsigned Context,const ParsedTemplateInfo & TemplateInfo,ParsingDeclRAIIObject & DiagsFromTParams,SourceLocation & DeclEnd,AccessSpecifier AS,AttributeList * AccessAttrs)159f4a2713aSLionel Sambuc Parser::ParseSingleDeclarationAfterTemplate(
160f4a2713aSLionel Sambuc                                        unsigned Context,
161f4a2713aSLionel Sambuc                                        const ParsedTemplateInfo &TemplateInfo,
162f4a2713aSLionel Sambuc                                        ParsingDeclRAIIObject &DiagsFromTParams,
163f4a2713aSLionel Sambuc                                        SourceLocation &DeclEnd,
164f4a2713aSLionel Sambuc                                        AccessSpecifier AS,
165f4a2713aSLionel Sambuc                                        AttributeList *AccessAttrs) {
166f4a2713aSLionel Sambuc   assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
167f4a2713aSLionel Sambuc          "Template information required");
168f4a2713aSLionel Sambuc 
169*0a6a1f1dSLionel Sambuc   if (Tok.is(tok::kw_static_assert)) {
170*0a6a1f1dSLionel Sambuc     // A static_assert declaration may not be templated.
171*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
172*0a6a1f1dSLionel Sambuc       << TemplateInfo.getSourceRange();
173*0a6a1f1dSLionel Sambuc     // Parse the static_assert declaration to improve error recovery.
174*0a6a1f1dSLionel Sambuc     return ParseStaticAssertDeclaration(DeclEnd);
175*0a6a1f1dSLionel Sambuc   }
176*0a6a1f1dSLionel Sambuc 
177f4a2713aSLionel Sambuc   if (Context == Declarator::MemberContext) {
178f4a2713aSLionel Sambuc     // We are parsing a member template.
179f4a2713aSLionel Sambuc     ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
180f4a2713aSLionel Sambuc                                    &DiagsFromTParams);
181*0a6a1f1dSLionel Sambuc     return nullptr;
182f4a2713aSLionel Sambuc   }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc   ParsedAttributesWithRange prefixAttrs(AttrFactory);
185f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(prefixAttrs);
186f4a2713aSLionel Sambuc 
187f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_using))
188f4a2713aSLionel Sambuc     return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
189f4a2713aSLionel Sambuc                                             prefixAttrs);
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc   // Parse the declaration specifiers, stealing any diagnostics from
192f4a2713aSLionel Sambuc   // the template parameters.
193f4a2713aSLionel Sambuc   ParsingDeclSpec DS(*this, &DiagsFromTParams);
194f4a2713aSLionel Sambuc 
195f4a2713aSLionel Sambuc   ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
196f4a2713aSLionel Sambuc                              getDeclSpecContextFromDeclaratorContext(Context));
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   if (Tok.is(tok::semi)) {
199f4a2713aSLionel Sambuc     ProhibitAttributes(prefixAttrs);
200f4a2713aSLionel Sambuc     DeclEnd = ConsumeToken();
201f4a2713aSLionel Sambuc     Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
202f4a2713aSLionel Sambuc         getCurScope(), AS, DS,
203f4a2713aSLionel Sambuc         TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
204f4a2713aSLionel Sambuc                                     : MultiTemplateParamsArg(),
205f4a2713aSLionel Sambuc         TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation);
206f4a2713aSLionel Sambuc     DS.complete(Decl);
207f4a2713aSLionel Sambuc     return Decl;
208f4a2713aSLionel Sambuc   }
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc   // Move the attributes from the prefix into the DS.
211f4a2713aSLionel Sambuc   if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
212f4a2713aSLionel Sambuc     ProhibitAttributes(prefixAttrs);
213f4a2713aSLionel Sambuc   else
214f4a2713aSLionel Sambuc     DS.takeAttributesFrom(prefixAttrs);
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   // Parse the declarator.
217f4a2713aSLionel Sambuc   ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
218f4a2713aSLionel Sambuc   ParseDeclarator(DeclaratorInfo);
219f4a2713aSLionel Sambuc   // Error parsing the declarator?
220f4a2713aSLionel Sambuc   if (!DeclaratorInfo.hasName()) {
221f4a2713aSLionel Sambuc     // If so, skip until the semi-colon or a }.
222f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
223f4a2713aSLionel Sambuc     if (Tok.is(tok::semi))
224f4a2713aSLionel Sambuc       ConsumeToken();
225*0a6a1f1dSLionel Sambuc     return nullptr;
226f4a2713aSLionel Sambuc   }
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc   LateParsedAttrList LateParsedAttrs(true);
229f4a2713aSLionel Sambuc   if (DeclaratorInfo.isFunctionDeclarator())
230f4a2713aSLionel Sambuc     MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc   if (DeclaratorInfo.isFunctionDeclarator() &&
233f4a2713aSLionel Sambuc       isStartOfFunctionDefinition(DeclaratorInfo)) {
234*0a6a1f1dSLionel Sambuc 
235*0a6a1f1dSLionel Sambuc     // Function definitions are only allowed at file scope and in C++ classes.
236*0a6a1f1dSLionel Sambuc     // The C++ inline method definition case is handled elsewhere, so we only
237*0a6a1f1dSLionel Sambuc     // need to handle the file scope definition case.
238*0a6a1f1dSLionel Sambuc     if (Context != Declarator::FileContext) {
239*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_function_definition_not_allowed);
240*0a6a1f1dSLionel Sambuc       SkipMalformedDecl();
241*0a6a1f1dSLionel Sambuc       return nullptr;
242*0a6a1f1dSLionel Sambuc     }
243*0a6a1f1dSLionel Sambuc 
244f4a2713aSLionel Sambuc     if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
245f4a2713aSLionel Sambuc       // Recover by ignoring the 'typedef'. This was probably supposed to be
246f4a2713aSLionel Sambuc       // the 'typename' keyword, which we should have already suggested adding
247f4a2713aSLionel Sambuc       // if it's appropriate.
248f4a2713aSLionel Sambuc       Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
249f4a2713aSLionel Sambuc         << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
250f4a2713aSLionel Sambuc       DS.ClearStorageClassSpecs();
251f4a2713aSLionel Sambuc     }
252f4a2713aSLionel Sambuc 
253f4a2713aSLionel Sambuc     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
254f4a2713aSLionel Sambuc       if (DeclaratorInfo.getName().getKind() != UnqualifiedId::IK_TemplateId) {
255f4a2713aSLionel Sambuc         // If the declarator-id is not a template-id, issue a diagnostic and
256f4a2713aSLionel Sambuc         // recover by ignoring the 'template' keyword.
257f4a2713aSLionel Sambuc         Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
258f4a2713aSLionel Sambuc         return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
259f4a2713aSLionel Sambuc                                        &LateParsedAttrs);
260f4a2713aSLionel Sambuc       } else {
261f4a2713aSLionel Sambuc         SourceLocation LAngleLoc
262f4a2713aSLionel Sambuc           = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
263f4a2713aSLionel Sambuc         Diag(DeclaratorInfo.getIdentifierLoc(),
264f4a2713aSLionel Sambuc              diag::err_explicit_instantiation_with_definition)
265f4a2713aSLionel Sambuc             << SourceRange(TemplateInfo.TemplateLoc)
266f4a2713aSLionel Sambuc             << FixItHint::CreateInsertion(LAngleLoc, "<>");
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc         // Recover as if it were an explicit specialization.
269f4a2713aSLionel Sambuc         TemplateParameterLists FakedParamLists;
270f4a2713aSLionel Sambuc         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
271*0a6a1f1dSLionel Sambuc             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
272*0a6a1f1dSLionel Sambuc             0, LAngleLoc));
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc         return ParseFunctionDefinition(
275f4a2713aSLionel Sambuc             DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
276f4a2713aSLionel Sambuc                                                /*isSpecialization=*/true,
277f4a2713aSLionel Sambuc                                                /*LastParamListWasEmpty=*/true),
278f4a2713aSLionel Sambuc             &LateParsedAttrs);
279f4a2713aSLionel Sambuc       }
280f4a2713aSLionel Sambuc     }
281f4a2713aSLionel Sambuc     return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
282f4a2713aSLionel Sambuc                                    &LateParsedAttrs);
283f4a2713aSLionel Sambuc   }
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc   // Parse this declaration.
286f4a2713aSLionel Sambuc   Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
287f4a2713aSLionel Sambuc                                                    TemplateInfo);
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc   if (Tok.is(tok::comma)) {
290f4a2713aSLionel Sambuc     Diag(Tok, diag::err_multiple_template_declarators)
291f4a2713aSLionel Sambuc       << (int)TemplateInfo.Kind;
292f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
293f4a2713aSLionel Sambuc     return ThisDecl;
294f4a2713aSLionel Sambuc   }
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc   // Eat the semi colon after the declaration.
297f4a2713aSLionel Sambuc   ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
298f4a2713aSLionel Sambuc   if (LateParsedAttrs.size() > 0)
299f4a2713aSLionel Sambuc     ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
300f4a2713aSLionel Sambuc   DeclaratorInfo.complete(ThisDecl);
301f4a2713aSLionel Sambuc   return ThisDecl;
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc 
304f4a2713aSLionel Sambuc /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
305f4a2713aSLionel Sambuc /// angle brackets. Depth is the depth of this template-parameter-list, which
306f4a2713aSLionel Sambuc /// is the number of template headers directly enclosing this template header.
307f4a2713aSLionel Sambuc /// TemplateParams is the current list of template parameters we're building.
308f4a2713aSLionel Sambuc /// The template parameter we parse will be added to this list. LAngleLoc and
309f4a2713aSLionel Sambuc /// RAngleLoc will receive the positions of the '<' and '>', respectively,
310f4a2713aSLionel Sambuc /// that enclose this template parameter list.
311f4a2713aSLionel Sambuc ///
312f4a2713aSLionel Sambuc /// \returns true if an error occurred, false otherwise.
ParseTemplateParameters(unsigned Depth,SmallVectorImpl<Decl * > & TemplateParams,SourceLocation & LAngleLoc,SourceLocation & RAngleLoc)313f4a2713aSLionel Sambuc bool Parser::ParseTemplateParameters(unsigned Depth,
314f4a2713aSLionel Sambuc                                SmallVectorImpl<Decl*> &TemplateParams,
315f4a2713aSLionel Sambuc                                      SourceLocation &LAngleLoc,
316f4a2713aSLionel Sambuc                                      SourceLocation &RAngleLoc) {
317f4a2713aSLionel Sambuc   // Get the template parameter list.
318*0a6a1f1dSLionel Sambuc   if (!TryConsumeToken(tok::less, LAngleLoc)) {
319f4a2713aSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
320f4a2713aSLionel Sambuc     return true;
321f4a2713aSLionel Sambuc   }
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   // Try to parse the template parameter list.
324f4a2713aSLionel Sambuc   bool Failed = false;
325f4a2713aSLionel Sambuc   if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater))
326f4a2713aSLionel Sambuc     Failed = ParseTemplateParameterList(Depth, TemplateParams);
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc   if (Tok.is(tok::greatergreater)) {
329f4a2713aSLionel Sambuc     // No diagnostic required here: a template-parameter-list can only be
330f4a2713aSLionel Sambuc     // followed by a declaration or, for a template template parameter, the
331f4a2713aSLionel Sambuc     // 'class' keyword. Therefore, the second '>' will be diagnosed later.
332f4a2713aSLionel Sambuc     // This matters for elegant diagnosis of:
333f4a2713aSLionel Sambuc     //   template<template<typename>> struct S;
334f4a2713aSLionel Sambuc     Tok.setKind(tok::greater);
335f4a2713aSLionel Sambuc     RAngleLoc = Tok.getLocation();
336f4a2713aSLionel Sambuc     Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
337*0a6a1f1dSLionel Sambuc   } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
338*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
339f4a2713aSLionel Sambuc     return true;
340f4a2713aSLionel Sambuc   }
341f4a2713aSLionel Sambuc   return false;
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc /// ParseTemplateParameterList - Parse a template parameter list. If
345f4a2713aSLionel Sambuc /// the parsing fails badly (i.e., closing bracket was left out), this
346f4a2713aSLionel Sambuc /// will try to put the token stream in a reasonable position (closing
347f4a2713aSLionel Sambuc /// a statement, etc.) and return false.
348f4a2713aSLionel Sambuc ///
349f4a2713aSLionel Sambuc ///       template-parameter-list:    [C++ temp]
350f4a2713aSLionel Sambuc ///         template-parameter
351f4a2713aSLionel Sambuc ///         template-parameter-list ',' template-parameter
352f4a2713aSLionel Sambuc bool
ParseTemplateParameterList(unsigned Depth,SmallVectorImpl<Decl * > & TemplateParams)353f4a2713aSLionel Sambuc Parser::ParseTemplateParameterList(unsigned Depth,
354f4a2713aSLionel Sambuc                              SmallVectorImpl<Decl*> &TemplateParams) {
355f4a2713aSLionel Sambuc   while (1) {
356f4a2713aSLionel Sambuc     if (Decl *TmpParam
357f4a2713aSLionel Sambuc           = ParseTemplateParameter(Depth, TemplateParams.size())) {
358f4a2713aSLionel Sambuc       TemplateParams.push_back(TmpParam);
359f4a2713aSLionel Sambuc     } else {
360f4a2713aSLionel Sambuc       // If we failed to parse a template parameter, skip until we find
361f4a2713aSLionel Sambuc       // a comma or closing brace.
362f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::greater, tok::greatergreater,
363f4a2713aSLionel Sambuc                 StopAtSemi | StopBeforeMatch);
364f4a2713aSLionel Sambuc     }
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc     // Did we find a comma or the end of the template parameter list?
367f4a2713aSLionel Sambuc     if (Tok.is(tok::comma)) {
368f4a2713aSLionel Sambuc       ConsumeToken();
369f4a2713aSLionel Sambuc     } else if (Tok.is(tok::greater) || Tok.is(tok::greatergreater)) {
370f4a2713aSLionel Sambuc       // Don't consume this... that's done by template parser.
371f4a2713aSLionel Sambuc       break;
372f4a2713aSLionel Sambuc     } else {
373f4a2713aSLionel Sambuc       // Somebody probably forgot to close the template. Skip ahead and
374f4a2713aSLionel Sambuc       // try to get out of the expression. This error is currently
375f4a2713aSLionel Sambuc       // subsumed by whatever goes on in ParseTemplateParameter.
376f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_expected_comma_greater);
377f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::greater, tok::greatergreater,
378f4a2713aSLionel Sambuc                 StopAtSemi | StopBeforeMatch);
379f4a2713aSLionel Sambuc       return false;
380f4a2713aSLionel Sambuc     }
381f4a2713aSLionel Sambuc   }
382f4a2713aSLionel Sambuc   return true;
383f4a2713aSLionel Sambuc }
384f4a2713aSLionel Sambuc 
385f4a2713aSLionel Sambuc /// \brief Determine whether the parser is at the start of a template
386f4a2713aSLionel Sambuc /// type parameter.
isStartOfTemplateTypeParameter()387f4a2713aSLionel Sambuc bool Parser::isStartOfTemplateTypeParameter() {
388f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_class)) {
389f4a2713aSLionel Sambuc     // "class" may be the start of an elaborated-type-specifier or a
390f4a2713aSLionel Sambuc     // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
391f4a2713aSLionel Sambuc     switch (NextToken().getKind()) {
392f4a2713aSLionel Sambuc     case tok::equal:
393f4a2713aSLionel Sambuc     case tok::comma:
394f4a2713aSLionel Sambuc     case tok::greater:
395f4a2713aSLionel Sambuc     case tok::greatergreater:
396f4a2713aSLionel Sambuc     case tok::ellipsis:
397f4a2713aSLionel Sambuc       return true;
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc     case tok::identifier:
400f4a2713aSLionel Sambuc       // This may be either a type-parameter or an elaborated-type-specifier.
401f4a2713aSLionel Sambuc       // We have to look further.
402f4a2713aSLionel Sambuc       break;
403f4a2713aSLionel Sambuc 
404f4a2713aSLionel Sambuc     default:
405f4a2713aSLionel Sambuc       return false;
406f4a2713aSLionel Sambuc     }
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc     switch (GetLookAheadToken(2).getKind()) {
409f4a2713aSLionel Sambuc     case tok::equal:
410f4a2713aSLionel Sambuc     case tok::comma:
411f4a2713aSLionel Sambuc     case tok::greater:
412f4a2713aSLionel Sambuc     case tok::greatergreater:
413f4a2713aSLionel Sambuc       return true;
414f4a2713aSLionel Sambuc 
415f4a2713aSLionel Sambuc     default:
416f4a2713aSLionel Sambuc       return false;
417f4a2713aSLionel Sambuc     }
418f4a2713aSLionel Sambuc   }
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc   if (Tok.isNot(tok::kw_typename))
421f4a2713aSLionel Sambuc     return false;
422f4a2713aSLionel Sambuc 
423f4a2713aSLionel Sambuc   // C++ [temp.param]p2:
424f4a2713aSLionel Sambuc   //   There is no semantic difference between class and typename in a
425f4a2713aSLionel Sambuc   //   template-parameter. typename followed by an unqualified-id
426f4a2713aSLionel Sambuc   //   names a template type parameter. typename followed by a
427f4a2713aSLionel Sambuc   //   qualified-id denotes the type in a non-type
428f4a2713aSLionel Sambuc   //   parameter-declaration.
429f4a2713aSLionel Sambuc   Token Next = NextToken();
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc   // If we have an identifier, skip over it.
432f4a2713aSLionel Sambuc   if (Next.getKind() == tok::identifier)
433f4a2713aSLionel Sambuc     Next = GetLookAheadToken(2);
434f4a2713aSLionel Sambuc 
435f4a2713aSLionel Sambuc   switch (Next.getKind()) {
436f4a2713aSLionel Sambuc   case tok::equal:
437f4a2713aSLionel Sambuc   case tok::comma:
438f4a2713aSLionel Sambuc   case tok::greater:
439f4a2713aSLionel Sambuc   case tok::greatergreater:
440f4a2713aSLionel Sambuc   case tok::ellipsis:
441f4a2713aSLionel Sambuc     return true;
442f4a2713aSLionel Sambuc 
443f4a2713aSLionel Sambuc   default:
444f4a2713aSLionel Sambuc     return false;
445f4a2713aSLionel Sambuc   }
446f4a2713aSLionel Sambuc }
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
449f4a2713aSLionel Sambuc ///
450f4a2713aSLionel Sambuc ///       template-parameter: [C++ temp.param]
451f4a2713aSLionel Sambuc ///         type-parameter
452f4a2713aSLionel Sambuc ///         parameter-declaration
453f4a2713aSLionel Sambuc ///
454f4a2713aSLionel Sambuc ///       type-parameter: (see below)
455f4a2713aSLionel Sambuc ///         'class' ...[opt] identifier[opt]
456f4a2713aSLionel Sambuc ///         'class' identifier[opt] '=' type-id
457f4a2713aSLionel Sambuc ///         'typename' ...[opt] identifier[opt]
458f4a2713aSLionel Sambuc ///         'typename' identifier[opt] '=' type-id
459f4a2713aSLionel Sambuc ///         'template' '<' template-parameter-list '>'
460f4a2713aSLionel Sambuc ///               'class' ...[opt] identifier[opt]
461f4a2713aSLionel Sambuc ///         'template' '<' template-parameter-list '>' 'class' identifier[opt]
462f4a2713aSLionel Sambuc ///               = id-expression
ParseTemplateParameter(unsigned Depth,unsigned Position)463f4a2713aSLionel Sambuc Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
464f4a2713aSLionel Sambuc   if (isStartOfTemplateTypeParameter())
465f4a2713aSLionel Sambuc     return ParseTypeParameter(Depth, Position);
466f4a2713aSLionel Sambuc 
467f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_template))
468f4a2713aSLionel Sambuc     return ParseTemplateTemplateParameter(Depth, Position);
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc   // If it's none of the above, then it must be a parameter declaration.
471f4a2713aSLionel Sambuc   // NOTE: This will pick up errors in the closure of the template parameter
472f4a2713aSLionel Sambuc   // list (e.g., template < ; Check here to implement >> style closures.
473f4a2713aSLionel Sambuc   return ParseNonTypeTemplateParameter(Depth, Position);
474f4a2713aSLionel Sambuc }
475f4a2713aSLionel Sambuc 
476f4a2713aSLionel Sambuc /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
477f4a2713aSLionel Sambuc /// Other kinds of template parameters are parsed in
478f4a2713aSLionel Sambuc /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
479f4a2713aSLionel Sambuc ///
480f4a2713aSLionel Sambuc ///       type-parameter:     [C++ temp.param]
481f4a2713aSLionel Sambuc ///         'class' ...[opt][C++0x] identifier[opt]
482f4a2713aSLionel Sambuc ///         'class' identifier[opt] '=' type-id
483f4a2713aSLionel Sambuc ///         'typename' ...[opt][C++0x] identifier[opt]
484f4a2713aSLionel Sambuc ///         'typename' identifier[opt] '=' type-id
ParseTypeParameter(unsigned Depth,unsigned Position)485f4a2713aSLionel Sambuc Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
486f4a2713aSLionel Sambuc   assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) &&
487f4a2713aSLionel Sambuc          "A type-parameter starts with 'class' or 'typename'");
488f4a2713aSLionel Sambuc 
489f4a2713aSLionel Sambuc   // Consume the 'class' or 'typename' keyword.
490f4a2713aSLionel Sambuc   bool TypenameKeyword = Tok.is(tok::kw_typename);
491f4a2713aSLionel Sambuc   SourceLocation KeyLoc = ConsumeToken();
492f4a2713aSLionel Sambuc 
493f4a2713aSLionel Sambuc   // Grab the ellipsis (if given).
494f4a2713aSLionel Sambuc   SourceLocation EllipsisLoc;
495*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
496f4a2713aSLionel Sambuc     Diag(EllipsisLoc,
497f4a2713aSLionel Sambuc          getLangOpts().CPlusPlus11
498f4a2713aSLionel Sambuc            ? diag::warn_cxx98_compat_variadic_templates
499f4a2713aSLionel Sambuc            : diag::ext_variadic_templates);
500f4a2713aSLionel Sambuc   }
501f4a2713aSLionel Sambuc 
502f4a2713aSLionel Sambuc   // Grab the template parameter name (if given)
503f4a2713aSLionel Sambuc   SourceLocation NameLoc;
504*0a6a1f1dSLionel Sambuc   IdentifierInfo *ParamName = nullptr;
505f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier)) {
506f4a2713aSLionel Sambuc     ParamName = Tok.getIdentifierInfo();
507f4a2713aSLionel Sambuc     NameLoc = ConsumeToken();
508f4a2713aSLionel Sambuc   } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
509f4a2713aSLionel Sambuc              Tok.is(tok::greater) || Tok.is(tok::greatergreater)) {
510f4a2713aSLionel Sambuc     // Unnamed template parameter. Don't have to do anything here, just
511f4a2713aSLionel Sambuc     // don't consume this token.
512f4a2713aSLionel Sambuc   } else {
513*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
514*0a6a1f1dSLionel Sambuc     return nullptr;
515f4a2713aSLionel Sambuc   }
516f4a2713aSLionel Sambuc 
517*0a6a1f1dSLionel Sambuc   // Recover from misplaced ellipsis.
518*0a6a1f1dSLionel Sambuc   bool AlreadyHasEllipsis = EllipsisLoc.isValid();
519*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
520*0a6a1f1dSLionel Sambuc     DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
521*0a6a1f1dSLionel Sambuc 
522f4a2713aSLionel Sambuc   // Grab a default argument (if available).
523f4a2713aSLionel Sambuc   // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
524f4a2713aSLionel Sambuc   // we introduce the type parameter into the local scope.
525f4a2713aSLionel Sambuc   SourceLocation EqualLoc;
526f4a2713aSLionel Sambuc   ParsedType DefaultArg;
527*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::equal, EqualLoc))
528*0a6a1f1dSLionel Sambuc     DefaultArg = ParseTypeName(/*Range=*/nullptr,
529f4a2713aSLionel Sambuc                                Declarator::TemplateTypeArgContext).get();
530f4a2713aSLionel Sambuc 
531*0a6a1f1dSLionel Sambuc   return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc,
532*0a6a1f1dSLionel Sambuc                                     KeyLoc, ParamName, NameLoc, Depth, Position,
533*0a6a1f1dSLionel Sambuc                                     EqualLoc, DefaultArg);
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc 
536f4a2713aSLionel Sambuc /// ParseTemplateTemplateParameter - Handle the parsing of template
537f4a2713aSLionel Sambuc /// template parameters.
538f4a2713aSLionel Sambuc ///
539f4a2713aSLionel Sambuc ///       type-parameter:    [C++ temp.param]
540*0a6a1f1dSLionel Sambuc ///         'template' '<' template-parameter-list '>' type-parameter-key
541f4a2713aSLionel Sambuc ///                  ...[opt] identifier[opt]
542*0a6a1f1dSLionel Sambuc ///         'template' '<' template-parameter-list '>' type-parameter-key
543*0a6a1f1dSLionel Sambuc ///                  identifier[opt] = id-expression
544*0a6a1f1dSLionel Sambuc ///       type-parameter-key:
545*0a6a1f1dSLionel Sambuc ///         'class'
546*0a6a1f1dSLionel Sambuc ///         'typename'       [C++1z]
547f4a2713aSLionel Sambuc Decl *
ParseTemplateTemplateParameter(unsigned Depth,unsigned Position)548f4a2713aSLionel Sambuc Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
549f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
550f4a2713aSLionel Sambuc 
551f4a2713aSLionel Sambuc   // Handle the template <...> part.
552f4a2713aSLionel Sambuc   SourceLocation TemplateLoc = ConsumeToken();
553f4a2713aSLionel Sambuc   SmallVector<Decl*,8> TemplateParams;
554f4a2713aSLionel Sambuc   SourceLocation LAngleLoc, RAngleLoc;
555f4a2713aSLionel Sambuc   {
556f4a2713aSLionel Sambuc     ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
557f4a2713aSLionel Sambuc     if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
558f4a2713aSLionel Sambuc                                RAngleLoc)) {
559*0a6a1f1dSLionel Sambuc       return nullptr;
560f4a2713aSLionel Sambuc     }
561f4a2713aSLionel Sambuc   }
562f4a2713aSLionel Sambuc 
563*0a6a1f1dSLionel Sambuc   // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
564f4a2713aSLionel Sambuc   // Generate a meaningful error if the user forgot to put class before the
565f4a2713aSLionel Sambuc   // identifier, comma, or greater. Provide a fixit if the identifier, comma,
566*0a6a1f1dSLionel Sambuc   // or greater appear immediately or after 'struct'. In the latter case,
567*0a6a1f1dSLionel Sambuc   // replace the keyword with 'class'.
568*0a6a1f1dSLionel Sambuc   if (!TryConsumeToken(tok::kw_class)) {
569f4a2713aSLionel Sambuc     bool Replace = Tok.is(tok::kw_typename) || Tok.is(tok::kw_struct);
570*0a6a1f1dSLionel Sambuc     const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
571*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::kw_typename)) {
572*0a6a1f1dSLionel Sambuc       Diag(Tok.getLocation(),
573*0a6a1f1dSLionel Sambuc            getLangOpts().CPlusPlus1z
574*0a6a1f1dSLionel Sambuc                ? diag::warn_cxx14_compat_template_template_param_typename
575*0a6a1f1dSLionel Sambuc                : diag::ext_template_template_param_typename)
576*0a6a1f1dSLionel Sambuc         << (!getLangOpts().CPlusPlus1z
577*0a6a1f1dSLionel Sambuc                 ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
578*0a6a1f1dSLionel Sambuc                 : FixItHint());
579*0a6a1f1dSLionel Sambuc     } else if (Next.is(tok::identifier) || Next.is(tok::comma) ||
580f4a2713aSLionel Sambuc                Next.is(tok::greater) || Next.is(tok::greatergreater) ||
581*0a6a1f1dSLionel Sambuc                Next.is(tok::ellipsis)) {
582f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
583f4a2713aSLionel Sambuc         << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
584f4a2713aSLionel Sambuc                     : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
585*0a6a1f1dSLionel Sambuc     } else
586f4a2713aSLionel Sambuc       Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
587f4a2713aSLionel Sambuc 
588f4a2713aSLionel Sambuc     if (Replace)
589f4a2713aSLionel Sambuc       ConsumeToken();
590*0a6a1f1dSLionel Sambuc   }
591f4a2713aSLionel Sambuc 
592f4a2713aSLionel Sambuc   // Parse the ellipsis, if given.
593f4a2713aSLionel Sambuc   SourceLocation EllipsisLoc;
594*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
595f4a2713aSLionel Sambuc     Diag(EllipsisLoc,
596f4a2713aSLionel Sambuc          getLangOpts().CPlusPlus11
597f4a2713aSLionel Sambuc            ? diag::warn_cxx98_compat_variadic_templates
598f4a2713aSLionel Sambuc            : diag::ext_variadic_templates);
599f4a2713aSLionel Sambuc 
600f4a2713aSLionel Sambuc   // Get the identifier, if given.
601f4a2713aSLionel Sambuc   SourceLocation NameLoc;
602*0a6a1f1dSLionel Sambuc   IdentifierInfo *ParamName = nullptr;
603f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier)) {
604f4a2713aSLionel Sambuc     ParamName = Tok.getIdentifierInfo();
605f4a2713aSLionel Sambuc     NameLoc = ConsumeToken();
606f4a2713aSLionel Sambuc   } else if (Tok.is(tok::equal) || Tok.is(tok::comma) ||
607f4a2713aSLionel Sambuc              Tok.is(tok::greater) || Tok.is(tok::greatergreater)) {
608f4a2713aSLionel Sambuc     // Unnamed template parameter. Don't have to do anything here, just
609f4a2713aSLionel Sambuc     // don't consume this token.
610f4a2713aSLionel Sambuc   } else {
611*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
612*0a6a1f1dSLionel Sambuc     return nullptr;
613f4a2713aSLionel Sambuc   }
614f4a2713aSLionel Sambuc 
615*0a6a1f1dSLionel Sambuc   // Recover from misplaced ellipsis.
616*0a6a1f1dSLionel Sambuc   bool AlreadyHasEllipsis = EllipsisLoc.isValid();
617*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
618*0a6a1f1dSLionel Sambuc     DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
619*0a6a1f1dSLionel Sambuc 
620f4a2713aSLionel Sambuc   TemplateParameterList *ParamList =
621f4a2713aSLionel Sambuc     Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
622f4a2713aSLionel Sambuc                                        TemplateLoc, LAngleLoc,
623f4a2713aSLionel Sambuc                                        TemplateParams.data(),
624f4a2713aSLionel Sambuc                                        TemplateParams.size(),
625f4a2713aSLionel Sambuc                                        RAngleLoc);
626f4a2713aSLionel Sambuc 
627f4a2713aSLionel Sambuc   // Grab a default argument (if available).
628f4a2713aSLionel Sambuc   // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
629f4a2713aSLionel Sambuc   // we introduce the template parameter into the local scope.
630f4a2713aSLionel Sambuc   SourceLocation EqualLoc;
631f4a2713aSLionel Sambuc   ParsedTemplateArgument DefaultArg;
632*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::equal, EqualLoc)) {
633f4a2713aSLionel Sambuc     DefaultArg = ParseTemplateTemplateArgument();
634f4a2713aSLionel Sambuc     if (DefaultArg.isInvalid()) {
635f4a2713aSLionel Sambuc       Diag(Tok.getLocation(),
636f4a2713aSLionel Sambuc            diag::err_default_template_template_parameter_not_template);
637f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::greater, tok::greatergreater,
638f4a2713aSLionel Sambuc                 StopAtSemi | StopBeforeMatch);
639f4a2713aSLionel Sambuc     }
640f4a2713aSLionel Sambuc   }
641f4a2713aSLionel Sambuc 
642f4a2713aSLionel Sambuc   return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
643f4a2713aSLionel Sambuc                                                 ParamList, EllipsisLoc,
644f4a2713aSLionel Sambuc                                                 ParamName, NameLoc, Depth,
645f4a2713aSLionel Sambuc                                                 Position, EqualLoc, DefaultArg);
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc 
648f4a2713aSLionel Sambuc /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
649f4a2713aSLionel Sambuc /// template parameters (e.g., in "template<int Size> class array;").
650f4a2713aSLionel Sambuc ///
651f4a2713aSLionel Sambuc ///       template-parameter:
652f4a2713aSLionel Sambuc ///         ...
653f4a2713aSLionel Sambuc ///         parameter-declaration
654f4a2713aSLionel Sambuc Decl *
ParseNonTypeTemplateParameter(unsigned Depth,unsigned Position)655f4a2713aSLionel Sambuc Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
656f4a2713aSLionel Sambuc   // Parse the declaration-specifiers (i.e., the type).
657f4a2713aSLionel Sambuc   // FIXME: The type should probably be restricted in some way... Not all
658f4a2713aSLionel Sambuc   // declarators (parts of declarators?) are accepted for parameters.
659f4a2713aSLionel Sambuc   DeclSpec DS(AttrFactory);
660f4a2713aSLionel Sambuc   ParseDeclarationSpecifiers(DS);
661f4a2713aSLionel Sambuc 
662f4a2713aSLionel Sambuc   // Parse this as a typename.
663f4a2713aSLionel Sambuc   Declarator ParamDecl(DS, Declarator::TemplateParamContext);
664f4a2713aSLionel Sambuc   ParseDeclarator(ParamDecl);
665f4a2713aSLionel Sambuc   if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
666f4a2713aSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected_template_parameter);
667*0a6a1f1dSLionel Sambuc     return nullptr;
668f4a2713aSLionel Sambuc   }
669f4a2713aSLionel Sambuc 
670*0a6a1f1dSLionel Sambuc   // Recover from misplaced ellipsis.
671*0a6a1f1dSLionel Sambuc   SourceLocation EllipsisLoc;
672*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
673*0a6a1f1dSLionel Sambuc     DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
674*0a6a1f1dSLionel Sambuc 
675f4a2713aSLionel Sambuc   // If there is a default value, parse it.
676f4a2713aSLionel Sambuc   // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
677f4a2713aSLionel Sambuc   // we introduce the template parameter into the local scope.
678f4a2713aSLionel Sambuc   SourceLocation EqualLoc;
679f4a2713aSLionel Sambuc   ExprResult DefaultArg;
680*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::equal, EqualLoc)) {
681f4a2713aSLionel Sambuc     // C++ [temp.param]p15:
682f4a2713aSLionel Sambuc     //   When parsing a default template-argument for a non-type
683f4a2713aSLionel Sambuc     //   template-parameter, the first non-nested > is taken as the
684f4a2713aSLionel Sambuc     //   end of the template-parameter-list rather than a greater-than
685f4a2713aSLionel Sambuc     //   operator.
686f4a2713aSLionel Sambuc     GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
687f4a2713aSLionel Sambuc     EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
688f4a2713aSLionel Sambuc 
689*0a6a1f1dSLionel Sambuc     DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
690f4a2713aSLionel Sambuc     if (DefaultArg.isInvalid())
691f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
692f4a2713aSLionel Sambuc   }
693f4a2713aSLionel Sambuc 
694f4a2713aSLionel Sambuc   // Create the parameter.
695f4a2713aSLionel Sambuc   return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
696f4a2713aSLionel Sambuc                                                Depth, Position, EqualLoc,
697*0a6a1f1dSLionel Sambuc                                                DefaultArg.get());
698*0a6a1f1dSLionel Sambuc }
699*0a6a1f1dSLionel Sambuc 
DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,SourceLocation CorrectLoc,bool AlreadyHasEllipsis,bool IdentifierHasName)700*0a6a1f1dSLionel Sambuc void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
701*0a6a1f1dSLionel Sambuc                                        SourceLocation CorrectLoc,
702*0a6a1f1dSLionel Sambuc                                        bool AlreadyHasEllipsis,
703*0a6a1f1dSLionel Sambuc                                        bool IdentifierHasName) {
704*0a6a1f1dSLionel Sambuc   FixItHint Insertion;
705*0a6a1f1dSLionel Sambuc   if (!AlreadyHasEllipsis)
706*0a6a1f1dSLionel Sambuc     Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
707*0a6a1f1dSLionel Sambuc   Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
708*0a6a1f1dSLionel Sambuc       << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
709*0a6a1f1dSLionel Sambuc       << !IdentifierHasName;
710*0a6a1f1dSLionel Sambuc }
711*0a6a1f1dSLionel Sambuc 
DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,Declarator & D)712*0a6a1f1dSLionel Sambuc void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
713*0a6a1f1dSLionel Sambuc                                                    Declarator &D) {
714*0a6a1f1dSLionel Sambuc   assert(EllipsisLoc.isValid());
715*0a6a1f1dSLionel Sambuc   bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
716*0a6a1f1dSLionel Sambuc   if (!AlreadyHasEllipsis)
717*0a6a1f1dSLionel Sambuc     D.setEllipsisLoc(EllipsisLoc);
718*0a6a1f1dSLionel Sambuc   DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
719*0a6a1f1dSLionel Sambuc                             AlreadyHasEllipsis, D.hasName());
720f4a2713aSLionel Sambuc }
721f4a2713aSLionel Sambuc 
722f4a2713aSLionel Sambuc /// \brief Parses a '>' at the end of a template list.
723f4a2713aSLionel Sambuc ///
724f4a2713aSLionel Sambuc /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
725f4a2713aSLionel Sambuc /// to determine if these tokens were supposed to be a '>' followed by
726f4a2713aSLionel Sambuc /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
727f4a2713aSLionel Sambuc ///
728f4a2713aSLionel Sambuc /// \param RAngleLoc the location of the consumed '>'.
729f4a2713aSLionel Sambuc ///
730f4a2713aSLionel Sambuc /// \param ConsumeLastToken if true, the '>' is not consumed.
731f4a2713aSLionel Sambuc ///
732f4a2713aSLionel Sambuc /// \returns true, if current token does not start with '>', false otherwise.
ParseGreaterThanInTemplateList(SourceLocation & RAngleLoc,bool ConsumeLastToken)733f4a2713aSLionel Sambuc bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc,
734f4a2713aSLionel Sambuc                                             bool ConsumeLastToken) {
735f4a2713aSLionel Sambuc   // What will be left once we've consumed the '>'.
736f4a2713aSLionel Sambuc   tok::TokenKind RemainingToken;
737f4a2713aSLionel Sambuc   const char *ReplacementStr = "> >";
738f4a2713aSLionel Sambuc 
739f4a2713aSLionel Sambuc   switch (Tok.getKind()) {
740f4a2713aSLionel Sambuc   default:
741*0a6a1f1dSLionel Sambuc     Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
742f4a2713aSLionel Sambuc     return true;
743f4a2713aSLionel Sambuc 
744f4a2713aSLionel Sambuc   case tok::greater:
745f4a2713aSLionel Sambuc     // Determine the location of the '>' token. Only consume this token
746f4a2713aSLionel Sambuc     // if the caller asked us to.
747f4a2713aSLionel Sambuc     RAngleLoc = Tok.getLocation();
748f4a2713aSLionel Sambuc     if (ConsumeLastToken)
749f4a2713aSLionel Sambuc       ConsumeToken();
750f4a2713aSLionel Sambuc     return false;
751f4a2713aSLionel Sambuc 
752f4a2713aSLionel Sambuc   case tok::greatergreater:
753f4a2713aSLionel Sambuc     RemainingToken = tok::greater;
754f4a2713aSLionel Sambuc     break;
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc   case tok::greatergreatergreater:
757f4a2713aSLionel Sambuc     RemainingToken = tok::greatergreater;
758f4a2713aSLionel Sambuc     break;
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc   case tok::greaterequal:
761f4a2713aSLionel Sambuc     RemainingToken = tok::equal;
762f4a2713aSLionel Sambuc     ReplacementStr = "> =";
763f4a2713aSLionel Sambuc     break;
764f4a2713aSLionel Sambuc 
765f4a2713aSLionel Sambuc   case tok::greatergreaterequal:
766f4a2713aSLionel Sambuc     RemainingToken = tok::greaterequal;
767f4a2713aSLionel Sambuc     break;
768f4a2713aSLionel Sambuc   }
769f4a2713aSLionel Sambuc 
770f4a2713aSLionel Sambuc   // This template-id is terminated by a token which starts with a '>'. Outside
771f4a2713aSLionel Sambuc   // C++11, this is now error recovery, and in C++11, this is error recovery if
772*0a6a1f1dSLionel Sambuc   // the token isn't '>>' or '>>>'.
773*0a6a1f1dSLionel Sambuc   // '>>>' is for CUDA, where this sequence of characters is parsed into
774*0a6a1f1dSLionel Sambuc   // tok::greatergreatergreater, rather than two separate tokens.
775f4a2713aSLionel Sambuc 
776f4a2713aSLionel Sambuc   RAngleLoc = Tok.getLocation();
777f4a2713aSLionel Sambuc 
778f4a2713aSLionel Sambuc   // The source range of the '>>' or '>=' at the start of the token.
779f4a2713aSLionel Sambuc   CharSourceRange ReplacementRange =
780f4a2713aSLionel Sambuc       CharSourceRange::getCharRange(RAngleLoc,
781f4a2713aSLionel Sambuc           Lexer::AdvanceToTokenCharacter(RAngleLoc, 2, PP.getSourceManager(),
782f4a2713aSLionel Sambuc                                          getLangOpts()));
783f4a2713aSLionel Sambuc 
784f4a2713aSLionel Sambuc   // A hint to put a space between the '>>'s. In order to make the hint as
785f4a2713aSLionel Sambuc   // clear as possible, we include the characters either side of the space in
786f4a2713aSLionel Sambuc   // the replacement, rather than just inserting a space at SecondCharLoc.
787f4a2713aSLionel Sambuc   FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
788f4a2713aSLionel Sambuc                                                  ReplacementStr);
789f4a2713aSLionel Sambuc 
790f4a2713aSLionel Sambuc   // A hint to put another space after the token, if it would otherwise be
791f4a2713aSLionel Sambuc   // lexed differently.
792f4a2713aSLionel Sambuc   FixItHint Hint2;
793f4a2713aSLionel Sambuc   Token Next = NextToken();
794f4a2713aSLionel Sambuc   if ((RemainingToken == tok::greater ||
795f4a2713aSLionel Sambuc        RemainingToken == tok::greatergreater) &&
796f4a2713aSLionel Sambuc       (Next.is(tok::greater) || Next.is(tok::greatergreater) ||
797f4a2713aSLionel Sambuc        Next.is(tok::greatergreatergreater) || Next.is(tok::equal) ||
798f4a2713aSLionel Sambuc        Next.is(tok::greaterequal) || Next.is(tok::greatergreaterequal) ||
799f4a2713aSLionel Sambuc        Next.is(tok::equalequal)) &&
800f4a2713aSLionel Sambuc       areTokensAdjacent(Tok, Next))
801f4a2713aSLionel Sambuc     Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
802f4a2713aSLionel Sambuc 
803f4a2713aSLionel Sambuc   unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
804*0a6a1f1dSLionel Sambuc   if (getLangOpts().CPlusPlus11 &&
805*0a6a1f1dSLionel Sambuc       (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
806f4a2713aSLionel Sambuc     DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
807f4a2713aSLionel Sambuc   else if (Tok.is(tok::greaterequal))
808f4a2713aSLionel Sambuc     DiagId = diag::err_right_angle_bracket_equal_needs_space;
809f4a2713aSLionel Sambuc   Diag(Tok.getLocation(), DiagId) << Hint1 << Hint2;
810f4a2713aSLionel Sambuc 
811f4a2713aSLionel Sambuc   // Strip the initial '>' from the token.
812f4a2713aSLionel Sambuc   if (RemainingToken == tok::equal && Next.is(tok::equal) &&
813f4a2713aSLionel Sambuc       areTokensAdjacent(Tok, Next)) {
814f4a2713aSLionel Sambuc     // Join two adjacent '=' tokens into one, for cases like:
815f4a2713aSLionel Sambuc     //   void (*p)() = f<int>;
816f4a2713aSLionel Sambuc     //   return f<int>==p;
817f4a2713aSLionel Sambuc     ConsumeToken();
818f4a2713aSLionel Sambuc     Tok.setKind(tok::equalequal);
819f4a2713aSLionel Sambuc     Tok.setLength(Tok.getLength() + 1);
820f4a2713aSLionel Sambuc   } else {
821f4a2713aSLionel Sambuc     Tok.setKind(RemainingToken);
822f4a2713aSLionel Sambuc     Tok.setLength(Tok.getLength() - 1);
823f4a2713aSLionel Sambuc   }
824f4a2713aSLionel Sambuc   Tok.setLocation(Lexer::AdvanceToTokenCharacter(RAngleLoc, 1,
825f4a2713aSLionel Sambuc                                                  PP.getSourceManager(),
826f4a2713aSLionel Sambuc                                                  getLangOpts()));
827f4a2713aSLionel Sambuc 
828f4a2713aSLionel Sambuc   if (!ConsumeLastToken) {
829f4a2713aSLionel Sambuc     // Since we're not supposed to consume the '>' token, we need to push
830f4a2713aSLionel Sambuc     // this token and revert the current token back to the '>'.
831f4a2713aSLionel Sambuc     PP.EnterToken(Tok);
832f4a2713aSLionel Sambuc     Tok.setKind(tok::greater);
833f4a2713aSLionel Sambuc     Tok.setLength(1);
834f4a2713aSLionel Sambuc     Tok.setLocation(RAngleLoc);
835f4a2713aSLionel Sambuc   }
836f4a2713aSLionel Sambuc   return false;
837f4a2713aSLionel Sambuc }
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc 
840f4a2713aSLionel Sambuc /// \brief Parses a template-id that after the template name has
841f4a2713aSLionel Sambuc /// already been parsed.
842f4a2713aSLionel Sambuc ///
843f4a2713aSLionel Sambuc /// This routine takes care of parsing the enclosed template argument
844f4a2713aSLionel Sambuc /// list ('<' template-parameter-list [opt] '>') and placing the
845f4a2713aSLionel Sambuc /// results into a form that can be transferred to semantic analysis.
846f4a2713aSLionel Sambuc ///
847f4a2713aSLionel Sambuc /// \param Template the template declaration produced by isTemplateName
848f4a2713aSLionel Sambuc ///
849f4a2713aSLionel Sambuc /// \param TemplateNameLoc the source location of the template name
850f4a2713aSLionel Sambuc ///
851f4a2713aSLionel Sambuc /// \param SS if non-NULL, the nested-name-specifier preceding the
852f4a2713aSLionel Sambuc /// template name.
853f4a2713aSLionel Sambuc ///
854f4a2713aSLionel Sambuc /// \param ConsumeLastToken if true, then we will consume the last
855f4a2713aSLionel Sambuc /// token that forms the template-id. Otherwise, we will leave the
856f4a2713aSLionel Sambuc /// last token in the stream (e.g., so that it can be replaced with an
857f4a2713aSLionel Sambuc /// annotation token).
858f4a2713aSLionel Sambuc bool
ParseTemplateIdAfterTemplateName(TemplateTy Template,SourceLocation TemplateNameLoc,const CXXScopeSpec & SS,bool ConsumeLastToken,SourceLocation & LAngleLoc,TemplateArgList & TemplateArgs,SourceLocation & RAngleLoc)859f4a2713aSLionel Sambuc Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
860f4a2713aSLionel Sambuc                                          SourceLocation TemplateNameLoc,
861f4a2713aSLionel Sambuc                                          const CXXScopeSpec &SS,
862f4a2713aSLionel Sambuc                                          bool ConsumeLastToken,
863f4a2713aSLionel Sambuc                                          SourceLocation &LAngleLoc,
864f4a2713aSLionel Sambuc                                          TemplateArgList &TemplateArgs,
865f4a2713aSLionel Sambuc                                          SourceLocation &RAngleLoc) {
866f4a2713aSLionel Sambuc   assert(Tok.is(tok::less) && "Must have already parsed the template-name");
867f4a2713aSLionel Sambuc 
868f4a2713aSLionel Sambuc   // Consume the '<'.
869f4a2713aSLionel Sambuc   LAngleLoc = ConsumeToken();
870f4a2713aSLionel Sambuc 
871f4a2713aSLionel Sambuc   // Parse the optional template-argument-list.
872f4a2713aSLionel Sambuc   bool Invalid = false;
873f4a2713aSLionel Sambuc   {
874f4a2713aSLionel Sambuc     GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
875f4a2713aSLionel Sambuc     if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
876f4a2713aSLionel Sambuc       Invalid = ParseTemplateArgumentList(TemplateArgs);
877f4a2713aSLionel Sambuc 
878f4a2713aSLionel Sambuc     if (Invalid) {
879f4a2713aSLionel Sambuc       // Try to find the closing '>'.
880f4a2713aSLionel Sambuc       if (ConsumeLastToken)
881f4a2713aSLionel Sambuc         SkipUntil(tok::greater, StopAtSemi);
882f4a2713aSLionel Sambuc       else
883f4a2713aSLionel Sambuc         SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
884f4a2713aSLionel Sambuc       return true;
885f4a2713aSLionel Sambuc     }
886f4a2713aSLionel Sambuc   }
887f4a2713aSLionel Sambuc 
888f4a2713aSLionel Sambuc   return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken);
889f4a2713aSLionel Sambuc }
890f4a2713aSLionel Sambuc 
891f4a2713aSLionel Sambuc /// \brief Replace the tokens that form a simple-template-id with an
892f4a2713aSLionel Sambuc /// annotation token containing the complete template-id.
893f4a2713aSLionel Sambuc ///
894f4a2713aSLionel Sambuc /// The first token in the stream must be the name of a template that
895f4a2713aSLionel Sambuc /// is followed by a '<'. This routine will parse the complete
896f4a2713aSLionel Sambuc /// simple-template-id and replace the tokens with a single annotation
897f4a2713aSLionel Sambuc /// token with one of two different kinds: if the template-id names a
898f4a2713aSLionel Sambuc /// type (and \p AllowTypeAnnotation is true), the annotation token is
899f4a2713aSLionel Sambuc /// a type annotation that includes the optional nested-name-specifier
900f4a2713aSLionel Sambuc /// (\p SS). Otherwise, the annotation token is a template-id
901f4a2713aSLionel Sambuc /// annotation that does not include the optional
902f4a2713aSLionel Sambuc /// nested-name-specifier.
903f4a2713aSLionel Sambuc ///
904f4a2713aSLionel Sambuc /// \param Template  the declaration of the template named by the first
905f4a2713aSLionel Sambuc /// token (an identifier), as returned from \c Action::isTemplateName().
906f4a2713aSLionel Sambuc ///
907f4a2713aSLionel Sambuc /// \param TNK the kind of template that \p Template
908f4a2713aSLionel Sambuc /// refers to, as returned from \c Action::isTemplateName().
909f4a2713aSLionel Sambuc ///
910f4a2713aSLionel Sambuc /// \param SS if non-NULL, the nested-name-specifier that precedes
911f4a2713aSLionel Sambuc /// this template name.
912f4a2713aSLionel Sambuc ///
913f4a2713aSLionel Sambuc /// \param TemplateKWLoc if valid, specifies that this template-id
914f4a2713aSLionel Sambuc /// annotation was preceded by the 'template' keyword and gives the
915f4a2713aSLionel Sambuc /// location of that keyword. If invalid (the default), then this
916f4a2713aSLionel Sambuc /// template-id was not preceded by a 'template' keyword.
917f4a2713aSLionel Sambuc ///
918f4a2713aSLionel Sambuc /// \param AllowTypeAnnotation if true (the default), then a
919f4a2713aSLionel Sambuc /// simple-template-id that refers to a class template, template
920f4a2713aSLionel Sambuc /// template parameter, or other template that produces a type will be
921f4a2713aSLionel Sambuc /// replaced with a type annotation token. Otherwise, the
922f4a2713aSLionel Sambuc /// simple-template-id is always replaced with a template-id
923f4a2713aSLionel Sambuc /// annotation token.
924f4a2713aSLionel Sambuc ///
925f4a2713aSLionel Sambuc /// If an unrecoverable parse error occurs and no annotation token can be
926f4a2713aSLionel Sambuc /// formed, this function returns true.
927f4a2713aSLionel Sambuc ///
AnnotateTemplateIdToken(TemplateTy Template,TemplateNameKind TNK,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,UnqualifiedId & TemplateName,bool AllowTypeAnnotation)928f4a2713aSLionel Sambuc bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
929f4a2713aSLionel Sambuc                                      CXXScopeSpec &SS,
930f4a2713aSLionel Sambuc                                      SourceLocation TemplateKWLoc,
931f4a2713aSLionel Sambuc                                      UnqualifiedId &TemplateName,
932f4a2713aSLionel Sambuc                                      bool AllowTypeAnnotation) {
933f4a2713aSLionel Sambuc   assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
934f4a2713aSLionel Sambuc   assert(Template && Tok.is(tok::less) &&
935f4a2713aSLionel Sambuc          "Parser isn't at the beginning of a template-id");
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc   // Consume the template-name.
938f4a2713aSLionel Sambuc   SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
939f4a2713aSLionel Sambuc 
940f4a2713aSLionel Sambuc   // Parse the enclosed template argument list.
941f4a2713aSLionel Sambuc   SourceLocation LAngleLoc, RAngleLoc;
942f4a2713aSLionel Sambuc   TemplateArgList TemplateArgs;
943f4a2713aSLionel Sambuc   bool Invalid = ParseTemplateIdAfterTemplateName(Template,
944f4a2713aSLionel Sambuc                                                   TemplateNameLoc,
945f4a2713aSLionel Sambuc                                                   SS, false, LAngleLoc,
946f4a2713aSLionel Sambuc                                                   TemplateArgs,
947f4a2713aSLionel Sambuc                                                   RAngleLoc);
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc   if (Invalid) {
950f4a2713aSLionel Sambuc     // If we failed to parse the template ID but skipped ahead to a >, we're not
951f4a2713aSLionel Sambuc     // going to be able to form a token annotation.  Eat the '>' if present.
952*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::greater);
953f4a2713aSLionel Sambuc     return true;
954f4a2713aSLionel Sambuc   }
955f4a2713aSLionel Sambuc 
956f4a2713aSLionel Sambuc   ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
957f4a2713aSLionel Sambuc 
958f4a2713aSLionel Sambuc   // Build the annotation token.
959f4a2713aSLionel Sambuc   if (TNK == TNK_Type_template && AllowTypeAnnotation) {
960f4a2713aSLionel Sambuc     TypeResult Type
961f4a2713aSLionel Sambuc       = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
962f4a2713aSLionel Sambuc                                     Template, TemplateNameLoc,
963f4a2713aSLionel Sambuc                                     LAngleLoc, TemplateArgsPtr, RAngleLoc);
964f4a2713aSLionel Sambuc     if (Type.isInvalid()) {
965f4a2713aSLionel Sambuc       // If we failed to parse the template ID but skipped ahead to a >, we're not
966f4a2713aSLionel Sambuc       // going to be able to form a token annotation.  Eat the '>' if present.
967*0a6a1f1dSLionel Sambuc       TryConsumeToken(tok::greater);
968f4a2713aSLionel Sambuc       return true;
969f4a2713aSLionel Sambuc     }
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc     Tok.setKind(tok::annot_typename);
972f4a2713aSLionel Sambuc     setTypeAnnotation(Tok, Type.get());
973f4a2713aSLionel Sambuc     if (SS.isNotEmpty())
974f4a2713aSLionel Sambuc       Tok.setLocation(SS.getBeginLoc());
975f4a2713aSLionel Sambuc     else if (TemplateKWLoc.isValid())
976f4a2713aSLionel Sambuc       Tok.setLocation(TemplateKWLoc);
977f4a2713aSLionel Sambuc     else
978f4a2713aSLionel Sambuc       Tok.setLocation(TemplateNameLoc);
979f4a2713aSLionel Sambuc   } else {
980f4a2713aSLionel Sambuc     // Build a template-id annotation token that can be processed
981f4a2713aSLionel Sambuc     // later.
982f4a2713aSLionel Sambuc     Tok.setKind(tok::annot_template_id);
983f4a2713aSLionel Sambuc     TemplateIdAnnotation *TemplateId
984f4a2713aSLionel Sambuc       = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
985f4a2713aSLionel Sambuc     TemplateId->TemplateNameLoc = TemplateNameLoc;
986f4a2713aSLionel Sambuc     if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
987f4a2713aSLionel Sambuc       TemplateId->Name = TemplateName.Identifier;
988f4a2713aSLionel Sambuc       TemplateId->Operator = OO_None;
989f4a2713aSLionel Sambuc     } else {
990*0a6a1f1dSLionel Sambuc       TemplateId->Name = nullptr;
991f4a2713aSLionel Sambuc       TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
992f4a2713aSLionel Sambuc     }
993f4a2713aSLionel Sambuc     TemplateId->SS = SS;
994f4a2713aSLionel Sambuc     TemplateId->TemplateKWLoc = TemplateKWLoc;
995f4a2713aSLionel Sambuc     TemplateId->Template = Template;
996f4a2713aSLionel Sambuc     TemplateId->Kind = TNK;
997f4a2713aSLionel Sambuc     TemplateId->LAngleLoc = LAngleLoc;
998f4a2713aSLionel Sambuc     TemplateId->RAngleLoc = RAngleLoc;
999f4a2713aSLionel Sambuc     ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
1000f4a2713aSLionel Sambuc     for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
1001f4a2713aSLionel Sambuc       Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
1002f4a2713aSLionel Sambuc     Tok.setAnnotationValue(TemplateId);
1003f4a2713aSLionel Sambuc     if (TemplateKWLoc.isValid())
1004f4a2713aSLionel Sambuc       Tok.setLocation(TemplateKWLoc);
1005f4a2713aSLionel Sambuc     else
1006f4a2713aSLionel Sambuc       Tok.setLocation(TemplateNameLoc);
1007f4a2713aSLionel Sambuc   }
1008f4a2713aSLionel Sambuc 
1009f4a2713aSLionel Sambuc   // Common fields for the annotation token
1010f4a2713aSLionel Sambuc   Tok.setAnnotationEndLoc(RAngleLoc);
1011f4a2713aSLionel Sambuc 
1012f4a2713aSLionel Sambuc   // In case the tokens were cached, have Preprocessor replace them with the
1013f4a2713aSLionel Sambuc   // annotation token.
1014f4a2713aSLionel Sambuc   PP.AnnotateCachedTokens(Tok);
1015f4a2713aSLionel Sambuc   return false;
1016f4a2713aSLionel Sambuc }
1017f4a2713aSLionel Sambuc 
1018f4a2713aSLionel Sambuc /// \brief Replaces a template-id annotation token with a type
1019f4a2713aSLionel Sambuc /// annotation token.
1020f4a2713aSLionel Sambuc ///
1021f4a2713aSLionel Sambuc /// If there was a failure when forming the type from the template-id,
1022f4a2713aSLionel Sambuc /// a type annotation token will still be created, but will have a
1023f4a2713aSLionel Sambuc /// NULL type pointer to signify an error.
AnnotateTemplateIdTokenAsType()1024f4a2713aSLionel Sambuc void Parser::AnnotateTemplateIdTokenAsType() {
1025f4a2713aSLionel Sambuc   assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
1026f4a2713aSLionel Sambuc 
1027f4a2713aSLionel Sambuc   TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1028f4a2713aSLionel Sambuc   assert((TemplateId->Kind == TNK_Type_template ||
1029f4a2713aSLionel Sambuc           TemplateId->Kind == TNK_Dependent_template_name) &&
1030f4a2713aSLionel Sambuc          "Only works for type and dependent templates");
1031f4a2713aSLionel Sambuc 
1032f4a2713aSLionel Sambuc   ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1033f4a2713aSLionel Sambuc                                      TemplateId->NumArgs);
1034f4a2713aSLionel Sambuc 
1035f4a2713aSLionel Sambuc   TypeResult Type
1036f4a2713aSLionel Sambuc     = Actions.ActOnTemplateIdType(TemplateId->SS,
1037f4a2713aSLionel Sambuc                                   TemplateId->TemplateKWLoc,
1038f4a2713aSLionel Sambuc                                   TemplateId->Template,
1039f4a2713aSLionel Sambuc                                   TemplateId->TemplateNameLoc,
1040f4a2713aSLionel Sambuc                                   TemplateId->LAngleLoc,
1041f4a2713aSLionel Sambuc                                   TemplateArgsPtr,
1042f4a2713aSLionel Sambuc                                   TemplateId->RAngleLoc);
1043f4a2713aSLionel Sambuc   // Create the new "type" annotation token.
1044f4a2713aSLionel Sambuc   Tok.setKind(tok::annot_typename);
1045f4a2713aSLionel Sambuc   setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
1046f4a2713aSLionel Sambuc   if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
1047f4a2713aSLionel Sambuc     Tok.setLocation(TemplateId->SS.getBeginLoc());
1048f4a2713aSLionel Sambuc   // End location stays the same
1049f4a2713aSLionel Sambuc 
1050f4a2713aSLionel Sambuc   // Replace the template-id annotation token, and possible the scope-specifier
1051f4a2713aSLionel Sambuc   // that precedes it, with the typename annotation token.
1052f4a2713aSLionel Sambuc   PP.AnnotateCachedTokens(Tok);
1053f4a2713aSLionel Sambuc }
1054f4a2713aSLionel Sambuc 
1055f4a2713aSLionel Sambuc /// \brief Determine whether the given token can end a template argument.
isEndOfTemplateArgument(Token Tok)1056f4a2713aSLionel Sambuc static bool isEndOfTemplateArgument(Token Tok) {
1057f4a2713aSLionel Sambuc   return Tok.is(tok::comma) || Tok.is(tok::greater) ||
1058f4a2713aSLionel Sambuc          Tok.is(tok::greatergreater);
1059f4a2713aSLionel Sambuc }
1060f4a2713aSLionel Sambuc 
1061f4a2713aSLionel Sambuc /// \brief Parse a C++ template template argument.
ParseTemplateTemplateArgument()1062f4a2713aSLionel Sambuc ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
1063f4a2713aSLionel Sambuc   if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
1064f4a2713aSLionel Sambuc       !Tok.is(tok::annot_cxxscope))
1065f4a2713aSLionel Sambuc     return ParsedTemplateArgument();
1066f4a2713aSLionel Sambuc 
1067f4a2713aSLionel Sambuc   // C++0x [temp.arg.template]p1:
1068f4a2713aSLionel Sambuc   //   A template-argument for a template template-parameter shall be the name
1069f4a2713aSLionel Sambuc   //   of a class template or an alias template, expressed as id-expression.
1070f4a2713aSLionel Sambuc   //
1071f4a2713aSLionel Sambuc   // We parse an id-expression that refers to a class template or alias
1072f4a2713aSLionel Sambuc   // template. The grammar we parse is:
1073f4a2713aSLionel Sambuc   //
1074f4a2713aSLionel Sambuc   //   nested-name-specifier[opt] template[opt] identifier ...[opt]
1075f4a2713aSLionel Sambuc   //
1076f4a2713aSLionel Sambuc   // followed by a token that terminates a template argument, such as ',',
1077f4a2713aSLionel Sambuc   // '>', or (in some cases) '>>'.
1078f4a2713aSLionel Sambuc   CXXScopeSpec SS; // nested-name-specifier, if present
1079f4a2713aSLionel Sambuc   ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1080f4a2713aSLionel Sambuc                                  /*EnteringContext=*/false);
1081f4a2713aSLionel Sambuc 
1082f4a2713aSLionel Sambuc   ParsedTemplateArgument Result;
1083f4a2713aSLionel Sambuc   SourceLocation EllipsisLoc;
1084f4a2713aSLionel Sambuc   if (SS.isSet() && Tok.is(tok::kw_template)) {
1085f4a2713aSLionel Sambuc     // Parse the optional 'template' keyword following the
1086f4a2713aSLionel Sambuc     // nested-name-specifier.
1087f4a2713aSLionel Sambuc     SourceLocation TemplateKWLoc = ConsumeToken();
1088f4a2713aSLionel Sambuc 
1089f4a2713aSLionel Sambuc     if (Tok.is(tok::identifier)) {
1090f4a2713aSLionel Sambuc       // We appear to have a dependent template name.
1091f4a2713aSLionel Sambuc       UnqualifiedId Name;
1092f4a2713aSLionel Sambuc       Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1093f4a2713aSLionel Sambuc       ConsumeToken(); // the identifier
1094f4a2713aSLionel Sambuc 
1095*0a6a1f1dSLionel Sambuc       TryConsumeToken(tok::ellipsis, EllipsisLoc);
1096f4a2713aSLionel Sambuc 
1097f4a2713aSLionel Sambuc       // If the next token signals the end of a template argument,
1098f4a2713aSLionel Sambuc       // then we have a dependent template name that could be a template
1099f4a2713aSLionel Sambuc       // template argument.
1100f4a2713aSLionel Sambuc       TemplateTy Template;
1101f4a2713aSLionel Sambuc       if (isEndOfTemplateArgument(Tok) &&
1102f4a2713aSLionel Sambuc           Actions.ActOnDependentTemplateName(getCurScope(),
1103f4a2713aSLionel Sambuc                                              SS, TemplateKWLoc, Name,
1104f4a2713aSLionel Sambuc                                              /*ObjectType=*/ ParsedType(),
1105f4a2713aSLionel Sambuc                                              /*EnteringContext=*/false,
1106f4a2713aSLionel Sambuc                                              Template))
1107f4a2713aSLionel Sambuc         Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1108f4a2713aSLionel Sambuc     }
1109f4a2713aSLionel Sambuc   } else if (Tok.is(tok::identifier)) {
1110f4a2713aSLionel Sambuc     // We may have a (non-dependent) template name.
1111f4a2713aSLionel Sambuc     TemplateTy Template;
1112f4a2713aSLionel Sambuc     UnqualifiedId Name;
1113f4a2713aSLionel Sambuc     Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1114f4a2713aSLionel Sambuc     ConsumeToken(); // the identifier
1115f4a2713aSLionel Sambuc 
1116*0a6a1f1dSLionel Sambuc     TryConsumeToken(tok::ellipsis, EllipsisLoc);
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc     if (isEndOfTemplateArgument(Tok)) {
1119f4a2713aSLionel Sambuc       bool MemberOfUnknownSpecialization;
1120f4a2713aSLionel Sambuc       TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
1121f4a2713aSLionel Sambuc                                                /*hasTemplateKeyword=*/false,
1122f4a2713aSLionel Sambuc                                                     Name,
1123f4a2713aSLionel Sambuc                                                /*ObjectType=*/ ParsedType(),
1124f4a2713aSLionel Sambuc                                                     /*EnteringContext=*/false,
1125f4a2713aSLionel Sambuc                                                     Template,
1126f4a2713aSLionel Sambuc                                                 MemberOfUnknownSpecialization);
1127f4a2713aSLionel Sambuc       if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
1128f4a2713aSLionel Sambuc         // We have an id-expression that refers to a class template or
1129f4a2713aSLionel Sambuc         // (C++0x) alias template.
1130f4a2713aSLionel Sambuc         Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
1131f4a2713aSLionel Sambuc       }
1132f4a2713aSLionel Sambuc     }
1133f4a2713aSLionel Sambuc   }
1134f4a2713aSLionel Sambuc 
1135f4a2713aSLionel Sambuc   // If this is a pack expansion, build it as such.
1136f4a2713aSLionel Sambuc   if (EllipsisLoc.isValid() && !Result.isInvalid())
1137f4a2713aSLionel Sambuc     Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
1138f4a2713aSLionel Sambuc 
1139f4a2713aSLionel Sambuc   return Result;
1140f4a2713aSLionel Sambuc }
1141f4a2713aSLionel Sambuc 
1142f4a2713aSLionel Sambuc /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
1143f4a2713aSLionel Sambuc ///
1144f4a2713aSLionel Sambuc ///       template-argument: [C++ 14.2]
1145f4a2713aSLionel Sambuc ///         constant-expression
1146f4a2713aSLionel Sambuc ///         type-id
1147f4a2713aSLionel Sambuc ///         id-expression
ParseTemplateArgument()1148f4a2713aSLionel Sambuc ParsedTemplateArgument Parser::ParseTemplateArgument() {
1149f4a2713aSLionel Sambuc   // C++ [temp.arg]p2:
1150f4a2713aSLionel Sambuc   //   In a template-argument, an ambiguity between a type-id and an
1151f4a2713aSLionel Sambuc   //   expression is resolved to a type-id, regardless of the form of
1152f4a2713aSLionel Sambuc   //   the corresponding template-parameter.
1153f4a2713aSLionel Sambuc   //
1154f4a2713aSLionel Sambuc   // Therefore, we initially try to parse a type-id.
1155f4a2713aSLionel Sambuc   if (isCXXTypeId(TypeIdAsTemplateArgument)) {
1156f4a2713aSLionel Sambuc     SourceLocation Loc = Tok.getLocation();
1157*0a6a1f1dSLionel Sambuc     TypeResult TypeArg = ParseTypeName(/*Range=*/nullptr,
1158f4a2713aSLionel Sambuc                                        Declarator::TemplateTypeArgContext);
1159f4a2713aSLionel Sambuc     if (TypeArg.isInvalid())
1160f4a2713aSLionel Sambuc       return ParsedTemplateArgument();
1161f4a2713aSLionel Sambuc 
1162f4a2713aSLionel Sambuc     return ParsedTemplateArgument(ParsedTemplateArgument::Type,
1163f4a2713aSLionel Sambuc                                   TypeArg.get().getAsOpaquePtr(),
1164f4a2713aSLionel Sambuc                                   Loc);
1165f4a2713aSLionel Sambuc   }
1166f4a2713aSLionel Sambuc 
1167f4a2713aSLionel Sambuc   // Try to parse a template template argument.
1168f4a2713aSLionel Sambuc   {
1169f4a2713aSLionel Sambuc     TentativeParsingAction TPA(*this);
1170f4a2713aSLionel Sambuc 
1171f4a2713aSLionel Sambuc     ParsedTemplateArgument TemplateTemplateArgument
1172f4a2713aSLionel Sambuc       = ParseTemplateTemplateArgument();
1173f4a2713aSLionel Sambuc     if (!TemplateTemplateArgument.isInvalid()) {
1174f4a2713aSLionel Sambuc       TPA.Commit();
1175f4a2713aSLionel Sambuc       return TemplateTemplateArgument;
1176f4a2713aSLionel Sambuc     }
1177f4a2713aSLionel Sambuc 
1178f4a2713aSLionel Sambuc     // Revert this tentative parse to parse a non-type template argument.
1179f4a2713aSLionel Sambuc     TPA.Revert();
1180f4a2713aSLionel Sambuc   }
1181f4a2713aSLionel Sambuc 
1182f4a2713aSLionel Sambuc   // Parse a non-type template argument.
1183f4a2713aSLionel Sambuc   SourceLocation Loc = Tok.getLocation();
1184f4a2713aSLionel Sambuc   ExprResult ExprArg = ParseConstantExpression(MaybeTypeCast);
1185f4a2713aSLionel Sambuc   if (ExprArg.isInvalid() || !ExprArg.get())
1186f4a2713aSLionel Sambuc     return ParsedTemplateArgument();
1187f4a2713aSLionel Sambuc 
1188f4a2713aSLionel Sambuc   return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
1189*0a6a1f1dSLionel Sambuc                                 ExprArg.get(), Loc);
1190f4a2713aSLionel Sambuc }
1191f4a2713aSLionel Sambuc 
1192f4a2713aSLionel Sambuc /// \brief Determine whether the current tokens can only be parsed as a
1193f4a2713aSLionel Sambuc /// template argument list (starting with the '<') and never as a '<'
1194f4a2713aSLionel Sambuc /// expression.
IsTemplateArgumentList(unsigned Skip)1195f4a2713aSLionel Sambuc bool Parser::IsTemplateArgumentList(unsigned Skip) {
1196f4a2713aSLionel Sambuc   struct AlwaysRevertAction : TentativeParsingAction {
1197f4a2713aSLionel Sambuc     AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
1198f4a2713aSLionel Sambuc     ~AlwaysRevertAction() { Revert(); }
1199f4a2713aSLionel Sambuc   } Tentative(*this);
1200f4a2713aSLionel Sambuc 
1201f4a2713aSLionel Sambuc   while (Skip) {
1202f4a2713aSLionel Sambuc     ConsumeToken();
1203f4a2713aSLionel Sambuc     --Skip;
1204f4a2713aSLionel Sambuc   }
1205f4a2713aSLionel Sambuc 
1206f4a2713aSLionel Sambuc   // '<'
1207*0a6a1f1dSLionel Sambuc   if (!TryConsumeToken(tok::less))
1208f4a2713aSLionel Sambuc     return false;
1209f4a2713aSLionel Sambuc 
1210f4a2713aSLionel Sambuc   // An empty template argument list.
1211f4a2713aSLionel Sambuc   if (Tok.is(tok::greater))
1212f4a2713aSLionel Sambuc     return true;
1213f4a2713aSLionel Sambuc 
1214f4a2713aSLionel Sambuc   // See whether we have declaration specifiers, which indicate a type.
1215*0a6a1f1dSLionel Sambuc   while (isCXXDeclarationSpecifier() == TPResult::True)
1216f4a2713aSLionel Sambuc     ConsumeToken();
1217f4a2713aSLionel Sambuc 
1218f4a2713aSLionel Sambuc   // If we have a '>' or a ',' then this is a template argument list.
1219f4a2713aSLionel Sambuc   return Tok.is(tok::greater) || Tok.is(tok::comma);
1220f4a2713aSLionel Sambuc }
1221f4a2713aSLionel Sambuc 
1222f4a2713aSLionel Sambuc /// ParseTemplateArgumentList - Parse a C++ template-argument-list
1223f4a2713aSLionel Sambuc /// (C++ [temp.names]). Returns true if there was an error.
1224f4a2713aSLionel Sambuc ///
1225f4a2713aSLionel Sambuc ///       template-argument-list: [C++ 14.2]
1226f4a2713aSLionel Sambuc ///         template-argument
1227f4a2713aSLionel Sambuc ///         template-argument-list ',' template-argument
1228f4a2713aSLionel Sambuc bool
ParseTemplateArgumentList(TemplateArgList & TemplateArgs)1229f4a2713aSLionel Sambuc Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
1230f4a2713aSLionel Sambuc   // Template argument lists are constant-evaluation contexts.
1231f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated);
1232*0a6a1f1dSLionel Sambuc   ColonProtectionRAIIObject ColonProtection(*this, false);
1233f4a2713aSLionel Sambuc 
1234*0a6a1f1dSLionel Sambuc   do {
1235f4a2713aSLionel Sambuc     ParsedTemplateArgument Arg = ParseTemplateArgument();
1236*0a6a1f1dSLionel Sambuc     SourceLocation EllipsisLoc;
1237*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
1238f4a2713aSLionel Sambuc       Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
1239f4a2713aSLionel Sambuc 
1240f4a2713aSLionel Sambuc     if (Arg.isInvalid()) {
1241f4a2713aSLionel Sambuc       SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
1242f4a2713aSLionel Sambuc       return true;
1243f4a2713aSLionel Sambuc     }
1244f4a2713aSLionel Sambuc 
1245f4a2713aSLionel Sambuc     // Save this template argument.
1246f4a2713aSLionel Sambuc     TemplateArgs.push_back(Arg);
1247f4a2713aSLionel Sambuc 
1248f4a2713aSLionel Sambuc     // If the next token is a comma, consume it and keep reading
1249f4a2713aSLionel Sambuc     // arguments.
1250*0a6a1f1dSLionel Sambuc   } while (TryConsumeToken(tok::comma));
1251f4a2713aSLionel Sambuc 
1252f4a2713aSLionel Sambuc   return false;
1253f4a2713aSLionel Sambuc }
1254f4a2713aSLionel Sambuc 
1255f4a2713aSLionel Sambuc /// \brief Parse a C++ explicit template instantiation
1256f4a2713aSLionel Sambuc /// (C++ [temp.explicit]).
1257f4a2713aSLionel Sambuc ///
1258f4a2713aSLionel Sambuc ///       explicit-instantiation:
1259f4a2713aSLionel Sambuc ///         'extern' [opt] 'template' declaration
1260f4a2713aSLionel Sambuc ///
1261f4a2713aSLionel Sambuc /// Note that the 'extern' is a GNU extension and C++11 feature.
ParseExplicitInstantiation(unsigned Context,SourceLocation ExternLoc,SourceLocation TemplateLoc,SourceLocation & DeclEnd,AccessSpecifier AS)1262f4a2713aSLionel Sambuc Decl *Parser::ParseExplicitInstantiation(unsigned Context,
1263f4a2713aSLionel Sambuc                                          SourceLocation ExternLoc,
1264f4a2713aSLionel Sambuc                                          SourceLocation TemplateLoc,
1265f4a2713aSLionel Sambuc                                          SourceLocation &DeclEnd,
1266f4a2713aSLionel Sambuc                                          AccessSpecifier AS) {
1267f4a2713aSLionel Sambuc   // This isn't really required here.
1268f4a2713aSLionel Sambuc   ParsingDeclRAIIObject
1269f4a2713aSLionel Sambuc     ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
1270f4a2713aSLionel Sambuc 
1271f4a2713aSLionel Sambuc   return ParseSingleDeclarationAfterTemplate(Context,
1272f4a2713aSLionel Sambuc                                              ParsedTemplateInfo(ExternLoc,
1273f4a2713aSLionel Sambuc                                                                 TemplateLoc),
1274f4a2713aSLionel Sambuc                                              ParsingTemplateParams,
1275f4a2713aSLionel Sambuc                                              DeclEnd, AS);
1276f4a2713aSLionel Sambuc }
1277f4a2713aSLionel Sambuc 
getSourceRange() const1278f4a2713aSLionel Sambuc SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
1279f4a2713aSLionel Sambuc   if (TemplateParams)
1280f4a2713aSLionel Sambuc     return getTemplateParamsRange(TemplateParams->data(),
1281f4a2713aSLionel Sambuc                                   TemplateParams->size());
1282f4a2713aSLionel Sambuc 
1283f4a2713aSLionel Sambuc   SourceRange R(TemplateLoc);
1284f4a2713aSLionel Sambuc   if (ExternLoc.isValid())
1285f4a2713aSLionel Sambuc     R.setBegin(ExternLoc);
1286f4a2713aSLionel Sambuc   return R;
1287f4a2713aSLionel Sambuc }
1288f4a2713aSLionel Sambuc 
LateTemplateParserCallback(void * P,LateParsedTemplate & LPT)1289f4a2713aSLionel Sambuc void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
1290f4a2713aSLionel Sambuc   ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
1291f4a2713aSLionel Sambuc }
1292f4a2713aSLionel Sambuc 
1293f4a2713aSLionel Sambuc /// \brief Late parse a C++ function template in Microsoft mode.
ParseLateTemplatedFuncDef(LateParsedTemplate & LPT)1294f4a2713aSLionel Sambuc void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
1295f4a2713aSLionel Sambuc   if (!LPT.D)
1296f4a2713aSLionel Sambuc      return;
1297f4a2713aSLionel Sambuc 
1298f4a2713aSLionel Sambuc   // Get the FunctionDecl.
1299*0a6a1f1dSLionel Sambuc   FunctionDecl *FunD = LPT.D->getAsFunction();
1300f4a2713aSLionel Sambuc   // Track template parameter depth.
1301f4a2713aSLionel Sambuc   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1302f4a2713aSLionel Sambuc 
1303f4a2713aSLionel Sambuc   // To restore the context after late parsing.
1304f4a2713aSLionel Sambuc   Sema::ContextRAII GlobalSavedContext(Actions, Actions.CurContext);
1305f4a2713aSLionel Sambuc 
1306f4a2713aSLionel Sambuc   SmallVector<ParseScope*, 4> TemplateParamScopeStack;
1307f4a2713aSLionel Sambuc 
1308f4a2713aSLionel Sambuc   // Get the list of DeclContexts to reenter.
1309f4a2713aSLionel Sambuc   SmallVector<DeclContext*, 4> DeclContextsToReenter;
1310*0a6a1f1dSLionel Sambuc   DeclContext *DD = FunD;
1311f4a2713aSLionel Sambuc   while (DD && !DD->isTranslationUnit()) {
1312f4a2713aSLionel Sambuc     DeclContextsToReenter.push_back(DD);
1313f4a2713aSLionel Sambuc     DD = DD->getLexicalParent();
1314f4a2713aSLionel Sambuc   }
1315f4a2713aSLionel Sambuc 
1316f4a2713aSLionel Sambuc   // Reenter template scopes from outermost to innermost.
1317f4a2713aSLionel Sambuc   SmallVectorImpl<DeclContext *>::reverse_iterator II =
1318f4a2713aSLionel Sambuc       DeclContextsToReenter.rbegin();
1319f4a2713aSLionel Sambuc   for (; II != DeclContextsToReenter.rend(); ++II) {
1320*0a6a1f1dSLionel Sambuc     TemplateParamScopeStack.push_back(new ParseScope(this,
1321*0a6a1f1dSLionel Sambuc           Scope::TemplateParamScope));
1322*0a6a1f1dSLionel Sambuc     unsigned NumParamLists =
1323*0a6a1f1dSLionel Sambuc       Actions.ActOnReenterTemplateScope(getCurScope(), cast<Decl>(*II));
1324*0a6a1f1dSLionel Sambuc     CurTemplateDepthTracker.addDepth(NumParamLists);
1325*0a6a1f1dSLionel Sambuc     if (*II != FunD) {
1326f4a2713aSLionel Sambuc       TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope));
1327f4a2713aSLionel Sambuc       Actions.PushDeclContext(Actions.getCurScope(), *II);
1328f4a2713aSLionel Sambuc     }
1329f4a2713aSLionel Sambuc   }
1330f4a2713aSLionel Sambuc 
1331f4a2713aSLionel Sambuc   assert(!LPT.Toks.empty() && "Empty body!");
1332f4a2713aSLionel Sambuc 
1333f4a2713aSLionel Sambuc   // Append the current token at the end of the new token stream so that it
1334f4a2713aSLionel Sambuc   // doesn't get lost.
1335f4a2713aSLionel Sambuc   LPT.Toks.push_back(Tok);
1336f4a2713aSLionel Sambuc   PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false);
1337f4a2713aSLionel Sambuc 
1338f4a2713aSLionel Sambuc   // Consume the previously pushed token.
1339f4a2713aSLionel Sambuc   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1340f4a2713aSLionel Sambuc   assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
1341f4a2713aSLionel Sambuc          && "Inline method not starting with '{', ':' or 'try'");
1342f4a2713aSLionel Sambuc 
1343f4a2713aSLionel Sambuc   // Parse the method body. Function body parsing code is similar enough
1344f4a2713aSLionel Sambuc   // to be re-used for method bodies as well.
1345f4a2713aSLionel Sambuc   ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
1346f4a2713aSLionel Sambuc 
1347f4a2713aSLionel Sambuc   // Recreate the containing function DeclContext.
1348*0a6a1f1dSLionel Sambuc   Sema::ContextRAII FunctionSavedContext(Actions,
1349*0a6a1f1dSLionel Sambuc                                          Actions.getContainingDC(FunD));
1350f4a2713aSLionel Sambuc 
1351f4a2713aSLionel Sambuc   Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
1352f4a2713aSLionel Sambuc 
1353f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_try)) {
1354f4a2713aSLionel Sambuc     ParseFunctionTryBlock(LPT.D, FnScope);
1355f4a2713aSLionel Sambuc   } else {
1356f4a2713aSLionel Sambuc     if (Tok.is(tok::colon))
1357f4a2713aSLionel Sambuc       ParseConstructorInitializer(LPT.D);
1358f4a2713aSLionel Sambuc     else
1359f4a2713aSLionel Sambuc       Actions.ActOnDefaultCtorInitializers(LPT.D);
1360f4a2713aSLionel Sambuc 
1361f4a2713aSLionel Sambuc     if (Tok.is(tok::l_brace)) {
1362*0a6a1f1dSLionel Sambuc       assert((!isa<FunctionTemplateDecl>(LPT.D) ||
1363*0a6a1f1dSLionel Sambuc               cast<FunctionTemplateDecl>(LPT.D)
1364*0a6a1f1dSLionel Sambuc                       ->getTemplateParameters()
1365*0a6a1f1dSLionel Sambuc                       ->getDepth() == TemplateParameterDepth - 1) &&
1366f4a2713aSLionel Sambuc              "TemplateParameterDepth should be greater than the depth of "
1367f4a2713aSLionel Sambuc              "current template being instantiated!");
1368f4a2713aSLionel Sambuc       ParseFunctionStatementBody(LPT.D, FnScope);
1369f4a2713aSLionel Sambuc       Actions.UnmarkAsLateParsedTemplate(FunD);
1370f4a2713aSLionel Sambuc     } else
1371*0a6a1f1dSLionel Sambuc       Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
1372f4a2713aSLionel Sambuc   }
1373f4a2713aSLionel Sambuc 
1374f4a2713aSLionel Sambuc   // Exit scopes.
1375f4a2713aSLionel Sambuc   FnScope.Exit();
1376f4a2713aSLionel Sambuc   SmallVectorImpl<ParseScope *>::reverse_iterator I =
1377f4a2713aSLionel Sambuc    TemplateParamScopeStack.rbegin();
1378f4a2713aSLionel Sambuc   for (; I != TemplateParamScopeStack.rend(); ++I)
1379f4a2713aSLionel Sambuc     delete *I;
1380f4a2713aSLionel Sambuc }
1381f4a2713aSLionel Sambuc 
1382f4a2713aSLionel Sambuc /// \brief Lex a delayed template function for late parsing.
LexTemplateFunctionForLateParsing(CachedTokens & Toks)1383f4a2713aSLionel Sambuc void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
1384f4a2713aSLionel Sambuc   tok::TokenKind kind = Tok.getKind();
1385f4a2713aSLionel Sambuc   if (!ConsumeAndStoreFunctionPrologue(Toks)) {
1386f4a2713aSLionel Sambuc     // Consume everything up to (and including) the matching right brace.
1387f4a2713aSLionel Sambuc     ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1388f4a2713aSLionel Sambuc   }
1389f4a2713aSLionel Sambuc 
1390f4a2713aSLionel Sambuc   // If we're in a function-try-block, we need to store all the catch blocks.
1391f4a2713aSLionel Sambuc   if (kind == tok::kw_try) {
1392f4a2713aSLionel Sambuc     while (Tok.is(tok::kw_catch)) {
1393f4a2713aSLionel Sambuc       ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1394f4a2713aSLionel Sambuc       ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1395f4a2713aSLionel Sambuc     }
1396f4a2713aSLionel Sambuc   }
1397f4a2713aSLionel Sambuc }
1398