clang  3.9.0
Parse/Parser.cpp
Go to the documentation of this file.
1 //===--- Parser.cpp - C Language Family Parser ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Parser interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Parse/Parser.h"
15 #include "RAIIObjectsForParser.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclTemplate.h"
20 #include "clang/Sema/DeclSpec.h"
22 #include "clang/Sema/Scope.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
25 
26 
27 namespace {
28 /// \brief A comment handler that passes comments found by the preprocessor
29 /// to the parser action.
30 class ActionCommentHandler : public CommentHandler {
31  Sema &S;
32 
33 public:
34  explicit ActionCommentHandler(Sema &S) : S(S) { }
35 
36  bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
37  S.ActOnComment(Comment);
38  return false;
39  }
40 };
41 
42 /// \brief RAIIObject to destroy the contents of a SmallVector of
43 /// TemplateIdAnnotation pointers and clear the vector.
44 class DestroyTemplateIdAnnotationsRAIIObj {
46 
47 public:
48  DestroyTemplateIdAnnotationsRAIIObj(
50  : Container(Container) {}
51 
52  ~DestroyTemplateIdAnnotationsRAIIObj() {
54  Container.begin(),
55  E = Container.end();
56  I != E; ++I)
57  (*I)->Destroy();
58  Container.clear();
59  }
60 };
61 } // end anonymous namespace
62 
63 IdentifierInfo *Parser::getSEHExceptKeyword() {
64  // __except is accepted as a (contextual) keyword
65  if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
66  Ident__except = PP.getIdentifierInfo("__except");
67 
68  return Ident__except;
69 }
70 
71 Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
72  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
73  GreaterThanIsOperator(true), ColonIsSacred(false),
74  InMessageExpression(false), TemplateParameterDepth(0),
75  ParsingInObjCContainer(false) {
76  SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
77  Tok.startToken();
78  Tok.setKind(tok::eof);
79  Actions.CurScope = nullptr;
80  NumCachedScopes = 0;
81  ParenCount = BracketCount = BraceCount = 0;
82  CurParsedObjCImpl = nullptr;
83 
84  // Add #pragma handlers. These are removed and destroyed in the
85  // destructor.
86  initializePragmaHandlers();
87 
88  CommentSemaHandler.reset(new ActionCommentHandler(actions));
89  PP.addCommentHandler(CommentSemaHandler.get());
90 
91  PP.setCodeCompletionHandler(*this);
92 }
93 
95  return Diags.Report(Loc, DiagID);
96 }
97 
98 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
99  return Diag(Tok.getLocation(), DiagID);
100 }
101 
102 /// \brief Emits a diagnostic suggesting parentheses surrounding a
103 /// given range.
104 ///
105 /// \param Loc The location where we'll emit the diagnostic.
106 /// \param DK The kind of diagnostic to emit.
107 /// \param ParenRange Source range enclosing code that should be parenthesized.
108 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
109  SourceRange ParenRange) {
110  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
111  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
112  // We can't display the parentheses, so just dig the
113  // warning/error and return.
114  Diag(Loc, DK);
115  return;
116  }
117 
118  Diag(Loc, DK)
119  << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
120  << FixItHint::CreateInsertion(EndLoc, ")");
121 }
122 
123 static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
124  switch (ExpectedTok) {
125  case tok::semi:
126  return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
127  default: return false;
128  }
129 }
130 
131 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
132  StringRef Msg) {
133  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
134  ConsumeAnyToken();
135  return false;
136  }
137 
138  // Detect common single-character typos and resume.
139  if (IsCommonTypo(ExpectedTok, Tok)) {
140  SourceLocation Loc = Tok.getLocation();
141  {
142  DiagnosticBuilder DB = Diag(Loc, DiagID);
144  SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
145  if (DiagID == diag::err_expected)
146  DB << ExpectedTok;
147  else if (DiagID == diag::err_expected_after)
148  DB << Msg << ExpectedTok;
149  else
150  DB << Msg;
151  }
152 
153  // Pretend there wasn't a problem.
154  ConsumeAnyToken();
155  return false;
156  }
157 
158  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
159  const char *Spelling = nullptr;
160  if (EndLoc.isValid())
161  Spelling = tok::getPunctuatorSpelling(ExpectedTok);
162 
163  DiagnosticBuilder DB =
164  Spelling
165  ? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling)
166  : Diag(Tok, DiagID);
167  if (DiagID == diag::err_expected)
168  DB << ExpectedTok;
169  else if (DiagID == diag::err_expected_after)
170  DB << Msg << ExpectedTok;
171  else
172  DB << Msg;
173 
174  return true;
175 }
176 
177 bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
178  if (TryConsumeToken(tok::semi))
179  return false;
180 
181  if (Tok.is(tok::code_completion)) {
182  handleUnexpectedCodeCompletionToken();
183  return false;
184  }
185 
186  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
187  NextToken().is(tok::semi)) {
188  Diag(Tok, diag::err_extraneous_token_before_semi)
189  << PP.getSpelling(Tok)
191  ConsumeAnyToken(); // The ')' or ']'.
192  ConsumeToken(); // The ';'.
193  return false;
194  }
195 
196  return ExpectAndConsume(tok::semi, DiagID);
197 }
198 
199 void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, unsigned TST) {
200  if (!Tok.is(tok::semi)) return;
201 
202  bool HadMultipleSemis = false;
203  SourceLocation StartLoc = Tok.getLocation();
204  SourceLocation EndLoc = Tok.getLocation();
205  ConsumeToken();
206 
207  while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
208  HadMultipleSemis = true;
209  EndLoc = Tok.getLocation();
210  ConsumeToken();
211  }
212 
213  // C++11 allows extra semicolons at namespace scope, but not in any of the
214  // other contexts.
215  if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
216  if (getLangOpts().CPlusPlus11)
217  Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
218  << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
219  else
220  Diag(StartLoc, diag::ext_extra_semi_cxx11)
221  << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
222  return;
223  }
224 
225  if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
226  Diag(StartLoc, diag::ext_extra_semi)
228  Actions.getASTContext().getPrintingPolicy())
229  << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
230  else
231  // A single semicolon is valid after a member function definition.
232  Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
233  << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
234 }
235 
236 //===----------------------------------------------------------------------===//
237 // Error recovery.
238 //===----------------------------------------------------------------------===//
239 
241  return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
242 }
243 
244 /// SkipUntil - Read tokens until we get to the specified token, then consume
245 /// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the
246 /// token will ever occur, this skips to the next token, or to some likely
247 /// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
248 /// character.
249 ///
250 /// If SkipUntil finds the specified token, it returns true, otherwise it
251 /// returns false.
253  // We always want this function to skip at least one token if the first token
254  // isn't T and if not at EOF.
255  bool isFirstTokenSkipped = true;
256  while (1) {
257  // If we found one of the tokens, stop and return true.
258  for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
259  if (Tok.is(Toks[i])) {
260  if (HasFlagsSet(Flags, StopBeforeMatch)) {
261  // Noop, don't consume the token.
262  } else {
263  ConsumeAnyToken();
264  }
265  return true;
266  }
267  }
268 
269  // Important special case: The caller has given up and just wants us to
270  // skip the rest of the file. Do this without recursing, since we can
271  // get here precisely because the caller detected too much recursion.
272  if (Toks.size() == 1 && Toks[0] == tok::eof &&
273  !HasFlagsSet(Flags, StopAtSemi) &&
275  while (Tok.isNot(tok::eof))
276  ConsumeAnyToken();
277  return true;
278  }
279 
280  switch (Tok.getKind()) {
281  case tok::eof:
282  // Ran out of tokens.
283  return false;
284 
285  case tok::annot_pragma_openmp:
286  case tok::annot_pragma_openmp_end:
287  // Stop before an OpenMP pragma boundary.
288  case tok::annot_module_begin:
289  case tok::annot_module_end:
290  case tok::annot_module_include:
291  // Stop before we change submodules. They generally indicate a "good"
292  // place to pick up parsing again (except in the special case where
293  // we're trying to skip to EOF).
294  return false;
295 
296  case tok::code_completion:
297  if (!HasFlagsSet(Flags, StopAtCodeCompletion))
298  handleUnexpectedCodeCompletionToken();
299  return false;
300 
301  case tok::l_paren:
302  // Recursively skip properly-nested parens.
303  ConsumeParen();
304  if (HasFlagsSet(Flags, StopAtCodeCompletion))
305  SkipUntil(tok::r_paren, StopAtCodeCompletion);
306  else
307  SkipUntil(tok::r_paren);
308  break;
309  case tok::l_square:
310  // Recursively skip properly-nested square brackets.
311  ConsumeBracket();
312  if (HasFlagsSet(Flags, StopAtCodeCompletion))
313  SkipUntil(tok::r_square, StopAtCodeCompletion);
314  else
315  SkipUntil(tok::r_square);
316  break;
317  case tok::l_brace:
318  // Recursively skip properly-nested braces.
319  ConsumeBrace();
320  if (HasFlagsSet(Flags, StopAtCodeCompletion))
321  SkipUntil(tok::r_brace, StopAtCodeCompletion);
322  else
323  SkipUntil(tok::r_brace);
324  break;
325 
326  // Okay, we found a ']' or '}' or ')', which we think should be balanced.
327  // Since the user wasn't looking for this token (if they were, it would
328  // already be handled), this isn't balanced. If there is a LHS token at a
329  // higher level, we will assume that this matches the unbalanced token
330  // and return it. Otherwise, this is a spurious RHS token, which we skip.
331  case tok::r_paren:
332  if (ParenCount && !isFirstTokenSkipped)
333  return false; // Matches something.
334  ConsumeParen();
335  break;
336  case tok::r_square:
337  if (BracketCount && !isFirstTokenSkipped)
338  return false; // Matches something.
339  ConsumeBracket();
340  break;
341  case tok::r_brace:
342  if (BraceCount && !isFirstTokenSkipped)
343  return false; // Matches something.
344  ConsumeBrace();
345  break;
346 
347  case tok::string_literal:
348  case tok::wide_string_literal:
349  case tok::utf8_string_literal:
350  case tok::utf16_string_literal:
351  case tok::utf32_string_literal:
352  ConsumeStringToken();
353  break;
354 
355  case tok::semi:
356  if (HasFlagsSet(Flags, StopAtSemi))
357  return false;
358  // FALL THROUGH.
359  default:
360  // Skip this token.
361  ConsumeToken();
362  break;
363  }
364  isFirstTokenSkipped = false;
365  }
366 }
367 
368 //===----------------------------------------------------------------------===//
369 // Scope manipulation
370 //===----------------------------------------------------------------------===//
371 
372 /// EnterScope - Start a new scope.
373 void Parser::EnterScope(unsigned ScopeFlags) {
374  if (NumCachedScopes) {
375  Scope *N = ScopeCache[--NumCachedScopes];
376  N->Init(getCurScope(), ScopeFlags);
377  Actions.CurScope = N;
378  } else {
379  Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
380  }
381 }
382 
383 /// ExitScope - Pop a scope off the scope stack.
385  assert(getCurScope() && "Scope imbalance!");
386 
387  // Inform the actions module that this scope is going away if there are any
388  // decls in it.
389  Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
390 
391  Scope *OldScope = getCurScope();
392  Actions.CurScope = OldScope->getParent();
393 
394  if (NumCachedScopes == ScopeCacheSize)
395  delete OldScope;
396  else
397  ScopeCache[NumCachedScopes++] = OldScope;
398 }
399 
400 /// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
401 /// this object does nothing.
402 Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
403  bool ManageFlags)
404  : CurScope(ManageFlags ? Self->getCurScope() : nullptr) {
405  if (CurScope) {
406  OldFlags = CurScope->getFlags();
407  CurScope->setFlags(ScopeFlags);
408  }
409 }
410 
411 /// Restore the flags for the current scope to what they were before this
412 /// object overrode them.
413 Parser::ParseScopeFlags::~ParseScopeFlags() {
414  if (CurScope)
415  CurScope->setFlags(OldFlags);
416 }
417 
418 
419 //===----------------------------------------------------------------------===//
420 // C99 6.9: External Definitions.
421 //===----------------------------------------------------------------------===//
422 
424  // If we still have scopes active, delete the scope tree.
425  delete getCurScope();
426  Actions.CurScope = nullptr;
427 
428  // Free the scope cache.
429  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
430  delete ScopeCache[i];
431 
432  resetPragmaHandlers();
433 
434  PP.removeCommentHandler(CommentSemaHandler.get());
435 
437 
438  if (getLangOpts().DelayedTemplateParsing &&
439  !PP.isIncrementalProcessingEnabled() && !TemplateIds.empty()) {
440  // If an ASTConsumer parsed delay-parsed templates in their
441  // HandleTranslationUnit() method, TemplateIds created there were not
442  // guarded by a DestroyTemplateIdAnnotationsRAIIObj object in
443  // ParseTopLevelDecl(). Destroy them here.
444  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
445  }
446 
447  assert(TemplateIds.empty() && "Still alive TemplateIdAnnotations around?");
448 }
449 
450 /// Initialize - Warm up the parser.
451 ///
453  // Create the translation unit scope. Install it as the current scope.
454  assert(getCurScope() == nullptr && "A scope is already active?");
457 
458  // Initialization for Objective-C context sensitive keywords recognition.
459  // Referenced in Parser::ParseObjCTypeQualifierList.
460  if (getLangOpts().ObjC1) {
461  ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
462  ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
463  ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
464  ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
465  ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
466  ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
467  ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull");
468  ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable");
469  ObjCTypeQuals[objc_null_unspecified]
470  = &PP.getIdentifierTable().get("null_unspecified");
471  }
472 
473  Ident_instancetype = nullptr;
474  Ident_final = nullptr;
475  Ident_sealed = nullptr;
476  Ident_override = nullptr;
477 
478  Ident_super = &PP.getIdentifierTable().get("super");
479 
480  Ident_vector = nullptr;
481  Ident_bool = nullptr;
482  Ident_pixel = nullptr;
483  if (getLangOpts().AltiVec || getLangOpts().ZVector) {
484  Ident_vector = &PP.getIdentifierTable().get("vector");
485  Ident_bool = &PP.getIdentifierTable().get("bool");
486  }
487  if (getLangOpts().AltiVec)
488  Ident_pixel = &PP.getIdentifierTable().get("pixel");
489 
490  Ident_introduced = nullptr;
491  Ident_deprecated = nullptr;
492  Ident_obsoleted = nullptr;
493  Ident_unavailable = nullptr;
494  Ident_strict = nullptr;
495  Ident_replacement = nullptr;
496 
497  Ident__except = nullptr;
498 
499  Ident__exception_code = Ident__exception_info = nullptr;
500  Ident__abnormal_termination = Ident___exception_code = nullptr;
501  Ident___exception_info = Ident___abnormal_termination = nullptr;
502  Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr;
503  Ident_AbnormalTermination = nullptr;
504 
505  if(getLangOpts().Borland) {
506  Ident__exception_info = PP.getIdentifierInfo("_exception_info");
507  Ident___exception_info = PP.getIdentifierInfo("__exception_info");
508  Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
509  Ident__exception_code = PP.getIdentifierInfo("_exception_code");
510  Ident___exception_code = PP.getIdentifierInfo("__exception_code");
511  Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
512  Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
513  Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
514  Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
515 
516  PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
517  PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
518  PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
519  PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
520  PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
521  PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
522  PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
523  PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
524  PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
525  }
526 
527  Actions.Initialize();
528 
529  // Prime the lexer look-ahead.
530  ConsumeToken();
531 }
532 
533 void Parser::LateTemplateParserCleanupCallback(void *P) {
534  // While this RAII helper doesn't bracket any actual work, the destructor will
535  // clean up annotations that were created during ActOnEndOfTranslationUnit
536  // when incremental processing is enabled.
537  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(((Parser *)P)->TemplateIds);
538 }
539 
540 /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
541 /// action tells us to. This returns true if the EOF was encountered.
543  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
544 
545  // Skip over the EOF token, flagging end of previous input for incremental
546  // processing
547  if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
548  ConsumeToken();
549 
550  Result = nullptr;
551  switch (Tok.getKind()) {
552  case tok::annot_pragma_unused:
553  HandlePragmaUnused();
554  return false;
555 
556  case tok::annot_module_include:
557  Actions.ActOnModuleInclude(Tok.getLocation(),
558  reinterpret_cast<Module *>(
559  Tok.getAnnotationValue()));
560  ConsumeToken();
561  return false;
562 
563  case tok::annot_module_begin:
564  Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>(
565  Tok.getAnnotationValue()));
566  ConsumeToken();
567  return false;
568 
569  case tok::annot_module_end:
570  Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>(
571  Tok.getAnnotationValue()));
572  ConsumeToken();
573  return false;
574 
575  case tok::eof:
576  // Late template parsing can begin.
577  if (getLangOpts().DelayedTemplateParsing)
578  Actions.SetLateTemplateParser(LateTemplateParserCallback,
580  LateTemplateParserCleanupCallback : nullptr,
581  this);
583  Actions.ActOnEndOfTranslationUnit();
584  //else don't tell Sema that we ended parsing: more input might come.
585  return true;
586 
587  default:
588  break;
589  }
590 
591  ParsedAttributesWithRange attrs(AttrFactory);
592  MaybeParseCXX11Attributes(attrs);
593  MaybeParseMicrosoftAttributes(attrs);
594 
595  Result = ParseExternalDeclaration(attrs);
596  return false;
597 }
598 
599 /// ParseExternalDeclaration:
600 ///
601 /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
602 /// function-definition
603 /// declaration
604 /// [GNU] asm-definition
605 /// [GNU] __extension__ external-declaration
606 /// [OBJC] objc-class-definition
607 /// [OBJC] objc-class-declaration
608 /// [OBJC] objc-alias-declaration
609 /// [OBJC] objc-protocol-definition
610 /// [OBJC] objc-method-definition
611 /// [OBJC] @end
612 /// [C++] linkage-specification
613 /// [GNU] asm-definition:
614 /// simple-asm-expr ';'
615 /// [C++11] empty-declaration
616 /// [C++11] attribute-declaration
617 ///
618 /// [C++11] empty-declaration:
619 /// ';'
620 ///
621 /// [C++0x/GNU] 'extern' 'template' declaration
623 Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
624  ParsingDeclSpec *DS) {
625  DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(TemplateIds);
626  ParenBraceBracketBalancer BalancerRAIIObj(*this);
627 
628  if (PP.isCodeCompletionReached()) {
629  cutOffParsing();
630  return nullptr;
631  }
632 
633  Decl *SingleDecl = nullptr;
634  switch (Tok.getKind()) {
635  case tok::annot_pragma_vis:
636  HandlePragmaVisibility();
637  return nullptr;
638  case tok::annot_pragma_pack:
639  HandlePragmaPack();
640  return nullptr;
641  case tok::annot_pragma_msstruct:
642  HandlePragmaMSStruct();
643  return nullptr;
644  case tok::annot_pragma_align:
645  HandlePragmaAlign();
646  return nullptr;
647  case tok::annot_pragma_weak:
648  HandlePragmaWeak();
649  return nullptr;
650  case tok::annot_pragma_weakalias:
651  HandlePragmaWeakAlias();
652  return nullptr;
653  case tok::annot_pragma_redefine_extname:
654  HandlePragmaRedefineExtname();
655  return nullptr;
656  case tok::annot_pragma_fp_contract:
657  HandlePragmaFPContract();
658  return nullptr;
659  case tok::annot_pragma_opencl_extension:
660  HandlePragmaOpenCLExtension();
661  return nullptr;
662  case tok::annot_pragma_openmp: {
664  return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, attrs);
665  }
666  case tok::annot_pragma_ms_pointers_to_members:
667  HandlePragmaMSPointersToMembers();
668  return nullptr;
669  case tok::annot_pragma_ms_vtordisp:
670  HandlePragmaMSVtorDisp();
671  return nullptr;
672  case tok::annot_pragma_ms_pragma:
673  HandlePragmaMSPragma();
674  return nullptr;
675  case tok::annot_pragma_dump:
676  HandlePragmaDump();
677  return nullptr;
678  case tok::semi:
679  // Either a C++11 empty-declaration or attribute-declaration.
680  SingleDecl = Actions.ActOnEmptyDeclaration(getCurScope(),
681  attrs.getList(),
682  Tok.getLocation());
683  ConsumeExtraSemi(OutsideFunction);
684  break;
685  case tok::r_brace:
686  Diag(Tok, diag::err_extraneous_closing_brace);
687  ConsumeBrace();
688  return nullptr;
689  case tok::eof:
690  Diag(Tok, diag::err_expected_external_declaration);
691  return nullptr;
692  case tok::kw___extension__: {
693  // __extension__ silences extension warnings in the subexpression.
694  ExtensionRAIIObject O(Diags); // Use RAII to do this.
695  ConsumeToken();
696  return ParseExternalDeclaration(attrs);
697  }
698  case tok::kw_asm: {
699  ProhibitAttributes(attrs);
700 
701  SourceLocation StartLoc = Tok.getLocation();
702  SourceLocation EndLoc;
703 
704  ExprResult Result(ParseSimpleAsm(&EndLoc));
705 
706  // Check if GNU-style InlineAsm is disabled.
707  // Empty asm string is allowed because it will not introduce
708  // any assembly code.
709  if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
710  const auto *SL = cast<StringLiteral>(Result.get());
711  if (!SL->getString().trim().empty())
712  Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
713  }
714 
715  ExpectAndConsume(tok::semi, diag::err_expected_after,
716  "top-level asm block");
717 
718  if (Result.isInvalid())
719  return nullptr;
720  SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
721  break;
722  }
723  case tok::at:
724  return ParseObjCAtDirectives();
725  case tok::minus:
726  case tok::plus:
727  if (!getLangOpts().ObjC1) {
728  Diag(Tok, diag::err_expected_external_declaration);
729  ConsumeToken();
730  return nullptr;
731  }
732  SingleDecl = ParseObjCMethodDefinition();
733  break;
734  case tok::code_completion:
736  CurParsedObjCImpl? Sema::PCC_ObjCImplementation
738  cutOffParsing();
739  return nullptr;
740  case tok::kw_using:
741  case tok::kw_namespace:
742  case tok::kw_typedef:
743  case tok::kw_template:
744  case tok::kw_export: // As in 'export template'
745  case tok::kw_static_assert:
746  case tok::kw__Static_assert:
747  // A function definition cannot start with any of these keywords.
748  {
749  SourceLocation DeclEnd;
750  return ParseDeclaration(Declarator::FileContext, DeclEnd, attrs);
751  }
752 
753  case tok::kw_static:
754  // Parse (then ignore) 'static' prior to a template instantiation. This is
755  // a GCC extension that we intentionally do not support.
756  if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
757  Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
758  << 0;
759  SourceLocation DeclEnd;
760  return ParseDeclaration(Declarator::FileContext, DeclEnd, attrs);
761  }
762  goto dont_know;
763 
764  case tok::kw_inline:
765  if (getLangOpts().CPlusPlus) {
766  tok::TokenKind NextKind = NextToken().getKind();
767 
768  // Inline namespaces. Allowed as an extension even in C++03.
769  if (NextKind == tok::kw_namespace) {
770  SourceLocation DeclEnd;
771  return ParseDeclaration(Declarator::FileContext, DeclEnd, attrs);
772  }
773 
774  // Parse (then ignore) 'inline' prior to a template instantiation. This is
775  // a GCC extension that we intentionally do not support.
776  if (NextKind == tok::kw_template) {
777  Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
778  << 1;
779  SourceLocation DeclEnd;
780  return ParseDeclaration(Declarator::FileContext, DeclEnd, attrs);
781  }
782  }
783  goto dont_know;
784 
785  case tok::kw_extern:
786  if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
787  // Extern templates
788  SourceLocation ExternLoc = ConsumeToken();
789  SourceLocation TemplateLoc = ConsumeToken();
790  Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
791  diag::warn_cxx98_compat_extern_template :
792  diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
793  SourceLocation DeclEnd;
794  return Actions.ConvertDeclToDeclGroup(
795  ParseExplicitInstantiation(Declarator::FileContext,
796  ExternLoc, TemplateLoc, DeclEnd));
797  }
798  goto dont_know;
799 
800  case tok::kw___if_exists:
801  case tok::kw___if_not_exists:
802  ParseMicrosoftIfExistsExternalDeclaration();
803  return nullptr;
804 
805  default:
806  dont_know:
807  // We can't tell whether this is a function-definition or declaration yet.
808  return ParseDeclarationOrFunctionDefinition(attrs, DS);
809  }
810 
811  // This routine returns a DeclGroup, if the thing we parsed only contains a
812  // single decl, convert it now.
813  return Actions.ConvertDeclToDeclGroup(SingleDecl);
814 }
815 
816 /// \brief Determine whether the current token, if it occurs after a
817 /// declarator, continues a declaration or declaration list.
818 bool Parser::isDeclarationAfterDeclarator() {
819  // Check for '= delete' or '= default'
820  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
821  const Token &KW = NextToken();
822  if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
823  return false;
824  }
825 
826  return Tok.is(tok::equal) || // int X()= -> not a function def
827  Tok.is(tok::comma) || // int X(), -> not a function def
828  Tok.is(tok::semi) || // int X(); -> not a function def
829  Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
830  Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
831  (getLangOpts().CPlusPlus &&
832  Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
833 }
834 
835 /// \brief Determine whether the current token, if it occurs after a
836 /// declarator, indicates the start of a function definition.
837 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
838  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
839  if (Tok.is(tok::l_brace)) // int X() {}
840  return true;
841 
842  // Handle K&R C argument lists: int X(f) int f; {}
843  if (!getLangOpts().CPlusPlus &&
844  Declarator.getFunctionTypeInfo().isKNRPrototype())
845  return isDeclarationSpecifier();
846 
847  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
848  const Token &KW = NextToken();
849  return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
850  }
851 
852  return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
853  Tok.is(tok::kw_try); // X() try { ... }
854 }
855 
856 /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
857 /// a declaration. We can't tell which we have until we read up to the
858 /// compound-statement in function-definition. TemplateParams, if
859 /// non-NULL, provides the template parameters when we're parsing a
860 /// C++ template-declaration.
861 ///
862 /// function-definition: [C99 6.9.1]
863 /// decl-specs declarator declaration-list[opt] compound-statement
864 /// [C90] function-definition: [C99 6.7.1] - implicit int result
865 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
866 ///
867 /// declaration: [C99 6.7]
868 /// declaration-specifiers init-declarator-list[opt] ';'
869 /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
870 /// [OMP] threadprivate-directive [TODO]
871 ///
873 Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
874  ParsingDeclSpec &DS,
875  AccessSpecifier AS) {
876  // Parse the common declaration-specifiers piece.
877  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
878 
879  // If we had a free-standing type definition with a missing semicolon, we
880  // may get this far before the problem becomes obvious.
881  if (DS.hasTagDefinition() &&
882  DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_top_level))
883  return nullptr;
884 
885  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
886  // declaration-specifiers init-declarator-list[opt] ';'
887  if (Tok.is(tok::semi)) {
888  ProhibitAttributes(attrs);
889  ConsumeToken();
890  RecordDecl *AnonRecord = nullptr;
891  Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
892  DS, AnonRecord);
893  DS.complete(TheDecl);
894  if (AnonRecord) {
895  Decl* decls[] = {AnonRecord, TheDecl};
896  return Actions.BuildDeclaratorGroup(decls, /*TypeMayContainAuto=*/false);
897  }
898  return Actions.ConvertDeclToDeclGroup(TheDecl);
899  }
900 
901  DS.takeAttributesFrom(attrs);
902 
903  // ObjC2 allows prefix attributes on class interfaces and protocols.
904  // FIXME: This still needs better diagnostics. We should only accept
905  // attributes here, no types, etc.
906  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
907  SourceLocation AtLoc = ConsumeToken(); // the "@"
908  if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
909  !Tok.isObjCAtKeyword(tok::objc_protocol)) {
910  Diag(Tok, diag::err_objc_unexpected_attr);
911  SkipUntil(tok::semi); // FIXME: better skip?
912  return nullptr;
913  }
914 
915  DS.abort();
916 
917  const char *PrevSpec = nullptr;
918  unsigned DiagID;
919  if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
920  Actions.getASTContext().getPrintingPolicy()))
921  Diag(AtLoc, DiagID) << PrevSpec;
922 
923  if (Tok.isObjCAtKeyword(tok::objc_protocol))
924  return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
925 
926  return Actions.ConvertDeclToDeclGroup(
927  ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
928  }
929 
930  // If the declspec consisted only of 'extern' and we have a string
931  // literal following it, this must be a C++ linkage specifier like
932  // 'extern "C"'.
933  if (getLangOpts().CPlusPlus && isTokenStringLiteral() &&
936  Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
937  return Actions.ConvertDeclToDeclGroup(TheDecl);
938  }
939 
940  return ParseDeclGroup(DS, Declarator::FileContext);
941 }
942 
944 Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
945  ParsingDeclSpec *DS,
946  AccessSpecifier AS) {
947  if (DS) {
948  return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
949  } else {
950  ParsingDeclSpec PDS(*this);
951  // Must temporarily exit the objective-c container scope for
952  // parsing c constructs and re-enter objc container scope
953  // afterwards.
954  ObjCDeclContextSwitch ObjCDC(*this);
955 
956  return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
957  }
958 }
959 
960 /// ParseFunctionDefinition - We parsed and verified that the specified
961 /// Declarator is well formed. If this is a K&R-style function, read the
962 /// parameters declaration-list, then start the compound-statement.
963 ///
964 /// function-definition: [C99 6.9.1]
965 /// decl-specs declarator declaration-list[opt] compound-statement
966 /// [C90] function-definition: [C99 6.7.1] - implicit int result
967 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
968 /// [C++] function-definition: [C++ 8.4]
969 /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
970 /// function-body
971 /// [C++] function-definition: [C++ 8.4]
972 /// decl-specifier-seq[opt] declarator function-try-block
973 ///
974 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
975  const ParsedTemplateInfo &TemplateInfo,
976  LateParsedAttrList *LateParsedAttrs) {
977  // Poison SEH identifiers so they are flagged as illegal in function bodies.
978  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
980 
981  // If this is C90 and the declspecs were completely missing, fudge in an
982  // implicit int. We do this here because this is the only place where
983  // declaration-specifiers are completely optional in the grammar.
984  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
985  const char *PrevSpec;
986  unsigned DiagID;
987  const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
989  D.getIdentifierLoc(),
990  PrevSpec, DiagID,
991  Policy);
993  }
994 
995  // If this declaration was formed with a K&R-style identifier list for the
996  // arguments, parse declarations for all of the args next.
997  // int foo(a,b) int a; float b; {}
998  if (FTI.isKNRPrototype())
999  ParseKNRParamDeclarations(D);
1000 
1001  // We should have either an opening brace or, in a C++ constructor,
1002  // we may have a colon.
1003  if (Tok.isNot(tok::l_brace) &&
1004  (!getLangOpts().CPlusPlus ||
1005  (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
1006  Tok.isNot(tok::equal)))) {
1007  Diag(Tok, diag::err_expected_fn_body);
1008 
1009  // Skip over garbage, until we get to '{'. Don't eat the '{'.
1010  SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
1011 
1012  // If we didn't find the '{', bail out.
1013  if (Tok.isNot(tok::l_brace))
1014  return nullptr;
1015  }
1016 
1017  // Check to make sure that any normal attributes are allowed to be on
1018  // a definition. Late parsed attributes are checked at the end.
1019  if (Tok.isNot(tok::equal)) {
1020  AttributeList *DtorAttrs = D.getAttributes();
1021  while (DtorAttrs) {
1022  if (DtorAttrs->isKnownToGCC() &&
1023  !DtorAttrs->isCXX11Attribute()) {
1024  Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
1025  << DtorAttrs->getName();
1026  }
1027  DtorAttrs = DtorAttrs->getNext();
1028  }
1029  }
1030 
1031  // In delayed template parsing mode, for function template we consume the
1032  // tokens and store them for late parsing at the end of the translation unit.
1033  if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
1034  TemplateInfo.Kind == ParsedTemplateInfo::Template &&
1035  Actions.canDelayFunctionBody(D)) {
1036  MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
1037 
1038  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1039  Scope *ParentScope = getCurScope()->getParent();
1040 
1042  Decl *DP = Actions.HandleDeclarator(ParentScope, D,
1044  D.complete(DP);
1045  D.getMutableDeclSpec().abort();
1046 
1047  if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
1048  trySkippingFunctionBody()) {
1049  BodyScope.Exit();
1050  return Actions.ActOnSkippedFunctionBody(DP);
1051  }
1052 
1053  CachedTokens Toks;
1054  LexTemplateFunctionForLateParsing(Toks);
1055 
1056  if (DP) {
1057  FunctionDecl *FnD = DP->getAsFunction();
1058  Actions.CheckForFunctionRedefinition(FnD);
1059  Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
1060  }
1061  return DP;
1062  }
1063  else if (CurParsedObjCImpl &&
1064  !TemplateInfo.TemplateParams &&
1065  (Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
1066  Tok.is(tok::colon)) &&
1067  Actions.CurContext->isTranslationUnit()) {
1068  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1069  Scope *ParentScope = getCurScope()->getParent();
1070 
1072  Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
1074  D.complete(FuncDecl);
1075  D.getMutableDeclSpec().abort();
1076  if (FuncDecl) {
1077  // Consume the tokens and store them for later parsing.
1078  StashAwayMethodOrFunctionBodyTokens(FuncDecl);
1079  CurParsedObjCImpl->HasCFunction = true;
1080  return FuncDecl;
1081  }
1082  // FIXME: Should we really fall through here?
1083  }
1084 
1085  // Enter a scope for the function body.
1086  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1087 
1088  // Tell the actions module that we have entered a function definition with the
1089  // specified Declarator for the function.
1090  Sema::SkipBodyInfo SkipBody;
1091  Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
1092  TemplateInfo.TemplateParams
1093  ? *TemplateInfo.TemplateParams
1095  &SkipBody);
1096 
1097  if (SkipBody.ShouldSkip) {
1098  SkipFunctionBody();
1099  return Res;
1100  }
1101 
1102  // Break out of the ParsingDeclarator context before we parse the body.
1103  D.complete(Res);
1104 
1105  // Break out of the ParsingDeclSpec context, too. This const_cast is
1106  // safe because we're always the sole owner.
1107  D.getMutableDeclSpec().abort();
1108 
1109  if (TryConsumeToken(tok::equal)) {
1110  assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
1111 
1112  bool Delete = false;
1113  SourceLocation KWLoc;
1114  if (TryConsumeToken(tok::kw_delete, KWLoc)) {
1115  Diag(KWLoc, getLangOpts().CPlusPlus11
1116  ? diag::warn_cxx98_compat_defaulted_deleted_function
1117  : diag::ext_defaulted_deleted_function)
1118  << 1 /* deleted */;
1119  Actions.SetDeclDeleted(Res, KWLoc);
1120  Delete = true;
1121  } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
1122  Diag(KWLoc, getLangOpts().CPlusPlus11
1123  ? diag::warn_cxx98_compat_defaulted_deleted_function
1124  : diag::ext_defaulted_deleted_function)
1125  << 0 /* defaulted */;
1126  Actions.SetDeclDefaulted(Res, KWLoc);
1127  } else {
1128  llvm_unreachable("function definition after = not 'delete' or 'default'");
1129  }
1130 
1131  if (Tok.is(tok::comma)) {
1132  Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
1133  << Delete;
1134  SkipUntil(tok::semi);
1135  } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
1136  Delete ? "delete" : "default")) {
1137  SkipUntil(tok::semi);
1138  }
1139 
1140  Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
1141  Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
1142  return Res;
1143  }
1144 
1145  if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
1146  trySkippingFunctionBody()) {
1147  BodyScope.Exit();
1148  Actions.ActOnSkippedFunctionBody(Res);
1149  return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
1150  }
1151 
1152  if (Tok.is(tok::kw_try))
1153  return ParseFunctionTryBlock(Res, BodyScope);
1154 
1155  // If we have a colon, then we're probably parsing a C++
1156  // ctor-initializer.
1157  if (Tok.is(tok::colon)) {
1158  ParseConstructorInitializer(Res);
1159 
1160  // Recover from error.
1161  if (!Tok.is(tok::l_brace)) {
1162  BodyScope.Exit();
1163  Actions.ActOnFinishFunctionBody(Res, nullptr);
1164  return Res;
1165  }
1166  } else
1167  Actions.ActOnDefaultCtorInitializers(Res);
1168 
1169  // Late attributes are parsed in the same scope as the function body.
1170  if (LateParsedAttrs)
1171  ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1172 
1173  return ParseFunctionStatementBody(Res, BodyScope);
1174 }
1175 
1176 void Parser::SkipFunctionBody() {
1177  if (Tok.is(tok::equal)) {
1178  SkipUntil(tok::semi);
1179  return;
1180  }
1181 
1182  bool IsFunctionTryBlock = Tok.is(tok::kw_try);
1183  if (IsFunctionTryBlock)
1184  ConsumeToken();
1185 
1186  CachedTokens Skipped;
1187  if (ConsumeAndStoreFunctionPrologue(Skipped))
1189  else {
1190  SkipUntil(tok::r_brace);
1191  while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
1192  SkipUntil(tok::l_brace);
1193  SkipUntil(tok::r_brace);
1194  }
1195  }
1196 }
1197 
1198 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
1199 /// types for a function with a K&R-style identifier list for arguments.
1200 void Parser::ParseKNRParamDeclarations(Declarator &D) {
1201  // We know that the top-level of this declarator is a function.
1203 
1204  // Enter function-declaration scope, limiting any declarators to the
1205  // function prototype scope, including parameter declarators.
1206  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1208 
1209  // Read all the argument declarations.
1210  while (isDeclarationSpecifier()) {
1211  SourceLocation DSStart = Tok.getLocation();
1212 
1213  // Parse the common declaration-specifiers piece.
1214  DeclSpec DS(AttrFactory);
1215  ParseDeclarationSpecifiers(DS);
1216 
1217  // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1218  // least one declarator'.
1219  // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
1220  // the declarations though. It's trivial to ignore them, really hard to do
1221  // anything else with them.
1222  if (TryConsumeToken(tok::semi)) {
1223  Diag(DSStart, diag::err_declaration_does_not_declare_param);
1224  continue;
1225  }
1226 
1227  // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1228  // than register.
1232  diag::err_invalid_storage_class_in_func_decl);
1234  }
1237  diag::err_invalid_storage_class_in_func_decl);
1239  }
1240 
1241  // Parse the first declarator attached to this declspec.
1242  Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
1243  ParseDeclarator(ParmDeclarator);
1244 
1245  // Handle the full declarator list.
1246  while (1) {
1247  // If attributes are present, parse them.
1248  MaybeParseGNUAttributes(ParmDeclarator);
1249 
1250  // Ask the actions module to compute the type for this declarator.
1251  Decl *Param =
1252  Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
1253 
1254  if (Param &&
1255  // A missing identifier has already been diagnosed.
1256  ParmDeclarator.getIdentifier()) {
1257 
1258  // Scan the argument list looking for the correct param to apply this
1259  // type.
1260  for (unsigned i = 0; ; ++i) {
1261  // C99 6.9.1p6: those declarators shall declare only identifiers from
1262  // the identifier list.
1263  if (i == FTI.NumParams) {
1264  Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
1265  << ParmDeclarator.getIdentifier();
1266  break;
1267  }
1268 
1269  if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
1270  // Reject redefinitions of parameters.
1271  if (FTI.Params[i].Param) {
1272  Diag(ParmDeclarator.getIdentifierLoc(),
1273  diag::err_param_redefinition)
1274  << ParmDeclarator.getIdentifier();
1275  } else {
1276  FTI.Params[i].Param = Param;
1277  }
1278  break;
1279  }
1280  }
1281  }
1282 
1283  // If we don't have a comma, it is either the end of the list (a ';') or
1284  // an error, bail out.
1285  if (Tok.isNot(tok::comma))
1286  break;
1287 
1288  ParmDeclarator.clear();
1289 
1290  // Consume the comma.
1291  ParmDeclarator.setCommaLoc(ConsumeToken());
1292 
1293  // Parse the next declarator.
1294  ParseDeclarator(ParmDeclarator);
1295  }
1296 
1297  // Consume ';' and continue parsing.
1298  if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
1299  continue;
1300 
1301  // Otherwise recover by skipping to next semi or mandatory function body.
1302  if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
1303  break;
1304  TryConsumeToken(tok::semi);
1305  }
1306 
1307  // The actions module must verify that all arguments were declared.
1309 }
1310 
1311 
1312 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
1313 /// allowed to be a wide string, and is not subject to character translation.
1314 ///
1315 /// [GNU] asm-string-literal:
1316 /// string-literal
1317 ///
1318 ExprResult Parser::ParseAsmStringLiteral() {
1319  if (!isTokenStringLiteral()) {
1320  Diag(Tok, diag::err_expected_string_literal)
1321  << /*Source='in...'*/0 << "'asm'";
1322  return ExprError();
1323  }
1324 
1325  ExprResult AsmString(ParseStringLiteralExpression());
1326  if (!AsmString.isInvalid()) {
1327  const auto *SL = cast<StringLiteral>(AsmString.get());
1328  if (!SL->isAscii()) {
1329  Diag(Tok, diag::err_asm_operand_wide_string_literal)
1330  << SL->isWide()
1331  << SL->getSourceRange();
1332  return ExprError();
1333  }
1334  }
1335  return AsmString;
1336 }
1337 
1338 /// ParseSimpleAsm
1339 ///
1340 /// [GNU] simple-asm-expr:
1341 /// 'asm' '(' asm-string-literal ')'
1342 ///
1343 ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1344  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1345  SourceLocation Loc = ConsumeToken();
1346 
1347  if (Tok.is(tok::kw_volatile)) {
1348  // Remove from the end of 'asm' to the end of 'volatile'.
1349  SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1350  PP.getLocForEndOfToken(Tok.getLocation()));
1351 
1352  Diag(Tok, diag::warn_file_asm_volatile)
1353  << FixItHint::CreateRemoval(RemovalRange);
1354  ConsumeToken();
1355  }
1356 
1357  BalancedDelimiterTracker T(*this, tok::l_paren);
1358  if (T.consumeOpen()) {
1359  Diag(Tok, diag::err_expected_lparen_after) << "asm";
1360  return ExprError();
1361  }
1362 
1363  ExprResult Result(ParseAsmStringLiteral());
1364 
1365  if (!Result.isInvalid()) {
1366  // Close the paren and get the location of the end bracket
1367  T.consumeClose();
1368  if (EndLoc)
1369  *EndLoc = T.getCloseLocation();
1370  } else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
1371  if (EndLoc)
1372  *EndLoc = Tok.getLocation();
1373  ConsumeParen();
1374  }
1375 
1376  return Result;
1377 }
1378 
1379 /// \brief Get the TemplateIdAnnotation from the token and put it in the
1380 /// cleanup pool so that it gets destroyed when parsing the current top level
1381 /// declaration is finished.
1382 TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
1383  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
1385  Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
1386  return Id;
1387 }
1388 
1389 void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
1390  // Push the current token back into the token stream (or revert it if it is
1391  // cached) and use an annotation scope token for current token.
1392  if (PP.isBacktrackEnabled())
1393  PP.RevertCachedTokens(1);
1394  else
1395  PP.EnterToken(Tok);
1396  Tok.setKind(tok::annot_cxxscope);
1398  Tok.setAnnotationRange(SS.getRange());
1399 
1400  // In case the tokens were cached, have Preprocessor replace them
1401  // with the annotation token. We don't need to do this if we've
1402  // just reverted back to a prior state.
1403  if (IsNewAnnotation)
1404  PP.AnnotateCachedTokens(Tok);
1405 }
1406 
1407 /// \brief Attempt to classify the name at the current token position. This may
1408 /// form a type, scope or primary expression annotation, or replace the token
1409 /// with a typo-corrected keyword. This is only appropriate when the current
1410 /// name must refer to an entity which has already been declared.
1411 ///
1412 /// \param IsAddressOfOperand Must be \c true if the name is preceded by an '&'
1413 /// and might possibly have a dependent nested name specifier.
1414 /// \param CCC Indicates how to perform typo-correction for this name. If NULL,
1415 /// no typo correction will be performed.
1416 Parser::AnnotatedNameKind
1417 Parser::TryAnnotateName(bool IsAddressOfOperand,
1418  std::unique_ptr<CorrectionCandidateCallback> CCC) {
1419  assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
1420 
1421  const bool EnteringContext = false;
1422  const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1423 
1424  CXXScopeSpec SS;
1425  if (getLangOpts().CPlusPlus &&
1426  ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext))
1427  return ANK_Error;
1428 
1429  if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
1430  if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
1431  !WasScopeAnnotation))
1432  return ANK_Error;
1433  return ANK_Unresolved;
1434  }
1435 
1437  SourceLocation NameLoc = Tok.getLocation();
1438 
1439  // FIXME: Move the tentative declaration logic into ClassifyName so we can
1440  // typo-correct to tentatively-declared identifiers.
1441  if (isTentativelyDeclared(Name)) {
1442  // Identifier has been tentatively declared, and thus cannot be resolved as
1443  // an expression. Fall back to annotating it as a type.
1444  if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, false, SS,
1445  !WasScopeAnnotation))
1446  return ANK_Error;
1447  return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
1448  }
1449 
1450  Token Next = NextToken();
1451 
1452  // Look up and classify the identifier. We don't perform any typo-correction
1453  // after a scope specifier, because in general we can't recover from typos
1454  // there (eg, after correcting 'A::tempalte B<X>::C' [sic], we would need to
1455  // jump back into scope specifier parsing).
1456  Sema::NameClassification Classification = Actions.ClassifyName(
1457  getCurScope(), SS, Name, NameLoc, Next, IsAddressOfOperand,
1458  SS.isEmpty() ? std::move(CCC) : nullptr);
1459 
1460  switch (Classification.getKind()) {
1461  case Sema::NC_Error:
1462  return ANK_Error;
1463 
1464  case Sema::NC_Keyword:
1465  // The identifier was typo-corrected to a keyword.
1466  Tok.setIdentifierInfo(Name);
1467  Tok.setKind(Name->getTokenID());
1468  PP.TypoCorrectToken(Tok);
1469  if (SS.isNotEmpty())
1470  AnnotateScopeToken(SS, !WasScopeAnnotation);
1471  // We've "annotated" this as a keyword.
1472  return ANK_Success;
1473 
1474  case Sema::NC_Unknown:
1475  // It's not something we know about. Leave it unannotated.
1476  break;
1477 
1478  case Sema::NC_Type: {
1479  SourceLocation BeginLoc = NameLoc;
1480  if (SS.isNotEmpty())
1481  BeginLoc = SS.getBeginLoc();
1482 
1483  /// An Objective-C object type followed by '<' is a specialization of
1484  /// a parameterized class type or a protocol-qualified type.
1485  ParsedType Ty = Classification.getType();
1486  if (getLangOpts().ObjC1 && NextToken().is(tok::less) &&
1487  (Ty.get()->isObjCObjectType() ||
1488  Ty.get()->isObjCObjectPointerType())) {
1489  // Consume the name.
1491  SourceLocation NewEndLoc;
1492  TypeResult NewType
1493  = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
1494  /*consumeLastToken=*/false,
1495  NewEndLoc);
1496  if (NewType.isUsable())
1497  Ty = NewType.get();
1498  }
1499 
1500  Tok.setKind(tok::annot_typename);
1501  setTypeAnnotation(Tok, Ty);
1502  Tok.setAnnotationEndLoc(Tok.getLocation());
1503  Tok.setLocation(BeginLoc);
1504  PP.AnnotateCachedTokens(Tok);
1505  return ANK_Success;
1506  }
1507 
1508  case Sema::NC_Expression:
1509  Tok.setKind(tok::annot_primary_expr);
1510  setExprAnnotation(Tok, Classification.getExpression());
1511  Tok.setAnnotationEndLoc(NameLoc);
1512  if (SS.isNotEmpty())
1513  Tok.setLocation(SS.getBeginLoc());
1514  PP.AnnotateCachedTokens(Tok);
1515  return ANK_Success;
1516 
1517  case Sema::NC_TypeTemplate:
1518  if (Next.isNot(tok::less)) {
1519  // This may be a type template being used as a template template argument.
1520  if (SS.isNotEmpty())
1521  AnnotateScopeToken(SS, !WasScopeAnnotation);
1522  return ANK_TemplateName;
1523  }
1524  // Fall through.
1525  case Sema::NC_VarTemplate:
1527  // We have a type, variable or function template followed by '<'.
1528  ConsumeToken();
1529  UnqualifiedId Id;
1530  Id.setIdentifier(Name, NameLoc);
1531  if (AnnotateTemplateIdToken(
1532  TemplateTy::make(Classification.getTemplateName()),
1533  Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
1534  return ANK_Error;
1535  return ANK_Success;
1536  }
1537 
1539  llvm_unreachable("already parsed nested name specifier");
1540  }
1541 
1542  // Unable to classify the name, but maybe we can annotate a scope specifier.
1543  if (SS.isNotEmpty())
1544  AnnotateScopeToken(SS, !WasScopeAnnotation);
1545  return ANK_Unresolved;
1546 }
1547 
1548 bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
1549  assert(Tok.isNot(tok::identifier));
1550  Diag(Tok, diag::ext_keyword_as_ident)
1551  << PP.getSpelling(Tok)
1552  << DisableKeyword;
1553  if (DisableKeyword)
1555  Tok.setKind(tok::identifier);
1556  return true;
1557 }
1558 
1559 /// TryAnnotateTypeOrScopeToken - If the current token position is on a
1560 /// typename (possibly qualified in C++) or a C++ scope specifier not followed
1561 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1562 /// with a single annotation token representing the typename or C++ scope
1563 /// respectively.
1564 /// This simplifies handling of C++ scope specifiers and allows efficient
1565 /// backtracking without the need to re-parse and resolve nested-names and
1566 /// typenames.
1567 /// It will mainly be called when we expect to treat identifiers as typenames
1568 /// (if they are typenames). For example, in C we do not expect identifiers
1569 /// inside expressions to be treated as typenames so it will not be called
1570 /// for expressions in C.
1571 /// The benefit for C/ObjC is that a typename will be annotated and
1572 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
1573 /// will not be called twice, once to check whether we have a declaration
1574 /// specifier, and another one to get the actual type inside
1575 /// ParseDeclarationSpecifiers).
1576 ///
1577 /// This returns true if an error occurred.
1578 ///
1579 /// Note that this routine emits an error if you call it with ::new or ::delete
1580 /// as the current tokens, so only call it in contexts where these are invalid.
1581 bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
1582  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1583  Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
1584  Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
1585  Tok.is(tok::kw___super)) &&
1586  "Cannot be a type or scope token!");
1587 
1588  if (Tok.is(tok::kw_typename)) {
1589  // MSVC lets you do stuff like:
1590  // typename typedef T_::D D;
1591  //
1592  // We will consume the typedef token here and put it back after we have
1593  // parsed the first identifier, transforming it into something more like:
1594  // typename T_::D typedef D;
1595  if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
1596  Token TypedefToken;
1597  PP.Lex(TypedefToken);
1598  bool Result = TryAnnotateTypeOrScopeToken(EnteringContext, NeedType);
1599  PP.EnterToken(Tok);
1600  Tok = TypedefToken;
1601  if (!Result)
1602  Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1603  return Result;
1604  }
1605 
1606  // Parse a C++ typename-specifier, e.g., "typename T::type".
1607  //
1608  // typename-specifier:
1609  // 'typename' '::' [opt] nested-name-specifier identifier
1610  // 'typename' '::' [opt] nested-name-specifier template [opt]
1611  // simple-template-id
1612  SourceLocation TypenameLoc = ConsumeToken();
1613  CXXScopeSpec SS;
1614  if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1615  /*EnteringContext=*/false, nullptr,
1616  /*IsTypename*/ true))
1617  return true;
1618  if (!SS.isSet()) {
1619  if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
1620  Tok.is(tok::annot_decltype)) {
1621  // Attempt to recover by skipping the invalid 'typename'
1622  if (Tok.is(tok::annot_decltype) ||
1623  (!TryAnnotateTypeOrScopeToken(EnteringContext, NeedType) &&
1624  Tok.isAnnotation())) {
1625  unsigned DiagID = diag::err_expected_qualified_after_typename;
1626  // MS compatibility: MSVC permits using known types with typename.
1627  // e.g. "typedef typename T* pointer_type"
1628  if (getLangOpts().MicrosoftExt)
1629  DiagID = diag::warn_expected_qualified_after_typename;
1630  Diag(Tok.getLocation(), DiagID);
1631  return false;
1632  }
1633  }
1634 
1635  Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1636  return true;
1637  }
1638 
1639  TypeResult Ty;
1640  if (Tok.is(tok::identifier)) {
1641  // FIXME: check whether the next token is '<', first!
1642  Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1643  *Tok.getIdentifierInfo(),
1644  Tok.getLocation());
1645  } else if (Tok.is(tok::annot_template_id)) {
1646  TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1647  if (TemplateId->Kind != TNK_Type_template &&
1648  TemplateId->Kind != TNK_Dependent_template_name) {
1649  Diag(Tok, diag::err_typename_refers_to_non_type_template)
1650  << Tok.getAnnotationRange();
1651  return true;
1652  }
1653 
1654  ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1655  TemplateId->NumArgs);
1656 
1657  Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1658  TemplateId->TemplateKWLoc,
1659  TemplateId->Template,
1660  TemplateId->TemplateNameLoc,
1661  TemplateId->LAngleLoc,
1662  TemplateArgsPtr,
1663  TemplateId->RAngleLoc);
1664  } else {
1665  Diag(Tok, diag::err_expected_type_name_after_typename)
1666  << SS.getRange();
1667  return true;
1668  }
1669 
1670  SourceLocation EndLoc = Tok.getLastLoc();
1671  Tok.setKind(tok::annot_typename);
1672  setTypeAnnotation(Tok, Ty.isInvalid() ? nullptr : Ty.get());
1673  Tok.setAnnotationEndLoc(EndLoc);
1674  Tok.setLocation(TypenameLoc);
1675  PP.AnnotateCachedTokens(Tok);
1676  return false;
1677  }
1678 
1679  // Remembers whether the token was originally a scope annotation.
1680  bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1681 
1682  CXXScopeSpec SS;
1683  if (getLangOpts().CPlusPlus)
1684  if (ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext))
1685  return true;
1686 
1687  return TryAnnotateTypeOrScopeTokenAfterScopeSpec(EnteringContext, NeedType,
1688  SS, !WasScopeAnnotation);
1689 }
1690 
1691 /// \brief Try to annotate a type or scope token, having already parsed an
1692 /// optional scope specifier. \p IsNewScope should be \c true unless the scope
1693 /// specifier was extracted from an existing tok::annot_cxxscope annotation.
1695  bool NeedType,
1696  CXXScopeSpec &SS,
1697  bool IsNewScope) {
1698  if (Tok.is(tok::identifier)) {
1699  IdentifierInfo *CorrectedII = nullptr;
1700  // Determine whether the identifier is a type name.
1701  if (ParsedType Ty = Actions.getTypeName(
1702  *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
1703  false, NextToken().is(tok::period), nullptr,
1704  /*IsCtorOrDtorName=*/false,
1705  /*NonTrivialTypeSourceInfo*/ true,
1706  NeedType ? &CorrectedII : nullptr)) {
1707  // A FixIt was applied as a result of typo correction
1708  if (CorrectedII)
1709  Tok.setIdentifierInfo(CorrectedII);
1710 
1711  SourceLocation BeginLoc = Tok.getLocation();
1712  if (SS.isNotEmpty()) // it was a C++ qualified type name.
1713  BeginLoc = SS.getBeginLoc();
1714 
1715  /// An Objective-C object type followed by '<' is a specialization of
1716  /// a parameterized class type or a protocol-qualified type.
1717  if (getLangOpts().ObjC1 && NextToken().is(tok::less) &&
1718  (Ty.get()->isObjCObjectType() ||
1719  Ty.get()->isObjCObjectPointerType())) {
1720  // Consume the name.
1721  SourceLocation IdentifierLoc = ConsumeToken();
1722  SourceLocation NewEndLoc;
1723  TypeResult NewType
1724  = parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
1725  /*consumeLastToken=*/false,
1726  NewEndLoc);
1727  if (NewType.isUsable())
1728  Ty = NewType.get();
1729  }
1730 
1731  // This is a typename. Replace the current token in-place with an
1732  // annotation type token.
1733  Tok.setKind(tok::annot_typename);
1734  setTypeAnnotation(Tok, Ty);
1735  Tok.setAnnotationEndLoc(Tok.getLocation());
1736  Tok.setLocation(BeginLoc);
1737 
1738  // In case the tokens were cached, have Preprocessor replace
1739  // them with the annotation token.
1740  PP.AnnotateCachedTokens(Tok);
1741  return false;
1742  }
1743 
1744  if (!getLangOpts().CPlusPlus) {
1745  // If we're in C, we can't have :: tokens at all (the lexer won't return
1746  // them). If the identifier is not a type, then it can't be scope either,
1747  // just early exit.
1748  return false;
1749  }
1750 
1751  // If this is a template-id, annotate with a template-id or type token.
1752  if (NextToken().is(tok::less)) {
1753  TemplateTy Template;
1755  TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1756  bool MemberOfUnknownSpecialization;
1757  if (TemplateNameKind TNK =
1758  Actions.isTemplateName(getCurScope(), SS,
1759  /*hasTemplateKeyword=*/false, TemplateName,
1760  /*ObjectType=*/nullptr, EnteringContext,
1761  Template, MemberOfUnknownSpecialization)) {
1762  // Consume the identifier.
1763  ConsumeToken();
1764  if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1765  TemplateName)) {
1766  // If an unrecoverable error occurred, we need to return true here,
1767  // because the token stream is in a damaged state. We may not return
1768  // a valid identifier.
1769  return true;
1770  }
1771  }
1772  }
1773 
1774  // The current token, which is either an identifier or a
1775  // template-id, is not part of the annotation. Fall through to
1776  // push that token back into the stream and complete the C++ scope
1777  // specifier annotation.
1778  }
1779 
1780  if (Tok.is(tok::annot_template_id)) {
1781  TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1782  if (TemplateId->Kind == TNK_Type_template) {
1783  // A template-id that refers to a type was parsed into a
1784  // template-id annotation in a context where we weren't allowed
1785  // to produce a type annotation token. Update the template-id
1786  // annotation token to a type annotation token now.
1787  AnnotateTemplateIdTokenAsType();
1788  return false;
1789  }
1790  }
1791 
1792  if (SS.isEmpty())
1793  return false;
1794 
1795  // A C++ scope specifier that isn't followed by a typename.
1796  AnnotateScopeToken(SS, IsNewScope);
1797  return false;
1798 }
1799 
1800 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
1801 /// annotates C++ scope specifiers and template-ids. This returns
1802 /// true if there was an error that could not be recovered from.
1803 ///
1804 /// Note that this routine emits an error if you call it with ::new or ::delete
1805 /// as the current tokens, so only call it in contexts where these are invalid.
1806 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
1807  assert(getLangOpts().CPlusPlus &&
1808  "Call sites of this function should be guarded by checking for C++");
1809  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1810  (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
1811  Tok.is(tok::kw_decltype) || Tok.is(tok::kw___super)) &&
1812  "Cannot be a type or scope token!");
1813 
1814  CXXScopeSpec SS;
1815  if (ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext))
1816  return true;
1817  if (SS.isEmpty())
1818  return false;
1819 
1820  AnnotateScopeToken(SS, true);
1821  return false;
1822 }
1823 
1824 bool Parser::isTokenEqualOrEqualTypo() {
1825  tok::TokenKind Kind = Tok.getKind();
1826  switch (Kind) {
1827  default:
1828  return false;
1829  case tok::ampequal: // &=
1830  case tok::starequal: // *=
1831  case tok::plusequal: // +=
1832  case tok::minusequal: // -=
1833  case tok::exclaimequal: // !=
1834  case tok::slashequal: // /=
1835  case tok::percentequal: // %=
1836  case tok::lessequal: // <=
1837  case tok::lesslessequal: // <<=
1838  case tok::greaterequal: // >=
1839  case tok::greatergreaterequal: // >>=
1840  case tok::caretequal: // ^=
1841  case tok::pipeequal: // |=
1842  case tok::equalequal: // ==
1843  Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1844  << Kind
1846  case tok::equal:
1847  return true;
1848  }
1849 }
1850 
1851 SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
1852  assert(Tok.is(tok::code_completion));
1853  PrevTokLocation = Tok.getLocation();
1854 
1855  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1856  if (S->getFlags() & Scope::FnScope) {
1859  cutOffParsing();
1860  return PrevTokLocation;
1861  }
1862 
1863  if (S->getFlags() & Scope::ClassScope) {
1865  cutOffParsing();
1866  return PrevTokLocation;
1867  }
1868  }
1869 
1870  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1871  cutOffParsing();
1872  return PrevTokLocation;
1873 }
1874 
1875 // Code-completion pass-through functions
1876 
1877 void Parser::CodeCompleteDirective(bool InConditional) {
1878  Actions.CodeCompletePreprocessorDirective(InConditional);
1879 }
1880 
1881 void Parser::CodeCompleteInConditionalExclusion() {
1883 }
1884 
1885 void Parser::CodeCompleteMacroName(bool IsDefinition) {
1886  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1887 }
1888 
1889 void Parser::CodeCompletePreprocessorExpression() {
1891 }
1892 
1893 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1895  unsigned ArgumentIndex) {
1896  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1897  ArgumentIndex);
1898 }
1899 
1900 void Parser::CodeCompleteNaturalLanguage() {
1901  Actions.CodeCompleteNaturalLanguage();
1902 }
1903 
1904 bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1905  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1906  "Expected '__if_exists' or '__if_not_exists'");
1907  Result.IsIfExists = Tok.is(tok::kw___if_exists);
1908  Result.KeywordLoc = ConsumeToken();
1909 
1910  BalancedDelimiterTracker T(*this, tok::l_paren);
1911  if (T.consumeOpen()) {
1912  Diag(Tok, diag::err_expected_lparen_after)
1913  << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1914  return true;
1915  }
1916 
1917  // Parse nested-name-specifier.
1918  if (getLangOpts().CPlusPlus)
1919  ParseOptionalCXXScopeSpecifier(Result.SS, nullptr,
1920  /*EnteringContext=*/false);
1921 
1922  // Check nested-name specifier.
1923  if (Result.SS.isInvalid()) {
1924  T.skipToEnd();
1925  return true;
1926  }
1927 
1928  // Parse the unqualified-id.
1929  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1930  if (ParseUnqualifiedId(Result.SS, false, true, true, nullptr, TemplateKWLoc,
1931  Result.Name)) {
1932  T.skipToEnd();
1933  return true;
1934  }
1935 
1936  if (T.consumeClose())
1937  return true;
1938 
1939  // Check if the symbol exists.
1940  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
1941  Result.IsIfExists, Result.SS,
1942  Result.Name)) {
1943  case Sema::IER_Exists:
1944  Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
1945  break;
1946 
1948  Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
1949  break;
1950 
1951  case Sema::IER_Dependent:
1952  Result.Behavior = IEB_Dependent;
1953  break;
1954 
1955  case Sema::IER_Error:
1956  return true;
1957  }
1958 
1959  return false;
1960 }
1961 
1962 void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
1963  IfExistsCondition Result;
1964  if (ParseMicrosoftIfExistsCondition(Result))
1965  return;
1966 
1967  BalancedDelimiterTracker Braces(*this, tok::l_brace);
1968  if (Braces.consumeOpen()) {
1969  Diag(Tok, diag::err_expected) << tok::l_brace;
1970  return;
1971  }
1972 
1973  switch (Result.Behavior) {
1974  case IEB_Parse:
1975  // Parse declarations below.
1976  break;
1977 
1978  case IEB_Dependent:
1979  llvm_unreachable("Cannot have a dependent external declaration");
1980 
1981  case IEB_Skip:
1982  Braces.skipToEnd();
1983  return;
1984  }
1985 
1986  // Parse the declarations.
1987  // FIXME: Support module import within __if_exists?
1988  while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
1989  ParsedAttributesWithRange attrs(AttrFactory);
1990  MaybeParseCXX11Attributes(attrs);
1991  MaybeParseMicrosoftAttributes(attrs);
1992  DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1993  if (Result && !getCurScope()->getParent())
1994  Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
1995  }
1996  Braces.consumeClose();
1997 }
1998 
1999 Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
2000  assert(Tok.isObjCAtKeyword(tok::objc_import) &&
2001  "Improper start to module import");
2002  SourceLocation ImportLoc = ConsumeToken();
2003 
2005 
2006  // Parse the module path.
2007  do {
2008  if (!Tok.is(tok::identifier)) {
2009  if (Tok.is(tok::code_completion)) {
2010  Actions.CodeCompleteModuleImport(ImportLoc, Path);
2011  cutOffParsing();
2012  return nullptr;
2013  }
2014 
2015  Diag(Tok, diag::err_module_expected_ident);
2016  SkipUntil(tok::semi);
2017  return nullptr;
2018  }
2019 
2020  // Record this part of the module path.
2021  Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
2022  ConsumeToken();
2023 
2024  if (Tok.is(tok::period)) {
2025  ConsumeToken();
2026  continue;
2027  }
2028 
2029  break;
2030  } while (true);
2031 
2032  if (PP.hadModuleLoaderFatalFailure()) {
2033  // With a fatal failure in the module loader, we abort parsing.
2034  cutOffParsing();
2035  return nullptr;
2036  }
2037 
2038  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
2039  ExpectAndConsumeSemi(diag::err_module_expected_semi);
2040  if (Import.isInvalid())
2041  return nullptr;
2042 
2043  return Actions.ConvertDeclToDeclGroup(Import.get());
2044 }
2045 
2046 /// \brief Try recover parser when module annotation appears where it must not
2047 /// be found.
2048 /// \returns false if the recover was successful and parsing may be continued, or
2049 /// true if parser must bail out to top level and handle the token there.
2050 bool Parser::parseMisplacedModuleImport() {
2051  while (true) {
2052  switch (Tok.getKind()) {
2053  case tok::annot_module_end:
2054  // Inform caller that recovery failed, the error must be handled at upper
2055  // level.
2056  return true;
2057  case tok::annot_module_begin:
2058  Actions.diagnoseMisplacedModuleImport(reinterpret_cast<Module *>(
2059  Tok.getAnnotationValue()), Tok.getLocation());
2060  return true;
2061  case tok::annot_module_include:
2062  // Module import found where it should not be, for instance, inside a
2063  // namespace. Recover by importing the module.
2064  Actions.ActOnModuleInclude(Tok.getLocation(),
2065  reinterpret_cast<Module *>(
2066  Tok.getAnnotationValue()));
2067  ConsumeToken();
2068  // If there is another module import, process it.
2069  continue;
2070  default:
2071  return false;
2072  }
2073  }
2074  return false;
2075 }
2076 
2077 bool BalancedDelimiterTracker::diagnoseOverflow() {
2078  P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
2079  << P.getLangOpts().BracketDepth;
2080  P.Diag(P.Tok, diag::note_bracket_depth);
2081  P.cutOffParsing();
2082  return true;
2083 }
2084 
2086  const char *Msg,
2087  tok::TokenKind SkipToTok) {
2088  LOpen = P.Tok.getLocation();
2089  if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
2090  if (SkipToTok != tok::unknown)
2091  P.SkipUntil(SkipToTok, Parser::StopAtSemi);
2092  return true;
2093  }
2094 
2095  if (getDepth() < MaxDepth)
2096  return false;
2097 
2098  return diagnoseOverflow();
2099 }
2100 
2101 bool BalancedDelimiterTracker::diagnoseMissingClose() {
2102  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
2103 
2104  if (P.Tok.is(tok::annot_module_end))
2105  P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
2106  else
2107  P.Diag(P.Tok, diag::err_expected) << Close;
2108  P.Diag(LOpen, diag::note_matching) << Kind;
2109 
2110  // If we're not already at some kind of closing bracket, skip to our closing
2111  // token.
2112  if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
2113  P.Tok.isNot(tok::r_square) &&
2114  P.SkipUntil(Close, FinalToken,
2116  P.Tok.is(Close))
2117  LClose = P.ConsumeAnyToken();
2118  return true;
2119 }
2120 
2122  P.SkipUntil(Close, Parser::StopBeforeMatch);
2123  consumeClose();
2124 }
MutableArrayRef< TemplateParameterList * > MultiTemplateParamsArg
Definition: Ownership.h:266
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:265
SourceLocation getThreadStorageClassSpecLoc() const
Definition: DeclSpec.h:457
Defines the clang::ASTContext interface.
SourceLocation getEnd() const
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
DeclaratorChunk::FunctionTypeInfo & getFunctionTypeInfo()
getFunctionTypeInfo - Retrieves the function type info object (looking through parentheses).
Definition: DeclSpec.h:2097
This is a scope that corresponds to the parameters within a function prototype.
Definition: Scope.h:80
bool isInvalid() const
Definition: Ownership.h:160
void Initialize()
Perform initialization that occurs after the parser has been initialized but before it parses anythin...
Definition: Sema.cpp:142
void Initialize()
Initialize - Warm up the parser.
Code completion occurs within a class, struct, or union.
Definition: Sema.h:9181
const LangOptions & getLangOpts() const
Definition: Parse/Parser.h:251
SourceLocation TemplateNameLoc
TemplateNameLoc - The location of the template name within the source.
const Scope * getParent() const
getParent - Return the scope that this is nested in.
Definition: Scope.h:218
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Definition: Preprocessor.h:934
The name refers to a dependent template name.
Definition: TemplateKinds.h:38
Defines the C++ template declaration subclasses.
StringRef P
SCS getStorageClassSpec() const
Definition: DeclSpec.h:447
void CodeCompleteNaturalLanguage()
PtrTy get() const
Definition: Ownership.h:164
bool TryAnnotateCXXScopeToken(bool EnteringContext=false)
TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only annotates C++ scope specifiers and ...
This indicates that the scope corresponds to a function, which means that labels are set here...
Definition: Scope.h:46
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1124
static const char * getSpecifierName(DeclSpec::TST T, const PrintingPolicy &Policy)
Turn a type-specifier-type into a string like "_Bool" or "union".
Definition: DeclSpec.cpp:447
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:93
TemplateNameKind Kind
The kind of template that Template refers to.
Wrapper for void* pointer.
Definition: Ownership.h:46
Parser - This implements a parser for the C family of languages.
Definition: Parse/Parser.h:57
void * SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS)
Given a C++ nested-name-specifier, produce an annotation value that the parser can use later to recon...
DeclGroupPtrTy BuildDeclaratorGroup(MutableArrayRef< Decl * > Group, bool TypeMayContainAuto=true)
BuildDeclaratorGroup - convert a list of declarations into a declaration group, performing any necess...
Definition: SemaDecl.cpp:10700
void ActOnDefaultCtorInitializers(Decl *CDtorDecl)
static const TSCS TSCS_unspecified
Definition: DeclSpec.h:246
void EnterToken(const Token &Tok)
Enters a token in the token stream to be lexed next.
Information about one declarator, including the parsed type information and the identifier.
Definition: DeclSpec.h:1624
TypeSpecifierType
Specifies the kind of type.
Definition: Specifiers.h:45
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
Code completion occurs within an Objective-C implementation or category implementation.
Definition: Sema.h:9187
RAII object that makes sure paren/bracket/brace count is correct after declaration/statement parsing...
Decl * ActOnParamDeclarator(Scope *S, Declarator &D)
ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() to introduce parameters into fun...
Definition: SemaDecl.cpp:10795
bool canSkipFunctionBody(Decl *D)
Determine whether we can skip parsing the body of a function definition, assuming we don't care about...
Definition: SemaDecl.cpp:11417
const ParsingDeclSpec & getDeclSpec() const
friend class ObjCDeclContextSwitch
Definition: Parse/Parser.h:61
void MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, CachedTokens &Toks)
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool isEmpty() const
No scope specifier.
Definition: DeclSpec.h:189
bool SkipUntil(tok::TokenKind T, SkipUntilFlags Flags=static_cast< SkipUntilFlags >(0))
SkipUntil - Read tokens until we get to the specified token, then consume it (unless StopBeforeMatch ...
Definition: Parse/Parser.h:885
Information about a template-id annotation token.
void CodeCompleteOrdinaryName(Scope *S, ParserCompletionContext CompletionContext)
Decl * ActOnStartOfFunctionDef(Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, SkipBodyInfo *SkipBody=nullptr)
Definition: SemaDecl.cpp:11075
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3253
const Token & NextToken()
NextToken - This peeks ahead one token and returns it without consuming it.
Definition: Parse/Parser.h:558
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
bool TryConsumeToken(tok::TokenKind Expected)
Definition: Parse/Parser.h:300
void SetPoisonReason(IdentifierInfo *II, unsigned DiagID)
Specifies the reason for poisoning an identifier.
One of these records is kept for each identifier that is lexed.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType=nullptr)
Definition: SemaDecl.cpp:54
bool isFileID() const
bool isEmpty() const
isEmpty - Return true if this declaration specifier is completely empty: no tokens were parsed in the...
Definition: DeclSpec.h:603
void ActOnEndOfTranslationUnit()
ActOnEndOfTranslationUnit - This is called at the very end of the translation unit when EOF is reache...
Definition: Sema.cpp:651
bool isTranslationUnit() const
Definition: DeclBase.h:1283
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
void CodeCompletePreprocessorMacroArgument(Scope *S, IdentifierInfo *Macro, MacroInfo *MacroInfo, unsigned Argument)
void setKind(tok::TokenKind K)
Definition: Token.h:90
RAII class that helps handle the parsing of an open/close delimiter pair, such as braces { ...
void removeCommentHandler(CommentHandler *Handler)
Remove the specified comment handler.
void ClearStorageClassSpecs()
Definition: DeclSpec.h:461
Describes a module or submodule.
Definition: Basic/Module.h:47
void diagnoseMisplacedModuleImport(Module *M, SourceLocation ImportLoc)
Check if module import may be found in the current context, emit error if not.
Definition: SemaDecl.cpp:15060
Code completion occurs at top-level or namespace context.
Definition: Sema.h:9179
static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R)
Code completion occurs within the body of a function on a recovery path, where we do not have a speci...
Definition: Sema.h:9211
Represents a C++ unqualified-id that has been parsed.
Definition: DeclSpec.h:884
void SetRangeBegin(SourceLocation Loc)
SetRangeBegin - Set the start of the source range to Loc, unless it's invalid.
Definition: DeclSpec.h:1767
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path)
ParsedTemplateArgument * getTemplateArgs()
Retrieves a pointer to the template arguments.
bool hadModuleLoaderFatalFailure() const
Definition: Preprocessor.h:718
IfExistsResult CheckMicrosoftIfExistsSymbol(Scope *S, CXXScopeSpec &SS, const DeclarationNameInfo &TargetNameInfo)
void ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod)
The parser has processed a module import translated from a #include or similar preprocessing directiv...
Definition: SemaDecl.cpp:15106
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:39
Decl * ActOnFileScopeAsmDecl(Expr *expr, SourceLocation AsmLoc, SourceLocation RParenLoc)
Definition: SemaDecl.cpp:15014
Represents a C++ nested-name-specifier or a global scope specifier.
Definition: DeclSpec.h:63
tok::TokenKind getKind() const
Definition: Token.h:89
void setCodeCompletionHandler(CodeCompletionHandler &Handler)
Set the code completion handler to the given object.
Definition: Preprocessor.h:969
Decl * ActOnFinishFunctionBody(Decl *Decl, Stmt *Body)
Definition: SemaDecl.cpp:11437
SourceRange getSourceRange() const LLVM_READONLY
Definition: DeclSpec.h:502
detail::InMemoryDirectory::const_iterator I
bool isInvalid() const
SourceRange getRange() const
Definition: DeclSpec.h:68
SourceLocation TemplateKWLoc
TemplateKWLoc - The location of the template keyword within the source.
SourceLocation LAngleLoc
The location of the '<' before the template argument list.
void * getAnnotationValue() const
Definition: Token.h:223
An error occurred.
Definition: Sema.h:4130
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:263
Decl * ActOnEmptyDeclaration(Scope *S, AttributeList *AttrList, SourceLocation SemiLoc)
Handle a C++11 empty-declaration and attribute-declaration.
bool isFunctionDeclarator(unsigned &idx) const
isFunctionDeclarator - This method returns true if the declarator is a function declarator (looking t...
Definition: DeclSpec.h:2066
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:873
A class for parsing a declarator.
void clearCodeCompletionHandler()
Clear out the code completion handler.
Definition: Preprocessor.h:979
NameClassificationKind getKind() const
Definition: Sema.h:1607
unsigned NumParams
NumParams - This is the number of formal parameters specified by the declarator.
Definition: DeclSpec.h:1247
Stop at code completion.
Definition: Parse/Parser.h:868
void setAnnotationRange(SourceRange R)
Definition: Token.h:160
SourceRange getAnnotationRange() const
SourceRange of the group of tokens that this annotation token represents.
Definition: Token.h:157
void setAnnotationValue(void *val)
Definition: Token.h:227
Decl * ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS, DeclSpec &DS, RecordDecl *&AnonRecord)
ParsedFreeStandingDeclSpec - This method is invoked when a declspec with no declarator (e...
Definition: SemaDecl.cpp:3714
TemplateNameKind getTemplateNameKind() const
Definition: Sema.h:1625
void AnnotateCachedTokens(const Token &Tok)
We notify the Preprocessor that if it is caching tokens (because backtrack is enabled) it should repl...
ParsedType getTypeName(const IdentifierInfo &II, SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS=nullptr, bool isClassName=false, bool HasTrailingDot=false, ParsedType ObjectType=nullptr, bool IsCtorOrDtorName=false, bool WantNontrivialTypeSourceInfo=false, IdentifierInfo **CorrectedII=nullptr)
If the identifier refers to a type name within this scope, return the declaration of that type...
Definition: SemaDecl.cpp:249
This file defines the classes used to store parsed information about declaration-specifiers and decla...
void SkipMalformedDecl()
SkipMalformedDecl - Read tokens until we get to some likely good stopping point for skipping past a s...
Definition: ParseDecl.cpp:1648
void RevertCachedTokens(unsigned N)
When backtracking is enabled and tokens are cached, this allows to revert a specific number of tokens...
Represents a C++ template name within the type system.
Definition: TemplateName.h:176
void CodeCompletePreprocessorExpression()
const char * getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple punctuation tokens like '!' or '', and returns NULL for literal and...
Definition: TokenKinds.cpp:32
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:123
TemplateNameKind
Specifies the kind of template name that an identifier refers to.
Definition: TemplateKinds.h:21
bool isNot(tok::TokenKind K) const
Definition: Token.h:95
static const TST TST_int
Definition: DeclSpec.h:278
void SetDeclDeleted(Decl *dcl, SourceLocation DelLoc)
Wraps an identifier and optional source location for the identifier.
Definition: AttributeList.h:72
TemplateNameKind isTemplateName(Scope *S, CXXScopeSpec &SS, bool hasTemplateKeyword, UnqualifiedId &Name, ParsedType ObjectType, bool EnteringContext, TemplateTy &Template, bool &MemberOfUnknownSpecialization)
The symbol exists.
Definition: Sema.h:4120
The result type of a method or function.
SourceLocation getStorageClassSpecLoc() const
Definition: DeclSpec.h:456
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition: Parse/Parser.h:267
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:553
NameClassification ClassifyName(Scope *S, CXXScopeSpec &SS, IdentifierInfo *&Name, SourceLocation NameLoc, const Token &NextToken, bool IsAddressOfOperand, std::unique_ptr< CorrectionCandidateCallback > CCC=nullptr)
Perform name lookup on the given name, classifying it based on the results of name lookup and the fol...
Definition: SemaDecl.cpp:775
This is a scope that corresponds to the parameters within a function prototype for a function declara...
Definition: Scope.h:86
void CheckForFunctionRedefinition(FunctionDecl *FD, const FunctionDecl *EffectiveDefinition=nullptr, SkipBodyInfo *SkipBody=nullptr)
Definition: SemaDecl.cpp:11147
Decl * ActOnSkippedFunctionBody(Decl *Decl)
Definition: SemaDecl.cpp:11429
A class for parsing a DeclSpec.
bool isKNRPrototype() const
isKNRPrototype - Return true if this is a K&R style identifier list, like "void foo(a,b,c)".
Definition: DeclSpec.h:1334
void ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, SourceLocation LocAfterDecls)
Definition: SemaDecl.cpp:11038
#define false
Definition: stdbool.h:33
Kind
Stop skipping at semicolon.
Definition: Parse/Parser.h:865
void TypoCorrectToken(const Token &Tok)
Update the current token to represent the provided identifier, in order to cache an action performed ...
SmallVectorImpl< AnnotatedLine * >::const_iterator Next
bool ParseTopLevelDecl()
Definition: Parse/Parser.h:283
Encodes a location in the source.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
bool isValid() const
Return true if this is a valid SourceLocation object.
void ExitScope()
ExitScope - Pop a scope off the scope stack.
ASTContext & getASTContext() const
Definition: Sema.h:1069
bool isCodeCompletionReached() const
Returns true if code-completion is enabled and we have hit the code-completion point.
Parser(Preprocessor &PP, Sema &Actions, bool SkipFunctionBodies)
void setAnnotationEndLoc(SourceLocation L)
Definition: Token.h:141
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:697
Scope * getCurScope() const
Definition: Parse/Parser.h:258
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
Definition: Lexer.cpp:36
void setIdentifierInfo(IdentifierInfo *II)
Definition: Token.h:185
ExtensionRAIIObject - This saves the state of extension warnings when constructed and disables them...
void SetDeclDefaulted(Decl *dcl, SourceLocation DefaultLoc)
bool canDelayFunctionBody(const Declarator &D)
Determine whether we can delay parsing the body of a function or function template until it is used...
Definition: SemaDecl.cpp:11393
void CodeCompletePreprocessorDirective(bool InConditional)
void Lex(Token &Result)
Lex the next token for this preprocessor.
void EnterScope(unsigned ScopeFlags)
EnterScope - Start a new scope.
bool isInvalid() const
An error occurred during parsing of the scope specifier.
Definition: DeclSpec.h:194
bool TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool EnteringContext, bool NeedType, CXXScopeSpec &SS, bool IsNewScope)
Try to annotate a type or scope token, having already parsed an optional scope specifier.
SourceLocation getLastLoc() const
Definition: Token.h:146
ASTConsumer & getASTConsumer() const
Definition: Sema.h:1070
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok)
void SetLateTemplateParser(LateTemplateParserCB *LTP, LateTemplateParserCleanupCB *LTPCleanup, void *P)
Definition: Sema.h:563
SourceLocation getBegin() const
SourceLocation getBeginLoc() const
Definition: DeclSpec.h:72
PtrTy get() const
Definition: Ownership.h:75
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:94
The name is a dependent name, so the results will differ from one instantiation to the next...
Definition: Sema.h:4127
void Init(Scope *parent, unsigned flags)
Init - This is used by the parser to implement scope caching.
Definition: Scope.cpp:88
bool ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, bool AllowDestructorName, bool AllowConstructorName, ParsedType ObjectType, SourceLocation &TemplateKWLoc, UnqualifiedId &Result)
Parse a C++ unqualified-id (or a C identifier), which describes the name of an entity.
void CodeCompletePreprocessorMacroName(bool IsDefinition)
void setFunctionDefinitionKind(FunctionDefinitionKind Val)
Definition: DeclSpec.h:2237
The scope of a struct/union/class definition.
Definition: Scope.h:64
TSCS getThreadStorageClassSpec() const
Definition: DeclSpec.h:448
bool expectAndConsume(unsigned DiagID=diag::err_expected, const char *Msg="", tok::TokenKind SkipToTok=tok::unknown)
void addCommentHandler(CommentHandler *Handler)
Add the specified comment handler to the preprocessor.
SourceLocation RAngleLoc
The location of the '>' after the template argument list.
void ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod)
The parsed has entered a submodule.
Definition: SemaDecl.cpp:15141
bool hasTagDefinition() const
Definition: DeclSpec.cpp:354
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:104
ParsingDeclSpec & getMutableDeclSpec() const
SkipUntilFlags
Control flags for SkipUntil functions.
Definition: Parse/Parser.h:864
detail::InMemoryDirectory::const_iterator E
TypeResult ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, const CXXScopeSpec &SS, const IdentifierInfo &II, SourceLocation IdLoc)
Called when the parser has parsed a C++ typename specifier, e.g., "typename T::type".
The name refers to a template whose specialization produces a type.
Definition: TemplateKinds.h:30
static const TST TST_unspecified
Definition: DeclSpec.h:272
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:34
bool isObjCObjectType() const
Definition: Type.h:5557
bool isNotEmpty() const
A scope specifier is present, but may be valid or invalid.
Definition: DeclSpec.h:191
ExprResult getExpression() const
Definition: Sema.h:1614
~Parser() override
IdentifierInfo * getName() const
void takeAttributesFrom(ParsedAttributes &attrs)
Definition: DeclSpec.h:752
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
bool isKnownToGCC() const
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
TemplateName getTemplateName() const
Definition: Sema.h:1619
SourceLocation getLoc() const
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:78
SmallVector< TemplateParameterList *, 4 > TemplateParameterLists
Definition: Parse/Parser.h:270
DeclResult ActOnModuleImport(SourceLocation AtLoc, SourceLocation ImportLoc, ModuleIdPath Path)
The parser has processed a module import declaration.
Definition: SemaDecl.cpp:15064
bool isUsable() const
Definition: Ownership.h:161
This is a scope that can contain a declaration.
Definition: Scope.h:58
bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, unsigned &DiagID, const PrintingPolicy &Policy)
Definition: DeclSpec.cpp:691
NamedDecl * HandleDeclarator(Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParameterLists)
Definition: SemaDecl.cpp:4916
void ActOnTranslationUnitScope(Scope *S)
Definition: Sema.cpp:69
bool isCXX11Attribute() const
Captures information about "declaration specifiers".
Definition: DeclSpec.h:228
SourceLocation getIdentifierLoc() const
Definition: DeclSpec.h:1978
SourceLocation ConsumeToken()
ConsumeToken - Consume the current 'peek token' and lex the next one.
Definition: Parse/Parser.h:292
bool isObjCObjectPointerType() const
Definition: Type.h:5554
bool TryAnnotateTypeOrScopeToken(bool EnteringContext=false, bool NeedType=false)
TryAnnotateTypeOrScopeToken - If the current token position is on a typename (possibly qualified in C...
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:311
void ActOnPopScope(SourceLocation Loc, Scope *S)
Scope actions.
Definition: SemaDecl.cpp:1633
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:115
void revertTokenIDToIdentifier()
Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 compatibility.
ExprResult ExprError()
Definition: Ownership.h:268
void ActOnComment(SourceRange Comment)
Definition: Sema.cpp:1225
Abstract base class that describes a handler that will receive source ranges for each of the comments...
static OpaquePtr make(TemplateNameP)
Definition: Ownership.h:55
unsigned getParsedSpecifiers() const
Return a bitmask of which flavors of specifiers this DeclSpec includes.
Definition: DeclSpec.cpp:363
bool isSet() const
Deprecated.
Definition: DeclSpec.h:209
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:19
void setLocation(SourceLocation L)
Definition: Token.h:131
void ActOnModuleEnd(SourceLocation DirectiveLoc, Module *Mod)
The parser has left a submodule.
Definition: SemaDecl.cpp:15149
AttributeList * getNext() const
#define true
Definition: stdbool.h:32
A trivial tuple used to represent a source range.
void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc)
Specify that this unqualified-id was parsed as an identifier.
Definition: DeclSpec.h:988
unsigned NumArgs
NumArgs - The number of template arguments.
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:117
ParsedType getType() const
Definition: Sema.h:1609
The symbol does not exist.
Definition: Sema.h:4123
void CodeCompleteInPreprocessorConditionalExclusion(Scope *S)
ParamInfo * Params
Params - This is a pointer to a new[]'d array of ParamInfo objects that describe the parameters speci...
Definition: DeclSpec.h:1286
ParsedAttributes & getAttributes()
Definition: DeclSpec.h:749
void startToken()
Reset all flags to cleared.
Definition: Token.h:168
ParsedTemplateTy Template
The declaration of the template corresponding to the template-name.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:97
AttributeList - Represents a syntactic attribute.
Definition: AttributeList.h:94
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
Stop skipping at specified token, but don't skip the token itself.
Definition: Parse/Parser.h:867
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:176
const AttributeList * getAttributes() const
Definition: DeclSpec.h:2184