1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/AST/ASTContext.h" |
14 | #include "clang/AST/DeclTemplate.h" |
15 | #include "clang/Parse/ParseDiagnostic.h" |
16 | #include "clang/Parse/Parser.h" |
17 | #include "clang/Parse/RAIIObjectsForParser.h" |
18 | #include "clang/Sema/DeclSpec.h" |
19 | #include "clang/Sema/ParsedTemplate.h" |
20 | #include "clang/Sema/Scope.h" |
21 | using namespace clang; |
22 | |
23 | |
24 | |
25 | Decl *Parser::ParseDeclarationStartingWithTemplate( |
26 | DeclaratorContext Context, SourceLocation &DeclEnd, |
27 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
28 | ObjCDeclContextSwitch ObjCDC(*this); |
29 | |
30 | if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { |
31 | return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(), |
32 | DeclEnd, AccessAttrs, AS); |
33 | } |
34 | return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs, |
35 | AS); |
36 | } |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | Decl *Parser::ParseTemplateDeclarationOrSpecialization( |
54 | DeclaratorContext Context, SourceLocation &DeclEnd, |
55 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
56 | (0) . __assert_fail ("Tok.isOneOf(tok..kw_export, tok..kw_template) && \"Token does not start a template declaration.\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 57, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.isOneOf(tok::kw_export, tok::kw_template) && |
57 | (0) . __assert_fail ("Tok.isOneOf(tok..kw_export, tok..kw_template) && \"Token does not start a template declaration.\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 57, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Token does not start a template declaration."); |
58 | |
59 | |
60 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
61 | |
62 | |
63 | |
64 | ParsingDeclRAIIObject |
65 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | bool isSpecialization = true; |
89 | bool LastParamListWasEmpty = false; |
90 | TemplateParameterLists ParamLists; |
91 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
92 | |
93 | do { |
94 | |
95 | SourceLocation ExportLoc; |
96 | TryConsumeToken(tok::kw_export, ExportLoc); |
97 | |
98 | |
99 | SourceLocation TemplateLoc; |
100 | if (!TryConsumeToken(tok::kw_template, TemplateLoc)) { |
101 | Diag(Tok.getLocation(), diag::err_expected_template); |
102 | return nullptr; |
103 | } |
104 | |
105 | |
106 | SourceLocation LAngleLoc, RAngleLoc; |
107 | SmallVector<NamedDecl*, 4> TemplateParams; |
108 | if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(), |
109 | TemplateParams, LAngleLoc, RAngleLoc)) { |
110 | |
111 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
112 | TryConsumeToken(tok::semi); |
113 | return nullptr; |
114 | } |
115 | |
116 | ExprResult OptionalRequiresClauseConstraintER; |
117 | if (!TemplateParams.empty()) { |
118 | isSpecialization = false; |
119 | ++CurTemplateDepthTracker; |
120 | |
121 | if (TryConsumeToken(tok::kw_requires)) { |
122 | OptionalRequiresClauseConstraintER = |
123 | Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression()); |
124 | if (!OptionalRequiresClauseConstraintER.isUsable()) { |
125 | |
126 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
127 | TryConsumeToken(tok::semi); |
128 | return nullptr; |
129 | } |
130 | } |
131 | } else { |
132 | LastParamListWasEmpty = true; |
133 | } |
134 | |
135 | ParamLists.push_back(Actions.ActOnTemplateParameterList( |
136 | CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc, |
137 | TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get())); |
138 | } while (Tok.isOneOf(tok::kw_export, tok::kw_template)); |
139 | |
140 | unsigned NewFlags = getCurScope()->getFlags() & ~Scope::TemplateParamScope; |
141 | ParseScopeFlags TemplateScopeFlags(this, NewFlags, isSpecialization); |
142 | |
143 | |
144 | return ParseSingleDeclarationAfterTemplate( |
145 | Context, |
146 | ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty), |
147 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); |
148 | } |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | Decl *Parser::ParseSingleDeclarationAfterTemplate( |
161 | DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, |
162 | ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, |
163 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
164 | (0) . __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo..NonTemplate && \"Template information required\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 165, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
165 | (0) . __assert_fail ("TemplateInfo.Kind != ParsedTemplateInfo..NonTemplate && \"Template information required\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 165, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Template information required"); |
166 | |
167 | if (Tok.is(tok::kw_static_assert)) { |
168 | |
169 | Diag(Tok.getLocation(), diag::err_templated_invalid_declaration) |
170 | << TemplateInfo.getSourceRange(); |
171 | |
172 | return ParseStaticAssertDeclaration(DeclEnd); |
173 | } |
174 | |
175 | if (Context == DeclaratorContext::MemberContext) { |
176 | |
177 | ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, |
178 | &DiagsFromTParams); |
179 | return nullptr; |
180 | } |
181 | |
182 | ParsedAttributesWithRange prefixAttrs(AttrFactory); |
183 | MaybeParseCXX11Attributes(prefixAttrs); |
184 | |
185 | if (Tok.is(tok::kw_using)) { |
186 | auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, |
187 | prefixAttrs); |
188 | if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl()) |
189 | return nullptr; |
190 | return usingDeclPtr.get().getSingleDecl(); |
191 | } |
192 | |
193 | |
194 | |
195 | ParsingDeclSpec DS(*this, &DiagsFromTParams); |
196 | |
197 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, |
198 | getDeclSpecContextFromDeclaratorContext(Context)); |
199 | |
200 | if (Tok.is(tok::semi)) { |
201 | ProhibitAttributes(prefixAttrs); |
202 | DeclEnd = ConsumeToken(); |
203 | RecordDecl *AnonRecord = nullptr; |
204 | Decl *Decl = Actions.ParsedFreeStandingDeclSpec( |
205 | getCurScope(), AS, DS, |
206 | TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams |
207 | : MultiTemplateParamsArg(), |
208 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation, |
209 | AnonRecord); |
210 | (0) . __assert_fail ("!AnonRecord && \"Anonymous unions/structs should not be valid with template\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 211, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!AnonRecord && |
211 | (0) . __assert_fail ("!AnonRecord && \"Anonymous unions/structs should not be valid with template\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 211, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Anonymous unions/structs should not be valid with template"); |
212 | DS.complete(Decl); |
213 | return Decl; |
214 | } |
215 | |
216 | |
217 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
218 | ProhibitAttributes(prefixAttrs); |
219 | else |
220 | DS.takeAttributesFrom(prefixAttrs); |
221 | |
222 | |
223 | ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context); |
224 | ParseDeclarator(DeclaratorInfo); |
225 | |
226 | if (!DeclaratorInfo.hasName()) { |
227 | |
228 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
229 | if (Tok.is(tok::semi)) |
230 | ConsumeToken(); |
231 | return nullptr; |
232 | } |
233 | |
234 | LateParsedAttrList LateParsedAttrs(true); |
235 | if (DeclaratorInfo.isFunctionDeclarator()) |
236 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
237 | |
238 | if (DeclaratorInfo.isFunctionDeclarator() && |
239 | isStartOfFunctionDefinition(DeclaratorInfo)) { |
240 | |
241 | |
242 | |
243 | |
244 | if (Context != DeclaratorContext::FileContext) { |
245 | Diag(Tok, diag::err_function_definition_not_allowed); |
246 | SkipMalformedDecl(); |
247 | return nullptr; |
248 | } |
249 | |
250 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
251 | |
252 | |
253 | |
254 | Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) |
255 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
256 | DS.ClearStorageClassSpecs(); |
257 | } |
258 | |
259 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
260 | if (DeclaratorInfo.getName().getKind() != |
261 | UnqualifiedIdKind::IK_TemplateId) { |
262 | |
263 | |
264 | Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; |
265 | return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), |
266 | &LateParsedAttrs); |
267 | } else { |
268 | SourceLocation LAngleLoc |
269 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
270 | Diag(DeclaratorInfo.getIdentifierLoc(), |
271 | diag::err_explicit_instantiation_with_definition) |
272 | << SourceRange(TemplateInfo.TemplateLoc) |
273 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
274 | |
275 | |
276 | TemplateParameterLists FakedParamLists; |
277 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
278 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
279 | LAngleLoc, nullptr)); |
280 | |
281 | return ParseFunctionDefinition( |
282 | DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, |
283 | , |
284 | ), |
285 | &LateParsedAttrs); |
286 | } |
287 | } |
288 | return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, |
289 | &LateParsedAttrs); |
290 | } |
291 | |
292 | |
293 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, |
294 | TemplateInfo); |
295 | |
296 | if (Tok.is(tok::comma)) { |
297 | Diag(Tok, diag::err_multiple_template_declarators) |
298 | << (int)TemplateInfo.Kind; |
299 | SkipUntil(tok::semi); |
300 | return ThisDecl; |
301 | } |
302 | |
303 | |
304 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
305 | if (LateParsedAttrs.size() > 0) |
306 | ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); |
307 | DeclaratorInfo.complete(ThisDecl); |
308 | return ThisDecl; |
309 | } |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
318 | |
319 | |
320 | bool Parser::ParseTemplateParameters( |
321 | unsigned Depth, SmallVectorImpl<NamedDecl *> &TemplateParams, |
322 | SourceLocation &LAngleLoc, SourceLocation &RAngleLoc) { |
323 | |
324 | if (!TryConsumeToken(tok::less, LAngleLoc)) { |
325 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
326 | return true; |
327 | } |
328 | |
329 | |
330 | bool Failed = false; |
331 | if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) |
332 | Failed = ParseTemplateParameterList(Depth, TemplateParams); |
333 | |
334 | if (Tok.is(tok::greatergreater)) { |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | Tok.setKind(tok::greater); |
341 | RAngleLoc = Tok.getLocation(); |
342 | Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); |
343 | } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) { |
344 | Diag(Tok.getLocation(), diag::err_expected) << tok::greater; |
345 | return true; |
346 | } |
347 | return false; |
348 | } |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | bool |
359 | Parser::ParseTemplateParameterList(const unsigned Depth, |
360 | SmallVectorImpl<NamedDecl*> &TemplateParams) { |
361 | while (1) { |
362 | |
363 | if (NamedDecl *TmpParam |
364 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
365 | TemplateParams.push_back(TmpParam); |
366 | } else { |
367 | |
368 | |
369 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
370 | StopAtSemi | StopBeforeMatch); |
371 | } |
372 | |
373 | |
374 | if (Tok.is(tok::comma)) { |
375 | ConsumeToken(); |
376 | } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) { |
377 | |
378 | break; |
379 | } else { |
380 | |
381 | |
382 | |
383 | Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
384 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
385 | StopAtSemi | StopBeforeMatch); |
386 | return false; |
387 | } |
388 | } |
389 | return true; |
390 | } |
391 | |
392 | |
393 | |
394 | bool Parser::isStartOfTemplateTypeParameter() { |
395 | if (Tok.is(tok::kw_class)) { |
396 | |
397 | |
398 | switch (NextToken().getKind()) { |
399 | case tok::equal: |
400 | case tok::comma: |
401 | case tok::greater: |
402 | case tok::greatergreater: |
403 | case tok::ellipsis: |
404 | return true; |
405 | |
406 | case tok::identifier: |
407 | |
408 | |
409 | break; |
410 | |
411 | default: |
412 | return false; |
413 | } |
414 | |
415 | switch (GetLookAheadToken(2).getKind()) { |
416 | case tok::equal: |
417 | case tok::comma: |
418 | case tok::greater: |
419 | case tok::greatergreater: |
420 | return true; |
421 | |
422 | default: |
423 | return false; |
424 | } |
425 | } |
426 | |
427 | |
428 | |
429 | if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef)) |
430 | return false; |
431 | |
432 | |
433 | |
434 | |
435 | |
436 | |
437 | |
438 | Token Next = NextToken(); |
439 | |
440 | |
441 | if (Next.getKind() == tok::identifier) |
442 | Next = GetLookAheadToken(2); |
443 | |
444 | switch (Next.getKind()) { |
445 | case tok::equal: |
446 | case tok::comma: |
447 | case tok::greater: |
448 | case tok::greatergreater: |
449 | case tok::ellipsis: |
450 | return true; |
451 | |
452 | case tok::kw_typename: |
453 | case tok::kw_typedef: |
454 | case tok::kw_class: |
455 | |
456 | |
457 | return true; |
458 | |
459 | default: |
460 | return false; |
461 | } |
462 | } |
463 | |
464 | |
465 | |
466 | |
467 | |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | |
478 | |
479 | NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
480 | if (isStartOfTemplateTypeParameter()) { |
481 | |
482 | if (Tok.is(tok::kw_typedef)) { |
483 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); |
484 | |
485 | Diag(Tok.getLocation(), diag::note_meant_to_use_typename) |
486 | << FixItHint::CreateReplacement(CharSourceRange::getCharRange( |
487 | Tok.getLocation(), Tok.getEndLoc()), |
488 | "typename"); |
489 | |
490 | Tok.setKind(tok::kw_typename); |
491 | } |
492 | |
493 | return ParseTypeParameter(Depth, Position); |
494 | } |
495 | |
496 | if (Tok.is(tok::kw_template)) |
497 | return ParseTemplateTemplateParameter(Depth, Position); |
498 | |
499 | |
500 | |
501 | |
502 | return ParseNonTypeTemplateParameter(Depth, Position); |
503 | } |
504 | |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { |
515 | (0) . __assert_fail ("Tok.isOneOf(tok..kw_class, tok..kw_typename) && \"A type-parameter starts with 'class' or 'typename'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 516, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.isOneOf(tok::kw_class, tok::kw_typename) && |
516 | (0) . __assert_fail ("Tok.isOneOf(tok..kw_class, tok..kw_typename) && \"A type-parameter starts with 'class' or 'typename'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 516, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "A type-parameter starts with 'class' or 'typename'"); |
517 | |
518 | |
519 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
520 | SourceLocation KeyLoc = ConsumeToken(); |
521 | |
522 | |
523 | SourceLocation EllipsisLoc; |
524 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
525 | Diag(EllipsisLoc, |
526 | getLangOpts().CPlusPlus11 |
527 | ? diag::warn_cxx98_compat_variadic_templates |
528 | : diag::ext_variadic_templates); |
529 | } |
530 | |
531 | |
532 | SourceLocation NameLoc; |
533 | IdentifierInfo *ParamName = nullptr; |
534 | if (Tok.is(tok::identifier)) { |
535 | ParamName = Tok.getIdentifierInfo(); |
536 | NameLoc = ConsumeToken(); |
537 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, |
538 | tok::greatergreater)) { |
539 | |
540 | |
541 | } else { |
542 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
543 | return nullptr; |
544 | } |
545 | |
546 | |
547 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); |
548 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
549 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); |
550 | |
551 | |
552 | |
553 | |
554 | SourceLocation EqualLoc; |
555 | ParsedType DefaultArg; |
556 | if (TryConsumeToken(tok::equal, EqualLoc)) |
557 | DefaultArg = ParseTypeName(, |
558 | DeclaratorContext::TemplateTypeArgContext).get(); |
559 | |
560 | return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc, |
561 | KeyLoc, ParamName, NameLoc, Depth, Position, |
562 | EqualLoc, DefaultArg); |
563 | } |
564 | |
565 | |
566 | |
567 | |
568 | |
569 | |
570 | |
571 | |
572 | |
573 | |
574 | |
575 | |
576 | NamedDecl * |
577 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
578 | (0) . __assert_fail ("Tok.is(tok..kw_template) && \"Expected 'template' keyword\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
579 | |
580 | |
581 | SourceLocation TemplateLoc = ConsumeToken(); |
582 | SmallVector<NamedDecl*,8> TemplateParams; |
583 | SourceLocation LAngleLoc, RAngleLoc; |
584 | { |
585 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
586 | if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
587 | RAngleLoc)) { |
588 | return nullptr; |
589 | } |
590 | } |
591 | |
592 | |
593 | |
594 | |
595 | |
596 | |
597 | if (!TryConsumeToken(tok::kw_class)) { |
598 | bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct); |
599 | const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok; |
600 | if (Tok.is(tok::kw_typename)) { |
601 | Diag(Tok.getLocation(), |
602 | getLangOpts().CPlusPlus17 |
603 | ? diag::warn_cxx14_compat_template_template_param_typename |
604 | : diag::ext_template_template_param_typename) |
605 | << (!getLangOpts().CPlusPlus17 |
606 | ? FixItHint::CreateReplacement(Tok.getLocation(), "class") |
607 | : FixItHint()); |
608 | } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater, |
609 | tok::greatergreater, tok::ellipsis)) { |
610 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param) |
611 | << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class") |
612 | : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); |
613 | } else |
614 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param); |
615 | |
616 | if (Replace) |
617 | ConsumeToken(); |
618 | } |
619 | |
620 | |
621 | SourceLocation EllipsisLoc; |
622 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
623 | Diag(EllipsisLoc, |
624 | getLangOpts().CPlusPlus11 |
625 | ? diag::warn_cxx98_compat_variadic_templates |
626 | : diag::ext_variadic_templates); |
627 | |
628 | |
629 | SourceLocation NameLoc; |
630 | IdentifierInfo *ParamName = nullptr; |
631 | if (Tok.is(tok::identifier)) { |
632 | ParamName = Tok.getIdentifierInfo(); |
633 | NameLoc = ConsumeToken(); |
634 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, |
635 | tok::greatergreater)) { |
636 | |
637 | |
638 | } else { |
639 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
640 | return nullptr; |
641 | } |
642 | |
643 | |
644 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); |
645 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
646 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); |
647 | |
648 | TemplateParameterList *ParamList = |
649 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
650 | TemplateLoc, LAngleLoc, |
651 | TemplateParams, |
652 | RAngleLoc, nullptr); |
653 | |
654 | |
655 | |
656 | |
657 | SourceLocation EqualLoc; |
658 | ParsedTemplateArgument DefaultArg; |
659 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
660 | DefaultArg = ParseTemplateTemplateArgument(); |
661 | if (DefaultArg.isInvalid()) { |
662 | Diag(Tok.getLocation(), |
663 | diag::err_default_template_template_parameter_not_template); |
664 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
665 | StopAtSemi | StopBeforeMatch); |
666 | } |
667 | } |
668 | |
669 | return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, |
670 | ParamList, EllipsisLoc, |
671 | ParamName, NameLoc, Depth, |
672 | Position, EqualLoc, DefaultArg); |
673 | } |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | NamedDecl * |
682 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
683 | |
684 | |
685 | |
686 | DeclSpec DS(AttrFactory); |
687 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
688 | DeclSpecContext::DSC_template_param); |
689 | |
690 | |
691 | Declarator ParamDecl(DS, DeclaratorContext::TemplateParamContext); |
692 | ParseDeclarator(ParamDecl); |
693 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
694 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); |
695 | return nullptr; |
696 | } |
697 | |
698 | |
699 | SourceLocation EllipsisLoc; |
700 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
701 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl); |
702 | |
703 | |
704 | |
705 | |
706 | SourceLocation EqualLoc; |
707 | ExprResult DefaultArg; |
708 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
709 | |
710 | |
711 | |
712 | |
713 | |
714 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
715 | EnterExpressionEvaluationContext ConstantEvaluated( |
716 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
717 | |
718 | DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
719 | if (DefaultArg.isInvalid()) |
720 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); |
721 | } |
722 | |
723 | |
724 | return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, |
725 | Depth, Position, EqualLoc, |
726 | DefaultArg.get()); |
727 | } |
728 | |
729 | void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc, |
730 | SourceLocation CorrectLoc, |
731 | bool AlreadyHasEllipsis, |
732 | bool IdentifierHasName) { |
733 | FixItHint Insertion; |
734 | if (!AlreadyHasEllipsis) |
735 | Insertion = FixItHint::CreateInsertion(CorrectLoc, "..."); |
736 | Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
737 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion |
738 | << !IdentifierHasName; |
739 | } |
740 | |
741 | void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc, |
742 | Declarator &D) { |
743 | assert(EllipsisLoc.isValid()); |
744 | bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid(); |
745 | if (!AlreadyHasEllipsis) |
746 | D.setEllipsisLoc(EllipsisLoc); |
747 | DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(), |
748 | AlreadyHasEllipsis, D.hasName()); |
749 | } |
750 | |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | |
757 | |
758 | |
759 | |
760 | |
761 | |
762 | |
763 | |
764 | |
765 | |
766 | bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc, |
767 | bool ConsumeLastToken, |
768 | bool ObjCGenericList) { |
769 | |
770 | tok::TokenKind RemainingToken; |
771 | const char *ReplacementStr = "> >"; |
772 | bool MergeWithNextToken = false; |
773 | |
774 | switch (Tok.getKind()) { |
775 | default: |
776 | Diag(Tok.getLocation(), diag::err_expected) << tok::greater; |
777 | return true; |
778 | |
779 | case tok::greater: |
780 | |
781 | |
782 | RAngleLoc = Tok.getLocation(); |
783 | if (ConsumeLastToken) |
784 | ConsumeToken(); |
785 | return false; |
786 | |
787 | case tok::greatergreater: |
788 | RemainingToken = tok::greater; |
789 | break; |
790 | |
791 | case tok::greatergreatergreater: |
792 | RemainingToken = tok::greatergreater; |
793 | break; |
794 | |
795 | case tok::greaterequal: |
796 | RemainingToken = tok::equal; |
797 | ReplacementStr = "> ="; |
798 | |
799 | |
800 | |
801 | |
802 | if (NextToken().is(tok::equal) && |
803 | areTokensAdjacent(Tok, NextToken())) { |
804 | RemainingToken = tok::equalequal; |
805 | MergeWithNextToken = true; |
806 | } |
807 | break; |
808 | |
809 | case tok::greatergreaterequal: |
810 | RemainingToken = tok::greaterequal; |
811 | break; |
812 | } |
813 | |
814 | |
815 | |
816 | |
817 | |
818 | |
819 | |
820 | |
821 | |
822 | SourceLocation TokBeforeGreaterLoc = PrevTokLocation; |
823 | SourceLocation TokLoc = Tok.getLocation(); |
824 | Token Next = NextToken(); |
825 | |
826 | |
827 | |
828 | |
829 | bool PreventMergeWithNextToken = |
830 | (RemainingToken == tok::greater || |
831 | RemainingToken == tok::greatergreater) && |
832 | (Next.isOneOf(tok::greater, tok::greatergreater, |
833 | tok::greatergreatergreater, tok::equal, tok::greaterequal, |
834 | tok::greatergreaterequal, tok::equalequal)) && |
835 | areTokensAdjacent(Tok, Next); |
836 | |
837 | |
838 | if (!ObjCGenericList) { |
839 | |
840 | CharSourceRange ReplacementRange = CharSourceRange::getCharRange( |
841 | TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(), |
842 | getLangOpts())); |
843 | |
844 | |
845 | |
846 | |
847 | FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, |
848 | ReplacementStr); |
849 | |
850 | |
851 | |
852 | FixItHint Hint2; |
853 | if (PreventMergeWithNextToken) |
854 | Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); |
855 | |
856 | unsigned DiagId = diag::err_two_right_angle_brackets_need_space; |
857 | if (getLangOpts().CPlusPlus11 && |
858 | (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater))) |
859 | DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; |
860 | else if (Tok.is(tok::greaterequal)) |
861 | DiagId = diag::err_right_angle_bracket_equal_needs_space; |
862 | Diag(TokLoc, DiagId) << Hint1 << Hint2; |
863 | } |
864 | |
865 | |
866 | |
867 | unsigned GreaterLength = Lexer::getTokenPrefixLength( |
868 | TokLoc, 1, PP.getSourceManager(), getLangOpts()); |
869 | |
870 | |
871 | |
872 | |
873 | RAngleLoc = PP.SplitToken(TokLoc, GreaterLength); |
874 | |
875 | |
876 | bool CachingTokens = PP.IsPreviousCachedToken(Tok); |
877 | |
878 | Token Greater = Tok; |
879 | Greater.setLocation(RAngleLoc); |
880 | Greater.setKind(tok::greater); |
881 | Greater.setLength(GreaterLength); |
882 | |
883 | unsigned OldLength = Tok.getLength(); |
884 | if (MergeWithNextToken) { |
885 | ConsumeToken(); |
886 | OldLength += Tok.getLength(); |
887 | } |
888 | |
889 | Tok.setKind(RemainingToken); |
890 | Tok.setLength(OldLength - GreaterLength); |
891 | |
892 | |
893 | |
894 | SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength); |
895 | if (PreventMergeWithNextToken) |
896 | AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength()); |
897 | Tok.setLocation(AfterGreaterLoc); |
898 | |
899 | |
900 | if (CachingTokens) { |
901 | |
902 | if (MergeWithNextToken) |
903 | PP.ReplacePreviousCachedToken({}); |
904 | |
905 | if (ConsumeLastToken) |
906 | PP.ReplacePreviousCachedToken({Greater, Tok}); |
907 | else |
908 | PP.ReplacePreviousCachedToken({Greater}); |
909 | } |
910 | |
911 | if (ConsumeLastToken) { |
912 | PrevTokLocation = RAngleLoc; |
913 | } else { |
914 | PrevTokLocation = TokBeforeGreaterLoc; |
915 | PP.EnterToken(Tok); |
916 | Tok = Greater; |
917 | } |
918 | |
919 | return false; |
920 | } |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | |
934 | bool |
935 | Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken, |
936 | SourceLocation &LAngleLoc, |
937 | TemplateArgList &TemplateArgs, |
938 | SourceLocation &RAngleLoc) { |
939 | (0) . __assert_fail ("Tok.is(tok..less) && \"Must have already parsed the template-name\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 939, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::less) && "Must have already parsed the template-name"); |
940 | |
941 | |
942 | LAngleLoc = ConsumeToken(); |
943 | |
944 | |
945 | bool Invalid = false; |
946 | { |
947 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
948 | if (!Tok.isOneOf(tok::greater, tok::greatergreater, |
949 | tok::greatergreatergreater, tok::greaterequal, |
950 | tok::greatergreaterequal)) |
951 | Invalid = ParseTemplateArgumentList(TemplateArgs); |
952 | |
953 | if (Invalid) { |
954 | |
955 | if (ConsumeLastToken) |
956 | SkipUntil(tok::greater, StopAtSemi); |
957 | else |
958 | SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); |
959 | return true; |
960 | } |
961 | } |
962 | |
963 | return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken, |
964 | ); |
965 | } |
966 | |
967 | |
968 | |
969 | |
970 | |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | |
977 | |
978 | |
979 | |
980 | |
981 | |
982 | |
983 | |
984 | |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | |
992 | |
993 | |
994 | |
995 | |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | |
1002 | |
1003 | |
1004 | bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, |
1005 | CXXScopeSpec &SS, |
1006 | SourceLocation TemplateKWLoc, |
1007 | UnqualifiedId &TemplateName, |
1008 | bool AllowTypeAnnotation) { |
1009 | (0) . __assert_fail ("getLangOpts().CPlusPlus && \"Can only annotate template-ids in C++\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1009, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++"); |
1010 | (0) . __assert_fail ("Template && Tok.is(tok..less) && \"Parser isn't at the beginning of a template-id\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1011, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Template && Tok.is(tok::less) && |
1011 | (0) . __assert_fail ("Template && Tok.is(tok..less) && \"Parser isn't at the beginning of a template-id\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1011, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Parser isn't at the beginning of a template-id"); |
1012 | |
1013 | |
1014 | SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); |
1015 | |
1016 | |
1017 | SourceLocation LAngleLoc, RAngleLoc; |
1018 | TemplateArgList TemplateArgs; |
1019 | bool Invalid = ParseTemplateIdAfterTemplateName(false, LAngleLoc, |
1020 | TemplateArgs, |
1021 | RAngleLoc); |
1022 | |
1023 | if (Invalid) { |
1024 | |
1025 | |
1026 | TryConsumeToken(tok::greater); |
1027 | return true; |
1028 | } |
1029 | |
1030 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); |
1031 | |
1032 | |
1033 | if (TNK == TNK_Type_template && AllowTypeAnnotation) { |
1034 | TypeResult Type = Actions.ActOnTemplateIdType( |
1035 | SS, TemplateKWLoc, Template, TemplateName.Identifier, |
1036 | TemplateNameLoc, LAngleLoc, TemplateArgsPtr, RAngleLoc); |
1037 | if (Type.isInvalid()) { |
1038 | |
1039 | |
1040 | |
1041 | TryConsumeToken(tok::greater); |
1042 | return true; |
1043 | } |
1044 | |
1045 | Tok.setKind(tok::annot_typename); |
1046 | setTypeAnnotation(Tok, Type.get()); |
1047 | if (SS.isNotEmpty()) |
1048 | Tok.setLocation(SS.getBeginLoc()); |
1049 | else if (TemplateKWLoc.isValid()) |
1050 | Tok.setLocation(TemplateKWLoc); |
1051 | else |
1052 | Tok.setLocation(TemplateNameLoc); |
1053 | } else { |
1054 | |
1055 | |
1056 | Tok.setKind(tok::annot_template_id); |
1057 | |
1058 | IdentifierInfo *TemplateII = |
1059 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier |
1060 | ? TemplateName.Identifier |
1061 | : nullptr; |
1062 | |
1063 | OverloadedOperatorKind OpKind = |
1064 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier |
1065 | ? OO_None |
1066 | : TemplateName.OperatorFunctionId.Operator; |
1067 | |
1068 | TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( |
1069 | SS, TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK, |
1070 | LAngleLoc, RAngleLoc, TemplateArgs, TemplateIds); |
1071 | |
1072 | Tok.setAnnotationValue(TemplateId); |
1073 | if (TemplateKWLoc.isValid()) |
1074 | Tok.setLocation(TemplateKWLoc); |
1075 | else |
1076 | Tok.setLocation(TemplateNameLoc); |
1077 | } |
1078 | |
1079 | |
1080 | Tok.setAnnotationEndLoc(RAngleLoc); |
1081 | |
1082 | |
1083 | |
1084 | PP.AnnotateCachedTokens(Tok); |
1085 | return false; |
1086 | } |
1087 | |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | void Parser::AnnotateTemplateIdTokenAsType(bool IsClassName) { |
1100 | (0) . __assert_fail ("Tok.is(tok..annot_template_id) && \"Requires template-id tokens\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1100, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); |
1101 | |
1102 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
1103 | (0) . __assert_fail ("(TemplateId->Kind == TNK_Type_template || TemplateId->Kind == TNK_Dependent_template_name) && \"Only works for type and dependent templates\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1105, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((TemplateId->Kind == TNK_Type_template || |
1104 | (0) . __assert_fail ("(TemplateId->Kind == TNK_Type_template || TemplateId->Kind == TNK_Dependent_template_name) && \"Only works for type and dependent templates\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1105, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> TemplateId->Kind == TNK_Dependent_template_name) && |
1105 | (0) . __assert_fail ("(TemplateId->Kind == TNK_Type_template || TemplateId->Kind == TNK_Dependent_template_name) && \"Only works for type and dependent templates\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1105, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only works for type and dependent templates"); |
1106 | |
1107 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
1108 | TemplateId->NumArgs); |
1109 | |
1110 | TypeResult Type |
1111 | = Actions.ActOnTemplateIdType(TemplateId->SS, |
1112 | TemplateId->TemplateKWLoc, |
1113 | TemplateId->Template, |
1114 | TemplateId->Name, |
1115 | TemplateId->TemplateNameLoc, |
1116 | TemplateId->LAngleLoc, |
1117 | TemplateArgsPtr, |
1118 | TemplateId->RAngleLoc, |
1119 | , |
1120 | IsClassName); |
1121 | |
1122 | Tok.setKind(tok::annot_typename); |
1123 | setTypeAnnotation(Tok, Type.isInvalid() ? nullptr : Type.get()); |
1124 | if (TemplateId->SS.isNotEmpty()) |
1125 | Tok.setLocation(TemplateId->SS.getBeginLoc()); |
1126 | |
1127 | |
1128 | |
1129 | |
1130 | PP.AnnotateCachedTokens(Tok); |
1131 | } |
1132 | |
1133 | |
1134 | static bool isEndOfTemplateArgument(Token Tok) { |
1135 | return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater); |
1136 | } |
1137 | |
1138 | |
1139 | ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { |
1140 | if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && |
1141 | !Tok.is(tok::annot_cxxscope)) |
1142 | return ParsedTemplateArgument(); |
1143 | |
1144 | |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | |
1153 | |
1154 | |
1155 | CXXScopeSpec SS; |
1156 | ParseOptionalCXXScopeSpecifier(SS, nullptr, |
1157 | ); |
1158 | |
1159 | ParsedTemplateArgument Result; |
1160 | SourceLocation EllipsisLoc; |
1161 | if (SS.isSet() && Tok.is(tok::kw_template)) { |
1162 | |
1163 | |
1164 | SourceLocation TemplateKWLoc = ConsumeToken(); |
1165 | |
1166 | if (Tok.is(tok::identifier)) { |
1167 | |
1168 | UnqualifiedId Name; |
1169 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
1170 | ConsumeToken(); |
1171 | |
1172 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
1173 | |
1174 | |
1175 | |
1176 | |
1177 | TemplateTy Template; |
1178 | if (isEndOfTemplateArgument(Tok) && |
1179 | Actions.ActOnDependentTemplateName( |
1180 | getCurScope(), SS, TemplateKWLoc, Name, |
1181 | , |
1182 | , Template)) |
1183 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); |
1184 | } |
1185 | } else if (Tok.is(tok::identifier)) { |
1186 | |
1187 | TemplateTy Template; |
1188 | UnqualifiedId Name; |
1189 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
1190 | ConsumeToken(); |
1191 | |
1192 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
1193 | |
1194 | if (isEndOfTemplateArgument(Tok)) { |
1195 | bool MemberOfUnknownSpecialization; |
1196 | TemplateNameKind TNK = Actions.isTemplateName( |
1197 | getCurScope(), SS, |
1198 | , Name, |
1199 | , |
1200 | , Template, MemberOfUnknownSpecialization); |
1201 | if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { |
1202 | |
1203 | |
1204 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); |
1205 | } |
1206 | } |
1207 | } |
1208 | |
1209 | |
1210 | if (EllipsisLoc.isValid() && !Result.isInvalid()) |
1211 | Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); |
1212 | |
1213 | return Result; |
1214 | } |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | ParsedTemplateArgument Parser::ParseTemplateArgument() { |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | EnterExpressionEvaluationContext EnterConstantEvaluated( |
1234 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated, |
1235 | , |
1236 | ::ExpressionEvaluationContextRecord::EK_TemplateArgument); |
1237 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { |
1238 | TypeResult TypeArg = ParseTypeName( |
1239 | , DeclaratorContext::TemplateArgContext); |
1240 | return Actions.ActOnTemplateTypeArgument(TypeArg); |
1241 | } |
1242 | |
1243 | |
1244 | { |
1245 | TentativeParsingAction TPA(*this); |
1246 | |
1247 | ParsedTemplateArgument TemplateTemplateArgument |
1248 | = ParseTemplateTemplateArgument(); |
1249 | if (!TemplateTemplateArgument.isInvalid()) { |
1250 | TPA.Commit(); |
1251 | return TemplateTemplateArgument; |
1252 | } |
1253 | |
1254 | |
1255 | TPA.Revert(); |
1256 | } |
1257 | |
1258 | |
1259 | SourceLocation Loc = Tok.getLocation(); |
1260 | ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast); |
1261 | if (ExprArg.isInvalid() || !ExprArg.get()) |
1262 | return ParsedTemplateArgument(); |
1263 | |
1264 | return ParsedTemplateArgument(ParsedTemplateArgument::NonType, |
1265 | ExprArg.get(), Loc); |
1266 | } |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | bool Parser::IsTemplateArgumentList(unsigned Skip) { |
1272 | struct AlwaysRevertAction : TentativeParsingAction { |
1273 | AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } |
1274 | ~AlwaysRevertAction() { Revert(); } |
1275 | } Tentative(*this); |
1276 | |
1277 | while (Skip) { |
1278 | ConsumeAnyToken(); |
1279 | --Skip; |
1280 | } |
1281 | |
1282 | |
1283 | if (!TryConsumeToken(tok::less)) |
1284 | return false; |
1285 | |
1286 | |
1287 | if (Tok.is(tok::greater)) |
1288 | return true; |
1289 | |
1290 | |
1291 | while (isCXXDeclarationSpecifier() == TPResult::True) |
1292 | ConsumeAnyToken(); |
1293 | |
1294 | |
1295 | return Tok.isOneOf(tok::greater, tok::comma); |
1296 | } |
1297 | |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | |
1303 | |
1304 | bool |
1305 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { |
1306 | |
1307 | ColonProtectionRAIIObject ColonProtection(*this, false); |
1308 | |
1309 | do { |
1310 | ParsedTemplateArgument Arg = ParseTemplateArgument(); |
1311 | SourceLocation EllipsisLoc; |
1312 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
1313 | Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); |
1314 | |
1315 | if (Arg.isInvalid()) { |
1316 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); |
1317 | return true; |
1318 | } |
1319 | |
1320 | |
1321 | TemplateArgs.push_back(Arg); |
1322 | |
1323 | |
1324 | |
1325 | } while (TryConsumeToken(tok::comma)); |
1326 | |
1327 | return false; |
1328 | } |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context, |
1338 | SourceLocation ExternLoc, |
1339 | SourceLocation TemplateLoc, |
1340 | SourceLocation &DeclEnd, |
1341 | ParsedAttributes &AccessAttrs, |
1342 | AccessSpecifier AS) { |
1343 | |
1344 | ParsingDeclRAIIObject |
1345 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); |
1346 | |
1347 | return ParseSingleDeclarationAfterTemplate( |
1348 | Context, ParsedTemplateInfo(ExternLoc, TemplateLoc), |
1349 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); |
1350 | } |
1351 | |
1352 | SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { |
1353 | if (TemplateParams) |
1354 | return getTemplateParamsRange(TemplateParams->data(), |
1355 | TemplateParams->size()); |
1356 | |
1357 | SourceRange R(TemplateLoc); |
1358 | if (ExternLoc.isValid()) |
1359 | R.setBegin(ExternLoc); |
1360 | return R; |
1361 | } |
1362 | |
1363 | void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { |
1364 | ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); |
1365 | } |
1366 | |
1367 | |
1368 | void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { |
1369 | if (!LPT.D) |
1370 | return; |
1371 | |
1372 | |
1373 | FunctionDecl *FunD = LPT.D->getAsFunction(); |
1374 | |
1375 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
1376 | |
1377 | |
1378 | Sema::ContextRAII GlobalSavedContext( |
1379 | Actions, Actions.Context.getTranslationUnitDecl()); |
1380 | |
1381 | SmallVector<ParseScope*, 4> TemplateParamScopeStack; |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | struct ContainingDC { |
1388 | ContainingDC(DeclContext *DC, bool ShouldPush) : Pair(DC, ShouldPush) {} |
1389 | llvm::PointerIntPair<DeclContext *, 1, bool> Pair; |
1390 | DeclContext *getDC() { return Pair.getPointer(); } |
1391 | bool shouldPushDC() { return Pair.getInt(); } |
1392 | }; |
1393 | SmallVector<ContainingDC, 4> DeclContextsToReenter; |
1394 | DeclContext *DD = FunD; |
1395 | DeclContext *NextContaining = Actions.getContainingDC(DD); |
1396 | while (DD && !DD->isTranslationUnit()) { |
1397 | bool ShouldPush = DD == NextContaining; |
1398 | DeclContextsToReenter.push_back({DD, ShouldPush}); |
1399 | if (ShouldPush) |
1400 | NextContaining = Actions.getContainingDC(DD); |
1401 | DD = DD->getLexicalParent(); |
1402 | } |
1403 | |
1404 | |
1405 | for (ContainingDC CDC : reverse(DeclContextsToReenter)) { |
1406 | TemplateParamScopeStack.push_back( |
1407 | new ParseScope(this, Scope::TemplateParamScope)); |
1408 | unsigned NumParamLists = Actions.ActOnReenterTemplateScope( |
1409 | getCurScope(), cast<Decl>(CDC.getDC())); |
1410 | CurTemplateDepthTracker.addDepth(NumParamLists); |
1411 | if (CDC.shouldPushDC()) { |
1412 | TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope)); |
1413 | Actions.PushDeclContext(Actions.getCurScope(), CDC.getDC()); |
1414 | } |
1415 | } |
1416 | |
1417 | (0) . __assert_fail ("!LPT.Toks.empty() && \"Empty body!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1417, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LPT.Toks.empty() && "Empty body!"); |
1418 | |
1419 | |
1420 | |
1421 | LPT.Toks.push_back(Tok); |
1422 | PP.EnterTokenStream(LPT.Toks, true); |
1423 | |
1424 | |
1425 | ConsumeAnyToken(); |
1426 | (0) . __assert_fail ("Tok.isOneOf(tok..l_brace, tok..colon, tok..kw_try) && \"Inline method not starting with '{', '.' or 'try'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1427, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) && |
1427 | (0) . __assert_fail ("Tok.isOneOf(tok..l_brace, tok..colon, tok..kw_try) && \"Inline method not starting with '{', '.' or 'try'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1427, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Inline method not starting with '{', ':' or 'try'"); |
1428 | |
1429 | |
1430 | |
1431 | ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | |
1432 | Scope::CompoundStmtScope); |
1433 | |
1434 | |
1435 | Sema::ContextRAII FunctionSavedContext(Actions, |
1436 | Actions.getContainingDC(FunD)); |
1437 | |
1438 | Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); |
1439 | |
1440 | if (Tok.is(tok::kw_try)) { |
1441 | ParseFunctionTryBlock(LPT.D, FnScope); |
1442 | } else { |
1443 | if (Tok.is(tok::colon)) |
1444 | ParseConstructorInitializer(LPT.D); |
1445 | else |
1446 | Actions.ActOnDefaultCtorInitializers(LPT.D); |
1447 | |
1448 | if (Tok.is(tok::l_brace)) { |
1449 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!isa<FunctionTemplateDecl>(LPT.D) || |
1450 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> cast<FunctionTemplateDecl>(LPT.D) |
1451 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ->getTemplateParameters() |
1452 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ->getDepth() == TemplateParameterDepth - 1) && |
1453 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "TemplateParameterDepth should be greater than the depth of " |
1454 | (0) . __assert_fail ("(!isa(LPT.D) || cast(LPT.D) ->getTemplateParameters() ->getDepth() == TemplateParameterDepth - 1) && \"TemplateParameterDepth should be greater than the depth of \" \"current template being instantiated!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "current template being instantiated!"); |
1455 | ParseFunctionStatementBody(LPT.D, FnScope); |
1456 | Actions.UnmarkAsLateParsedTemplate(FunD); |
1457 | } else |
1458 | Actions.ActOnFinishFunctionBody(LPT.D, nullptr); |
1459 | } |
1460 | |
1461 | |
1462 | FnScope.Exit(); |
1463 | SmallVectorImpl<ParseScope *>::reverse_iterator I = |
1464 | TemplateParamScopeStack.rbegin(); |
1465 | for (; I != TemplateParamScopeStack.rend(); ++I) |
1466 | delete *I; |
1467 | } |
1468 | |
1469 | |
1470 | void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { |
1471 | tok::TokenKind kind = Tok.getKind(); |
1472 | if (!ConsumeAndStoreFunctionPrologue(Toks)) { |
1473 | |
1474 | ConsumeAndStoreUntil(tok::r_brace, Toks, ); |
1475 | } |
1476 | |
1477 | |
1478 | if (kind == tok::kw_try) { |
1479 | while (Tok.is(tok::kw_catch)) { |
1480 | ConsumeAndStoreUntil(tok::l_brace, Toks, ); |
1481 | ConsumeAndStoreUntil(tok::r_brace, Toks, ); |
1482 | } |
1483 | } |
1484 | } |
1485 | |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) { |
1491 | TentativeParsingAction TPA(*this); |
1492 | |
1493 | if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater, |
1494 | StopAtSemi | StopBeforeMatch)) { |
1495 | TPA.Commit(); |
1496 | |
1497 | SourceLocation Greater; |
1498 | ParseGreaterThanInTemplateList(Greater, true, false); |
1499 | Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS, |
1500 | Less, Greater); |
1501 | return true; |
1502 | } |
1503 | |
1504 | |
1505 | |
1506 | TPA.Revert(); |
1507 | return false; |
1508 | } |
1509 | |
1510 | void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) { |
1511 | (0) . __assert_fail ("Tok.is(tok..less) && \"not at a potential angle bracket\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseTemplate.cpp", 1511, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::less) && "not at a potential angle bracket"); |
1512 | |
1513 | bool DependentTemplateName = false; |
1514 | if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName, |
1515 | DependentTemplateName)) |
1516 | return; |
1517 | |
1518 | |
1519 | |
1520 | |
1521 | |
1522 | if (NextToken().is(tok::greater) || |
1523 | (getLangOpts().CPlusPlus11 && |
1524 | NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) { |
1525 | SourceLocation Less = ConsumeToken(); |
1526 | SourceLocation Greater; |
1527 | ParseGreaterThanInTemplateList(Greater, true, false); |
1528 | Actions.diagnoseExprIntendedAsTemplateName( |
1529 | getCurScope(), PotentialTemplateName, Less, Greater); |
1530 | |
1531 | PotentialTemplateName = ExprError(); |
1532 | return; |
1533 | } |
1534 | |
1535 | |
1536 | |
1537 | { |
1538 | |
1539 | TentativeParsingAction TPA(*this); |
1540 | SourceLocation Less = ConsumeToken(); |
1541 | if (isTypeIdUnambiguously() && |
1542 | diagnoseUnknownTemplateId(PotentialTemplateName, Less)) { |
1543 | TPA.Commit(); |
1544 | |
1545 | PotentialTemplateName = ExprError(); |
1546 | return; |
1547 | } |
1548 | TPA.Revert(); |
1549 | } |
1550 | |
1551 | |
1552 | |
1553 | AngleBracketTracker::Priority Priority = |
1554 | (DependentTemplateName ? AngleBracketTracker::DependentName |
1555 | : AngleBracketTracker::PotentialTypo) | |
1556 | (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess |
1557 | : AngleBracketTracker::NoSpaceBeforeLess); |
1558 | AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(), |
1559 | Priority); |
1560 | } |
1561 | |
1562 | bool Parser::checkPotentialAngleBracketDelimiter( |
1563 | const AngleBracketTracker::Loc &LAngle, const Token &OpToken) { |
1564 | |
1565 | |
1566 | |
1567 | if (OpToken.is(tok::comma) && isTypeIdUnambiguously() && |
1568 | diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) { |
1569 | AngleBrackets.clear(*this); |
1570 | return true; |
1571 | } |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) && |
1577 | NextToken().is(tok::r_paren)) { |
1578 | Actions.diagnoseExprIntendedAsTemplateName( |
1579 | getCurScope(), LAngle.TemplateName, LAngle.LessLoc, |
1580 | OpToken.getLocation()); |
1581 | AngleBrackets.clear(*this); |
1582 | return true; |
1583 | } |
1584 | |
1585 | |
1586 | |
1587 | if (OpToken.is(tok::greater) || |
1588 | (getLangOpts().CPlusPlus11 && |
1589 | OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater))) |
1590 | AngleBrackets.clear(*this); |
1591 | return false; |
1592 | } |
1593 | |