Parser.cpp revision b031eab1c07fa2c5bd74c7e92f7c938bf3304729
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 "clang/Parse/ParseDiagnostic.h"
16#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
19#include "llvm/Support/raw_ostream.h"
20#include "RAIIObjectsForParser.h"
21#include "ParsePragma.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/ASTConsumer.h"
24using namespace clang;
25
26IdentifierInfo *Parser::getSEHExceptKeyword() {
27  // __except is accepted as a (contextual) keyword
28  if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
29    Ident__except = PP.getIdentifierInfo("__except");
30
31  return Ident__except;
32}
33
34Parser::Parser(Preprocessor &pp, Sema &actions)
35  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
36    GreaterThanIsOperator(true), ColonIsSacred(false),
37    InMessageExpression(false), TemplateParameterDepth(0) {
38  Tok.setKind(tok::eof);
39  Actions.CurScope = 0;
40  NumCachedScopes = 0;
41  ParenCount = BracketCount = BraceCount = 0;
42  CurParsedObjCImpl = 0;
43
44  // Add #pragma handlers. These are removed and destroyed in the
45  // destructor.
46  AlignHandler.reset(new PragmaAlignHandler(actions));
47  PP.AddPragmaHandler(AlignHandler.get());
48
49  GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
50  PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
51
52  OptionsHandler.reset(new PragmaOptionsHandler(actions));
53  PP.AddPragmaHandler(OptionsHandler.get());
54
55  PackHandler.reset(new PragmaPackHandler(actions));
56  PP.AddPragmaHandler(PackHandler.get());
57
58  MSStructHandler.reset(new PragmaMSStructHandler(actions));
59  PP.AddPragmaHandler(MSStructHandler.get());
60
61  UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
62  PP.AddPragmaHandler(UnusedHandler.get());
63
64  WeakHandler.reset(new PragmaWeakHandler(actions));
65  PP.AddPragmaHandler(WeakHandler.get());
66
67  RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler(actions));
68  PP.AddPragmaHandler(RedefineExtnameHandler.get());
69
70  FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
71  PP.AddPragmaHandler("STDC", FPContractHandler.get());
72
73  if (getLangOpts().OpenCL) {
74    OpenCLExtensionHandler.reset(
75                  new PragmaOpenCLExtensionHandler(actions, *this));
76    PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
77
78    PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
79  }
80
81  PP.setCodeCompletionHandler(*this);
82}
83
84/// If a crash happens while the parser is active, print out a line indicating
85/// what the current token is.
86void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
87  const Token &Tok = P.getCurToken();
88  if (Tok.is(tok::eof)) {
89    OS << "<eof> parser at end of file\n";
90    return;
91  }
92
93  if (Tok.getLocation().isInvalid()) {
94    OS << "<unknown> parser at unknown location\n";
95    return;
96  }
97
98  const Preprocessor &PP = P.getPreprocessor();
99  Tok.getLocation().print(OS, PP.getSourceManager());
100  if (Tok.isAnnotation())
101    OS << ": at annotation token \n";
102  else
103    OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
104}
105
106
107DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
108  return Diags.Report(Loc, DiagID);
109}
110
111DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
112  return Diag(Tok.getLocation(), DiagID);
113}
114
115/// \brief Emits a diagnostic suggesting parentheses surrounding a
116/// given range.
117///
118/// \param Loc The location where we'll emit the diagnostic.
119/// \param Loc The kind of diagnostic to emit.
120/// \param ParenRange Source range enclosing code that should be parenthesized.
121void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
122                                SourceRange ParenRange) {
123  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
124  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
125    // We can't display the parentheses, so just dig the
126    // warning/error and return.
127    Diag(Loc, DK);
128    return;
129  }
130
131  Diag(Loc, DK)
132    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
133    << FixItHint::CreateInsertion(EndLoc, ")");
134}
135
136static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
137  switch (ExpectedTok) {
138  case tok::semi: return Tok.is(tok::colon); // : for ;
139  default: return false;
140  }
141}
142
143/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
144/// input.  If so, it is consumed and false is returned.
145///
146/// If the input is malformed, this emits the specified diagnostic.  Next, if
147/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
148/// returned.
149bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
150                              const char *Msg, tok::TokenKind SkipToTok) {
151  if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
152    ConsumeAnyToken();
153    return false;
154  }
155
156  // Detect common single-character typos and resume.
157  if (IsCommonTypo(ExpectedTok, Tok)) {
158    SourceLocation Loc = Tok.getLocation();
159    Diag(Loc, DiagID)
160      << Msg
161      << FixItHint::CreateReplacement(SourceRange(Loc),
162                                      getTokenSimpleSpelling(ExpectedTok));
163    ConsumeAnyToken();
164
165    // Pretend there wasn't a problem.
166    return false;
167  }
168
169  const char *Spelling = 0;
170  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
171  if (EndLoc.isValid() &&
172      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
173    // Show what code to insert to fix this problem.
174    Diag(EndLoc, DiagID)
175      << Msg
176      << FixItHint::CreateInsertion(EndLoc, Spelling);
177  } else
178    Diag(Tok, DiagID) << Msg;
179
180  if (SkipToTok != tok::unknown)
181    SkipUntil(SkipToTok);
182  return true;
183}
184
185bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
186  if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
187    ConsumeAnyToken();
188    return false;
189  }
190
191  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
192      NextToken().is(tok::semi)) {
193    Diag(Tok, diag::err_extraneous_token_before_semi)
194      << PP.getSpelling(Tok)
195      << FixItHint::CreateRemoval(Tok.getLocation());
196    ConsumeAnyToken(); // The ')' or ']'.
197    ConsumeToken(); // The ';'.
198    return false;
199  }
200
201  return ExpectAndConsume(tok::semi, DiagID);
202}
203
204//===----------------------------------------------------------------------===//
205// Error recovery.
206//===----------------------------------------------------------------------===//
207
208/// SkipUntil - Read tokens until we get to the specified token, then consume
209/// it (unless DontConsume is true).  Because we cannot guarantee that the
210/// token will ever occur, this skips to the next token, or to some likely
211/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
212/// character.
213///
214/// If SkipUntil finds the specified token, it returns true, otherwise it
215/// returns false.
216bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
217                       bool StopAtSemi, bool DontConsume,
218                       bool StopAtCodeCompletion) {
219  // We always want this function to skip at least one token if the first token
220  // isn't T and if not at EOF.
221  bool isFirstTokenSkipped = true;
222  while (1) {
223    // If we found one of the tokens, stop and return true.
224    for (unsigned i = 0; i != NumToks; ++i) {
225      if (Tok.is(Toks[i])) {
226        if (DontConsume) {
227          // Noop, don't consume the token.
228        } else {
229          ConsumeAnyToken();
230        }
231        return true;
232      }
233    }
234
235    switch (Tok.getKind()) {
236    case tok::eof:
237      // Ran out of tokens.
238      return false;
239
240    case tok::code_completion:
241      if (!StopAtCodeCompletion)
242        ConsumeToken();
243      return false;
244
245    case tok::l_paren:
246      // Recursively skip properly-nested parens.
247      ConsumeParen();
248      SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
249      break;
250    case tok::l_square:
251      // Recursively skip properly-nested square brackets.
252      ConsumeBracket();
253      SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
254      break;
255    case tok::l_brace:
256      // Recursively skip properly-nested braces.
257      ConsumeBrace();
258      SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
259      break;
260
261    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
262    // Since the user wasn't looking for this token (if they were, it would
263    // already be handled), this isn't balanced.  If there is a LHS token at a
264    // higher level, we will assume that this matches the unbalanced token
265    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
266    case tok::r_paren:
267      if (ParenCount && !isFirstTokenSkipped)
268        return false;  // Matches something.
269      ConsumeParen();
270      break;
271    case tok::r_square:
272      if (BracketCount && !isFirstTokenSkipped)
273        return false;  // Matches something.
274      ConsumeBracket();
275      break;
276    case tok::r_brace:
277      if (BraceCount && !isFirstTokenSkipped)
278        return false;  // Matches something.
279      ConsumeBrace();
280      break;
281
282    case tok::string_literal:
283    case tok::wide_string_literal:
284    case tok::utf8_string_literal:
285    case tok::utf16_string_literal:
286    case tok::utf32_string_literal:
287      ConsumeStringToken();
288      break;
289
290    case tok::semi:
291      if (StopAtSemi)
292        return false;
293      // FALL THROUGH.
294    default:
295      // Skip this token.
296      ConsumeToken();
297      break;
298    }
299    isFirstTokenSkipped = false;
300  }
301}
302
303//===----------------------------------------------------------------------===//
304// Scope manipulation
305//===----------------------------------------------------------------------===//
306
307/// EnterScope - Start a new scope.
308void Parser::EnterScope(unsigned ScopeFlags) {
309  if (NumCachedScopes) {
310    Scope *N = ScopeCache[--NumCachedScopes];
311    N->Init(getCurScope(), ScopeFlags);
312    Actions.CurScope = N;
313  } else {
314    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
315  }
316}
317
318/// ExitScope - Pop a scope off the scope stack.
319void Parser::ExitScope() {
320  assert(getCurScope() && "Scope imbalance!");
321
322  // Inform the actions module that this scope is going away if there are any
323  // decls in it.
324  if (!getCurScope()->decl_empty())
325    Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
326
327  Scope *OldScope = getCurScope();
328  Actions.CurScope = OldScope->getParent();
329
330  if (NumCachedScopes == ScopeCacheSize)
331    delete OldScope;
332  else
333    ScopeCache[NumCachedScopes++] = OldScope;
334}
335
336/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
337/// this object does nothing.
338Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
339                                 bool ManageFlags)
340  : CurScope(ManageFlags ? Self->getCurScope() : 0) {
341  if (CurScope) {
342    OldFlags = CurScope->getFlags();
343    CurScope->setFlags(ScopeFlags);
344  }
345}
346
347/// Restore the flags for the current scope to what they were before this
348/// object overrode them.
349Parser::ParseScopeFlags::~ParseScopeFlags() {
350  if (CurScope)
351    CurScope->setFlags(OldFlags);
352}
353
354
355//===----------------------------------------------------------------------===//
356// C99 6.9: External Definitions.
357//===----------------------------------------------------------------------===//
358
359Parser::~Parser() {
360  // If we still have scopes active, delete the scope tree.
361  delete getCurScope();
362  Actions.CurScope = 0;
363
364  // Free the scope cache.
365  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
366    delete ScopeCache[i];
367
368  // Free LateParsedTemplatedFunction nodes.
369  for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
370      it != LateParsedTemplateMap.end(); ++it)
371    delete it->second;
372
373  // Remove the pragma handlers we installed.
374  PP.RemovePragmaHandler(AlignHandler.get());
375  AlignHandler.reset();
376  PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
377  GCCVisibilityHandler.reset();
378  PP.RemovePragmaHandler(OptionsHandler.get());
379  OptionsHandler.reset();
380  PP.RemovePragmaHandler(PackHandler.get());
381  PackHandler.reset();
382  PP.RemovePragmaHandler(MSStructHandler.get());
383  MSStructHandler.reset();
384  PP.RemovePragmaHandler(UnusedHandler.get());
385  UnusedHandler.reset();
386  PP.RemovePragmaHandler(WeakHandler.get());
387  WeakHandler.reset();
388  PP.RemovePragmaHandler(RedefineExtnameHandler.get());
389  RedefineExtnameHandler.reset();
390
391  if (getLangOpts().OpenCL) {
392    PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
393    OpenCLExtensionHandler.reset();
394    PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
395  }
396
397  PP.RemovePragmaHandler("STDC", FPContractHandler.get());
398  FPContractHandler.reset();
399  PP.clearCodeCompletionHandler();
400}
401
402/// Initialize - Warm up the parser.
403///
404void Parser::Initialize() {
405  // Create the translation unit scope.  Install it as the current scope.
406  assert(getCurScope() == 0 && "A scope is already active?");
407  EnterScope(Scope::DeclScope);
408  Actions.ActOnTranslationUnitScope(getCurScope());
409
410  // Prime the lexer look-ahead.
411  ConsumeToken();
412
413  if (Tok.is(tok::eof) &&
414      !getLangOpts().CPlusPlus)  // Empty source file is an extension in C
415    Diag(Tok, diag::ext_empty_source_file);
416
417  // Initialization for Objective-C context sensitive keywords recognition.
418  // Referenced in Parser::ParseObjCTypeQualifierList.
419  if (getLangOpts().ObjC1) {
420    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
421    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
422    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
423    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
424    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
425    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
426  }
427
428  Ident_instancetype = 0;
429  Ident_final = 0;
430  Ident_override = 0;
431
432  Ident_super = &PP.getIdentifierTable().get("super");
433
434  if (getLangOpts().AltiVec) {
435    Ident_vector = &PP.getIdentifierTable().get("vector");
436    Ident_pixel = &PP.getIdentifierTable().get("pixel");
437  }
438
439  Ident_introduced = 0;
440  Ident_deprecated = 0;
441  Ident_obsoleted = 0;
442  Ident_unavailable = 0;
443
444  Ident__except = 0;
445
446  Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
447  Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
448  Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
449
450  if(getLangOpts().Borland) {
451    Ident__exception_info        = PP.getIdentifierInfo("_exception_info");
452    Ident___exception_info       = PP.getIdentifierInfo("__exception_info");
453    Ident_GetExceptionInfo       = PP.getIdentifierInfo("GetExceptionInformation");
454    Ident__exception_code        = PP.getIdentifierInfo("_exception_code");
455    Ident___exception_code       = PP.getIdentifierInfo("__exception_code");
456    Ident_GetExceptionCode       = PP.getIdentifierInfo("GetExceptionCode");
457    Ident__abnormal_termination  = PP.getIdentifierInfo("_abnormal_termination");
458    Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
459    Ident_AbnormalTermination    = PP.getIdentifierInfo("AbnormalTermination");
460
461    PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
462    PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
463    PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
464    PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
465    PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
466    PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
467    PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
468    PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
469    PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
470  }
471}
472
473/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
474/// action tells us to.  This returns true if the EOF was encountered.
475bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
476  DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
477
478  // Skip over the EOF token, flagging end of previous input for incremental
479  // processing
480  if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
481    ConsumeToken();
482
483  while (Tok.is(tok::annot_pragma_unused))
484    HandlePragmaUnused();
485
486  Result = DeclGroupPtrTy();
487  if (Tok.is(tok::eof)) {
488    // Late template parsing can begin.
489    if (getLangOpts().DelayedTemplateParsing)
490      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
491    if (!PP.isIncrementalProcessingEnabled())
492      Actions.ActOnEndOfTranslationUnit();
493    //else don't tell Sema that we ended parsing: more input might come.
494
495    return true;
496  }
497
498  ParsedAttributesWithRange attrs(AttrFactory);
499  MaybeParseCXX0XAttributes(attrs);
500  MaybeParseMicrosoftAttributes(attrs);
501
502  Result = ParseExternalDeclaration(attrs);
503  return false;
504}
505
506/// ParseTranslationUnit:
507///       translation-unit: [C99 6.9]
508///         external-declaration
509///         translation-unit external-declaration
510void Parser::ParseTranslationUnit() {
511  Initialize();
512
513  DeclGroupPtrTy Res;
514  while (!ParseTopLevelDecl(Res))
515    /*parse them all*/;
516
517  ExitScope();
518  assert(getCurScope() == 0 && "Scope imbalance!");
519}
520
521/// ParseExternalDeclaration:
522///
523///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
524///         function-definition
525///         declaration
526/// [C++0x] empty-declaration
527/// [GNU]   asm-definition
528/// [GNU]   __extension__ external-declaration
529/// [OBJC]  objc-class-definition
530/// [OBJC]  objc-class-declaration
531/// [OBJC]  objc-alias-declaration
532/// [OBJC]  objc-protocol-definition
533/// [OBJC]  objc-method-definition
534/// [OBJC]  @end
535/// [C++]   linkage-specification
536/// [GNU] asm-definition:
537///         simple-asm-expr ';'
538///
539/// [C++0x] empty-declaration:
540///           ';'
541///
542/// [C++0x/GNU] 'extern' 'template' declaration
543Parser::DeclGroupPtrTy
544Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
545                                 ParsingDeclSpec *DS) {
546  DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
547  ParenBraceBracketBalancer BalancerRAIIObj(*this);
548
549  if (PP.isCodeCompletionReached()) {
550    cutOffParsing();
551    return DeclGroupPtrTy();
552  }
553
554  Decl *SingleDecl = 0;
555  switch (Tok.getKind()) {
556  case tok::annot_pragma_vis:
557    HandlePragmaVisibility();
558    return DeclGroupPtrTy();
559  case tok::annot_pragma_pack:
560    HandlePragmaPack();
561    return DeclGroupPtrTy();
562  case tok::semi:
563    Diag(Tok, getLangOpts().CPlusPlus0x ?
564         diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)
565      << FixItHint::CreateRemoval(Tok.getLocation());
566
567    ConsumeToken();
568    // TODO: Invoke action for top-level semicolon.
569    return DeclGroupPtrTy();
570  case tok::r_brace:
571    Diag(Tok, diag::err_extraneous_closing_brace);
572    ConsumeBrace();
573    return DeclGroupPtrTy();
574  case tok::eof:
575    Diag(Tok, diag::err_expected_external_declaration);
576    return DeclGroupPtrTy();
577  case tok::kw___extension__: {
578    // __extension__ silences extension warnings in the subexpression.
579    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
580    ConsumeToken();
581    return ParseExternalDeclaration(attrs);
582  }
583  case tok::kw_asm: {
584    ProhibitAttributes(attrs);
585
586    SourceLocation StartLoc = Tok.getLocation();
587    SourceLocation EndLoc;
588    ExprResult Result(ParseSimpleAsm(&EndLoc));
589
590    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
591                     "top-level asm block");
592
593    if (Result.isInvalid())
594      return DeclGroupPtrTy();
595    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
596    break;
597  }
598  case tok::at:
599    return ParseObjCAtDirectives();
600  case tok::minus:
601  case tok::plus:
602    if (!getLangOpts().ObjC1) {
603      Diag(Tok, diag::err_expected_external_declaration);
604      ConsumeToken();
605      return DeclGroupPtrTy();
606    }
607    SingleDecl = ParseObjCMethodDefinition();
608    break;
609  case tok::code_completion:
610      Actions.CodeCompleteOrdinaryName(getCurScope(),
611                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
612                                              : Sema::PCC_Namespace);
613    cutOffParsing();
614    return DeclGroupPtrTy();
615  case tok::kw_using:
616  case tok::kw_namespace:
617  case tok::kw_typedef:
618  case tok::kw_template:
619  case tok::kw_export:    // As in 'export template'
620  case tok::kw_static_assert:
621  case tok::kw__Static_assert:
622    // A function definition cannot start with a these keywords.
623    {
624      SourceLocation DeclEnd;
625      StmtVector Stmts(Actions);
626      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
627    }
628
629  case tok::kw_static:
630    // Parse (then ignore) 'static' prior to a template instantiation. This is
631    // a GCC extension that we intentionally do not support.
632    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
633      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
634        << 0;
635      SourceLocation DeclEnd;
636      StmtVector Stmts(Actions);
637      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
638    }
639    goto dont_know;
640
641  case tok::kw_inline:
642    if (getLangOpts().CPlusPlus) {
643      tok::TokenKind NextKind = NextToken().getKind();
644
645      // Inline namespaces. Allowed as an extension even in C++03.
646      if (NextKind == tok::kw_namespace) {
647        SourceLocation DeclEnd;
648        StmtVector Stmts(Actions);
649        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
650      }
651
652      // Parse (then ignore) 'inline' prior to a template instantiation. This is
653      // a GCC extension that we intentionally do not support.
654      if (NextKind == tok::kw_template) {
655        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
656          << 1;
657        SourceLocation DeclEnd;
658        StmtVector Stmts(Actions);
659        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
660      }
661    }
662    goto dont_know;
663
664  case tok::kw_extern:
665    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
666      // Extern templates
667      SourceLocation ExternLoc = ConsumeToken();
668      SourceLocation TemplateLoc = ConsumeToken();
669      Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
670             diag::warn_cxx98_compat_extern_template :
671             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
672      SourceLocation DeclEnd;
673      return Actions.ConvertDeclToDeclGroup(
674                  ParseExplicitInstantiation(Declarator::FileContext,
675                                             ExternLoc, TemplateLoc, DeclEnd));
676    }
677    // FIXME: Detect C++ linkage specifications here?
678    goto dont_know;
679
680  case tok::kw___if_exists:
681  case tok::kw___if_not_exists:
682    ParseMicrosoftIfExistsExternalDeclaration();
683    return DeclGroupPtrTy();
684
685  default:
686  dont_know:
687    // We can't tell whether this is a function-definition or declaration yet.
688    if (DS) {
689      DS->takeAttributesFrom(attrs);
690      return ParseDeclarationOrFunctionDefinition(*DS);
691    } else {
692      return ParseDeclarationOrFunctionDefinition(attrs);
693    }
694  }
695
696  // This routine returns a DeclGroup, if the thing we parsed only contains a
697  // single decl, convert it now.
698  return Actions.ConvertDeclToDeclGroup(SingleDecl);
699}
700
701/// \brief Determine whether the current token, if it occurs after a
702/// declarator, continues a declaration or declaration list.
703bool Parser::isDeclarationAfterDeclarator() {
704  // Check for '= delete' or '= default'
705  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
706    const Token &KW = NextToken();
707    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
708      return false;
709  }
710
711  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
712    Tok.is(tok::comma) ||           // int X(),  -> not a function def
713    Tok.is(tok::semi)  ||           // int X();  -> not a function def
714    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
715    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
716    (getLangOpts().CPlusPlus &&
717     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
718}
719
720/// \brief Determine whether the current token, if it occurs after a
721/// declarator, indicates the start of a function definition.
722bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
723  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
724  if (Tok.is(tok::l_brace))   // int X() {}
725    return true;
726
727  // Handle K&R C argument lists: int X(f) int f; {}
728  if (!getLangOpts().CPlusPlus &&
729      Declarator.getFunctionTypeInfo().isKNRPrototype())
730    return isDeclarationSpecifier();
731
732  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
733    const Token &KW = NextToken();
734    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
735  }
736
737  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
738         Tok.is(tok::kw_try);          // X() try { ... }
739}
740
741/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
742/// a declaration.  We can't tell which we have until we read up to the
743/// compound-statement in function-definition. TemplateParams, if
744/// non-NULL, provides the template parameters when we're parsing a
745/// C++ template-declaration.
746///
747///       function-definition: [C99 6.9.1]
748///         decl-specs      declarator declaration-list[opt] compound-statement
749/// [C90] function-definition: [C99 6.7.1] - implicit int result
750/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
751///
752///       declaration: [C99 6.7]
753///         declaration-specifiers init-declarator-list[opt] ';'
754/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
755/// [OMP]   threadprivate-directive                              [TODO]
756///
757Parser::DeclGroupPtrTy
758Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
759                                             AccessSpecifier AS) {
760  // Parse the common declaration-specifiers piece.
761  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
762
763  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
764  // declaration-specifiers init-declarator-list[opt] ';'
765  if (Tok.is(tok::semi)) {
766    ConsumeToken();
767    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
768    DS.complete(TheDecl);
769    return Actions.ConvertDeclToDeclGroup(TheDecl);
770  }
771
772  // ObjC2 allows prefix attributes on class interfaces and protocols.
773  // FIXME: This still needs better diagnostics. We should only accept
774  // attributes here, no types, etc.
775  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
776    SourceLocation AtLoc = ConsumeToken(); // the "@"
777    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
778        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
779      Diag(Tok, diag::err_objc_unexpected_attr);
780      SkipUntil(tok::semi); // FIXME: better skip?
781      return DeclGroupPtrTy();
782    }
783
784    DS.abort();
785
786    const char *PrevSpec = 0;
787    unsigned DiagID;
788    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
789      Diag(AtLoc, DiagID) << PrevSpec;
790
791    if (Tok.isObjCAtKeyword(tok::objc_protocol))
792      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
793
794    return Actions.ConvertDeclToDeclGroup(
795            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
796  }
797
798  // If the declspec consisted only of 'extern' and we have a string
799  // literal following it, this must be a C++ linkage specifier like
800  // 'extern "C"'.
801  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
802      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
803      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
804    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
805    return Actions.ConvertDeclToDeclGroup(TheDecl);
806  }
807
808  return ParseDeclGroup(DS, Declarator::FileContext, true);
809}
810
811Parser::DeclGroupPtrTy
812Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
813                                             AccessSpecifier AS) {
814  ParsingDeclSpec DS(*this);
815  DS.takeAttributesFrom(attrs);
816  // Must temporarily exit the objective-c container scope for
817  // parsing c constructs and re-enter objc container scope
818  // afterwards.
819  ObjCDeclContextSwitch ObjCDC(*this);
820
821  return ParseDeclarationOrFunctionDefinition(DS, AS);
822}
823
824/// ParseFunctionDefinition - We parsed and verified that the specified
825/// Declarator is well formed.  If this is a K&R-style function, read the
826/// parameters declaration-list, then start the compound-statement.
827///
828///       function-definition: [C99 6.9.1]
829///         decl-specs      declarator declaration-list[opt] compound-statement
830/// [C90] function-definition: [C99 6.7.1] - implicit int result
831/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
832/// [C++] function-definition: [C++ 8.4]
833///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
834///         function-body
835/// [C++] function-definition: [C++ 8.4]
836///         decl-specifier-seq[opt] declarator function-try-block
837///
838Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
839                                      const ParsedTemplateInfo &TemplateInfo,
840                                      LateParsedAttrList *LateParsedAttrs) {
841  // Poison the SEH identifiers so they are flagged as illegal in function bodies
842  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
843  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
844
845  // If this is C90 and the declspecs were completely missing, fudge in an
846  // implicit int.  We do this here because this is the only place where
847  // declaration-specifiers are completely optional in the grammar.
848  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
849    const char *PrevSpec;
850    unsigned DiagID;
851    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
852                                           D.getIdentifierLoc(),
853                                           PrevSpec, DiagID);
854    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
855  }
856
857  // If this declaration was formed with a K&R-style identifier list for the
858  // arguments, parse declarations for all of the args next.
859  // int foo(a,b) int a; float b; {}
860  if (FTI.isKNRPrototype())
861    ParseKNRParamDeclarations(D);
862
863  // We should have either an opening brace or, in a C++ constructor,
864  // we may have a colon.
865  if (Tok.isNot(tok::l_brace) &&
866      (!getLangOpts().CPlusPlus ||
867       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
868        Tok.isNot(tok::equal)))) {
869    Diag(Tok, diag::err_expected_fn_body);
870
871    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
872    SkipUntil(tok::l_brace, true, true);
873
874    // If we didn't find the '{', bail out.
875    if (Tok.isNot(tok::l_brace))
876      return 0;
877  }
878
879  // Check to make sure that any normal attributes are allowed to be on
880  // a definition.  Late parsed attributes are checked at the end.
881  if (Tok.isNot(tok::equal)) {
882    AttributeList *DtorAttrs = D.getAttributes();
883    while (DtorAttrs) {
884      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
885        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
886          << DtorAttrs->getName()->getName();
887      }
888      DtorAttrs = DtorAttrs->getNext();
889    }
890  }
891
892  // In delayed template parsing mode, for function template we consume the
893  // tokens and store them for late parsing at the end of the translation unit.
894  if (getLangOpts().DelayedTemplateParsing &&
895      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
896    MultiTemplateParamsArg TemplateParameterLists(Actions,
897                                         TemplateInfo.TemplateParams->data(),
898                                         TemplateInfo.TemplateParams->size());
899
900    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
901    Scope *ParentScope = getCurScope()->getParent();
902
903    D.setFunctionDefinitionKind(FDK_Definition);
904    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
905                                        move(TemplateParameterLists));
906    D.complete(DP);
907    D.getMutableDeclSpec().abort();
908
909    if (DP) {
910      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
911
912      FunctionDecl *FnD = 0;
913      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
914        FnD = FunTmpl->getTemplatedDecl();
915      else
916        FnD = cast<FunctionDecl>(DP);
917      Actions.CheckForFunctionRedefinition(FnD);
918
919      LateParsedTemplateMap[FnD] = LPT;
920      Actions.MarkAsLateParsedTemplate(FnD);
921      LexTemplateFunctionForLateParsing(LPT->Toks);
922    } else {
923      CachedTokens Toks;
924      LexTemplateFunctionForLateParsing(Toks);
925    }
926    return DP;
927  }
928
929  // Enter a scope for the function body.
930  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
931
932  // Tell the actions module that we have entered a function definition with the
933  // specified Declarator for the function.
934  Decl *Res = TemplateInfo.TemplateParams?
935      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
936                              MultiTemplateParamsArg(Actions,
937                                          TemplateInfo.TemplateParams->data(),
938                                         TemplateInfo.TemplateParams->size()),
939                                              D)
940    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
941
942  // Break out of the ParsingDeclarator context before we parse the body.
943  D.complete(Res);
944
945  // Break out of the ParsingDeclSpec context, too.  This const_cast is
946  // safe because we're always the sole owner.
947  D.getMutableDeclSpec().abort();
948
949  if (Tok.is(tok::equal)) {
950    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
951    ConsumeToken();
952
953    Actions.ActOnFinishFunctionBody(Res, 0, false);
954
955    bool Delete = false;
956    SourceLocation KWLoc;
957    if (Tok.is(tok::kw_delete)) {
958      Diag(Tok, getLangOpts().CPlusPlus0x ?
959           diag::warn_cxx98_compat_deleted_function :
960           diag::ext_deleted_function);
961
962      KWLoc = ConsumeToken();
963      Actions.SetDeclDeleted(Res, KWLoc);
964      Delete = true;
965    } else if (Tok.is(tok::kw_default)) {
966      Diag(Tok, getLangOpts().CPlusPlus0x ?
967           diag::warn_cxx98_compat_defaulted_function :
968           diag::ext_defaulted_function);
969
970      KWLoc = ConsumeToken();
971      Actions.SetDeclDefaulted(Res, KWLoc);
972    } else {
973      llvm_unreachable("function definition after = not 'delete' or 'default'");
974    }
975
976    if (Tok.is(tok::comma)) {
977      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
978        << Delete;
979      SkipUntil(tok::semi);
980    } else {
981      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
982                       Delete ? "delete" : "default", tok::semi);
983    }
984
985    return Res;
986  }
987
988  if (Tok.is(tok::kw_try))
989    return ParseFunctionTryBlock(Res, BodyScope);
990
991  // If we have a colon, then we're probably parsing a C++
992  // ctor-initializer.
993  if (Tok.is(tok::colon)) {
994    ParseConstructorInitializer(Res);
995
996    // Recover from error.
997    if (!Tok.is(tok::l_brace)) {
998      BodyScope.Exit();
999      Actions.ActOnFinishFunctionBody(Res, 0);
1000      return Res;
1001    }
1002  } else
1003    Actions.ActOnDefaultCtorInitializers(Res);
1004
1005  // Late attributes are parsed in the same scope as the function body.
1006  if (LateParsedAttrs)
1007    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1008
1009  return ParseFunctionStatementBody(Res, BodyScope);
1010}
1011
1012/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
1013/// types for a function with a K&R-style identifier list for arguments.
1014void Parser::ParseKNRParamDeclarations(Declarator &D) {
1015  // We know that the top-level of this declarator is a function.
1016  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1017
1018  // Enter function-declaration scope, limiting any declarators to the
1019  // function prototype scope, including parameter declarators.
1020  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
1021
1022  // Read all the argument declarations.
1023  while (isDeclarationSpecifier()) {
1024    SourceLocation DSStart = Tok.getLocation();
1025
1026    // Parse the common declaration-specifiers piece.
1027    DeclSpec DS(AttrFactory);
1028    ParseDeclarationSpecifiers(DS);
1029
1030    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1031    // least one declarator'.
1032    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
1033    // the declarations though.  It's trivial to ignore them, really hard to do
1034    // anything else with them.
1035    if (Tok.is(tok::semi)) {
1036      Diag(DSStart, diag::err_declaration_does_not_declare_param);
1037      ConsumeToken();
1038      continue;
1039    }
1040
1041    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1042    // than register.
1043    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1044        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1045      Diag(DS.getStorageClassSpecLoc(),
1046           diag::err_invalid_storage_class_in_func_decl);
1047      DS.ClearStorageClassSpecs();
1048    }
1049    if (DS.isThreadSpecified()) {
1050      Diag(DS.getThreadSpecLoc(),
1051           diag::err_invalid_storage_class_in_func_decl);
1052      DS.ClearStorageClassSpecs();
1053    }
1054
1055    // Parse the first declarator attached to this declspec.
1056    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
1057    ParseDeclarator(ParmDeclarator);
1058
1059    // Handle the full declarator list.
1060    while (1) {
1061      // If attributes are present, parse them.
1062      MaybeParseGNUAttributes(ParmDeclarator);
1063
1064      // Ask the actions module to compute the type for this declarator.
1065      Decl *Param =
1066        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
1067
1068      if (Param &&
1069          // A missing identifier has already been diagnosed.
1070          ParmDeclarator.getIdentifier()) {
1071
1072        // Scan the argument list looking for the correct param to apply this
1073        // type.
1074        for (unsigned i = 0; ; ++i) {
1075          // C99 6.9.1p6: those declarators shall declare only identifiers from
1076          // the identifier list.
1077          if (i == FTI.NumArgs) {
1078            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
1079              << ParmDeclarator.getIdentifier();
1080            break;
1081          }
1082
1083          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
1084            // Reject redefinitions of parameters.
1085            if (FTI.ArgInfo[i].Param) {
1086              Diag(ParmDeclarator.getIdentifierLoc(),
1087                   diag::err_param_redefinition)
1088                 << ParmDeclarator.getIdentifier();
1089            } else {
1090              FTI.ArgInfo[i].Param = Param;
1091            }
1092            break;
1093          }
1094        }
1095      }
1096
1097      // If we don't have a comma, it is either the end of the list (a ';') or
1098      // an error, bail out.
1099      if (Tok.isNot(tok::comma))
1100        break;
1101
1102      ParmDeclarator.clear();
1103
1104      // Consume the comma.
1105      ParmDeclarator.setCommaLoc(ConsumeToken());
1106
1107      // Parse the next declarator.
1108      ParseDeclarator(ParmDeclarator);
1109    }
1110
1111    if (Tok.is(tok::semi)) {
1112      ConsumeToken();
1113    } else {
1114      Diag(Tok, diag::err_expected_semi_declaration);
1115      // Skip to end of block or statement
1116      SkipUntil(tok::semi, true);
1117      if (Tok.is(tok::semi))
1118        ConsumeToken();
1119    }
1120  }
1121
1122  // The actions module must verify that all arguments were declared.
1123  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
1124}
1125
1126
1127/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
1128/// allowed to be a wide string, and is not subject to character translation.
1129///
1130/// [GNU] asm-string-literal:
1131///         string-literal
1132///
1133Parser::ExprResult Parser::ParseAsmStringLiteral() {
1134  switch (Tok.getKind()) {
1135    case tok::string_literal:
1136      break;
1137    case tok::utf8_string_literal:
1138    case tok::utf16_string_literal:
1139    case tok::utf32_string_literal:
1140    case tok::wide_string_literal: {
1141      SourceLocation L = Tok.getLocation();
1142      Diag(Tok, diag::err_asm_operand_wide_string_literal)
1143        << (Tok.getKind() == tok::wide_string_literal)
1144        << SourceRange(L, L);
1145      return ExprError();
1146    }
1147    default:
1148      Diag(Tok, diag::err_expected_string_literal);
1149      return ExprError();
1150  }
1151
1152  return ParseStringLiteralExpression();
1153}
1154
1155/// ParseSimpleAsm
1156///
1157/// [GNU] simple-asm-expr:
1158///         'asm' '(' asm-string-literal ')'
1159///
1160Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1161  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1162  SourceLocation Loc = ConsumeToken();
1163
1164  if (Tok.is(tok::kw_volatile)) {
1165    // Remove from the end of 'asm' to the end of 'volatile'.
1166    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1167                             PP.getLocForEndOfToken(Tok.getLocation()));
1168
1169    Diag(Tok, diag::warn_file_asm_volatile)
1170      << FixItHint::CreateRemoval(RemovalRange);
1171    ConsumeToken();
1172  }
1173
1174  BalancedDelimiterTracker T(*this, tok::l_paren);
1175  if (T.consumeOpen()) {
1176    Diag(Tok, diag::err_expected_lparen_after) << "asm";
1177    return ExprError();
1178  }
1179
1180  ExprResult Result(ParseAsmStringLiteral());
1181
1182  if (Result.isInvalid()) {
1183    SkipUntil(tok::r_paren, true, true);
1184    if (EndLoc)
1185      *EndLoc = Tok.getLocation();
1186    ConsumeAnyToken();
1187  } else {
1188    // Close the paren and get the location of the end bracket
1189    T.consumeClose();
1190    if (EndLoc)
1191      *EndLoc = T.getCloseLocation();
1192  }
1193
1194  return move(Result);
1195}
1196
1197/// \brief Get the TemplateIdAnnotation from the token and put it in the
1198/// cleanup pool so that it gets destroyed when parsing the current top level
1199/// declaration is finished.
1200TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
1201  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
1202  TemplateIdAnnotation *
1203      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
1204  TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
1205                                          &TemplateIdAnnotation::Destroy>(Id);
1206  return Id;
1207}
1208
1209/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1210/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1211/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1212/// with a single annotation token representing the typename or C++ scope
1213/// respectively.
1214/// This simplifies handling of C++ scope specifiers and allows efficient
1215/// backtracking without the need to re-parse and resolve nested-names and
1216/// typenames.
1217/// It will mainly be called when we expect to treat identifiers as typenames
1218/// (if they are typenames). For example, in C we do not expect identifiers
1219/// inside expressions to be treated as typenames so it will not be called
1220/// for expressions in C.
1221/// The benefit for C/ObjC is that a typename will be annotated and
1222/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
1223/// will not be called twice, once to check whether we have a declaration
1224/// specifier, and another one to get the actual type inside
1225/// ParseDeclarationSpecifiers).
1226///
1227/// This returns true if an error occurred.
1228///
1229/// Note that this routine emits an error if you call it with ::new or ::delete
1230/// as the current tokens, so only call it in contexts where these are invalid.
1231bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
1232  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
1233          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
1234          || Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1235
1236  if (Tok.is(tok::kw_typename)) {
1237    // Parse a C++ typename-specifier, e.g., "typename T::type".
1238    //
1239    //   typename-specifier:
1240    //     'typename' '::' [opt] nested-name-specifier identifier
1241    //     'typename' '::' [opt] nested-name-specifier template [opt]
1242    //            simple-template-id
1243    SourceLocation TypenameLoc = ConsumeToken();
1244    CXXScopeSpec SS;
1245    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1246                                       /*EnteringContext=*/false,
1247                                       0, /*IsTypename*/true))
1248      return true;
1249    if (!SS.isSet()) {
1250      if (getLangOpts().MicrosoftExt)
1251        Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1252      else
1253        Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1254      return true;
1255    }
1256
1257    TypeResult Ty;
1258    if (Tok.is(tok::identifier)) {
1259      // FIXME: check whether the next token is '<', first!
1260      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1261                                     *Tok.getIdentifierInfo(),
1262                                     Tok.getLocation());
1263    } else if (Tok.is(tok::annot_template_id)) {
1264      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1265      if (TemplateId->Kind == TNK_Function_template) {
1266        Diag(Tok, diag::err_typename_refers_to_non_type_template)
1267          << Tok.getAnnotationRange();
1268        return true;
1269      }
1270
1271      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
1272                                         TemplateId->getTemplateArgs(),
1273                                         TemplateId->NumArgs);
1274
1275      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1276                                     TemplateId->TemplateKWLoc,
1277                                     TemplateId->Template,
1278                                     TemplateId->TemplateNameLoc,
1279                                     TemplateId->LAngleLoc,
1280                                     TemplateArgsPtr,
1281                                     TemplateId->RAngleLoc);
1282    } else {
1283      Diag(Tok, diag::err_expected_type_name_after_typename)
1284        << SS.getRange();
1285      return true;
1286    }
1287
1288    SourceLocation EndLoc = Tok.getLastLoc();
1289    Tok.setKind(tok::annot_typename);
1290    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
1291    Tok.setAnnotationEndLoc(EndLoc);
1292    Tok.setLocation(TypenameLoc);
1293    PP.AnnotateCachedTokens(Tok);
1294    return false;
1295  }
1296
1297  // Remembers whether the token was originally a scope annotation.
1298  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1299
1300  CXXScopeSpec SS;
1301  if (getLangOpts().CPlusPlus)
1302    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1303      return true;
1304
1305  if (Tok.is(tok::identifier)) {
1306    IdentifierInfo *CorrectedII = 0;
1307    // Determine whether the identifier is a type name.
1308    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1309                                            Tok.getLocation(), getCurScope(),
1310                                            &SS, false,
1311                                            NextToken().is(tok::period),
1312                                            ParsedType(),
1313                                            /*IsCtorOrDtorName=*/false,
1314                                            /*NonTrivialTypeSourceInfo*/true,
1315                                            NeedType ? &CorrectedII : NULL)) {
1316      // A FixIt was applied as a result of typo correction
1317      if (CorrectedII)
1318        Tok.setIdentifierInfo(CorrectedII);
1319      // This is a typename. Replace the current token in-place with an
1320      // annotation type token.
1321      Tok.setKind(tok::annot_typename);
1322      setTypeAnnotation(Tok, Ty);
1323      Tok.setAnnotationEndLoc(Tok.getLocation());
1324      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1325        Tok.setLocation(SS.getBeginLoc());
1326
1327      // In case the tokens were cached, have Preprocessor replace
1328      // them with the annotation token.
1329      PP.AnnotateCachedTokens(Tok);
1330      return false;
1331    }
1332
1333    if (!getLangOpts().CPlusPlus) {
1334      // If we're in C, we can't have :: tokens at all (the lexer won't return
1335      // them).  If the identifier is not a type, then it can't be scope either,
1336      // just early exit.
1337      return false;
1338    }
1339
1340    // If this is a template-id, annotate with a template-id or type token.
1341    if (NextToken().is(tok::less)) {
1342      TemplateTy Template;
1343      UnqualifiedId TemplateName;
1344      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1345      bool MemberOfUnknownSpecialization;
1346      if (TemplateNameKind TNK
1347          = Actions.isTemplateName(getCurScope(), SS,
1348                                   /*hasTemplateKeyword=*/false, TemplateName,
1349                                   /*ObjectType=*/ ParsedType(),
1350                                   EnteringContext,
1351                                   Template, MemberOfUnknownSpecialization)) {
1352        // Consume the identifier.
1353        ConsumeToken();
1354        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1355                                    TemplateName)) {
1356          // If an unrecoverable error occurred, we need to return true here,
1357          // because the token stream is in a damaged state.  We may not return
1358          // a valid identifier.
1359          return true;
1360        }
1361      }
1362    }
1363
1364    // The current token, which is either an identifier or a
1365    // template-id, is not part of the annotation. Fall through to
1366    // push that token back into the stream and complete the C++ scope
1367    // specifier annotation.
1368  }
1369
1370  if (Tok.is(tok::annot_template_id)) {
1371    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1372    if (TemplateId->Kind == TNK_Type_template) {
1373      // A template-id that refers to a type was parsed into a
1374      // template-id annotation in a context where we weren't allowed
1375      // to produce a type annotation token. Update the template-id
1376      // annotation token to a type annotation token now.
1377      AnnotateTemplateIdTokenAsType();
1378      return false;
1379    }
1380  }
1381
1382  if (SS.isEmpty())
1383    return false;
1384
1385  // A C++ scope specifier that isn't followed by a typename.
1386  // Push the current token back into the token stream (or revert it if it is
1387  // cached) and use an annotation scope token for current token.
1388  if (PP.isBacktrackEnabled())
1389    PP.RevertCachedTokens(1);
1390  else
1391    PP.EnterToken(Tok);
1392  Tok.setKind(tok::annot_cxxscope);
1393  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
1394  Tok.setAnnotationRange(SS.getRange());
1395
1396  // In case the tokens were cached, have Preprocessor replace them
1397  // with the annotation token.  We don't need to do this if we've
1398  // just reverted back to the state we were in before being called.
1399  if (!wasScopeAnnotation)
1400    PP.AnnotateCachedTokens(Tok);
1401  return false;
1402}
1403
1404/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
1405/// annotates C++ scope specifiers and template-ids.  This returns
1406/// true if the token was annotated or there was an error that could not be
1407/// recovered from.
1408///
1409/// Note that this routine emits an error if you call it with ::new or ::delete
1410/// as the current tokens, so only call it in contexts where these are invalid.
1411bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
1412  assert(getLangOpts().CPlusPlus &&
1413         "Call sites of this function should be guarded by checking for C++");
1414  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1415          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
1416         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1417
1418  CXXScopeSpec SS;
1419  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1420    return true;
1421  if (SS.isEmpty())
1422    return false;
1423
1424  // Push the current token back into the token stream (or revert it if it is
1425  // cached) and use an annotation scope token for current token.
1426  if (PP.isBacktrackEnabled())
1427    PP.RevertCachedTokens(1);
1428  else
1429    PP.EnterToken(Tok);
1430  Tok.setKind(tok::annot_cxxscope);
1431  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
1432  Tok.setAnnotationRange(SS.getRange());
1433
1434  // In case the tokens were cached, have Preprocessor replace them with the
1435  // annotation token.
1436  PP.AnnotateCachedTokens(Tok);
1437  return false;
1438}
1439
1440bool Parser::isTokenEqualOrEqualTypo() {
1441  tok::TokenKind Kind = Tok.getKind();
1442  switch (Kind) {
1443  default:
1444    return false;
1445  case tok::ampequal:            // &=
1446  case tok::starequal:           // *=
1447  case tok::plusequal:           // +=
1448  case tok::minusequal:          // -=
1449  case tok::exclaimequal:        // !=
1450  case tok::slashequal:          // /=
1451  case tok::percentequal:        // %=
1452  case tok::lessequal:           // <=
1453  case tok::lesslessequal:       // <<=
1454  case tok::greaterequal:        // >=
1455  case tok::greatergreaterequal: // >>=
1456  case tok::caretequal:          // ^=
1457  case tok::pipeequal:           // |=
1458  case tok::equalequal:          // ==
1459    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1460      << getTokenSimpleSpelling(Kind)
1461      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1462  case tok::equal:
1463    return true;
1464  }
1465}
1466
1467SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
1468  assert(Tok.is(tok::code_completion));
1469  PrevTokLocation = Tok.getLocation();
1470
1471  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1472    if (S->getFlags() & Scope::FnScope) {
1473      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
1474      cutOffParsing();
1475      return PrevTokLocation;
1476    }
1477
1478    if (S->getFlags() & Scope::ClassScope) {
1479      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
1480      cutOffParsing();
1481      return PrevTokLocation;
1482    }
1483  }
1484
1485  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1486  cutOffParsing();
1487  return PrevTokLocation;
1488}
1489
1490// Anchor the Parser::FieldCallback vtable to this translation unit.
1491// We use a spurious method instead of the destructor because
1492// destroying FieldCallbacks can actually be slightly
1493// performance-sensitive.
1494void Parser::FieldCallback::_anchor() {
1495}
1496
1497// Code-completion pass-through functions
1498
1499void Parser::CodeCompleteDirective(bool InConditional) {
1500  Actions.CodeCompletePreprocessorDirective(InConditional);
1501}
1502
1503void Parser::CodeCompleteInConditionalExclusion() {
1504  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1505}
1506
1507void Parser::CodeCompleteMacroName(bool IsDefinition) {
1508  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1509}
1510
1511void Parser::CodeCompletePreprocessorExpression() {
1512  Actions.CodeCompletePreprocessorExpression();
1513}
1514
1515void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1516                                       MacroInfo *MacroInfo,
1517                                       unsigned ArgumentIndex) {
1518  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1519                                                ArgumentIndex);
1520}
1521
1522void Parser::CodeCompleteNaturalLanguage() {
1523  Actions.CodeCompleteNaturalLanguage();
1524}
1525
1526bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1527  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1528         "Expected '__if_exists' or '__if_not_exists'");
1529  Result.IsIfExists = Tok.is(tok::kw___if_exists);
1530  Result.KeywordLoc = ConsumeToken();
1531
1532  BalancedDelimiterTracker T(*this, tok::l_paren);
1533  if (T.consumeOpen()) {
1534    Diag(Tok, diag::err_expected_lparen_after)
1535      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1536    return true;
1537  }
1538
1539  // Parse nested-name-specifier.
1540  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1541                                 /*EnteringContext=*/false);
1542
1543  // Check nested-name specifier.
1544  if (Result.SS.isInvalid()) {
1545    T.skipToEnd();
1546    return true;
1547  }
1548
1549  // Parse the unqualified-id.
1550  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1551  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1552                         TemplateKWLoc, Result.Name)) {
1553    T.skipToEnd();
1554    return true;
1555  }
1556
1557  if (T.consumeClose())
1558    return true;
1559
1560  // Check if the symbol exists.
1561  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
1562                                               Result.IsIfExists, Result.SS,
1563                                               Result.Name)) {
1564  case Sema::IER_Exists:
1565    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
1566    break;
1567
1568  case Sema::IER_DoesNotExist:
1569    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
1570    break;
1571
1572  case Sema::IER_Dependent:
1573    Result.Behavior = IEB_Dependent;
1574    break;
1575
1576  case Sema::IER_Error:
1577    return true;
1578  }
1579
1580  return false;
1581}
1582
1583void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
1584  IfExistsCondition Result;
1585  if (ParseMicrosoftIfExistsCondition(Result))
1586    return;
1587
1588  BalancedDelimiterTracker Braces(*this, tok::l_brace);
1589  if (Braces.consumeOpen()) {
1590    Diag(Tok, diag::err_expected_lbrace);
1591    return;
1592  }
1593
1594  switch (Result.Behavior) {
1595  case IEB_Parse:
1596    // Parse declarations below.
1597    break;
1598
1599  case IEB_Dependent:
1600    llvm_unreachable("Cannot have a dependent external declaration");
1601
1602  case IEB_Skip:
1603    Braces.skipToEnd();
1604    return;
1605  }
1606
1607  // Parse the declarations.
1608  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1609    ParsedAttributesWithRange attrs(AttrFactory);
1610    MaybeParseCXX0XAttributes(attrs);
1611    MaybeParseMicrosoftAttributes(attrs);
1612    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1613    if (Result && !getCurScope()->getParent())
1614      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
1615  }
1616  Braces.consumeClose();
1617}
1618
1619Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
1620  assert(Tok.isObjCAtKeyword(tok::objc___experimental_modules_import) &&
1621         "Improper start to module import");
1622  SourceLocation ImportLoc = ConsumeToken();
1623
1624  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1625
1626  // Parse the module path.
1627  do {
1628    if (!Tok.is(tok::identifier)) {
1629      if (Tok.is(tok::code_completion)) {
1630        Actions.CodeCompleteModuleImport(ImportLoc, Path);
1631        ConsumeCodeCompletionToken();
1632        SkipUntil(tok::semi);
1633        return DeclGroupPtrTy();
1634      }
1635
1636      Diag(Tok, diag::err_module_expected_ident);
1637      SkipUntil(tok::semi);
1638      return DeclGroupPtrTy();
1639    }
1640
1641    // Record this part of the module path.
1642    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
1643    ConsumeToken();
1644
1645    if (Tok.is(tok::period)) {
1646      ConsumeToken();
1647      continue;
1648    }
1649
1650    break;
1651  } while (true);
1652
1653  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
1654  ExpectAndConsumeSemi(diag::err_module_expected_semi);
1655  if (Import.isInvalid())
1656    return DeclGroupPtrTy();
1657
1658  return Actions.ConvertDeclToDeclGroup(Import.get());
1659}
1660
1661bool Parser::BalancedDelimiterTracker::diagnoseOverflow() {
1662  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
1663  P.SkipUntil(tok::eof);
1664  return true;
1665}
1666
1667bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
1668                                            const char *Msg,
1669                                            tok::TokenKind SkipToToc ) {
1670  LOpen = P.Tok.getLocation();
1671  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1672    return true;
1673
1674  if (getDepth() < MaxDepth)
1675    return false;
1676
1677  return diagnoseOverflow();
1678}
1679
1680bool Parser::BalancedDelimiterTracker::diagnoseMissingClose() {
1681  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1682
1683  const char *LHSName = "unknown";
1684  diag::kind DID;
1685  switch (Close) {
1686  default: llvm_unreachable("Unexpected balanced token");
1687  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1688  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1689  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
1690  }
1691  P.Diag(P.Tok, DID);
1692  P.Diag(LOpen, diag::note_matching) << LHSName;
1693  if (P.SkipUntil(Close))
1694    LClose = P.Tok.getLocation();
1695  return true;
1696}
1697
1698void Parser::BalancedDelimiterTracker::skipToEnd() {
1699  P.SkipUntil(Close, false);
1700}
1701