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