150
|
1 //===--- Parser.cpp - C Language Family Parser ----------------------------===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8 //
|
|
9 // This file implements the Parser interfaces.
|
|
10 //
|
|
11 //===----------------------------------------------------------------------===//
|
|
12
|
|
13 #include "clang/Parse/Parser.h"
|
|
14 #include "clang/AST/ASTConsumer.h"
|
|
15 #include "clang/AST/ASTContext.h"
|
|
16 #include "clang/AST/DeclTemplate.h"
|
252
|
17 #include "clang/AST/ASTLambda.h"
|
173
|
18 #include "clang/Basic/FileManager.h"
|
150
|
19 #include "clang/Parse/ParseDiagnostic.h"
|
|
20 #include "clang/Parse/RAIIObjectsForParser.h"
|
|
21 #include "clang/Sema/DeclSpec.h"
|
|
22 #include "clang/Sema/ParsedTemplate.h"
|
|
23 #include "clang/Sema/Scope.h"
|
|
24 #include "llvm/Support/Path.h"
|
|
25 using namespace clang;
|
|
26
|
|
27
|
|
28 namespace {
|
|
29 /// A comment handler that passes comments found by the preprocessor
|
|
30 /// to the parser action.
|
|
31 class ActionCommentHandler : public CommentHandler {
|
|
32 Sema &S;
|
|
33
|
|
34 public:
|
|
35 explicit ActionCommentHandler(Sema &S) : S(S) { }
|
|
36
|
|
37 bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
|
|
38 S.ActOnComment(Comment);
|
|
39 return false;
|
|
40 }
|
|
41 };
|
|
42 } // end anonymous namespace
|
|
43
|
|
44 IdentifierInfo *Parser::getSEHExceptKeyword() {
|
|
45 // __except is accepted as a (contextual) keyword
|
|
46 if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
|
|
47 Ident__except = PP.getIdentifierInfo("__except");
|
|
48
|
|
49 return Ident__except;
|
|
50 }
|
|
51
|
|
52 Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
|
207
|
53 : PP(pp), PreferredType(pp.isCodeCompletionEnabled()), Actions(actions),
|
|
54 Diags(PP.getDiagnostics()), GreaterThanIsOperator(true),
|
|
55 ColonIsSacred(false), InMessageExpression(false),
|
|
56 TemplateParameterDepth(0), ParsingInObjCContainer(false) {
|
150
|
57 SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
|
|
58 Tok.startToken();
|
|
59 Tok.setKind(tok::eof);
|
|
60 Actions.CurScope = nullptr;
|
|
61 NumCachedScopes = 0;
|
|
62 CurParsedObjCImpl = nullptr;
|
|
63
|
|
64 // Add #pragma handlers. These are removed and destroyed in the
|
|
65 // destructor.
|
|
66 initializePragmaHandlers();
|
|
67
|
|
68 CommentSemaHandler.reset(new ActionCommentHandler(actions));
|
|
69 PP.addCommentHandler(CommentSemaHandler.get());
|
|
70
|
|
71 PP.setCodeCompletionHandler(*this);
|
240
|
72 #ifndef noCbC
|
|
73 UniqueId = 0; // for CreateUniqueIdentifier()
|
|
74 #endif
|
150
|
75 }
|
|
76
|
|
77 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
|
|
78 return Diags.Report(Loc, DiagID);
|
|
79 }
|
|
80
|
|
81 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
|
|
82 return Diag(Tok.getLocation(), DiagID);
|
|
83 }
|
|
84
|
|
85 /// Emits a diagnostic suggesting parentheses surrounding a
|
|
86 /// given range.
|
|
87 ///
|
|
88 /// \param Loc The location where we'll emit the diagnostic.
|
|
89 /// \param DK The kind of diagnostic to emit.
|
|
90 /// \param ParenRange Source range enclosing code that should be parenthesized.
|
|
91 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
|
|
92 SourceRange ParenRange) {
|
|
93 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
|
|
94 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
|
|
95 // We can't display the parentheses, so just dig the
|
|
96 // warning/error and return.
|
|
97 Diag(Loc, DK);
|
|
98 return;
|
|
99 }
|
|
100
|
|
101 Diag(Loc, DK)
|
|
102 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
|
|
103 << FixItHint::CreateInsertion(EndLoc, ")");
|
|
104 }
|
|
105
|
|
106 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
|
|
107 switch (ExpectedTok) {
|
|
108 case tok::semi:
|
|
109 return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
|
|
110 default: return false;
|
|
111 }
|
|
112 }
|
|
113
|
|
114 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
|
|
115 StringRef Msg) {
|
|
116 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
|
|
117 ConsumeAnyToken();
|
|
118 return false;
|
|
119 }
|
|
120
|
|
121 // Detect common single-character typos and resume.
|
|
122 if (IsCommonTypo(ExpectedTok, Tok)) {
|
|
123 SourceLocation Loc = Tok.getLocation();
|
|
124 {
|
|
125 DiagnosticBuilder DB = Diag(Loc, DiagID);
|
|
126 DB << FixItHint::CreateReplacement(
|
|
127 SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
|
|
128 if (DiagID == diag::err_expected)
|
|
129 DB << ExpectedTok;
|
|
130 else if (DiagID == diag::err_expected_after)
|
|
131 DB << Msg << ExpectedTok;
|
|
132 else
|
|
133 DB << Msg;
|
|
134 }
|
|
135
|
|
136 // Pretend there wasn't a problem.
|
|
137 ConsumeAnyToken();
|
|
138 return false;
|
|
139 }
|
|
140
|
|
141 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
|
142 const char *Spelling = nullptr;
|
|
143 if (EndLoc.isValid())
|
|
144 Spelling = tok::getPunctuatorSpelling(ExpectedTok);
|
|
145
|
|
146 DiagnosticBuilder DB =
|
|
147 Spelling
|
|
148 ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling)
|
|
149 : Diag(Tok, DiagID);
|
|
150 if (DiagID == diag::err_expected)
|
|
151 DB << ExpectedTok;
|
|
152 else if (DiagID == diag::err_expected_after)
|
|
153 DB << Msg << ExpectedTok;
|
|
154 else
|
|
155 DB << Msg;
|
|
156
|
|
157 return true;
|
|
158 }
|
|
159
|
236
|
160 bool Parser::ExpectAndConsumeSemi(unsigned DiagID, StringRef TokenUsed) {
|
150
|
161 if (TryConsumeToken(tok::semi))
|
|
162 return false;
|
|
163
|
|
164 if (Tok.is(tok::code_completion)) {
|
|
165 handleUnexpectedCodeCompletionToken();
|
|
166 return false;
|
|
167 }
|
|
168
|
|
169 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
|
|
170 NextToken().is(tok::semi)) {
|
|
171 Diag(Tok, diag::err_extraneous_token_before_semi)
|
|
172 << PP.getSpelling(Tok)
|
|
173 << FixItHint::CreateRemoval(Tok.getLocation());
|
|
174 ConsumeAnyToken(); // The ')' or ']'.
|
|
175 ConsumeToken(); // The ';'.
|
|
176 return false;
|
|
177 }
|
|
178
|
236
|
179 return ExpectAndConsume(tok::semi, DiagID , TokenUsed);
|
150
|
180 }
|
|
181
|
|
182 void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
|
|
183 if (!Tok.is(tok::semi)) return;
|
|
184
|
|
185 bool HadMultipleSemis = false;
|
|
186 SourceLocation StartLoc = Tok.getLocation();
|
|
187 SourceLocation EndLoc = Tok.getLocation();
|
|
188 ConsumeToken();
|
|
189
|
|
190 while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
|
|
191 HadMultipleSemis = true;
|
|
192 EndLoc = Tok.getLocation();
|
|
193 ConsumeToken();
|
|
194 }
|
|
195
|
|
196 // C++11 allows extra semicolons at namespace scope, but not in any of the
|
|
197 // other contexts.
|
|
198 if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
|
|
199 if (getLangOpts().CPlusPlus11)
|
|
200 Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
|
|
201 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
202 else
|
|
203 Diag(StartLoc, diag::ext_extra_semi_cxx11)
|
|
204 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
205 return;
|
|
206 }
|
|
207
|
|
208 if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
|
|
209 Diag(StartLoc, diag::ext_extra_semi)
|
|
210 << Kind << DeclSpec::getSpecifierName(TST,
|
|
211 Actions.getASTContext().getPrintingPolicy())
|
|
212 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
213 else
|
|
214 // A single semicolon is valid after a member function definition.
|
|
215 Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
|
|
216 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
217 }
|
|
218
|
|
219 bool Parser::expectIdentifier() {
|
|
220 if (Tok.is(tok::identifier))
|
|
221 return false;
|
|
222 if (const auto *II = Tok.getIdentifierInfo()) {
|
|
223 if (II->isCPlusPlusKeyword(getLangOpts())) {
|
|
224 Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword)
|
|
225 << tok::identifier << Tok.getIdentifierInfo();
|
|
226 // Objective-C++: Recover by treating this keyword as a valid identifier.
|
|
227 return false;
|
|
228 }
|
|
229 }
|
|
230 Diag(Tok, diag::err_expected) << tok::identifier;
|
|
231 return true;
|
|
232 }
|
|
233
|
207
|
234 void Parser::checkCompoundToken(SourceLocation FirstTokLoc,
|
|
235 tok::TokenKind FirstTokKind, CompoundToken Op) {
|
|
236 if (FirstTokLoc.isInvalid())
|
|
237 return;
|
|
238 SourceLocation SecondTokLoc = Tok.getLocation();
|
|
239
|
|
240 // If either token is in a macro, we expect both tokens to come from the same
|
|
241 // macro expansion.
|
|
242 if ((FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID()) &&
|
|
243 PP.getSourceManager().getFileID(FirstTokLoc) !=
|
|
244 PP.getSourceManager().getFileID(SecondTokLoc)) {
|
|
245 Diag(FirstTokLoc, diag::warn_compound_token_split_by_macro)
|
|
246 << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
|
|
247 << static_cast<int>(Op) << SourceRange(FirstTokLoc);
|
|
248 Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here)
|
|
249 << (FirstTokKind == Tok.getKind()) << Tok.getKind()
|
|
250 << SourceRange(SecondTokLoc);
|
|
251 return;
|
|
252 }
|
|
253
|
|
254 // We expect the tokens to abut.
|
|
255 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
|
|
256 SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc);
|
|
257 if (SpaceLoc.isInvalid())
|
|
258 SpaceLoc = FirstTokLoc;
|
|
259 Diag(SpaceLoc, diag::warn_compound_token_split_by_whitespace)
|
|
260 << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
|
|
261 << static_cast<int>(Op) << SourceRange(FirstTokLoc, SecondTokLoc);
|
|
262 return;
|
|
263 }
|
|
264 }
|
|
265
|
150
|
266 //===----------------------------------------------------------------------===//
|
|
267 // Error recovery.
|
|
268 //===----------------------------------------------------------------------===//
|
|
269
|
|
270 static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {
|
|
271 return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
|
|
272 }
|
|
273
|
|
274 /// SkipUntil - Read tokens until we get to the specified token, then consume
|
|
275 /// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the
|
|
276 /// token will ever occur, this skips to the next token, or to some likely
|
|
277 /// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
|
|
278 /// character.
|
|
279 ///
|
|
280 /// If SkipUntil finds the specified token, it returns true, otherwise it
|
|
281 /// returns false.
|
|
282 bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) {
|
|
283 // We always want this function to skip at least one token if the first token
|
|
284 // isn't T and if not at EOF.
|
|
285 bool isFirstTokenSkipped = true;
|
236
|
286 while (true) {
|
150
|
287 // If we found one of the tokens, stop and return true.
|
|
288 for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
|
|
289 if (Tok.is(Toks[i])) {
|
|
290 if (HasFlagsSet(Flags, StopBeforeMatch)) {
|
|
291 // Noop, don't consume the token.
|
|
292 } else {
|
|
293 ConsumeAnyToken();
|
|
294 }
|
|
295 return true;
|
|
296 }
|
|
297 }
|
|
298
|
|
299 // Important special case: The caller has given up and just wants us to
|
|
300 // skip the rest of the file. Do this without recursing, since we can
|
|
301 // get here precisely because the caller detected too much recursion.
|
|
302 if (Toks.size() == 1 && Toks[0] == tok::eof &&
|
|
303 !HasFlagsSet(Flags, StopAtSemi) &&
|
|
304 !HasFlagsSet(Flags, StopAtCodeCompletion)) {
|
|
305 while (Tok.isNot(tok::eof))
|
|
306 ConsumeAnyToken();
|
|
307 return true;
|
|
308 }
|
|
309
|
|
310 switch (Tok.getKind()) {
|
|
311 case tok::eof:
|
|
312 // Ran out of tokens.
|
|
313 return false;
|
|
314
|
|
315 case tok::annot_pragma_openmp:
|
223
|
316 case tok::annot_attr_openmp:
|
150
|
317 case tok::annot_pragma_openmp_end:
|
|
318 // Stop before an OpenMP pragma boundary.
|
|
319 if (OpenMPDirectiveParsing)
|
|
320 return false;
|
|
321 ConsumeAnnotationToken();
|
|
322 break;
|
|
323 case tok::annot_module_begin:
|
|
324 case tok::annot_module_end:
|
|
325 case tok::annot_module_include:
|
252
|
326 case tok::annot_repl_input_end:
|
150
|
327 // Stop before we change submodules. They generally indicate a "good"
|
|
328 // place to pick up parsing again (except in the special case where
|
|
329 // we're trying to skip to EOF).
|
|
330 return false;
|
|
331
|
|
332 case tok::code_completion:
|
|
333 if (!HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
334 handleUnexpectedCodeCompletionToken();
|
|
335 return false;
|
|
336
|
|
337 case tok::l_paren:
|
|
338 // Recursively skip properly-nested parens.
|
|
339 ConsumeParen();
|
|
340 if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
341 SkipUntil(tok::r_paren, StopAtCodeCompletion);
|
|
342 else
|
|
343 SkipUntil(tok::r_paren);
|
|
344 break;
|
|
345 case tok::l_square:
|
|
346 // Recursively skip properly-nested square brackets.
|
|
347 ConsumeBracket();
|
|
348 if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
349 SkipUntil(tok::r_square, StopAtCodeCompletion);
|
|
350 else
|
|
351 SkipUntil(tok::r_square);
|
|
352 break;
|
|
353 case tok::l_brace:
|
|
354 // Recursively skip properly-nested braces.
|
|
355 ConsumeBrace();
|
|
356 if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
357 SkipUntil(tok::r_brace, StopAtCodeCompletion);
|
|
358 else
|
|
359 SkipUntil(tok::r_brace);
|
|
360 break;
|
|
361 case tok::question:
|
|
362 // Recursively skip ? ... : pairs; these function as brackets. But
|
|
363 // still stop at a semicolon if requested.
|
|
364 ConsumeToken();
|
|
365 SkipUntil(tok::colon,
|
|
366 SkipUntilFlags(unsigned(Flags) &
|
|
367 unsigned(StopAtCodeCompletion | StopAtSemi)));
|
|
368 break;
|
|
369
|
|
370 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
|
|
371 // Since the user wasn't looking for this token (if they were, it would
|
|
372 // already be handled), this isn't balanced. If there is a LHS token at a
|
|
373 // higher level, we will assume that this matches the unbalanced token
|
|
374 // and return it. Otherwise, this is a spurious RHS token, which we skip.
|
|
375 case tok::r_paren:
|
|
376 if (ParenCount && !isFirstTokenSkipped)
|
|
377 return false; // Matches something.
|
|
378 ConsumeParen();
|
|
379 break;
|
|
380 case tok::r_square:
|
|
381 if (BracketCount && !isFirstTokenSkipped)
|
|
382 return false; // Matches something.
|
|
383 ConsumeBracket();
|
|
384 break;
|
|
385 case tok::r_brace:
|
|
386 if (BraceCount && !isFirstTokenSkipped)
|
|
387 return false; // Matches something.
|
|
388 ConsumeBrace();
|
|
389 break;
|
|
390
|
|
391 case tok::semi:
|
|
392 if (HasFlagsSet(Flags, StopAtSemi))
|
|
393 return false;
|
236
|
394 [[fallthrough]];
|
150
|
395 default:
|
|
396 // Skip this token.
|
|
397 ConsumeAnyToken();
|
|
398 break;
|
|
399 }
|
|
400 isFirstTokenSkipped = false;
|
|
401 }
|
|
402 }
|
|
403
|
|
404 //===----------------------------------------------------------------------===//
|
|
405 // Scope manipulation
|
|
406 //===----------------------------------------------------------------------===//
|
|
407
|
|
408 /// EnterScope - Start a new scope.
|
|
409 void Parser::EnterScope(unsigned ScopeFlags) {
|
|
410 if (NumCachedScopes) {
|
|
411 Scope *N = ScopeCache[--NumCachedScopes];
|
|
412 N->Init(getCurScope(), ScopeFlags);
|
|
413 Actions.CurScope = N;
|
|
414 } else {
|
|
415 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
|
|
416 }
|
|
417 }
|
|
418
|
|
419 /// ExitScope - Pop a scope off the scope stack.
|
|
420 void Parser::ExitScope() {
|
|
421 assert(getCurScope() && "Scope imbalance!");
|
|
422
|
|
423 // Inform the actions module that this scope is going away if there are any
|
|
424 // decls in it.
|
|
425 Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
|
|
426
|
|
427 Scope *OldScope = getCurScope();
|
|
428 Actions.CurScope = OldScope->getParent();
|
|
429
|
|
430 if (NumCachedScopes == ScopeCacheSize)
|
|
431 delete OldScope;
|
|
432 else
|
|
433 ScopeCache[NumCachedScopes++] = OldScope;
|
|
434 }
|
|
435
|
|
436 /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
|
|
437 /// this object does nothing.
|
|
438 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
|
|
439 bool ManageFlags)
|
|
440 : CurScope(ManageFlags ? Self->getCurScope() : nullptr) {
|
|
441 if (CurScope) {
|
|
442 OldFlags = CurScope->getFlags();
|
|
443 CurScope->setFlags(ScopeFlags);
|
|
444 }
|
|
445 }
|
|
446
|
|
447 /// Restore the flags for the current scope to what they were before this
|
|
448 /// object overrode them.
|
|
449 Parser::ParseScopeFlags::~ParseScopeFlags() {
|
|
450 if (CurScope)
|
|
451 CurScope->setFlags(OldFlags);
|
|
452 }
|
|
453
|
|
454
|
|
455 //===----------------------------------------------------------------------===//
|
|
456 // C99 6.9: External Definitions.
|
|
457 //===----------------------------------------------------------------------===//
|
|
458
|
|
459 Parser::~Parser() {
|
|
460 // If we still have scopes active, delete the scope tree.
|
|
461 delete getCurScope();
|
|
462 Actions.CurScope = nullptr;
|
|
463
|
|
464 // Free the scope cache.
|
|
465 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
|
|
466 delete ScopeCache[i];
|
|
467
|
|
468 resetPragmaHandlers();
|
|
469
|
|
470 PP.removeCommentHandler(CommentSemaHandler.get());
|
|
471
|
|
472 PP.clearCodeCompletionHandler();
|
|
473
|
173
|
474 DestroyTemplateIds();
|
150
|
475 }
|
|
476
|
|
477 /// Initialize - Warm up the parser.
|
|
478 ///
|
|
479 void Parser::Initialize() {
|
|
480 // Create the translation unit scope. Install it as the current scope.
|
|
481 assert(getCurScope() == nullptr && "A scope is already active?");
|
|
482 EnterScope(Scope::DeclScope);
|
|
483 Actions.ActOnTranslationUnitScope(getCurScope());
|
|
484
|
|
485 // Initialization for Objective-C context sensitive keywords recognition.
|
|
486 // Referenced in Parser::ParseObjCTypeQualifierList.
|
|
487 if (getLangOpts().ObjC) {
|
|
488 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
|
|
489 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
|
|
490 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
|
|
491 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
|
|
492 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
|
|
493 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
|
|
494 ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull");
|
|
495 ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable");
|
|
496 ObjCTypeQuals[objc_null_unspecified]
|
|
497 = &PP.getIdentifierTable().get("null_unspecified");
|
|
498 }
|
|
499
|
|
500 Ident_instancetype = nullptr;
|
|
501 Ident_final = nullptr;
|
|
502 Ident_sealed = nullptr;
|
207
|
503 Ident_abstract = nullptr;
|
150
|
504 Ident_override = nullptr;
|
|
505 Ident_GNU_final = nullptr;
|
|
506 Ident_import = nullptr;
|
|
507 Ident_module = nullptr;
|
|
508
|
|
509 Ident_super = &PP.getIdentifierTable().get("super");
|
|
510
|
|
511 Ident_vector = nullptr;
|
|
512 Ident_bool = nullptr;
|
207
|
513 Ident_Bool = nullptr;
|
150
|
514 Ident_pixel = nullptr;
|
|
515 if (getLangOpts().AltiVec || getLangOpts().ZVector) {
|
|
516 Ident_vector = &PP.getIdentifierTable().get("vector");
|
|
517 Ident_bool = &PP.getIdentifierTable().get("bool");
|
207
|
518 Ident_Bool = &PP.getIdentifierTable().get("_Bool");
|
150
|
519 }
|
|
520 if (getLangOpts().AltiVec)
|
|
521 Ident_pixel = &PP.getIdentifierTable().get("pixel");
|
|
522
|
|
523 Ident_introduced = nullptr;
|
|
524 Ident_deprecated = nullptr;
|
|
525 Ident_obsoleted = nullptr;
|
|
526 Ident_unavailable = nullptr;
|
|
527 Ident_strict = nullptr;
|
|
528 Ident_replacement = nullptr;
|
|
529
|
252
|
530 Ident_language = Ident_defined_in = Ident_generated_declaration = Ident_USR =
|
|
531 nullptr;
|
150
|
532
|
|
533 Ident__except = nullptr;
|
|
534
|
|
535 Ident__exception_code = Ident__exception_info = nullptr;
|
|
536 Ident__abnormal_termination = Ident___exception_code = nullptr;
|
|
537 Ident___exception_info = Ident___abnormal_termination = nullptr;
|
|
538 Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr;
|
|
539 Ident_AbnormalTermination = nullptr;
|
|
540
|
|
541 if(getLangOpts().Borland) {
|
|
542 Ident__exception_info = PP.getIdentifierInfo("_exception_info");
|
|
543 Ident___exception_info = PP.getIdentifierInfo("__exception_info");
|
|
544 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
|
|
545 Ident__exception_code = PP.getIdentifierInfo("_exception_code");
|
|
546 Ident___exception_code = PP.getIdentifierInfo("__exception_code");
|
|
547 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
|
|
548 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
|
|
549 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
|
|
550 Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
|
|
551
|
|
552 PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
|
|
553 PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
|
|
554 PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
|
|
555 PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
|
|
556 PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
|
|
557 PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
|
|
558 PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
|
|
559 PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
|
|
560 PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
|
|
561 }
|
|
562
|
|
563 if (getLangOpts().CPlusPlusModules) {
|
|
564 Ident_import = PP.getIdentifierInfo("import");
|
|
565 Ident_module = PP.getIdentifierInfo("module");
|
|
566 }
|
|
567
|
|
568 Actions.Initialize();
|
|
569
|
|
570 // Prime the lexer look-ahead.
|
|
571 ConsumeToken();
|
|
572 }
|
|
573
|
173
|
574 void Parser::DestroyTemplateIds() {
|
|
575 for (TemplateIdAnnotation *Id : TemplateIds)
|
|
576 Id->Destroy();
|
|
577 TemplateIds.clear();
|
150
|
578 }
|
|
579
|
|
580 /// Parse the first top-level declaration in a translation unit.
|
|
581 ///
|
|
582 /// translation-unit:
|
|
583 /// [C] external-declaration
|
|
584 /// [C] translation-unit external-declaration
|
|
585 /// [C++] top-level-declaration-seq[opt]
|
|
586 /// [C++20] global-module-fragment[opt] module-declaration
|
|
587 /// top-level-declaration-seq[opt] private-module-fragment[opt]
|
|
588 ///
|
|
589 /// Note that in C, it is an error if there is no first declaration.
|
236
|
590 bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result,
|
|
591 Sema::ModuleImportState &ImportState) {
|
150
|
592 Actions.ActOnStartOfTranslationUnit();
|
|
593
|
236
|
594 // For C++20 modules, a module decl must be the first in the TU. We also
|
|
595 // need to track module imports.
|
|
596 ImportState = Sema::ModuleImportState::FirstDecl;
|
|
597 bool NoTopLevelDecls = ParseTopLevelDecl(Result, ImportState);
|
|
598
|
150
|
599 // C11 6.9p1 says translation units must have at least one top-level
|
|
600 // declaration. C++ doesn't have this restriction. We also don't want to
|
|
601 // complain if we have a precompiled header, although technically if the PCH
|
|
602 // is empty we should still emit the (pedantic) diagnostic.
|
207
|
603 // If the main file is a header, we're only pretending it's a TU; don't warn.
|
150
|
604 if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() &&
|
207
|
605 !getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile)
|
150
|
606 Diag(diag::ext_empty_translation_unit);
|
|
607
|
|
608 return NoTopLevelDecls;
|
|
609 }
|
|
610
|
|
611 /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
|
|
612 /// action tells us to. This returns true if the EOF was encountered.
|
|
613 ///
|
|
614 /// top-level-declaration:
|
|
615 /// declaration
|
|
616 /// [C++20] module-import-declaration
|
236
|
617 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
|
|
618 Sema::ModuleImportState &ImportState) {
|
173
|
619 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
|
150
|
620
|
|
621 Result = nullptr;
|
|
622 switch (Tok.getKind()) {
|
|
623 case tok::annot_pragma_unused:
|
|
624 HandlePragmaUnused();
|
|
625 return false;
|
|
626
|
|
627 case tok::kw_export:
|
|
628 switch (NextToken().getKind()) {
|
|
629 case tok::kw_module:
|
|
630 goto module_decl;
|
|
631
|
|
632 // Note: no need to handle kw_import here. We only form kw_import under
|
252
|
633 // the Standard C++ Modules, and in that case 'export import' is parsed as
|
|
634 // an export-declaration containing an import-declaration.
|
150
|
635
|
|
636 // Recognize context-sensitive C++20 'export module' and 'export import'
|
|
637 // declarations.
|
|
638 case tok::identifier: {
|
|
639 IdentifierInfo *II = NextToken().getIdentifierInfo();
|
|
640 if ((II == Ident_module || II == Ident_import) &&
|
|
641 GetLookAheadToken(2).isNot(tok::coloncolon)) {
|
|
642 if (II == Ident_module)
|
|
643 goto module_decl;
|
|
644 else
|
|
645 goto import_decl;
|
|
646 }
|
|
647 break;
|
|
648 }
|
|
649
|
|
650 default:
|
|
651 break;
|
|
652 }
|
|
653 break;
|
|
654
|
|
655 case tok::kw_module:
|
|
656 module_decl:
|
236
|
657 Result = ParseModuleDecl(ImportState);
|
150
|
658 return false;
|
|
659
|
236
|
660 case tok::kw_import:
|
150
|
661 import_decl: {
|
236
|
662 Decl *ImportDecl = ParseModuleImport(SourceLocation(), ImportState);
|
150
|
663 Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
|
|
664 return false;
|
|
665 }
|
|
666
|
236
|
667 case tok::annot_module_include: {
|
|
668 auto Loc = Tok.getLocation();
|
|
669 Module *Mod = reinterpret_cast<Module *>(Tok.getAnnotationValue());
|
|
670 // FIXME: We need a better way to disambiguate C++ clang modules and
|
|
671 // standard C++ modules.
|
|
672 if (!getLangOpts().CPlusPlusModules || !Mod->isHeaderUnit())
|
|
673 Actions.ActOnModuleInclude(Loc, Mod);
|
|
674 else {
|
|
675 DeclResult Import =
|
|
676 Actions.ActOnModuleImport(Loc, SourceLocation(), Loc, Mod);
|
|
677 Decl *ImportDecl = Import.isInvalid() ? nullptr : Import.get();
|
|
678 Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
|
|
679 }
|
150
|
680 ConsumeAnnotationToken();
|
|
681 return false;
|
236
|
682 }
|
150
|
683
|
|
684 case tok::annot_module_begin:
|
|
685 Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>(
|
|
686 Tok.getAnnotationValue()));
|
|
687 ConsumeAnnotationToken();
|
236
|
688 ImportState = Sema::ModuleImportState::NotACXX20Module;
|
150
|
689 return false;
|
|
690
|
|
691 case tok::annot_module_end:
|
|
692 Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>(
|
|
693 Tok.getAnnotationValue()));
|
|
694 ConsumeAnnotationToken();
|
236
|
695 ImportState = Sema::ModuleImportState::NotACXX20Module;
|
150
|
696 return false;
|
|
697
|
|
698 case tok::eof:
|
252
|
699 case tok::annot_repl_input_end:
|
150
|
700 // Check whether -fmax-tokens= was reached.
|
|
701 if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) {
|
|
702 PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total)
|
|
703 << PP.getTokenCount() << PP.getMaxTokens();
|
|
704 SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc();
|
|
705 if (OverrideLoc.isValid()) {
|
|
706 PP.Diag(OverrideLoc, diag::note_max_tokens_total_override);
|
|
707 }
|
|
708 }
|
|
709
|
|
710 // Late template parsing can begin.
|
207
|
711 Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr, this);
|
252
|
712 Actions.ActOnEndOfTranslationUnit();
|
150
|
713 //else don't tell Sema that we ended parsing: more input might come.
|
|
714 return true;
|
|
715
|
|
716 case tok::identifier:
|
|
717 // C++2a [basic.link]p3:
|
|
718 // A token sequence beginning with 'export[opt] module' or
|
|
719 // 'export[opt] import' and not immediately followed by '::'
|
|
720 // is never interpreted as the declaration of a top-level-declaration.
|
|
721 if ((Tok.getIdentifierInfo() == Ident_module ||
|
|
722 Tok.getIdentifierInfo() == Ident_import) &&
|
|
723 NextToken().isNot(tok::coloncolon)) {
|
|
724 if (Tok.getIdentifierInfo() == Ident_module)
|
|
725 goto module_decl;
|
|
726 else
|
|
727 goto import_decl;
|
|
728 }
|
|
729 break;
|
|
730
|
|
731 default:
|
|
732 break;
|
|
733 }
|
|
734
|
252
|
735 ParsedAttributes DeclAttrs(AttrFactory);
|
|
736 ParsedAttributes DeclSpecAttrs(AttrFactory);
|
|
737 // GNU attributes are applied to the declaration specification while the
|
|
738 // standard attributes are applied to the declaration. We parse the two
|
|
739 // attribute sets into different containters so we can apply them during
|
|
740 // the regular parsing process.
|
|
741 while (MaybeParseCXX11Attributes(DeclAttrs) ||
|
|
742 MaybeParseGNUAttributes(DeclSpecAttrs))
|
|
743 ;
|
150
|
744
|
252
|
745 Result = ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
|
236
|
746 // An empty Result might mean a line with ';' or some parsing error, ignore
|
|
747 // it.
|
|
748 if (Result) {
|
|
749 if (ImportState == Sema::ModuleImportState::FirstDecl)
|
|
750 // First decl was not modular.
|
|
751 ImportState = Sema::ModuleImportState::NotACXX20Module;
|
|
752 else if (ImportState == Sema::ModuleImportState::ImportAllowed)
|
|
753 // Non-imports disallow further imports.
|
|
754 ImportState = Sema::ModuleImportState::ImportFinished;
|
252
|
755 else if (ImportState ==
|
|
756 Sema::ModuleImportState::PrivateFragmentImportAllowed)
|
|
757 // Non-imports disallow further imports.
|
|
758 ImportState = Sema::ModuleImportState::PrivateFragmentImportFinished;
|
236
|
759 }
|
150
|
760 return false;
|
|
761 }
|
|
762
|
|
763 /// ParseExternalDeclaration:
|
|
764 ///
|
236
|
765 /// The `Attrs` that are passed in are C++11 attributes and appertain to the
|
|
766 /// declaration.
|
|
767 ///
|
150
|
768 /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
|
|
769 /// function-definition
|
|
770 /// declaration
|
|
771 /// [GNU] asm-definition
|
|
772 /// [GNU] __extension__ external-declaration
|
|
773 /// [OBJC] objc-class-definition
|
|
774 /// [OBJC] objc-class-declaration
|
|
775 /// [OBJC] objc-alias-declaration
|
|
776 /// [OBJC] objc-protocol-definition
|
|
777 /// [OBJC] objc-method-definition
|
|
778 /// [OBJC] @end
|
|
779 /// [C++] linkage-specification
|
|
780 /// [GNU] asm-definition:
|
|
781 /// simple-asm-expr ';'
|
|
782 /// [C++11] empty-declaration
|
|
783 /// [C++11] attribute-declaration
|
|
784 ///
|
|
785 /// [C++11] empty-declaration:
|
|
786 /// ';'
|
|
787 ///
|
|
788 /// [C++0x/GNU] 'extern' 'template' declaration
|
|
789 ///
|
252
|
790 /// [C++20] module-import-declaration
|
150
|
791 ///
|
252
|
792 Parser::DeclGroupPtrTy
|
|
793 Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
|
|
794 ParsedAttributes &DeclSpecAttrs,
|
|
795 ParsingDeclSpec *DS) {
|
173
|
796 DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
|
150
|
797 ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
|
798
|
|
799 if (PP.isCodeCompletionReached()) {
|
|
800 cutOffParsing();
|
|
801 return nullptr;
|
|
802 }
|
|
803
|
|
804 Decl *SingleDecl = nullptr;
|
|
805 switch (Tok.getKind()) {
|
|
806 case tok::annot_pragma_vis:
|
|
807 HandlePragmaVisibility();
|
|
808 return nullptr;
|
|
809 case tok::annot_pragma_pack:
|
|
810 HandlePragmaPack();
|
|
811 return nullptr;
|
|
812 case tok::annot_pragma_msstruct:
|
|
813 HandlePragmaMSStruct();
|
|
814 return nullptr;
|
|
815 case tok::annot_pragma_align:
|
|
816 HandlePragmaAlign();
|
|
817 return nullptr;
|
|
818 case tok::annot_pragma_weak:
|
|
819 HandlePragmaWeak();
|
|
820 return nullptr;
|
|
821 case tok::annot_pragma_weakalias:
|
|
822 HandlePragmaWeakAlias();
|
|
823 return nullptr;
|
|
824 case tok::annot_pragma_redefine_extname:
|
|
825 HandlePragmaRedefineExtname();
|
|
826 return nullptr;
|
|
827 case tok::annot_pragma_fp_contract:
|
|
828 HandlePragmaFPContract();
|
|
829 return nullptr;
|
|
830 case tok::annot_pragma_fenv_access:
|
236
|
831 case tok::annot_pragma_fenv_access_ms:
|
150
|
832 HandlePragmaFEnvAccess();
|
|
833 return nullptr;
|
207
|
834 case tok::annot_pragma_fenv_round:
|
|
835 HandlePragmaFEnvRound();
|
|
836 return nullptr;
|
173
|
837 case tok::annot_pragma_float_control:
|
|
838 HandlePragmaFloatControl();
|
|
839 return nullptr;
|
150
|
840 case tok::annot_pragma_fp:
|
|
841 HandlePragmaFP();
|
|
842 break;
|
|
843 case tok::annot_pragma_opencl_extension:
|
|
844 HandlePragmaOpenCLExtension();
|
|
845 return nullptr;
|
223
|
846 case tok::annot_attr_openmp:
|
150
|
847 case tok::annot_pragma_openmp: {
|
|
848 AccessSpecifier AS = AS_none;
|
236
|
849 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
|
150
|
850 }
|
|
851 case tok::annot_pragma_ms_pointers_to_members:
|
|
852 HandlePragmaMSPointersToMembers();
|
|
853 return nullptr;
|
|
854 case tok::annot_pragma_ms_vtordisp:
|
|
855 HandlePragmaMSVtorDisp();
|
|
856 return nullptr;
|
|
857 case tok::annot_pragma_ms_pragma:
|
|
858 HandlePragmaMSPragma();
|
|
859 return nullptr;
|
|
860 case tok::annot_pragma_dump:
|
|
861 HandlePragmaDump();
|
|
862 return nullptr;
|
|
863 case tok::annot_pragma_attribute:
|
|
864 HandlePragmaAttribute();
|
|
865 return nullptr;
|
|
866 case tok::semi:
|
|
867 // Either a C++11 empty-declaration or attribute-declaration.
|
|
868 SingleDecl =
|
236
|
869 Actions.ActOnEmptyDeclaration(getCurScope(), Attrs, Tok.getLocation());
|
150
|
870 ConsumeExtraSemi(OutsideFunction);
|
|
871 break;
|
|
872 case tok::r_brace:
|
|
873 Diag(Tok, diag::err_extraneous_closing_brace);
|
|
874 ConsumeBrace();
|
|
875 return nullptr;
|
|
876 case tok::eof:
|
|
877 Diag(Tok, diag::err_expected_external_declaration);
|
|
878 return nullptr;
|
|
879 case tok::kw___extension__: {
|
|
880 // __extension__ silences extension warnings in the subexpression.
|
|
881 ExtensionRAIIObject O(Diags); // Use RAII to do this.
|
|
882 ConsumeToken();
|
252
|
883 return ParseExternalDeclaration(Attrs, DeclSpecAttrs);
|
150
|
884 }
|
|
885 case tok::kw_asm: {
|
236
|
886 ProhibitAttributes(Attrs);
|
150
|
887
|
|
888 SourceLocation StartLoc = Tok.getLocation();
|
|
889 SourceLocation EndLoc;
|
|
890
|
|
891 ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc));
|
|
892
|
|
893 // Check if GNU-style InlineAsm is disabled.
|
|
894 // Empty asm string is allowed because it will not introduce
|
|
895 // any assembly code.
|
|
896 if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
|
|
897 const auto *SL = cast<StringLiteral>(Result.get());
|
|
898 if (!SL->getString().trim().empty())
|
|
899 Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
|
|
900 }
|
|
901
|
|
902 ExpectAndConsume(tok::semi, diag::err_expected_after,
|
|
903 "top-level asm block");
|
|
904
|
|
905 if (Result.isInvalid())
|
|
906 return nullptr;
|
|
907 SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
|
|
908 break;
|
|
909 }
|
|
910 case tok::at:
|
252
|
911 return ParseObjCAtDirectives(Attrs, DeclSpecAttrs);
|
150
|
912 case tok::minus:
|
|
913 case tok::plus:
|
|
914 if (!getLangOpts().ObjC) {
|
|
915 Diag(Tok, diag::err_expected_external_declaration);
|
|
916 ConsumeToken();
|
|
917 return nullptr;
|
|
918 }
|
|
919 SingleDecl = ParseObjCMethodDefinition();
|
|
920 break;
|
|
921 case tok::code_completion:
|
207
|
922 cutOffParsing();
|
150
|
923 if (CurParsedObjCImpl) {
|
|
924 // Code-complete Objective-C methods even without leading '-'/'+' prefix.
|
|
925 Actions.CodeCompleteObjCMethodDecl(getCurScope(),
|
252
|
926 /*IsInstanceMethod=*/std::nullopt,
|
150
|
927 /*ReturnType=*/nullptr);
|
|
928 }
|
|
929 Actions.CodeCompleteOrdinaryName(
|
|
930 getCurScope(),
|
|
931 CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace);
|
|
932 return nullptr;
|
236
|
933 case tok::kw_import: {
|
|
934 Sema::ModuleImportState IS = Sema::ModuleImportState::NotACXX20Module;
|
|
935 if (getLangOpts().CPlusPlusModules) {
|
|
936 llvm_unreachable("not expecting a c++20 import here");
|
|
937 ProhibitAttributes(Attrs);
|
|
938 }
|
|
939 SingleDecl = ParseModuleImport(SourceLocation(), IS);
|
|
940 } break;
|
150
|
941 case tok::kw_export:
|
252
|
942 if (getLangOpts().CPlusPlusModules) {
|
236
|
943 ProhibitAttributes(Attrs);
|
150
|
944 SingleDecl = ParseExportDeclaration();
|
|
945 break;
|
|
946 }
|
|
947 // This must be 'export template'. Parse it so we can diagnose our lack
|
|
948 // of support.
|
236
|
949 [[fallthrough]];
|
150
|
950 case tok::kw_using:
|
|
951 case tok::kw_namespace:
|
|
952 case tok::kw_typedef:
|
|
953 case tok::kw_template:
|
|
954 case tok::kw_static_assert:
|
|
955 case tok::kw__Static_assert:
|
|
956 // A function definition cannot start with any of these keywords.
|
|
957 {
|
|
958 SourceLocation DeclEnd;
|
236
|
959 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs,
|
252
|
960 DeclSpecAttrs);
|
150
|
961 }
|
|
962
|
236
|
963 case tok::kw_cbuffer:
|
|
964 case tok::kw_tbuffer:
|
|
965 if (getLangOpts().HLSL) {
|
|
966 SourceLocation DeclEnd;
|
|
967 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs,
|
252
|
968 DeclSpecAttrs);
|
236
|
969 }
|
|
970 goto dont_know;
|
|
971
|
150
|
972 case tok::kw_static:
|
|
973 // Parse (then ignore) 'static' prior to a template instantiation. This is
|
|
974 // a GCC extension that we intentionally do not support.
|
|
975 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
|
|
976 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
|
|
977 << 0;
|
|
978 SourceLocation DeclEnd;
|
236
|
979 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs,
|
252
|
980 DeclSpecAttrs);
|
150
|
981 }
|
|
982 goto dont_know;
|
|
983
|
|
984 case tok::kw_inline:
|
|
985 if (getLangOpts().CPlusPlus) {
|
|
986 tok::TokenKind NextKind = NextToken().getKind();
|
|
987
|
|
988 // Inline namespaces. Allowed as an extension even in C++03.
|
|
989 if (NextKind == tok::kw_namespace) {
|
|
990 SourceLocation DeclEnd;
|
236
|
991 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs,
|
252
|
992 DeclSpecAttrs);
|
150
|
993 }
|
|
994
|
|
995 // Parse (then ignore) 'inline' prior to a template instantiation. This is
|
|
996 // a GCC extension that we intentionally do not support.
|
|
997 if (NextKind == tok::kw_template) {
|
|
998 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
|
|
999 << 1;
|
|
1000 SourceLocation DeclEnd;
|
236
|
1001 return ParseDeclaration(DeclaratorContext::File, DeclEnd, Attrs,
|
252
|
1002 DeclSpecAttrs);
|
150
|
1003 }
|
|
1004 }
|
|
1005 goto dont_know;
|
|
1006
|
|
1007 case tok::kw_extern:
|
|
1008 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
|
|
1009 // Extern templates
|
|
1010 SourceLocation ExternLoc = ConsumeToken();
|
|
1011 SourceLocation TemplateLoc = ConsumeToken();
|
|
1012 Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
|
|
1013 diag::warn_cxx98_compat_extern_template :
|
|
1014 diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
|
|
1015 SourceLocation DeclEnd;
|
207
|
1016 return Actions.ConvertDeclToDeclGroup(ParseExplicitInstantiation(
|
236
|
1017 DeclaratorContext::File, ExternLoc, TemplateLoc, DeclEnd, Attrs));
|
150
|
1018 }
|
|
1019 goto dont_know;
|
|
1020
|
|
1021 case tok::kw___if_exists:
|
|
1022 case tok::kw___if_not_exists:
|
|
1023 ParseMicrosoftIfExistsExternalDeclaration();
|
|
1024 return nullptr;
|
|
1025
|
|
1026 case tok::kw_module:
|
|
1027 Diag(Tok, diag::err_unexpected_module_decl);
|
|
1028 SkipUntil(tok::semi);
|
|
1029 return nullptr;
|
|
1030
|
|
1031 default:
|
|
1032 dont_know:
|
|
1033 if (Tok.isEditorPlaceholder()) {
|
|
1034 ConsumeToken();
|
|
1035 return nullptr;
|
|
1036 }
|
252
|
1037 if (PP.isIncrementalProcessingEnabled() &&
|
|
1038 !isDeclarationStatement(/*DisambiguatingWithExpression=*/true))
|
|
1039 return ParseTopLevelStmtDecl();
|
|
1040
|
150
|
1041 // We can't tell whether this is a function-definition or declaration yet.
|
252
|
1042 if (!SingleDecl)
|
|
1043 return ParseDeclarationOrFunctionDefinition(Attrs, DeclSpecAttrs, DS);
|
150
|
1044 }
|
|
1045
|
|
1046 // This routine returns a DeclGroup, if the thing we parsed only contains a
|
|
1047 // single decl, convert it now.
|
|
1048 return Actions.ConvertDeclToDeclGroup(SingleDecl);
|
|
1049 }
|
|
1050
|
|
1051 /// Determine whether the current token, if it occurs after a
|
|
1052 /// declarator, continues a declaration or declaration list.
|
|
1053 bool Parser::isDeclarationAfterDeclarator() {
|
|
1054 // Check for '= delete' or '= default'
|
|
1055 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
|
|
1056 const Token &KW = NextToken();
|
|
1057 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
|
|
1058 return false;
|
|
1059 }
|
|
1060
|
|
1061 return Tok.is(tok::equal) || // int X()= -> not a function def
|
|
1062 Tok.is(tok::comma) || // int X(), -> not a function def
|
|
1063 Tok.is(tok::semi) || // int X(); -> not a function def
|
|
1064 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
|
|
1065 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
|
|
1066 (getLangOpts().CPlusPlus &&
|
|
1067 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
|
|
1068 }
|
|
1069
|
|
1070 /// Determine whether the current token, if it occurs after a
|
|
1071 /// declarator, indicates the start of a function definition.
|
|
1072 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
|
|
1073 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
|
|
1074 if (Tok.is(tok::l_brace)) // int X() {}
|
|
1075 return true;
|
|
1076
|
|
1077 // Handle K&R C argument lists: int X(f) int f; {}
|
|
1078 if (!getLangOpts().CPlusPlus &&
|
|
1079 Declarator.getFunctionTypeInfo().isKNRPrototype())
|
236
|
1080 return isDeclarationSpecifier(ImplicitTypenameContext::No);
|
150
|
1081
|
|
1082 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
|
|
1083 const Token &KW = NextToken();
|
|
1084 return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
|
|
1085 }
|
|
1086
|
|
1087 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
|
|
1088 Tok.is(tok::kw_try); // X() try { ... }
|
|
1089 }
|
|
1090
|
|
1091 /// Parse either a function-definition or a declaration. We can't tell which
|
|
1092 /// we have until we read up to the compound-statement in function-definition.
|
|
1093 /// TemplateParams, if non-NULL, provides the template parameters when we're
|
|
1094 /// parsing a C++ template-declaration.
|
|
1095 ///
|
|
1096 /// function-definition: [C99 6.9.1]
|
|
1097 /// decl-specs declarator declaration-list[opt] compound-statement
|
|
1098 /// [C90] function-definition: [C99 6.7.1] - implicit int result
|
|
1099 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
|
1100 ///
|
|
1101 /// declaration: [C99 6.7]
|
|
1102 /// declaration-specifiers init-declarator-list[opt] ';'
|
|
1103 /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
|
|
1104 /// [OMP] threadprivate-directive
|
|
1105 /// [OMP] allocate-directive [TODO]
|
|
1106 ///
|
236
|
1107 Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
|
252
|
1108 ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
|
|
1109 ParsingDeclSpec &DS, AccessSpecifier AS) {
|
|
1110 // Because we assume that the DeclSpec has not yet been initialised, we simply
|
|
1111 // overwrite the source range and attribute the provided leading declspec
|
|
1112 // attributes.
|
|
1113 assert(DS.getSourceRange().isInvalid() &&
|
|
1114 "expected uninitialised source range");
|
|
1115 DS.SetRangeStart(DeclSpecAttrs.Range.getBegin());
|
|
1116 DS.SetRangeEnd(DeclSpecAttrs.Range.getEnd());
|
|
1117 DS.takeAttributesFrom(DeclSpecAttrs);
|
|
1118
|
150
|
1119 MaybeParseMicrosoftAttributes(DS.getAttributes());
|
|
1120 // Parse the common declaration-specifiers piece.
|
|
1121 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS,
|
|
1122 DeclSpecContext::DSC_top_level);
|
|
1123
|
|
1124 // If we had a free-standing type definition with a missing semicolon, we
|
|
1125 // may get this far before the problem becomes obvious.
|
|
1126 if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition(
|
|
1127 DS, AS, DeclSpecContext::DSC_top_level))
|
|
1128 return nullptr;
|
|
1129
|
|
1130 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
|
|
1131 // declaration-specifiers init-declarator-list[opt] ';'
|
|
1132 if (Tok.is(tok::semi)) {
|
|
1133 auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
|
|
1134 assert(DeclSpec::isDeclRep(TKind));
|
|
1135 switch(TKind) {
|
|
1136 case DeclSpec::TST_class:
|
|
1137 return 5;
|
|
1138 case DeclSpec::TST_struct:
|
|
1139 return 6;
|
|
1140 case DeclSpec::TST_union:
|
|
1141 return 5;
|
|
1142 case DeclSpec::TST_enum:
|
|
1143 return 4;
|
|
1144 case DeclSpec::TST_interface:
|
|
1145 return 9;
|
|
1146 default:
|
|
1147 llvm_unreachable("we only expect to get the length of the class/struct/union/enum");
|
|
1148 }
|
|
1149
|
|
1150 };
|
|
1151 // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'
|
|
1152 SourceLocation CorrectLocationForAttributes =
|
|
1153 DeclSpec::isDeclRep(DS.getTypeSpecType())
|
|
1154 ? DS.getTypeSpecTypeLoc().getLocWithOffset(
|
|
1155 LengthOfTSTToken(DS.getTypeSpecType()))
|
|
1156 : SourceLocation();
|
236
|
1157 ProhibitAttributes(Attrs, CorrectLocationForAttributes);
|
150
|
1158 ConsumeToken();
|
|
1159 RecordDecl *AnonRecord = nullptr;
|
236
|
1160 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
|
|
1161 getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
|
150
|
1162 DS.complete(TheDecl);
|
252
|
1163 Actions.ActOnDefinedDeclarationSpecifier(TheDecl);
|
150
|
1164 if (AnonRecord) {
|
|
1165 Decl* decls[] = {AnonRecord, TheDecl};
|
|
1166 return Actions.BuildDeclaratorGroup(decls);
|
|
1167 }
|
|
1168 return Actions.ConvertDeclToDeclGroup(TheDecl);
|
|
1169 }
|
|
1170
|
252
|
1171 if (DS.hasTagDefinition())
|
|
1172 Actions.ActOnDefinedDeclarationSpecifier(DS.getRepAsDecl());
|
|
1173
|
150
|
1174 // ObjC2 allows prefix attributes on class interfaces and protocols.
|
|
1175 // FIXME: This still needs better diagnostics. We should only accept
|
|
1176 // attributes here, no types, etc.
|
|
1177 if (getLangOpts().ObjC && Tok.is(tok::at)) {
|
|
1178 SourceLocation AtLoc = ConsumeToken(); // the "@"
|
|
1179 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
|
|
1180 !Tok.isObjCAtKeyword(tok::objc_protocol) &&
|
|
1181 !Tok.isObjCAtKeyword(tok::objc_implementation)) {
|
|
1182 Diag(Tok, diag::err_objc_unexpected_attr);
|
|
1183 SkipUntil(tok::semi);
|
|
1184 return nullptr;
|
|
1185 }
|
|
1186
|
|
1187 DS.abort();
|
236
|
1188 DS.takeAttributesFrom(Attrs);
|
150
|
1189
|
|
1190 const char *PrevSpec = nullptr;
|
|
1191 unsigned DiagID;
|
|
1192 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
|
|
1193 Actions.getASTContext().getPrintingPolicy()))
|
|
1194 Diag(AtLoc, DiagID) << PrevSpec;
|
|
1195
|
|
1196 if (Tok.isObjCAtKeyword(tok::objc_protocol))
|
|
1197 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
|
|
1198
|
|
1199 if (Tok.isObjCAtKeyword(tok::objc_implementation))
|
|
1200 return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes());
|
|
1201
|
|
1202 return Actions.ConvertDeclToDeclGroup(
|
|
1203 ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
|
|
1204 }
|
|
1205
|
|
1206 // If the declspec consisted only of 'extern' and we have a string
|
|
1207 // literal following it, this must be a C++ linkage specifier like
|
|
1208 // 'extern "C"'.
|
|
1209 if (getLangOpts().CPlusPlus && isTokenStringLiteral() &&
|
|
1210 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
|
|
1211 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
|
236
|
1212 ProhibitAttributes(Attrs);
|
207
|
1213 Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::File);
|
150
|
1214 return Actions.ConvertDeclToDeclGroup(TheDecl);
|
|
1215 }
|
|
1216
|
236
|
1217 return ParseDeclGroup(DS, DeclaratorContext::File, Attrs);
|
150
|
1218 }
|
|
1219
|
236
|
1220 Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition(
|
252
|
1221 ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
|
|
1222 ParsingDeclSpec *DS, AccessSpecifier AS) {
|
150
|
1223 if (DS) {
|
252
|
1224 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS);
|
150
|
1225 } else {
|
|
1226 ParsingDeclSpec PDS(*this);
|
|
1227 // Must temporarily exit the objective-c container scope for
|
|
1228 // parsing c constructs and re-enter objc container scope
|
|
1229 // afterwards.
|
|
1230 ObjCDeclContextSwitch ObjCDC(*this);
|
|
1231
|
252
|
1232 return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, PDS, AS);
|
150
|
1233 }
|
|
1234 }
|
|
1235
|
|
1236 /// ParseFunctionDefinition - We parsed and verified that the specified
|
|
1237 /// Declarator is well formed. If this is a K&R-style function, read the
|
|
1238 /// parameters declaration-list, then start the compound-statement.
|
|
1239 ///
|
|
1240 /// function-definition: [C99 6.9.1]
|
|
1241 /// decl-specs declarator declaration-list[opt] compound-statement
|
|
1242 /// [C90] function-definition: [C99 6.7.1] - implicit int result
|
|
1243 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
|
1244 /// [C++] function-definition: [C++ 8.4]
|
|
1245 /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
|
|
1246 /// function-body
|
|
1247 /// [C++] function-definition: [C++ 8.4]
|
|
1248 /// decl-specifier-seq[opt] declarator function-try-block
|
|
1249 ///
|
|
1250 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
|
|
1251 const ParsedTemplateInfo &TemplateInfo,
|
|
1252 LateParsedAttrList *LateParsedAttrs) {
|
|
1253 // Poison SEH identifiers so they are flagged as illegal in function bodies.
|
|
1254 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
|
|
1255 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
|
|
1256 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
1257
|
236
|
1258 // If this is C89 and the declspecs were completely missing, fudge in an
|
150
|
1259 // implicit int. We do this here because this is the only place where
|
|
1260 // declaration-specifiers are completely optional in the grammar.
|
236
|
1261 if (getLangOpts().isImplicitIntRequired() && D.getDeclSpec().isEmpty()) {
|
|
1262 Diag(D.getIdentifierLoc(), diag::warn_missing_type_specifier)
|
|
1263 << D.getDeclSpec().getSourceRange();
|
150
|
1264 const char *PrevSpec;
|
|
1265 unsigned DiagID;
|
|
1266 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
|
|
1267 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
|
|
1268 D.getIdentifierLoc(),
|
|
1269 PrevSpec, DiagID,
|
|
1270 Policy);
|
|
1271 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
|
|
1272 }
|
|
1273
|
|
1274 // If this declaration was formed with a K&R-style identifier list for the
|
|
1275 // arguments, parse declarations for all of the args next.
|
|
1276 // int foo(a,b) int a; float b; {}
|
|
1277 if (FTI.isKNRPrototype())
|
|
1278 ParseKNRParamDeclarations(D);
|
|
1279
|
|
1280 // We should have either an opening brace or, in a C++ constructor,
|
|
1281 // we may have a colon.
|
|
1282 if (Tok.isNot(tok::l_brace) &&
|
|
1283 (!getLangOpts().CPlusPlus ||
|
|
1284 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
|
|
1285 Tok.isNot(tok::equal)))) {
|
|
1286 Diag(Tok, diag::err_expected_fn_body);
|
|
1287
|
|
1288 // Skip over garbage, until we get to '{'. Don't eat the '{'.
|
|
1289 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
|
|
1290
|
|
1291 // If we didn't find the '{', bail out.
|
|
1292 if (Tok.isNot(tok::l_brace))
|
|
1293 return nullptr;
|
|
1294 }
|
|
1295
|
|
1296 // Check to make sure that any normal attributes are allowed to be on
|
|
1297 // a definition. Late parsed attributes are checked at the end.
|
|
1298 if (Tok.isNot(tok::equal)) {
|
|
1299 for (const ParsedAttr &AL : D.getAttributes())
|
223
|
1300 if (AL.isKnownToGCC() && !AL.isStandardAttributeSyntax())
|
150
|
1301 Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL;
|
|
1302 }
|
|
1303
|
|
1304 // In delayed template parsing mode, for function template we consume the
|
|
1305 // tokens and store them for late parsing at the end of the translation unit.
|
|
1306 if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
|
|
1307 TemplateInfo.Kind == ParsedTemplateInfo::Template &&
|
|
1308 Actions.canDelayFunctionBody(D)) {
|
|
1309 MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
|
|
1310
|
|
1311 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
1312 Scope::CompoundStmtScope);
|
|
1313 Scope *ParentScope = getCurScope()->getParent();
|
|
1314
|
207
|
1315 D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
|
150
|
1316 Decl *DP = Actions.HandleDeclarator(ParentScope, D,
|
|
1317 TemplateParameterLists);
|
|
1318 D.complete(DP);
|
|
1319 D.getMutableDeclSpec().abort();
|
|
1320
|
|
1321 if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
|
|
1322 trySkippingFunctionBody()) {
|
|
1323 BodyScope.Exit();
|
|
1324 return Actions.ActOnSkippedFunctionBody(DP);
|
|
1325 }
|
|
1326
|
|
1327 CachedTokens Toks;
|
|
1328 LexTemplateFunctionForLateParsing(Toks);
|
|
1329
|
|
1330 if (DP) {
|
|
1331 FunctionDecl *FnD = DP->getAsFunction();
|
|
1332 Actions.CheckForFunctionRedefinition(FnD);
|
|
1333 Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
|
|
1334 }
|
|
1335 return DP;
|
|
1336 }
|
|
1337 else if (CurParsedObjCImpl &&
|
|
1338 !TemplateInfo.TemplateParams &&
|
|
1339 (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
|
|
1340 Tok.is(tok::colon)) &&
|
|
1341 Actions.CurContext->isTranslationUnit()) {
|
|
1342 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
1343 Scope::CompoundStmtScope);
|
|
1344 Scope *ParentScope = getCurScope()->getParent();
|
|
1345
|
207
|
1346 D.setFunctionDefinitionKind(FunctionDefinitionKind::Definition);
|
150
|
1347 Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
|
|
1348 MultiTemplateParamsArg());
|
|
1349 D.complete(FuncDecl);
|
|
1350 D.getMutableDeclSpec().abort();
|
|
1351 if (FuncDecl) {
|
|
1352 // Consume the tokens and store them for later parsing.
|
|
1353 StashAwayMethodOrFunctionBodyTokens(FuncDecl);
|
|
1354 CurParsedObjCImpl->HasCFunction = true;
|
|
1355 return FuncDecl;
|
|
1356 }
|
|
1357 // FIXME: Should we really fall through here?
|
|
1358 }
|
|
1359
|
|
1360 // Enter a scope for the function body.
|
|
1361 ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
1362 Scope::CompoundStmtScope);
|
|
1363
|
236
|
1364 // Parse function body eagerly if it is either '= delete;' or '= default;' as
|
|
1365 // ActOnStartOfFunctionDef needs to know whether the function is deleted.
|
|
1366 Sema::FnBodyKind BodyKind = Sema::FnBodyKind::Other;
|
|
1367 SourceLocation KWLoc;
|
|
1368 if (TryConsumeToken(tok::equal)) {
|
|
1369 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
|
|
1370
|
|
1371 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
|
|
1372 Diag(KWLoc, getLangOpts().CPlusPlus11
|
|
1373 ? diag::warn_cxx98_compat_defaulted_deleted_function
|
|
1374 : diag::ext_defaulted_deleted_function)
|
|
1375 << 1 /* deleted */;
|
|
1376 BodyKind = Sema::FnBodyKind::Delete;
|
|
1377 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
|
|
1378 Diag(KWLoc, getLangOpts().CPlusPlus11
|
|
1379 ? diag::warn_cxx98_compat_defaulted_deleted_function
|
|
1380 : diag::ext_defaulted_deleted_function)
|
|
1381 << 0 /* defaulted */;
|
|
1382 BodyKind = Sema::FnBodyKind::Default;
|
|
1383 } else {
|
|
1384 llvm_unreachable("function definition after = not 'delete' or 'default'");
|
|
1385 }
|
|
1386
|
|
1387 if (Tok.is(tok::comma)) {
|
|
1388 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
|
|
1389 << (BodyKind == Sema::FnBodyKind::Delete);
|
|
1390 SkipUntil(tok::semi);
|
|
1391 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
|
|
1392 BodyKind == Sema::FnBodyKind::Delete
|
|
1393 ? "delete"
|
|
1394 : "default")) {
|
|
1395 SkipUntil(tok::semi);
|
|
1396 }
|
|
1397 }
|
|
1398
|
150
|
1399 // Tell the actions module that we have entered a function definition with the
|
|
1400 // specified Declarator for the function.
|
|
1401 Sema::SkipBodyInfo SkipBody;
|
|
1402 Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
|
|
1403 TemplateInfo.TemplateParams
|
|
1404 ? *TemplateInfo.TemplateParams
|
|
1405 : MultiTemplateParamsArg(),
|
236
|
1406 &SkipBody, BodyKind);
|
150
|
1407
|
|
1408 if (SkipBody.ShouldSkip) {
|
236
|
1409 // Do NOT enter SkipFunctionBody if we already consumed the tokens.
|
|
1410 if (BodyKind == Sema::FnBodyKind::Other)
|
|
1411 SkipFunctionBody();
|
|
1412
|
252
|
1413 // ExpressionEvaluationContext is pushed in ActOnStartOfFunctionDef
|
|
1414 // and it would be popped in ActOnFinishFunctionBody.
|
|
1415 // We pop it explcitly here since ActOnFinishFunctionBody won't get called.
|
|
1416 //
|
|
1417 // Do not call PopExpressionEvaluationContext() if it is a lambda because
|
|
1418 // one is already popped when finishing the lambda in BuildLambdaExpr().
|
|
1419 //
|
|
1420 // FIXME: It looks not easy to balance PushExpressionEvaluationContext()
|
|
1421 // and PopExpressionEvaluationContext().
|
|
1422 if (!isLambdaCallOperator(dyn_cast_if_present<FunctionDecl>(Res)))
|
|
1423 Actions.PopExpressionEvaluationContext();
|
150
|
1424 return Res;
|
|
1425 }
|
|
1426
|
|
1427 // Break out of the ParsingDeclarator context before we parse the body.
|
|
1428 D.complete(Res);
|
|
1429
|
|
1430 // Break out of the ParsingDeclSpec context, too. This const_cast is
|
|
1431 // safe because we're always the sole owner.
|
|
1432 D.getMutableDeclSpec().abort();
|
|
1433
|
236
|
1434 if (BodyKind != Sema::FnBodyKind::Other) {
|
|
1435 Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind);
|
|
1436 Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
|
|
1437 Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
|
|
1438 return Res;
|
|
1439 }
|
|
1440
|
150
|
1441 // With abbreviated function templates - we need to explicitly add depth to
|
|
1442 // account for the implicit template parameter list induced by the template.
|
|
1443 if (auto *Template = dyn_cast_or_null<FunctionTemplateDecl>(Res))
|
|
1444 if (Template->isAbbreviated() &&
|
|
1445 Template->getTemplateParameters()->getParam(0)->isImplicit())
|
|
1446 // First template parameter is implicit - meaning no explicit template
|
|
1447 // parameter list was specified.
|
|
1448 CurTemplateDepthTracker.addDepth(1);
|
|
1449
|
|
1450 if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
|
|
1451 trySkippingFunctionBody()) {
|
|
1452 BodyScope.Exit();
|
|
1453 Actions.ActOnSkippedFunctionBody(Res);
|
|
1454 return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
|
|
1455 }
|
|
1456
|
|
1457 if (Tok.is(tok::kw_try))
|
|
1458 return ParseFunctionTryBlock(Res, BodyScope);
|
|
1459
|
|
1460 // If we have a colon, then we're probably parsing a C++
|
|
1461 // ctor-initializer.
|
|
1462 if (Tok.is(tok::colon)) {
|
|
1463 ParseConstructorInitializer(Res);
|
|
1464
|
|
1465 // Recover from error.
|
|
1466 if (!Tok.is(tok::l_brace)) {
|
|
1467 BodyScope.Exit();
|
|
1468 Actions.ActOnFinishFunctionBody(Res, nullptr);
|
|
1469 return Res;
|
|
1470 }
|
|
1471 } else
|
|
1472 Actions.ActOnDefaultCtorInitializers(Res);
|
|
1473
|
|
1474 // Late attributes are parsed in the same scope as the function body.
|
|
1475 if (LateParsedAttrs)
|
|
1476 ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
|
|
1477
|
240
|
1478 #ifndef noCbC
|
|
1479 curFuncName = "__cbc_";
|
|
1480 if (D.getIdentifier()) {
|
|
1481 curFuncName = D.getIdentifier()->getName().data();
|
|
1482 }
|
|
1483 #endif
|
150
|
1484 return ParseFunctionStatementBody(Res, BodyScope);
|
|
1485 }
|
|
1486
|
|
1487 void Parser::SkipFunctionBody() {
|
|
1488 if (Tok.is(tok::equal)) {
|
|
1489 SkipUntil(tok::semi);
|
|
1490 return;
|
|
1491 }
|
|
1492
|
|
1493 bool IsFunctionTryBlock = Tok.is(tok::kw_try);
|
|
1494 if (IsFunctionTryBlock)
|
|
1495 ConsumeToken();
|
|
1496
|
|
1497 CachedTokens Skipped;
|
|
1498 if (ConsumeAndStoreFunctionPrologue(Skipped))
|
|
1499 SkipMalformedDecl();
|
|
1500 else {
|
|
1501 SkipUntil(tok::r_brace);
|
|
1502 while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
|
|
1503 SkipUntil(tok::l_brace);
|
|
1504 SkipUntil(tok::r_brace);
|
|
1505 }
|
|
1506 }
|
|
1507 }
|
|
1508
|
|
1509 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
|
|
1510 /// types for a function with a K&R-style identifier list for arguments.
|
|
1511 void Parser::ParseKNRParamDeclarations(Declarator &D) {
|
|
1512 // We know that the top-level of this declarator is a function.
|
|
1513 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
|
|
1514
|
|
1515 // Enter function-declaration scope, limiting any declarators to the
|
|
1516 // function prototype scope, including parameter declarators.
|
|
1517 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
|
|
1518 Scope::FunctionDeclarationScope | Scope::DeclScope);
|
|
1519
|
|
1520 // Read all the argument declarations.
|
236
|
1521 while (isDeclarationSpecifier(ImplicitTypenameContext::No)) {
|
150
|
1522 SourceLocation DSStart = Tok.getLocation();
|
|
1523
|
|
1524 // Parse the common declaration-specifiers piece.
|
|
1525 DeclSpec DS(AttrFactory);
|
|
1526 ParseDeclarationSpecifiers(DS);
|
|
1527
|
|
1528 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
|
|
1529 // least one declarator'.
|
|
1530 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
|
|
1531 // the declarations though. It's trivial to ignore them, really hard to do
|
|
1532 // anything else with them.
|
|
1533 if (TryConsumeToken(tok::semi)) {
|
|
1534 Diag(DSStart, diag::err_declaration_does_not_declare_param);
|
|
1535 continue;
|
|
1536 }
|
|
1537
|
|
1538 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
|
|
1539 // than register.
|
|
1540 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
|
|
1541 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
|
|
1542 Diag(DS.getStorageClassSpecLoc(),
|
|
1543 diag::err_invalid_storage_class_in_func_decl);
|
|
1544 DS.ClearStorageClassSpecs();
|
|
1545 }
|
|
1546 if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) {
|
|
1547 Diag(DS.getThreadStorageClassSpecLoc(),
|
|
1548 diag::err_invalid_storage_class_in_func_decl);
|
|
1549 DS.ClearStorageClassSpecs();
|
|
1550 }
|
|
1551
|
|
1552 // Parse the first declarator attached to this declspec.
|
236
|
1553 Declarator ParmDeclarator(DS, ParsedAttributesView::none(),
|
|
1554 DeclaratorContext::KNRTypeList);
|
150
|
1555 ParseDeclarator(ParmDeclarator);
|
|
1556
|
|
1557 // Handle the full declarator list.
|
236
|
1558 while (true) {
|
150
|
1559 // If attributes are present, parse them.
|
|
1560 MaybeParseGNUAttributes(ParmDeclarator);
|
|
1561
|
|
1562 // Ask the actions module to compute the type for this declarator.
|
|
1563 Decl *Param =
|
|
1564 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
|
|
1565
|
|
1566 if (Param &&
|
|
1567 // A missing identifier has already been diagnosed.
|
|
1568 ParmDeclarator.getIdentifier()) {
|
|
1569
|
|
1570 // Scan the argument list looking for the correct param to apply this
|
|
1571 // type.
|
|
1572 for (unsigned i = 0; ; ++i) {
|
|
1573 // C99 6.9.1p6: those declarators shall declare only identifiers from
|
|
1574 // the identifier list.
|
|
1575 if (i == FTI.NumParams) {
|
|
1576 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
|
|
1577 << ParmDeclarator.getIdentifier();
|
|
1578 break;
|
|
1579 }
|
|
1580
|
|
1581 if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
|
|
1582 // Reject redefinitions of parameters.
|
|
1583 if (FTI.Params[i].Param) {
|
|
1584 Diag(ParmDeclarator.getIdentifierLoc(),
|
|
1585 diag::err_param_redefinition)
|
|
1586 << ParmDeclarator.getIdentifier();
|
|
1587 } else {
|
|
1588 FTI.Params[i].Param = Param;
|
|
1589 }
|
|
1590 break;
|
|
1591 }
|
|
1592 }
|
|
1593 }
|
|
1594
|
|
1595 // If we don't have a comma, it is either the end of the list (a ';') or
|
|
1596 // an error, bail out.
|
|
1597 if (Tok.isNot(tok::comma))
|
|
1598 break;
|
|
1599
|
|
1600 ParmDeclarator.clear();
|
|
1601
|
|
1602 // Consume the comma.
|
|
1603 ParmDeclarator.setCommaLoc(ConsumeToken());
|
|
1604
|
|
1605 // Parse the next declarator.
|
|
1606 ParseDeclarator(ParmDeclarator);
|
|
1607 }
|
|
1608
|
|
1609 // Consume ';' and continue parsing.
|
|
1610 if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
|
|
1611 continue;
|
|
1612
|
|
1613 // Otherwise recover by skipping to next semi or mandatory function body.
|
|
1614 if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
|
|
1615 break;
|
|
1616 TryConsumeToken(tok::semi);
|
|
1617 }
|
|
1618
|
|
1619 // The actions module must verify that all arguments were declared.
|
|
1620 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
|
|
1621 }
|
|
1622
|
|
1623
|
|
1624 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
|
|
1625 /// allowed to be a wide string, and is not subject to character translation.
|
|
1626 /// Unlike GCC, we also diagnose an empty string literal when parsing for an
|
|
1627 /// asm label as opposed to an asm statement, because such a construct does not
|
|
1628 /// behave well.
|
|
1629 ///
|
|
1630 /// [GNU] asm-string-literal:
|
|
1631 /// string-literal
|
|
1632 ///
|
|
1633 ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
|
|
1634 if (!isTokenStringLiteral()) {
|
|
1635 Diag(Tok, diag::err_expected_string_literal)
|
|
1636 << /*Source='in...'*/0 << "'asm'";
|
|
1637 return ExprError();
|
|
1638 }
|
|
1639
|
|
1640 ExprResult AsmString(ParseStringLiteralExpression());
|
|
1641 if (!AsmString.isInvalid()) {
|
|
1642 const auto *SL = cast<StringLiteral>(AsmString.get());
|
236
|
1643 if (!SL->isOrdinary()) {
|
150
|
1644 Diag(Tok, diag::err_asm_operand_wide_string_literal)
|
|
1645 << SL->isWide()
|
|
1646 << SL->getSourceRange();
|
|
1647 return ExprError();
|
|
1648 }
|
|
1649 if (ForAsmLabel && SL->getString().empty()) {
|
|
1650 Diag(Tok, diag::err_asm_operand_wide_string_literal)
|
|
1651 << 2 /* an empty */ << SL->getSourceRange();
|
|
1652 return ExprError();
|
|
1653 }
|
|
1654 }
|
|
1655 return AsmString;
|
|
1656 }
|
|
1657
|
|
1658 /// ParseSimpleAsm
|
|
1659 ///
|
|
1660 /// [GNU] simple-asm-expr:
|
|
1661 /// 'asm' '(' asm-string-literal ')'
|
|
1662 ///
|
|
1663 ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) {
|
|
1664 assert(Tok.is(tok::kw_asm) && "Not an asm!");
|
|
1665 SourceLocation Loc = ConsumeToken();
|
|
1666
|
173
|
1667 if (isGNUAsmQualifier(Tok)) {
|
|
1668 // Remove from the end of 'asm' to the end of the asm qualifier.
|
150
|
1669 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
|
|
1670 PP.getLocForEndOfToken(Tok.getLocation()));
|
173
|
1671 Diag(Tok, diag::err_global_asm_qualifier_ignored)
|
|
1672 << GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok))
|
|
1673 << FixItHint::CreateRemoval(RemovalRange);
|
150
|
1674 ConsumeToken();
|
|
1675 }
|
|
1676
|
|
1677 BalancedDelimiterTracker T(*this, tok::l_paren);
|
|
1678 if (T.consumeOpen()) {
|
|
1679 Diag(Tok, diag::err_expected_lparen_after) << "asm";
|
|
1680 return ExprError();
|
|
1681 }
|
|
1682
|
|
1683 ExprResult Result(ParseAsmStringLiteral(ForAsmLabel));
|
|
1684
|
|
1685 if (!Result.isInvalid()) {
|
|
1686 // Close the paren and get the location of the end bracket
|
|
1687 T.consumeClose();
|
|
1688 if (EndLoc)
|
|
1689 *EndLoc = T.getCloseLocation();
|
|
1690 } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
|
|
1691 if (EndLoc)
|
|
1692 *EndLoc = Tok.getLocation();
|
|
1693 ConsumeParen();
|
|
1694 }
|
|
1695
|
|
1696 return Result;
|
|
1697 }
|
|
1698
|
|
1699 /// Get the TemplateIdAnnotation from the token and put it in the
|
|
1700 /// cleanup pool so that it gets destroyed when parsing the current top level
|
|
1701 /// declaration is finished.
|
|
1702 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
|
|
1703 assert(tok.is(tok::annot_template_id) && "Expected template-id token");
|
|
1704 TemplateIdAnnotation *
|
|
1705 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
|
|
1706 return Id;
|
|
1707 }
|
|
1708
|
|
1709 void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
|
|
1710 // Push the current token back into the token stream (or revert it if it is
|
|
1711 // cached) and use an annotation scope token for current token.
|
|
1712 if (PP.isBacktrackEnabled())
|
|
1713 PP.RevertCachedTokens(1);
|
|
1714 else
|
|
1715 PP.EnterToken(Tok, /*IsReinject=*/true);
|
|
1716 Tok.setKind(tok::annot_cxxscope);
|
|
1717 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
|
|
1718 Tok.setAnnotationRange(SS.getRange());
|
|
1719
|
|
1720 // In case the tokens were cached, have Preprocessor replace them
|
|
1721 // with the annotation token. We don't need to do this if we've
|
|
1722 // just reverted back to a prior state.
|
|
1723 if (IsNewAnnotation)
|
|
1724 PP.AnnotateCachedTokens(Tok);
|
|
1725 }
|
|
1726
|
|
1727 /// Attempt to classify the name at the current token position. This may
|
|
1728 /// form a type, scope or primary expression annotation, or replace the token
|
|
1729 /// with a typo-corrected keyword. This is only appropriate when the current
|
|
1730 /// name must refer to an entity which has already been declared.
|
|
1731 ///
|
|
1732 /// \param CCC Indicates how to perform typo-correction for this name. If NULL,
|
|
1733 /// no typo correction will be performed.
|
236
|
1734 /// \param AllowImplicitTypename Whether we are in a context where a dependent
|
|
1735 /// nested-name-specifier without typename is treated as a type (e.g.
|
|
1736 /// T::type).
|
150
|
1737 Parser::AnnotatedNameKind
|
236
|
1738 Parser::TryAnnotateName(CorrectionCandidateCallback *CCC,
|
|
1739 ImplicitTypenameContext AllowImplicitTypename) {
|
150
|
1740 assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
|
|
1741
|
|
1742 const bool EnteringContext = false;
|
|
1743 const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
|
|
1744
|
|
1745 CXXScopeSpec SS;
|
|
1746 if (getLangOpts().CPlusPlus &&
|
173
|
1747 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
236
|
1748 /*ObjectHasErrors=*/false,
|
173
|
1749 EnteringContext))
|
150
|
1750 return ANK_Error;
|
|
1751
|
|
1752 if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
|
236
|
1753 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,
|
|
1754 AllowImplicitTypename))
|
150
|
1755 return ANK_Error;
|
|
1756 return ANK_Unresolved;
|
|
1757 }
|
|
1758
|
|
1759 IdentifierInfo *Name = Tok.getIdentifierInfo();
|
|
1760 SourceLocation NameLoc = Tok.getLocation();
|
|
1761
|
|
1762 // FIXME: Move the tentative declaration logic into ClassifyName so we can
|
|
1763 // typo-correct to tentatively-declared identifiers.
|
236
|
1764 if (isTentativelyDeclared(Name) && SS.isEmpty()) {
|
150
|
1765 // Identifier has been tentatively declared, and thus cannot be resolved as
|
|
1766 // an expression. Fall back to annotating it as a type.
|
236
|
1767 if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,
|
|
1768 AllowImplicitTypename))
|
150
|
1769 return ANK_Error;
|
|
1770 return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
|
|
1771 }
|
|
1772
|
|
1773 Token Next = NextToken();
|
|
1774
|
|
1775 // Look up and classify the identifier. We don't perform any typo-correction
|
|
1776 // after a scope specifier, because in general we can't recover from typos
|
|
1777 // there (eg, after correcting 'A::template B<X>::C' [sic], we would need to
|
|
1778 // jump back into scope specifier parsing).
|
|
1779 Sema::NameClassification Classification = Actions.ClassifyName(
|
|
1780 getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr);
|
|
1781
|
|
1782 // If name lookup found nothing and we guessed that this was a template name,
|
|
1783 // double-check before committing to that interpretation. C++20 requires that
|
|
1784 // we interpret this as a template-id if it can be, but if it can't be, then
|
|
1785 // this is an error recovery case.
|
|
1786 if (Classification.getKind() == Sema::NC_UndeclaredTemplate &&
|
|
1787 isTemplateArgumentList(1) == TPResult::False) {
|
|
1788 // It's not a template-id; re-classify without the '<' as a hint.
|
|
1789 Token FakeNext = Next;
|
|
1790 FakeNext.setKind(tok::unknown);
|
|
1791 Classification =
|
|
1792 Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext,
|
|
1793 SS.isEmpty() ? CCC : nullptr);
|
|
1794 }
|
|
1795
|
|
1796 switch (Classification.getKind()) {
|
|
1797 case Sema::NC_Error:
|
|
1798 return ANK_Error;
|
|
1799
|
|
1800 case Sema::NC_Keyword:
|
|
1801 // The identifier was typo-corrected to a keyword.
|
|
1802 Tok.setIdentifierInfo(Name);
|
|
1803 Tok.setKind(Name->getTokenID());
|
|
1804 PP.TypoCorrectToken(Tok);
|
|
1805 if (SS.isNotEmpty())
|
|
1806 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
1807 // We've "annotated" this as a keyword.
|
|
1808 return ANK_Success;
|
|
1809
|
|
1810 case Sema::NC_Unknown:
|
|
1811 // It's not something we know about. Leave it unannotated.
|
|
1812 break;
|
|
1813
|
|
1814 case Sema::NC_Type: {
|
207
|
1815 if (TryAltiVecVectorToken())
|
|
1816 // vector has been found as a type id when altivec is enabled but
|
|
1817 // this is followed by a declaration specifier so this is really the
|
|
1818 // altivec vector token. Leave it unannotated.
|
|
1819 break;
|
150
|
1820 SourceLocation BeginLoc = NameLoc;
|
|
1821 if (SS.isNotEmpty())
|
|
1822 BeginLoc = SS.getBeginLoc();
|
|
1823
|
|
1824 /// An Objective-C object type followed by '<' is a specialization of
|
|
1825 /// a parameterized class type or a protocol-qualified type.
|
|
1826 ParsedType Ty = Classification.getType();
|
|
1827 if (getLangOpts().ObjC && NextToken().is(tok::less) &&
|
|
1828 (Ty.get()->isObjCObjectType() ||
|
|
1829 Ty.get()->isObjCObjectPointerType())) {
|
|
1830 // Consume the name.
|
|
1831 SourceLocation IdentifierLoc = ConsumeToken();
|
|
1832 SourceLocation NewEndLoc;
|
|
1833 TypeResult NewType
|
|
1834 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
|
|
1835 /*consumeLastToken=*/false,
|
|
1836 NewEndLoc);
|
|
1837 if (NewType.isUsable())
|
|
1838 Ty = NewType.get();
|
|
1839 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
|
|
1840 return ANK_Error;
|
|
1841 }
|
|
1842
|
|
1843 Tok.setKind(tok::annot_typename);
|
|
1844 setTypeAnnotation(Tok, Ty);
|
|
1845 Tok.setAnnotationEndLoc(Tok.getLocation());
|
|
1846 Tok.setLocation(BeginLoc);
|
|
1847 PP.AnnotateCachedTokens(Tok);
|
|
1848 return ANK_Success;
|
|
1849 }
|
|
1850
|
207
|
1851 case Sema::NC_OverloadSet:
|
|
1852 Tok.setKind(tok::annot_overload_set);
|
150
|
1853 setExprAnnotation(Tok, Classification.getExpression());
|
|
1854 Tok.setAnnotationEndLoc(NameLoc);
|
|
1855 if (SS.isNotEmpty())
|
|
1856 Tok.setLocation(SS.getBeginLoc());
|
|
1857 PP.AnnotateCachedTokens(Tok);
|
|
1858 return ANK_Success;
|
|
1859
|
|
1860 case Sema::NC_NonType:
|
207
|
1861 if (TryAltiVecVectorToken())
|
|
1862 // vector has been found as a non-type id when altivec is enabled but
|
|
1863 // this is followed by a declaration specifier so this is really the
|
|
1864 // altivec vector token. Leave it unannotated.
|
|
1865 break;
|
150
|
1866 Tok.setKind(tok::annot_non_type);
|
|
1867 setNonTypeAnnotation(Tok, Classification.getNonTypeDecl());
|
|
1868 Tok.setLocation(NameLoc);
|
|
1869 Tok.setAnnotationEndLoc(NameLoc);
|
|
1870 PP.AnnotateCachedTokens(Tok);
|
|
1871 if (SS.isNotEmpty())
|
|
1872 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
1873 return ANK_Success;
|
|
1874
|
|
1875 case Sema::NC_UndeclaredNonType:
|
|
1876 case Sema::NC_DependentNonType:
|
|
1877 Tok.setKind(Classification.getKind() == Sema::NC_UndeclaredNonType
|
|
1878 ? tok::annot_non_type_undeclared
|
|
1879 : tok::annot_non_type_dependent);
|
|
1880 setIdentifierAnnotation(Tok, Name);
|
|
1881 Tok.setLocation(NameLoc);
|
|
1882 Tok.setAnnotationEndLoc(NameLoc);
|
|
1883 PP.AnnotateCachedTokens(Tok);
|
|
1884 if (SS.isNotEmpty())
|
|
1885 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
1886 return ANK_Success;
|
|
1887
|
|
1888 case Sema::NC_TypeTemplate:
|
|
1889 if (Next.isNot(tok::less)) {
|
|
1890 // This may be a type template being used as a template template argument.
|
|
1891 if (SS.isNotEmpty())
|
|
1892 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
1893 return ANK_TemplateName;
|
|
1894 }
|
236
|
1895 [[fallthrough]];
|
252
|
1896 case Sema::NC_Concept:
|
150
|
1897 case Sema::NC_VarTemplate:
|
|
1898 case Sema::NC_FunctionTemplate:
|
|
1899 case Sema::NC_UndeclaredTemplate: {
|
252
|
1900 bool IsConceptName = Classification.getKind() == Sema::NC_Concept;
|
|
1901 // We have a template name followed by '<'. Consume the identifier token so
|
|
1902 // we reach the '<' and annotate it.
|
|
1903 if (Next.is(tok::less))
|
|
1904 ConsumeToken();
|
150
|
1905 UnqualifiedId Id;
|
|
1906 Id.setIdentifier(Name, NameLoc);
|
|
1907 if (AnnotateTemplateIdToken(
|
|
1908 TemplateTy::make(Classification.getTemplateName()),
|
252
|
1909 Classification.getTemplateNameKind(), SS, SourceLocation(), Id,
|
|
1910 /*AllowTypeAnnotation=*/!IsConceptName,
|
|
1911 /*TypeConstraint=*/IsConceptName))
|
150
|
1912 return ANK_Error;
|
252
|
1913 if (SS.isNotEmpty())
|
|
1914 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
150
|
1915 return ANK_Success;
|
|
1916 }
|
|
1917 }
|
|
1918
|
|
1919 // Unable to classify the name, but maybe we can annotate a scope specifier.
|
|
1920 if (SS.isNotEmpty())
|
|
1921 AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
1922 return ANK_Unresolved;
|
|
1923 }
|
|
1924
|
|
1925 bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
|
|
1926 assert(Tok.isNot(tok::identifier));
|
|
1927 Diag(Tok, diag::ext_keyword_as_ident)
|
|
1928 << PP.getSpelling(Tok)
|
|
1929 << DisableKeyword;
|
|
1930 if (DisableKeyword)
|
|
1931 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
|
|
1932 Tok.setKind(tok::identifier);
|
|
1933 return true;
|
|
1934 }
|
|
1935
|
|
1936 /// TryAnnotateTypeOrScopeToken - If the current token position is on a
|
|
1937 /// typename (possibly qualified in C++) or a C++ scope specifier not followed
|
|
1938 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
|
|
1939 /// with a single annotation token representing the typename or C++ scope
|
|
1940 /// respectively.
|
|
1941 /// This simplifies handling of C++ scope specifiers and allows efficient
|
|
1942 /// backtracking without the need to re-parse and resolve nested-names and
|
|
1943 /// typenames.
|
|
1944 /// It will mainly be called when we expect to treat identifiers as typenames
|
|
1945 /// (if they are typenames). For example, in C we do not expect identifiers
|
|
1946 /// inside expressions to be treated as typenames so it will not be called
|
|
1947 /// for expressions in C.
|
|
1948 /// The benefit for C/ObjC is that a typename will be annotated and
|
|
1949 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
|
|
1950 /// will not be called twice, once to check whether we have a declaration
|
|
1951 /// specifier, and another one to get the actual type inside
|
|
1952 /// ParseDeclarationSpecifiers).
|
|
1953 ///
|
|
1954 /// This returns true if an error occurred.
|
|
1955 ///
|
|
1956 /// Note that this routine emits an error if you call it with ::new or ::delete
|
|
1957 /// as the current tokens, so only call it in contexts where these are invalid.
|
236
|
1958 bool Parser::TryAnnotateTypeOrScopeToken(
|
|
1959 ImplicitTypenameContext AllowImplicitTypename) {
|
150
|
1960 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
|
|
1961 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
|
|
1962 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
|
252
|
1963 Tok.is(tok::kw___super) || Tok.is(tok::kw_auto)) &&
|
150
|
1964 "Cannot be a type or scope token!");
|
|
1965
|
|
1966 if (Tok.is(tok::kw_typename)) {
|
|
1967 // MSVC lets you do stuff like:
|
|
1968 // typename typedef T_::D D;
|
|
1969 //
|
|
1970 // We will consume the typedef token here and put it back after we have
|
|
1971 // parsed the first identifier, transforming it into something more like:
|
|
1972 // typename T_::D typedef D;
|
|
1973 if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
|
|
1974 Token TypedefToken;
|
|
1975 PP.Lex(TypedefToken);
|
236
|
1976 bool Result = TryAnnotateTypeOrScopeToken(AllowImplicitTypename);
|
150
|
1977 PP.EnterToken(Tok, /*IsReinject=*/true);
|
|
1978 Tok = TypedefToken;
|
|
1979 if (!Result)
|
|
1980 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
|
|
1981 return Result;
|
|
1982 }
|
|
1983
|
|
1984 // Parse a C++ typename-specifier, e.g., "typename T::type".
|
|
1985 //
|
|
1986 // typename-specifier:
|
|
1987 // 'typename' '::' [opt] nested-name-specifier identifier
|
|
1988 // 'typename' '::' [opt] nested-name-specifier template [opt]
|
|
1989 // simple-template-id
|
|
1990 SourceLocation TypenameLoc = ConsumeToken();
|
|
1991 CXXScopeSpec SS;
|
|
1992 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
236
|
1993 /*ObjectHasErrors=*/false,
|
150
|
1994 /*EnteringContext=*/false, nullptr,
|
|
1995 /*IsTypename*/ true))
|
|
1996 return true;
|
|
1997 if (SS.isEmpty()) {
|
|
1998 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
|
|
1999 Tok.is(tok::annot_decltype)) {
|
|
2000 // Attempt to recover by skipping the invalid 'typename'
|
|
2001 if (Tok.is(tok::annot_decltype) ||
|
236
|
2002 (!TryAnnotateTypeOrScopeToken(AllowImplicitTypename) &&
|
|
2003 Tok.isAnnotation())) {
|
150
|
2004 unsigned DiagID = diag::err_expected_qualified_after_typename;
|
|
2005 // MS compatibility: MSVC permits using known types with typename.
|
|
2006 // e.g. "typedef typename T* pointer_type"
|
|
2007 if (getLangOpts().MicrosoftExt)
|
|
2008 DiagID = diag::warn_expected_qualified_after_typename;
|
|
2009 Diag(Tok.getLocation(), DiagID);
|
|
2010 return false;
|
|
2011 }
|
|
2012 }
|
|
2013 if (Tok.isEditorPlaceholder())
|
|
2014 return true;
|
|
2015
|
|
2016 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
|
|
2017 return true;
|
|
2018 }
|
|
2019
|
|
2020 TypeResult Ty;
|
|
2021 if (Tok.is(tok::identifier)) {
|
|
2022 // FIXME: check whether the next token is '<', first!
|
|
2023 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
|
|
2024 *Tok.getIdentifierInfo(),
|
|
2025 Tok.getLocation());
|
|
2026 } else if (Tok.is(tok::annot_template_id)) {
|
|
2027 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
173
|
2028 if (!TemplateId->mightBeType()) {
|
150
|
2029 Diag(Tok, diag::err_typename_refers_to_non_type_template)
|
|
2030 << Tok.getAnnotationRange();
|
|
2031 return true;
|
|
2032 }
|
|
2033
|
|
2034 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
|
|
2035 TemplateId->NumArgs);
|
|
2036
|
173
|
2037 Ty = TemplateId->isInvalid()
|
|
2038 ? TypeError()
|
|
2039 : Actions.ActOnTypenameType(
|
|
2040 getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc,
|
|
2041 TemplateId->Template, TemplateId->Name,
|
|
2042 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
|
|
2043 TemplateArgsPtr, TemplateId->RAngleLoc);
|
150
|
2044 } else {
|
|
2045 Diag(Tok, diag::err_expected_type_name_after_typename)
|
|
2046 << SS.getRange();
|
|
2047 return true;
|
|
2048 }
|
|
2049
|
|
2050 SourceLocation EndLoc = Tok.getLastLoc();
|
|
2051 Tok.setKind(tok::annot_typename);
|
173
|
2052 setTypeAnnotation(Tok, Ty);
|
150
|
2053 Tok.setAnnotationEndLoc(EndLoc);
|
|
2054 Tok.setLocation(TypenameLoc);
|
|
2055 PP.AnnotateCachedTokens(Tok);
|
|
2056 return false;
|
|
2057 }
|
|
2058
|
|
2059 // Remembers whether the token was originally a scope annotation.
|
|
2060 bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
|
|
2061
|
|
2062 CXXScopeSpec SS;
|
|
2063 if (getLangOpts().CPlusPlus)
|
173
|
2064 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
236
|
2065 /*ObjectHasErrors=*/false,
|
173
|
2066 /*EnteringContext*/ false))
|
150
|
2067 return true;
|
|
2068
|
236
|
2069 return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation,
|
|
2070 AllowImplicitTypename);
|
150
|
2071 }
|
|
2072
|
|
2073 /// Try to annotate a type or scope token, having already parsed an
|
|
2074 /// optional scope specifier. \p IsNewScope should be \c true unless the scope
|
|
2075 /// specifier was extracted from an existing tok::annot_cxxscope annotation.
|
236
|
2076 bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(
|
|
2077 CXXScopeSpec &SS, bool IsNewScope,
|
|
2078 ImplicitTypenameContext AllowImplicitTypename) {
|
150
|
2079 if (Tok.is(tok::identifier)) {
|
|
2080 // Determine whether the identifier is a type name.
|
|
2081 if (ParsedType Ty = Actions.getTypeName(
|
|
2082 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
|
|
2083 false, NextToken().is(tok::period), nullptr,
|
|
2084 /*IsCtorOrDtorName=*/false,
|
236
|
2085 /*NonTrivialTypeSourceInfo=*/true,
|
|
2086 /*IsClassTemplateDeductionContext=*/true, AllowImplicitTypename)) {
|
150
|
2087 SourceLocation BeginLoc = Tok.getLocation();
|
|
2088 if (SS.isNotEmpty()) // it was a C++ qualified type name.
|
|
2089 BeginLoc = SS.getBeginLoc();
|
|
2090
|
|
2091 /// An Objective-C object type followed by '<' is a specialization of
|
|
2092 /// a parameterized class type or a protocol-qualified type.
|
|
2093 if (getLangOpts().ObjC && NextToken().is(tok::less) &&
|
|
2094 (Ty.get()->isObjCObjectType() ||
|
|
2095 Ty.get()->isObjCObjectPointerType())) {
|
|
2096 // Consume the name.
|
|
2097 SourceLocation IdentifierLoc = ConsumeToken();
|
|
2098 SourceLocation NewEndLoc;
|
|
2099 TypeResult NewType
|
|
2100 = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
|
|
2101 /*consumeLastToken=*/false,
|
|
2102 NewEndLoc);
|
|
2103 if (NewType.isUsable())
|
|
2104 Ty = NewType.get();
|
|
2105 else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
|
|
2106 return false;
|
|
2107 }
|
|
2108
|
|
2109 // This is a typename. Replace the current token in-place with an
|
|
2110 // annotation type token.
|
|
2111 Tok.setKind(tok::annot_typename);
|
|
2112 setTypeAnnotation(Tok, Ty);
|
|
2113 Tok.setAnnotationEndLoc(Tok.getLocation());
|
|
2114 Tok.setLocation(BeginLoc);
|
|
2115
|
|
2116 // In case the tokens were cached, have Preprocessor replace
|
|
2117 // them with the annotation token.
|
|
2118 PP.AnnotateCachedTokens(Tok);
|
|
2119 return false;
|
|
2120 }
|
|
2121
|
|
2122 if (!getLangOpts().CPlusPlus) {
|
252
|
2123 // If we're in C, the only place we can have :: tokens is C23
|
236
|
2124 // attribute which is parsed elsewhere. If the identifier is not a type,
|
|
2125 // then it can't be scope either, just early exit.
|
150
|
2126 return false;
|
|
2127 }
|
|
2128
|
|
2129 // If this is a template-id, annotate with a template-id or type token.
|
|
2130 // FIXME: This appears to be dead code. We already have formed template-id
|
|
2131 // tokens when parsing the scope specifier; this can never form a new one.
|
|
2132 if (NextToken().is(tok::less)) {
|
|
2133 TemplateTy Template;
|
|
2134 UnqualifiedId TemplateName;
|
|
2135 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
|
|
2136 bool MemberOfUnknownSpecialization;
|
|
2137 if (TemplateNameKind TNK = Actions.isTemplateName(
|
|
2138 getCurScope(), SS,
|
|
2139 /*hasTemplateKeyword=*/false, TemplateName,
|
|
2140 /*ObjectType=*/nullptr, /*EnteringContext*/false, Template,
|
|
2141 MemberOfUnknownSpecialization)) {
|
|
2142 // Only annotate an undeclared template name as a template-id if the
|
|
2143 // following tokens have the form of a template argument list.
|
|
2144 if (TNK != TNK_Undeclared_template ||
|
|
2145 isTemplateArgumentList(1) != TPResult::False) {
|
|
2146 // Consume the identifier.
|
|
2147 ConsumeToken();
|
|
2148 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
|
|
2149 TemplateName)) {
|
|
2150 // If an unrecoverable error occurred, we need to return true here,
|
|
2151 // because the token stream is in a damaged state. We may not
|
|
2152 // return a valid identifier.
|
|
2153 return true;
|
|
2154 }
|
|
2155 }
|
|
2156 }
|
|
2157 }
|
|
2158
|
|
2159 // The current token, which is either an identifier or a
|
|
2160 // template-id, is not part of the annotation. Fall through to
|
|
2161 // push that token back into the stream and complete the C++ scope
|
|
2162 // specifier annotation.
|
|
2163 }
|
|
2164
|
|
2165 if (Tok.is(tok::annot_template_id)) {
|
|
2166 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
|
2167 if (TemplateId->Kind == TNK_Type_template) {
|
|
2168 // A template-id that refers to a type was parsed into a
|
|
2169 // template-id annotation in a context where we weren't allowed
|
|
2170 // to produce a type annotation token. Update the template-id
|
|
2171 // annotation token to a type annotation token now.
|
236
|
2172 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
|
150
|
2173 return false;
|
|
2174 }
|
|
2175 }
|
|
2176
|
|
2177 if (SS.isEmpty())
|
|
2178 return false;
|
|
2179
|
|
2180 // A C++ scope specifier that isn't followed by a typename.
|
|
2181 AnnotateScopeToken(SS, IsNewScope);
|
|
2182 return false;
|
|
2183 }
|
|
2184
|
|
2185 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
|
|
2186 /// annotates C++ scope specifiers and template-ids. This returns
|
|
2187 /// true if there was an error that could not be recovered from.
|
|
2188 ///
|
|
2189 /// Note that this routine emits an error if you call it with ::new or ::delete
|
|
2190 /// as the current tokens, so only call it in contexts where these are invalid.
|
|
2191 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
|
|
2192 assert(getLangOpts().CPlusPlus &&
|
|
2193 "Call sites of this function should be guarded by checking for C++");
|
|
2194 assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");
|
|
2195
|
|
2196 CXXScopeSpec SS;
|
173
|
2197 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
236
|
2198 /*ObjectHasErrors=*/false,
|
173
|
2199 EnteringContext))
|
150
|
2200 return true;
|
|
2201 if (SS.isEmpty())
|
|
2202 return false;
|
|
2203
|
|
2204 AnnotateScopeToken(SS, true);
|
|
2205 return false;
|
|
2206 }
|
|
2207
|
|
2208 bool Parser::isTokenEqualOrEqualTypo() {
|
|
2209 tok::TokenKind Kind = Tok.getKind();
|
|
2210 switch (Kind) {
|
|
2211 default:
|
|
2212 return false;
|
|
2213 case tok::ampequal: // &=
|
|
2214 case tok::starequal: // *=
|
|
2215 case tok::plusequal: // +=
|
|
2216 case tok::minusequal: // -=
|
|
2217 case tok::exclaimequal: // !=
|
|
2218 case tok::slashequal: // /=
|
|
2219 case tok::percentequal: // %=
|
|
2220 case tok::lessequal: // <=
|
|
2221 case tok::lesslessequal: // <<=
|
|
2222 case tok::greaterequal: // >=
|
|
2223 case tok::greatergreaterequal: // >>=
|
|
2224 case tok::caretequal: // ^=
|
|
2225 case tok::pipeequal: // |=
|
|
2226 case tok::equalequal: // ==
|
|
2227 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
|
|
2228 << Kind
|
|
2229 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
|
236
|
2230 [[fallthrough]];
|
150
|
2231 case tok::equal:
|
|
2232 return true;
|
|
2233 }
|
|
2234 }
|
|
2235
|
|
2236 SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
|
|
2237 assert(Tok.is(tok::code_completion));
|
|
2238 PrevTokLocation = Tok.getLocation();
|
|
2239
|
|
2240 for (Scope *S = getCurScope(); S; S = S->getParent()) {
|
236
|
2241 if (S->isFunctionScope()) {
|
207
|
2242 cutOffParsing();
|
150
|
2243 Actions.CodeCompleteOrdinaryName(getCurScope(),
|
|
2244 Sema::PCC_RecoveryInFunction);
|
|
2245 return PrevTokLocation;
|
|
2246 }
|
|
2247
|
236
|
2248 if (S->isClassScope()) {
|
207
|
2249 cutOffParsing();
|
150
|
2250 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
|
|
2251 return PrevTokLocation;
|
|
2252 }
|
|
2253 }
|
|
2254
|
207
|
2255 cutOffParsing();
|
150
|
2256 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
|
|
2257 return PrevTokLocation;
|
|
2258 }
|
|
2259
|
|
2260 // Code-completion pass-through functions
|
|
2261
|
|
2262 void Parser::CodeCompleteDirective(bool InConditional) {
|
|
2263 Actions.CodeCompletePreprocessorDirective(InConditional);
|
|
2264 }
|
|
2265
|
|
2266 void Parser::CodeCompleteInConditionalExclusion() {
|
|
2267 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
|
|
2268 }
|
|
2269
|
|
2270 void Parser::CodeCompleteMacroName(bool IsDefinition) {
|
|
2271 Actions.CodeCompletePreprocessorMacroName(IsDefinition);
|
|
2272 }
|
|
2273
|
|
2274 void Parser::CodeCompletePreprocessorExpression() {
|
|
2275 Actions.CodeCompletePreprocessorExpression();
|
|
2276 }
|
|
2277
|
|
2278 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
|
|
2279 MacroInfo *MacroInfo,
|
|
2280 unsigned ArgumentIndex) {
|
|
2281 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
|
|
2282 ArgumentIndex);
|
|
2283 }
|
|
2284
|
|
2285 void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {
|
|
2286 Actions.CodeCompleteIncludedFile(Dir, IsAngled);
|
|
2287 }
|
|
2288
|
|
2289 void Parser::CodeCompleteNaturalLanguage() {
|
|
2290 Actions.CodeCompleteNaturalLanguage();
|
|
2291 }
|
|
2292
|
|
2293 bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
|
|
2294 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
|
|
2295 "Expected '__if_exists' or '__if_not_exists'");
|
|
2296 Result.IsIfExists = Tok.is(tok::kw___if_exists);
|
|
2297 Result.KeywordLoc = ConsumeToken();
|
|
2298
|
|
2299 BalancedDelimiterTracker T(*this, tok::l_paren);
|
|
2300 if (T.consumeOpen()) {
|
|
2301 Diag(Tok, diag::err_expected_lparen_after)
|
|
2302 << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
|
|
2303 return true;
|
|
2304 }
|
|
2305
|
|
2306 // Parse nested-name-specifier.
|
|
2307 if (getLangOpts().CPlusPlus)
|
173
|
2308 ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,
|
236
|
2309 /*ObjectHasErrors=*/false,
|
150
|
2310 /*EnteringContext=*/false);
|
|
2311
|
|
2312 // Check nested-name specifier.
|
|
2313 if (Result.SS.isInvalid()) {
|
|
2314 T.skipToEnd();
|
|
2315 return true;
|
|
2316 }
|
|
2317
|
|
2318 // Parse the unqualified-id.
|
|
2319 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
|
173
|
2320 if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,
|
|
2321 /*ObjectHadErrors=*/false, /*EnteringContext*/ false,
|
|
2322 /*AllowDestructorName*/ true,
|
|
2323 /*AllowConstructorName*/ true,
|
|
2324 /*AllowDeductionGuide*/ false, &TemplateKWLoc,
|
|
2325 Result.Name)) {
|
150
|
2326 T.skipToEnd();
|
|
2327 return true;
|
|
2328 }
|
|
2329
|
|
2330 if (T.consumeClose())
|
|
2331 return true;
|
|
2332
|
|
2333 // Check if the symbol exists.
|
|
2334 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
|
|
2335 Result.IsIfExists, Result.SS,
|
|
2336 Result.Name)) {
|
|
2337 case Sema::IER_Exists:
|
|
2338 Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
|
|
2339 break;
|
|
2340
|
|
2341 case Sema::IER_DoesNotExist:
|
|
2342 Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
|
|
2343 break;
|
|
2344
|
|
2345 case Sema::IER_Dependent:
|
|
2346 Result.Behavior = IEB_Dependent;
|
|
2347 break;
|
|
2348
|
|
2349 case Sema::IER_Error:
|
|
2350 return true;
|
|
2351 }
|
|
2352
|
|
2353 return false;
|
|
2354 }
|
|
2355
|
|
2356 void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
|
|
2357 IfExistsCondition Result;
|
|
2358 if (ParseMicrosoftIfExistsCondition(Result))
|
|
2359 return;
|
|
2360
|
|
2361 BalancedDelimiterTracker Braces(*this, tok::l_brace);
|
|
2362 if (Braces.consumeOpen()) {
|
|
2363 Diag(Tok, diag::err_expected) << tok::l_brace;
|
|
2364 return;
|
|
2365 }
|
|
2366
|
|
2367 switch (Result.Behavior) {
|
|
2368 case IEB_Parse:
|
|
2369 // Parse declarations below.
|
|
2370 break;
|
|
2371
|
|
2372 case IEB_Dependent:
|
|
2373 llvm_unreachable("Cannot have a dependent external declaration");
|
|
2374
|
|
2375 case IEB_Skip:
|
|
2376 Braces.skipToEnd();
|
|
2377 return;
|
|
2378 }
|
|
2379
|
|
2380 // Parse the declarations.
|
|
2381 // FIXME: Support module import within __if_exists?
|
|
2382 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
|
236
|
2383 ParsedAttributes Attrs(AttrFactory);
|
|
2384 MaybeParseCXX11Attributes(Attrs);
|
252
|
2385 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
|
|
2386 DeclGroupPtrTy Result = ParseExternalDeclaration(Attrs, EmptyDeclSpecAttrs);
|
150
|
2387 if (Result && !getCurScope()->getParent())
|
|
2388 Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
|
|
2389 }
|
|
2390 Braces.consumeClose();
|
|
2391 }
|
|
2392
|
|
2393 /// Parse a declaration beginning with the 'module' keyword or C++20
|
|
2394 /// context-sensitive keyword (optionally preceded by 'export').
|
|
2395 ///
|
252
|
2396 /// module-declaration: [C++20]
|
150
|
2397 /// 'export'[opt] 'module' module-name attribute-specifier-seq[opt] ';'
|
|
2398 ///
|
|
2399 /// global-module-fragment: [C++2a]
|
|
2400 /// 'module' ';' top-level-declaration-seq[opt]
|
|
2401 /// module-declaration: [C++2a]
|
|
2402 /// 'export'[opt] 'module' module-name module-partition[opt]
|
|
2403 /// attribute-specifier-seq[opt] ';'
|
|
2404 /// private-module-fragment: [C++2a]
|
|
2405 /// 'module' ':' 'private' ';' top-level-declaration-seq[opt]
|
236
|
2406 Parser::DeclGroupPtrTy
|
|
2407 Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
|
150
|
2408 SourceLocation StartLoc = Tok.getLocation();
|
|
2409
|
|
2410 Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)
|
|
2411 ? Sema::ModuleDeclKind::Interface
|
|
2412 : Sema::ModuleDeclKind::Implementation;
|
|
2413
|
|
2414 assert(
|
|
2415 (Tok.is(tok::kw_module) ||
|
|
2416 (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&
|
|
2417 "not a module declaration");
|
|
2418 SourceLocation ModuleLoc = ConsumeToken();
|
|
2419
|
|
2420 // Attributes appear after the module name, not before.
|
|
2421 // FIXME: Suggest moving the attributes later with a fixit.
|
|
2422 DiagnoseAndSkipCXX11Attributes();
|
|
2423
|
|
2424 // Parse a global-module-fragment, if present.
|
|
2425 if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {
|
|
2426 SourceLocation SemiLoc = ConsumeToken();
|
236
|
2427 if (ImportState != Sema::ModuleImportState::FirstDecl) {
|
150
|
2428 Diag(StartLoc, diag::err_global_module_introducer_not_at_start)
|
|
2429 << SourceRange(StartLoc, SemiLoc);
|
|
2430 return nullptr;
|
|
2431 }
|
|
2432 if (MDK == Sema::ModuleDeclKind::Interface) {
|
|
2433 Diag(StartLoc, diag::err_module_fragment_exported)
|
|
2434 << /*global*/0 << FixItHint::CreateRemoval(StartLoc);
|
|
2435 }
|
236
|
2436 ImportState = Sema::ModuleImportState::GlobalFragment;
|
150
|
2437 return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);
|
|
2438 }
|
|
2439
|
|
2440 // Parse a private-module-fragment, if present.
|
|
2441 if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&
|
|
2442 NextToken().is(tok::kw_private)) {
|
|
2443 if (MDK == Sema::ModuleDeclKind::Interface) {
|
|
2444 Diag(StartLoc, diag::err_module_fragment_exported)
|
|
2445 << /*private*/1 << FixItHint::CreateRemoval(StartLoc);
|
|
2446 }
|
|
2447 ConsumeToken();
|
|
2448 SourceLocation PrivateLoc = ConsumeToken();
|
|
2449 DiagnoseAndSkipCXX11Attributes();
|
|
2450 ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);
|
252
|
2451 ImportState = ImportState == Sema::ModuleImportState::ImportAllowed
|
|
2452 ? Sema::ModuleImportState::PrivateFragmentImportAllowed
|
|
2453 : Sema::ModuleImportState::PrivateFragmentImportFinished;
|
150
|
2454 return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);
|
|
2455 }
|
|
2456
|
|
2457 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
236
|
2458 if (ParseModuleName(ModuleLoc, Path, /*IsImport*/ false))
|
150
|
2459 return nullptr;
|
|
2460
|
|
2461 // Parse the optional module-partition.
|
236
|
2462 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
|
150
|
2463 if (Tok.is(tok::colon)) {
|
|
2464 SourceLocation ColonLoc = ConsumeToken();
|
236
|
2465 if (!getLangOpts().CPlusPlusModules)
|
|
2466 Diag(ColonLoc, diag::err_unsupported_module_partition)
|
|
2467 << SourceRange(ColonLoc, Partition.back().second);
|
|
2468 // Recover by ignoring the partition name.
|
|
2469 else if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/ false))
|
150
|
2470 return nullptr;
|
|
2471 }
|
|
2472
|
|
2473 // We don't support any module attributes yet; just parse them and diagnose.
|
236
|
2474 ParsedAttributes Attrs(AttrFactory);
|
150
|
2475 MaybeParseCXX11Attributes(Attrs);
|
236
|
2476 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr,
|
252
|
2477 diag::err_keyword_not_module_attr,
|
236
|
2478 /*DiagnoseEmptyAttrs=*/false,
|
|
2479 /*WarnOnUnknownAttrs=*/true);
|
150
|
2480
|
|
2481 ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
2482
|
236
|
2483 return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, Partition,
|
|
2484 ImportState);
|
150
|
2485 }
|
|
2486
|
|
2487 /// Parse a module import declaration. This is essentially the same for
|
236
|
2488 /// Objective-C and C++20 except for the leading '@' (in ObjC) and the
|
|
2489 /// trailing optional attributes (in C++).
|
150
|
2490 ///
|
|
2491 /// [ObjC] @import declaration:
|
|
2492 /// '@' 'import' module-name ';'
|
|
2493 /// [ModTS] module-import-declaration:
|
|
2494 /// 'import' module-name attribute-specifier-seq[opt] ';'
|
236
|
2495 /// [C++20] module-import-declaration:
|
150
|
2496 /// 'export'[opt] 'import' module-name
|
|
2497 /// attribute-specifier-seq[opt] ';'
|
|
2498 /// 'export'[opt] 'import' module-partition
|
|
2499 /// attribute-specifier-seq[opt] ';'
|
|
2500 /// 'export'[opt] 'import' header-name
|
|
2501 /// attribute-specifier-seq[opt] ';'
|
236
|
2502 Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
|
|
2503 Sema::ModuleImportState &ImportState) {
|
150
|
2504 SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;
|
|
2505
|
|
2506 SourceLocation ExportLoc;
|
|
2507 TryConsumeToken(tok::kw_export, ExportLoc);
|
|
2508
|
|
2509 assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)
|
|
2510 : Tok.isObjCAtKeyword(tok::objc_import)) &&
|
|
2511 "Improper start to module import");
|
|
2512 bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
|
|
2513 SourceLocation ImportLoc = ConsumeToken();
|
|
2514
|
236
|
2515 // For C++20 modules, we can have "name" or ":Partition name" as valid input.
|
150
|
2516 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
236
|
2517 bool IsPartition = false;
|
150
|
2518 Module *HeaderUnit = nullptr;
|
|
2519 if (Tok.is(tok::header_name)) {
|
|
2520 // This is a header import that the preprocessor decided we should skip
|
|
2521 // because it was malformed in some way. Parse and ignore it; it's already
|
|
2522 // been diagnosed.
|
|
2523 ConsumeToken();
|
|
2524 } else if (Tok.is(tok::annot_header_unit)) {
|
|
2525 // This is a header import that the preprocessor mapped to a module import.
|
|
2526 HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());
|
|
2527 ConsumeAnnotationToken();
|
236
|
2528 } else if (Tok.is(tok::colon)) {
|
150
|
2529 SourceLocation ColonLoc = ConsumeToken();
|
236
|
2530 if (!getLangOpts().CPlusPlusModules)
|
|
2531 Diag(ColonLoc, diag::err_unsupported_module_partition)
|
|
2532 << SourceRange(ColonLoc, Path.back().second);
|
|
2533 // Recover by leaving partition empty.
|
|
2534 else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true))
|
150
|
2535 return nullptr;
|
236
|
2536 else
|
|
2537 IsPartition = true;
|
150
|
2538 } else {
|
236
|
2539 if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true))
|
150
|
2540 return nullptr;
|
|
2541 }
|
|
2542
|
236
|
2543 ParsedAttributes Attrs(AttrFactory);
|
150
|
2544 MaybeParseCXX11Attributes(Attrs);
|
|
2545 // We don't support any module import attributes yet.
|
236
|
2546 ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr,
|
252
|
2547 diag::err_keyword_not_import_attr,
|
236
|
2548 /*DiagnoseEmptyAttrs=*/false,
|
|
2549 /*WarnOnUnknownAttrs=*/true);
|
150
|
2550
|
|
2551 if (PP.hadModuleLoaderFatalFailure()) {
|
|
2552 // With a fatal failure in the module loader, we abort parsing.
|
|
2553 cutOffParsing();
|
|
2554 return nullptr;
|
|
2555 }
|
|
2556
|
236
|
2557 // Diagnose mis-imports.
|
|
2558 bool SeenError = true;
|
|
2559 switch (ImportState) {
|
|
2560 case Sema::ModuleImportState::ImportAllowed:
|
|
2561 SeenError = false;
|
|
2562 break;
|
|
2563 case Sema::ModuleImportState::FirstDecl:
|
|
2564 case Sema::ModuleImportState::NotACXX20Module:
|
|
2565 // We can only import a partition within a module purview.
|
|
2566 if (IsPartition)
|
|
2567 Diag(ImportLoc, diag::err_partition_import_outside_module);
|
|
2568 else
|
|
2569 SeenError = false;
|
|
2570 break;
|
|
2571 case Sema::ModuleImportState::GlobalFragment:
|
252
|
2572 case Sema::ModuleImportState::PrivateFragmentImportAllowed:
|
|
2573 // We can only have pre-processor directives in the global module fragment
|
|
2574 // which allows pp-import, but not of a partition (since the global module
|
|
2575 // does not have partitions).
|
|
2576 // We cannot import a partition into a private module fragment, since
|
|
2577 // [module.private.frag]/1 disallows private module fragments in a multi-
|
|
2578 // TU module.
|
|
2579 if (IsPartition || (HeaderUnit && HeaderUnit->Kind !=
|
|
2580 Module::ModuleKind::ModuleHeaderUnit))
|
|
2581 Diag(ImportLoc, diag::err_import_in_wrong_fragment)
|
|
2582 << IsPartition
|
|
2583 << (ImportState == Sema::ModuleImportState::GlobalFragment ? 0 : 1);
|
236
|
2584 else
|
|
2585 SeenError = false;
|
|
2586 break;
|
|
2587 case Sema::ModuleImportState::ImportFinished:
|
252
|
2588 case Sema::ModuleImportState::PrivateFragmentImportFinished:
|
236
|
2589 if (getLangOpts().CPlusPlusModules)
|
|
2590 Diag(ImportLoc, diag::err_import_not_allowed_here);
|
|
2591 else
|
|
2592 SeenError = false;
|
|
2593 break;
|
|
2594 }
|
|
2595 if (SeenError) {
|
|
2596 ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
2597 return nullptr;
|
|
2598 }
|
|
2599
|
150
|
2600 DeclResult Import;
|
|
2601 if (HeaderUnit)
|
|
2602 Import =
|
|
2603 Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
|
|
2604 else if (!Path.empty())
|
236
|
2605 Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path,
|
|
2606 IsPartition);
|
150
|
2607 ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
2608 if (Import.isInvalid())
|
|
2609 return nullptr;
|
|
2610
|
|
2611 // Using '@import' in framework headers requires modules to be enabled so that
|
|
2612 // the header is parseable. Emit a warning to make the user aware.
|
|
2613 if (IsObjCAtImport && AtLoc.isValid()) {
|
|
2614 auto &SrcMgr = PP.getSourceManager();
|
236
|
2615 auto FE = SrcMgr.getFileEntryRefForID(SrcMgr.getFileID(AtLoc));
|
|
2616 if (FE && llvm::sys::path::parent_path(FE->getDir().getName())
|
150
|
2617 .endswith(".framework"))
|
|
2618 Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);
|
|
2619 }
|
|
2620
|
|
2621 return Import.get();
|
|
2622 }
|
|
2623
|
252
|
2624 /// Parse a C++ / Objective-C module name (both forms use the same
|
150
|
2625 /// grammar).
|
|
2626 ///
|
|
2627 /// module-name:
|
|
2628 /// module-name-qualifier[opt] identifier
|
|
2629 /// module-name-qualifier:
|
|
2630 /// module-name-qualifier[opt] identifier '.'
|
|
2631 bool Parser::ParseModuleName(
|
|
2632 SourceLocation UseLoc,
|
|
2633 SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
|
|
2634 bool IsImport) {
|
|
2635 // Parse the module path.
|
|
2636 while (true) {
|
|
2637 if (!Tok.is(tok::identifier)) {
|
|
2638 if (Tok.is(tok::code_completion)) {
|
207
|
2639 cutOffParsing();
|
150
|
2640 Actions.CodeCompleteModuleImport(UseLoc, Path);
|
|
2641 return true;
|
|
2642 }
|
|
2643
|
|
2644 Diag(Tok, diag::err_module_expected_ident) << IsImport;
|
|
2645 SkipUntil(tok::semi);
|
|
2646 return true;
|
|
2647 }
|
|
2648
|
|
2649 // Record this part of the module path.
|
|
2650 Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
|
|
2651 ConsumeToken();
|
|
2652
|
|
2653 if (Tok.isNot(tok::period))
|
|
2654 return false;
|
|
2655
|
|
2656 ConsumeToken();
|
|
2657 }
|
|
2658 }
|
|
2659
|
|
2660 /// Try recover parser when module annotation appears where it must not
|
|
2661 /// be found.
|
|
2662 /// \returns false if the recover was successful and parsing may be continued, or
|
|
2663 /// true if parser must bail out to top level and handle the token there.
|
|
2664 bool Parser::parseMisplacedModuleImport() {
|
|
2665 while (true) {
|
|
2666 switch (Tok.getKind()) {
|
|
2667 case tok::annot_module_end:
|
|
2668 // If we recovered from a misplaced module begin, we expect to hit a
|
|
2669 // misplaced module end too. Stay in the current context when this
|
|
2670 // happens.
|
|
2671 if (MisplacedModuleBeginCount) {
|
|
2672 --MisplacedModuleBeginCount;
|
|
2673 Actions.ActOnModuleEnd(Tok.getLocation(),
|
|
2674 reinterpret_cast<Module *>(
|
|
2675 Tok.getAnnotationValue()));
|
|
2676 ConsumeAnnotationToken();
|
|
2677 continue;
|
|
2678 }
|
|
2679 // Inform caller that recovery failed, the error must be handled at upper
|
|
2680 // level. This will generate the desired "missing '}' at end of module"
|
|
2681 // diagnostics on the way out.
|
|
2682 return true;
|
|
2683 case tok::annot_module_begin:
|
|
2684 // Recover by entering the module (Sema will diagnose).
|
|
2685 Actions.ActOnModuleBegin(Tok.getLocation(),
|
|
2686 reinterpret_cast<Module *>(
|
|
2687 Tok.getAnnotationValue()));
|
|
2688 ConsumeAnnotationToken();
|
|
2689 ++MisplacedModuleBeginCount;
|
|
2690 continue;
|
|
2691 case tok::annot_module_include:
|
|
2692 // Module import found where it should not be, for instance, inside a
|
|
2693 // namespace. Recover by importing the module.
|
|
2694 Actions.ActOnModuleInclude(Tok.getLocation(),
|
|
2695 reinterpret_cast<Module *>(
|
|
2696 Tok.getAnnotationValue()));
|
|
2697 ConsumeAnnotationToken();
|
|
2698 // If there is another module import, process it.
|
|
2699 continue;
|
|
2700 default:
|
|
2701 return false;
|
|
2702 }
|
|
2703 }
|
|
2704 return false;
|
|
2705 }
|
|
2706
|
|
2707 bool BalancedDelimiterTracker::diagnoseOverflow() {
|
|
2708 P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
|
|
2709 << P.getLangOpts().BracketDepth;
|
|
2710 P.Diag(P.Tok, diag::note_bracket_depth);
|
|
2711 P.cutOffParsing();
|
|
2712 return true;
|
|
2713 }
|
|
2714
|
|
2715 bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
|
|
2716 const char *Msg,
|
|
2717 tok::TokenKind SkipToTok) {
|
|
2718 LOpen = P.Tok.getLocation();
|
|
2719 if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
|
|
2720 if (SkipToTok != tok::unknown)
|
|
2721 P.SkipUntil(SkipToTok, Parser::StopAtSemi);
|
|
2722 return true;
|
|
2723 }
|
|
2724
|
|
2725 if (getDepth() < P.getLangOpts().BracketDepth)
|
|
2726 return false;
|
|
2727
|
|
2728 return diagnoseOverflow();
|
|
2729 }
|
|
2730
|
|
2731 bool BalancedDelimiterTracker::diagnoseMissingClose() {
|
|
2732 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
|
|
2733
|
|
2734 if (P.Tok.is(tok::annot_module_end))
|
|
2735 P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
|
|
2736 else
|
|
2737 P.Diag(P.Tok, diag::err_expected) << Close;
|
|
2738 P.Diag(LOpen, diag::note_matching) << Kind;
|
|
2739
|
|
2740 // If we're not already at some kind of closing bracket, skip to our closing
|
|
2741 // token.
|
|
2742 if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
|
|
2743 P.Tok.isNot(tok::r_square) &&
|
|
2744 P.SkipUntil(Close, FinalToken,
|
|
2745 Parser::StopAtSemi | Parser::StopBeforeMatch) &&
|
|
2746 P.Tok.is(Close))
|
|
2747 LClose = P.ConsumeAnyToken();
|
|
2748 return true;
|
|
2749 }
|
|
2750
|
|
2751 void BalancedDelimiterTracker::skipToEnd() {
|
|
2752 P.SkipUntil(Close, Parser::StopBeforeMatch);
|
|
2753 consumeClose();
|
|
2754 }
|