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