Parser.cpp revision 4e4d08403ca5cfd4d558fa2936215d3a4e5a528d
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  while (Tok.is(tok::annot_pragma_unused))
479    HandlePragmaUnused();
480
481  Result = DeclGroupPtrTy();
482  if (Tok.is(tok::eof)) {
483    // Late template parsing can begin.
484    if (getLangOpts().DelayedTemplateParsing)
485      Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
486
487    Actions.ActOnEndOfTranslationUnit();
488    return true;
489  }
490
491  ParsedAttributesWithRange attrs(AttrFactory);
492  MaybeParseCXX0XAttributes(attrs);
493  MaybeParseMicrosoftAttributes(attrs);
494
495  Result = ParseExternalDeclaration(attrs);
496  return false;
497}
498
499/// ParseTranslationUnit:
500///       translation-unit: [C99 6.9]
501///         external-declaration
502///         translation-unit external-declaration
503void Parser::ParseTranslationUnit() {
504  Initialize();
505
506  DeclGroupPtrTy Res;
507  while (!ParseTopLevelDecl(Res))
508    /*parse them all*/;
509
510  ExitScope();
511  assert(getCurScope() == 0 && "Scope imbalance!");
512}
513
514/// ParseExternalDeclaration:
515///
516///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
517///         function-definition
518///         declaration
519/// [C++0x] empty-declaration
520/// [GNU]   asm-definition
521/// [GNU]   __extension__ external-declaration
522/// [OBJC]  objc-class-definition
523/// [OBJC]  objc-class-declaration
524/// [OBJC]  objc-alias-declaration
525/// [OBJC]  objc-protocol-definition
526/// [OBJC]  objc-method-definition
527/// [OBJC]  @end
528/// [C++]   linkage-specification
529/// [GNU] asm-definition:
530///         simple-asm-expr ';'
531///
532/// [C++0x] empty-declaration:
533///           ';'
534///
535/// [C++0x/GNU] 'extern' 'template' declaration
536Parser::DeclGroupPtrTy
537Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
538                                 ParsingDeclSpec *DS) {
539  DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
540  ParenBraceBracketBalancer BalancerRAIIObj(*this);
541
542  if (PP.isCodeCompletionReached()) {
543    cutOffParsing();
544    return DeclGroupPtrTy();
545  }
546
547  Decl *SingleDecl = 0;
548  switch (Tok.getKind()) {
549  case tok::annot_pragma_vis:
550    HandlePragmaVisibility();
551    return DeclGroupPtrTy();
552  case tok::annot_pragma_pack:
553    HandlePragmaPack();
554    return DeclGroupPtrTy();
555  case tok::semi:
556    Diag(Tok, getLangOpts().CPlusPlus0x ?
557         diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)
558      << FixItHint::CreateRemoval(Tok.getLocation());
559
560    ConsumeToken();
561    // TODO: Invoke action for top-level semicolon.
562    return DeclGroupPtrTy();
563  case tok::r_brace:
564    Diag(Tok, diag::err_extraneous_closing_brace);
565    ConsumeBrace();
566    return DeclGroupPtrTy();
567  case tok::eof:
568    Diag(Tok, diag::err_expected_external_declaration);
569    return DeclGroupPtrTy();
570  case tok::kw___extension__: {
571    // __extension__ silences extension warnings in the subexpression.
572    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
573    ConsumeToken();
574    return ParseExternalDeclaration(attrs);
575  }
576  case tok::kw_asm: {
577    ProhibitAttributes(attrs);
578
579    SourceLocation StartLoc = Tok.getLocation();
580    SourceLocation EndLoc;
581    ExprResult Result(ParseSimpleAsm(&EndLoc));
582
583    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
584                     "top-level asm block");
585
586    if (Result.isInvalid())
587      return DeclGroupPtrTy();
588    SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
589    break;
590  }
591  case tok::at:
592    return ParseObjCAtDirectives();
593  case tok::minus:
594  case tok::plus:
595    if (!getLangOpts().ObjC1) {
596      Diag(Tok, diag::err_expected_external_declaration);
597      ConsumeToken();
598      return DeclGroupPtrTy();
599    }
600    SingleDecl = ParseObjCMethodDefinition();
601    break;
602  case tok::code_completion:
603      Actions.CodeCompleteOrdinaryName(getCurScope(),
604                             CurParsedObjCImpl? Sema::PCC_ObjCImplementation
605                                              : Sema::PCC_Namespace);
606    cutOffParsing();
607    return DeclGroupPtrTy();
608  case tok::kw_using:
609  case tok::kw_namespace:
610  case tok::kw_typedef:
611  case tok::kw_template:
612  case tok::kw_export:    // As in 'export template'
613  case tok::kw_static_assert:
614  case tok::kw__Static_assert:
615    // A function definition cannot start with a these keywords.
616    {
617      SourceLocation DeclEnd;
618      StmtVector Stmts(Actions);
619      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
620    }
621
622  case tok::kw_static:
623    // Parse (then ignore) 'static' prior to a template instantiation. This is
624    // a GCC extension that we intentionally do not support.
625    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
626      Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
627        << 0;
628      SourceLocation DeclEnd;
629      StmtVector Stmts(Actions);
630      return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
631    }
632    goto dont_know;
633
634  case tok::kw_inline:
635    if (getLangOpts().CPlusPlus) {
636      tok::TokenKind NextKind = NextToken().getKind();
637
638      // Inline namespaces. Allowed as an extension even in C++03.
639      if (NextKind == tok::kw_namespace) {
640        SourceLocation DeclEnd;
641        StmtVector Stmts(Actions);
642        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
643      }
644
645      // Parse (then ignore) 'inline' prior to a template instantiation. This is
646      // a GCC extension that we intentionally do not support.
647      if (NextKind == tok::kw_template) {
648        Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
649          << 1;
650        SourceLocation DeclEnd;
651        StmtVector Stmts(Actions);
652        return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
653      }
654    }
655    goto dont_know;
656
657  case tok::kw_extern:
658    if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
659      // Extern templates
660      SourceLocation ExternLoc = ConsumeToken();
661      SourceLocation TemplateLoc = ConsumeToken();
662      Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
663             diag::warn_cxx98_compat_extern_template :
664             diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
665      SourceLocation DeclEnd;
666      return Actions.ConvertDeclToDeclGroup(
667                  ParseExplicitInstantiation(Declarator::FileContext,
668                                             ExternLoc, TemplateLoc, DeclEnd));
669    }
670    // FIXME: Detect C++ linkage specifications here?
671    goto dont_know;
672
673  case tok::kw___if_exists:
674  case tok::kw___if_not_exists:
675    ParseMicrosoftIfExistsExternalDeclaration();
676    return DeclGroupPtrTy();
677
678  default:
679  dont_know:
680    // We can't tell whether this is a function-definition or declaration yet.
681    if (DS) {
682      DS->takeAttributesFrom(attrs);
683      return ParseDeclarationOrFunctionDefinition(*DS);
684    } else {
685      return ParseDeclarationOrFunctionDefinition(attrs);
686    }
687  }
688
689  // This routine returns a DeclGroup, if the thing we parsed only contains a
690  // single decl, convert it now.
691  return Actions.ConvertDeclToDeclGroup(SingleDecl);
692}
693
694/// \brief Determine whether the current token, if it occurs after a
695/// declarator, continues a declaration or declaration list.
696bool Parser::isDeclarationAfterDeclarator() {
697  // Check for '= delete' or '= default'
698  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
699    const Token &KW = NextToken();
700    if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
701      return false;
702  }
703
704  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
705    Tok.is(tok::comma) ||           // int X(),  -> not a function def
706    Tok.is(tok::semi)  ||           // int X();  -> not a function def
707    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
708    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
709    (getLangOpts().CPlusPlus &&
710     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
711}
712
713/// \brief Determine whether the current token, if it occurs after a
714/// declarator, indicates the start of a function definition.
715bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
716  assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
717  if (Tok.is(tok::l_brace))   // int X() {}
718    return true;
719
720  // Handle K&R C argument lists: int X(f) int f; {}
721  if (!getLangOpts().CPlusPlus &&
722      Declarator.getFunctionTypeInfo().isKNRPrototype())
723    return isDeclarationSpecifier();
724
725  if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
726    const Token &KW = NextToken();
727    return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
728  }
729
730  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
731         Tok.is(tok::kw_try);          // X() try { ... }
732}
733
734/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
735/// a declaration.  We can't tell which we have until we read up to the
736/// compound-statement in function-definition. TemplateParams, if
737/// non-NULL, provides the template parameters when we're parsing a
738/// C++ template-declaration.
739///
740///       function-definition: [C99 6.9.1]
741///         decl-specs      declarator declaration-list[opt] compound-statement
742/// [C90] function-definition: [C99 6.7.1] - implicit int result
743/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
744///
745///       declaration: [C99 6.7]
746///         declaration-specifiers init-declarator-list[opt] ';'
747/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
748/// [OMP]   threadprivate-directive                              [TODO]
749///
750Parser::DeclGroupPtrTy
751Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
752                                             AccessSpecifier AS) {
753  // Parse the common declaration-specifiers piece.
754  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
755
756  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
757  // declaration-specifiers init-declarator-list[opt] ';'
758  if (Tok.is(tok::semi)) {
759    ConsumeToken();
760    Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
761    DS.complete(TheDecl);
762    return Actions.ConvertDeclToDeclGroup(TheDecl);
763  }
764
765  // ObjC2 allows prefix attributes on class interfaces and protocols.
766  // FIXME: This still needs better diagnostics. We should only accept
767  // attributes here, no types, etc.
768  if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
769    SourceLocation AtLoc = ConsumeToken(); // the "@"
770    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
771        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
772      Diag(Tok, diag::err_objc_unexpected_attr);
773      SkipUntil(tok::semi); // FIXME: better skip?
774      return DeclGroupPtrTy();
775    }
776
777    DS.abort();
778
779    const char *PrevSpec = 0;
780    unsigned DiagID;
781    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
782      Diag(AtLoc, DiagID) << PrevSpec;
783
784    if (Tok.isObjCAtKeyword(tok::objc_protocol))
785      return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
786
787    return Actions.ConvertDeclToDeclGroup(
788            ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
789  }
790
791  // If the declspec consisted only of 'extern' and we have a string
792  // literal following it, this must be a C++ linkage specifier like
793  // 'extern "C"'.
794  if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
795      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
796      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
797    Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
798    return Actions.ConvertDeclToDeclGroup(TheDecl);
799  }
800
801  return ParseDeclGroup(DS, Declarator::FileContext, true);
802}
803
804Parser::DeclGroupPtrTy
805Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
806                                             AccessSpecifier AS) {
807  ParsingDeclSpec DS(*this);
808  DS.takeAttributesFrom(attrs);
809  // Must temporarily exit the objective-c container scope for
810  // parsing c constructs and re-enter objc container scope
811  // afterwards.
812  ObjCDeclContextSwitch ObjCDC(*this);
813
814  return ParseDeclarationOrFunctionDefinition(DS, AS);
815}
816
817/// ParseFunctionDefinition - We parsed and verified that the specified
818/// Declarator is well formed.  If this is a K&R-style function, read the
819/// parameters declaration-list, then start the compound-statement.
820///
821///       function-definition: [C99 6.9.1]
822///         decl-specs      declarator declaration-list[opt] compound-statement
823/// [C90] function-definition: [C99 6.7.1] - implicit int result
824/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
825/// [C++] function-definition: [C++ 8.4]
826///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
827///         function-body
828/// [C++] function-definition: [C++ 8.4]
829///         decl-specifier-seq[opt] declarator function-try-block
830///
831Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
832                                      const ParsedTemplateInfo &TemplateInfo,
833                                      LateParsedAttrList *LateParsedAttrs) {
834  // Poison the SEH identifiers so they are flagged as illegal in function bodies
835  PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
836  const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
837
838  // If this is C90 and the declspecs were completely missing, fudge in an
839  // implicit int.  We do this here because this is the only place where
840  // declaration-specifiers are completely optional in the grammar.
841  if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
842    const char *PrevSpec;
843    unsigned DiagID;
844    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
845                                           D.getIdentifierLoc(),
846                                           PrevSpec, DiagID);
847    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
848  }
849
850  // If this declaration was formed with a K&R-style identifier list for the
851  // arguments, parse declarations for all of the args next.
852  // int foo(a,b) int a; float b; {}
853  if (FTI.isKNRPrototype())
854    ParseKNRParamDeclarations(D);
855
856  // We should have either an opening brace or, in a C++ constructor,
857  // we may have a colon.
858  if (Tok.isNot(tok::l_brace) &&
859      (!getLangOpts().CPlusPlus ||
860       (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
861        Tok.isNot(tok::equal)))) {
862    Diag(Tok, diag::err_expected_fn_body);
863
864    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
865    SkipUntil(tok::l_brace, true, true);
866
867    // If we didn't find the '{', bail out.
868    if (Tok.isNot(tok::l_brace))
869      return 0;
870  }
871
872  // Check to make sure that any normal attributes are allowed to be on
873  // a definition.  Late parsed attributes are checked at the end.
874  if (Tok.isNot(tok::equal)) {
875    AttributeList *DtorAttrs = D.getAttributes();
876    while (DtorAttrs) {
877      if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
878        Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
879          << DtorAttrs->getName()->getName();
880      }
881      DtorAttrs = DtorAttrs->getNext();
882    }
883  }
884
885  // In delayed template parsing mode, for function template we consume the
886  // tokens and store them for late parsing at the end of the translation unit.
887  if (getLangOpts().DelayedTemplateParsing &&
888      TemplateInfo.Kind == ParsedTemplateInfo::Template) {
889    MultiTemplateParamsArg TemplateParameterLists(Actions,
890                                         TemplateInfo.TemplateParams->data(),
891                                         TemplateInfo.TemplateParams->size());
892
893    ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
894    Scope *ParentScope = getCurScope()->getParent();
895
896    D.setFunctionDefinitionKind(FDK_Definition);
897    Decl *DP = Actions.HandleDeclarator(ParentScope, D,
898                                        move(TemplateParameterLists));
899    D.complete(DP);
900    D.getMutableDeclSpec().abort();
901
902    if (DP) {
903      LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
904
905      FunctionDecl *FnD = 0;
906      if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
907        FnD = FunTmpl->getTemplatedDecl();
908      else
909        FnD = cast<FunctionDecl>(DP);
910      Actions.CheckForFunctionRedefinition(FnD);
911
912      LateParsedTemplateMap[FnD] = LPT;
913      Actions.MarkAsLateParsedTemplate(FnD);
914      LexTemplateFunctionForLateParsing(LPT->Toks);
915    } else {
916      CachedTokens Toks;
917      LexTemplateFunctionForLateParsing(Toks);
918    }
919    return DP;
920  }
921
922  // Enter a scope for the function body.
923  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
924
925  // Tell the actions module that we have entered a function definition with the
926  // specified Declarator for the function.
927  Decl *Res = TemplateInfo.TemplateParams?
928      Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
929                              MultiTemplateParamsArg(Actions,
930                                          TemplateInfo.TemplateParams->data(),
931                                         TemplateInfo.TemplateParams->size()),
932                                              D)
933    : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
934
935  // Break out of the ParsingDeclarator context before we parse the body.
936  D.complete(Res);
937
938  // Break out of the ParsingDeclSpec context, too.  This const_cast is
939  // safe because we're always the sole owner.
940  D.getMutableDeclSpec().abort();
941
942  if (Tok.is(tok::equal)) {
943    assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
944    ConsumeToken();
945
946    Actions.ActOnFinishFunctionBody(Res, 0, false);
947
948    bool Delete = false;
949    SourceLocation KWLoc;
950    if (Tok.is(tok::kw_delete)) {
951      Diag(Tok, getLangOpts().CPlusPlus0x ?
952           diag::warn_cxx98_compat_deleted_function :
953           diag::ext_deleted_function);
954
955      KWLoc = ConsumeToken();
956      Actions.SetDeclDeleted(Res, KWLoc);
957      Delete = true;
958    } else if (Tok.is(tok::kw_default)) {
959      Diag(Tok, getLangOpts().CPlusPlus0x ?
960           diag::warn_cxx98_compat_defaulted_function :
961           diag::ext_defaulted_function);
962
963      KWLoc = ConsumeToken();
964      Actions.SetDeclDefaulted(Res, KWLoc);
965    } else {
966      llvm_unreachable("function definition after = not 'delete' or 'default'");
967    }
968
969    if (Tok.is(tok::comma)) {
970      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
971        << Delete;
972      SkipUntil(tok::semi);
973    } else {
974      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
975                       Delete ? "delete" : "default", tok::semi);
976    }
977
978    return Res;
979  }
980
981  if (Tok.is(tok::kw_try))
982    return ParseFunctionTryBlock(Res, BodyScope);
983
984  // If we have a colon, then we're probably parsing a C++
985  // ctor-initializer.
986  if (Tok.is(tok::colon)) {
987    ParseConstructorInitializer(Res);
988
989    // Recover from error.
990    if (!Tok.is(tok::l_brace)) {
991      BodyScope.Exit();
992      Actions.ActOnFinishFunctionBody(Res, 0);
993      return Res;
994    }
995  } else
996    Actions.ActOnDefaultCtorInitializers(Res);
997
998  // Late attributes are parsed in the same scope as the function body.
999  if (LateParsedAttrs)
1000    ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1001
1002  return ParseFunctionStatementBody(Res, BodyScope);
1003}
1004
1005/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
1006/// types for a function with a K&R-style identifier list for arguments.
1007void Parser::ParseKNRParamDeclarations(Declarator &D) {
1008  // We know that the top-level of this declarator is a function.
1009  DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1010
1011  // Enter function-declaration scope, limiting any declarators to the
1012  // function prototype scope, including parameter declarators.
1013  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
1014
1015  // Read all the argument declarations.
1016  while (isDeclarationSpecifier()) {
1017    SourceLocation DSStart = Tok.getLocation();
1018
1019    // Parse the common declaration-specifiers piece.
1020    DeclSpec DS(AttrFactory);
1021    ParseDeclarationSpecifiers(DS);
1022
1023    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1024    // least one declarator'.
1025    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
1026    // the declarations though.  It's trivial to ignore them, really hard to do
1027    // anything else with them.
1028    if (Tok.is(tok::semi)) {
1029      Diag(DSStart, diag::err_declaration_does_not_declare_param);
1030      ConsumeToken();
1031      continue;
1032    }
1033
1034    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1035    // than register.
1036    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1037        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1038      Diag(DS.getStorageClassSpecLoc(),
1039           diag::err_invalid_storage_class_in_func_decl);
1040      DS.ClearStorageClassSpecs();
1041    }
1042    if (DS.isThreadSpecified()) {
1043      Diag(DS.getThreadSpecLoc(),
1044           diag::err_invalid_storage_class_in_func_decl);
1045      DS.ClearStorageClassSpecs();
1046    }
1047
1048    // Parse the first declarator attached to this declspec.
1049    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
1050    ParseDeclarator(ParmDeclarator);
1051
1052    // Handle the full declarator list.
1053    while (1) {
1054      // If attributes are present, parse them.
1055      MaybeParseGNUAttributes(ParmDeclarator);
1056
1057      // Ask the actions module to compute the type for this declarator.
1058      Decl *Param =
1059        Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
1060
1061      if (Param &&
1062          // A missing identifier has already been diagnosed.
1063          ParmDeclarator.getIdentifier()) {
1064
1065        // Scan the argument list looking for the correct param to apply this
1066        // type.
1067        for (unsigned i = 0; ; ++i) {
1068          // C99 6.9.1p6: those declarators shall declare only identifiers from
1069          // the identifier list.
1070          if (i == FTI.NumArgs) {
1071            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
1072              << ParmDeclarator.getIdentifier();
1073            break;
1074          }
1075
1076          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
1077            // Reject redefinitions of parameters.
1078            if (FTI.ArgInfo[i].Param) {
1079              Diag(ParmDeclarator.getIdentifierLoc(),
1080                   diag::err_param_redefinition)
1081                 << ParmDeclarator.getIdentifier();
1082            } else {
1083              FTI.ArgInfo[i].Param = Param;
1084            }
1085            break;
1086          }
1087        }
1088      }
1089
1090      // If we don't have a comma, it is either the end of the list (a ';') or
1091      // an error, bail out.
1092      if (Tok.isNot(tok::comma))
1093        break;
1094
1095      ParmDeclarator.clear();
1096
1097      // Consume the comma.
1098      ParmDeclarator.setCommaLoc(ConsumeToken());
1099
1100      // Parse the next declarator.
1101      ParseDeclarator(ParmDeclarator);
1102    }
1103
1104    if (Tok.is(tok::semi)) {
1105      ConsumeToken();
1106    } else {
1107      Diag(Tok, diag::err_parse_error);
1108      // Skip to end of block or statement
1109      SkipUntil(tok::semi, true);
1110      if (Tok.is(tok::semi))
1111        ConsumeToken();
1112    }
1113  }
1114
1115  // The actions module must verify that all arguments were declared.
1116  Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
1117}
1118
1119
1120/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
1121/// allowed to be a wide string, and is not subject to character translation.
1122///
1123/// [GNU] asm-string-literal:
1124///         string-literal
1125///
1126Parser::ExprResult Parser::ParseAsmStringLiteral() {
1127  switch (Tok.getKind()) {
1128    case tok::string_literal:
1129      break;
1130    case tok::utf8_string_literal:
1131    case tok::utf16_string_literal:
1132    case tok::utf32_string_literal:
1133    case tok::wide_string_literal: {
1134      SourceLocation L = Tok.getLocation();
1135      Diag(Tok, diag::err_asm_operand_wide_string_literal)
1136        << (Tok.getKind() == tok::wide_string_literal)
1137        << SourceRange(L, L);
1138      return ExprError();
1139    }
1140    default:
1141      Diag(Tok, diag::err_expected_string_literal);
1142      return ExprError();
1143  }
1144
1145  return ParseStringLiteralExpression();
1146}
1147
1148/// ParseSimpleAsm
1149///
1150/// [GNU] simple-asm-expr:
1151///         'asm' '(' asm-string-literal ')'
1152///
1153Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
1154  assert(Tok.is(tok::kw_asm) && "Not an asm!");
1155  SourceLocation Loc = ConsumeToken();
1156
1157  if (Tok.is(tok::kw_volatile)) {
1158    // Remove from the end of 'asm' to the end of 'volatile'.
1159    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1160                             PP.getLocForEndOfToken(Tok.getLocation()));
1161
1162    Diag(Tok, diag::warn_file_asm_volatile)
1163      << FixItHint::CreateRemoval(RemovalRange);
1164    ConsumeToken();
1165  }
1166
1167  BalancedDelimiterTracker T(*this, tok::l_paren);
1168  if (T.consumeOpen()) {
1169    Diag(Tok, diag::err_expected_lparen_after) << "asm";
1170    return ExprError();
1171  }
1172
1173  ExprResult Result(ParseAsmStringLiteral());
1174
1175  if (Result.isInvalid()) {
1176    SkipUntil(tok::r_paren, true, true);
1177    if (EndLoc)
1178      *EndLoc = Tok.getLocation();
1179    ConsumeAnyToken();
1180  } else {
1181    // Close the paren and get the location of the end bracket
1182    T.consumeClose();
1183    if (EndLoc)
1184      *EndLoc = T.getCloseLocation();
1185  }
1186
1187  return move(Result);
1188}
1189
1190/// \brief Get the TemplateIdAnnotation from the token and put it in the
1191/// cleanup pool so that it gets destroyed when parsing the current top level
1192/// declaration is finished.
1193TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
1194  assert(tok.is(tok::annot_template_id) && "Expected template-id token");
1195  TemplateIdAnnotation *
1196      Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
1197  TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
1198                                          &TemplateIdAnnotation::Destroy>(Id);
1199  return Id;
1200}
1201
1202/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1203/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1204/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1205/// with a single annotation token representing the typename or C++ scope
1206/// respectively.
1207/// This simplifies handling of C++ scope specifiers and allows efficient
1208/// backtracking without the need to re-parse and resolve nested-names and
1209/// typenames.
1210/// It will mainly be called when we expect to treat identifiers as typenames
1211/// (if they are typenames). For example, in C we do not expect identifiers
1212/// inside expressions to be treated as typenames so it will not be called
1213/// for expressions in C.
1214/// The benefit for C/ObjC is that a typename will be annotated and
1215/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
1216/// will not be called twice, once to check whether we have a declaration
1217/// specifier, and another one to get the actual type inside
1218/// ParseDeclarationSpecifiers).
1219///
1220/// This returns true if an error occurred.
1221///
1222/// Note that this routine emits an error if you call it with ::new or ::delete
1223/// as the current tokens, so only call it in contexts where these are invalid.
1224bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
1225  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
1226          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
1227          || Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1228
1229  if (Tok.is(tok::kw_typename)) {
1230    // Parse a C++ typename-specifier, e.g., "typename T::type".
1231    //
1232    //   typename-specifier:
1233    //     'typename' '::' [opt] nested-name-specifier identifier
1234    //     'typename' '::' [opt] nested-name-specifier template [opt]
1235    //            simple-template-id
1236    SourceLocation TypenameLoc = ConsumeToken();
1237    CXXScopeSpec SS;
1238    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1239                                       /*EnteringContext=*/false,
1240                                       0, /*IsTypename*/true))
1241      return true;
1242    if (!SS.isSet()) {
1243      if (getLangOpts().MicrosoftExt)
1244        Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1245      else
1246        Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1247      return true;
1248    }
1249
1250    TypeResult Ty;
1251    if (Tok.is(tok::identifier)) {
1252      // FIXME: check whether the next token is '<', first!
1253      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1254                                     *Tok.getIdentifierInfo(),
1255                                     Tok.getLocation());
1256    } else if (Tok.is(tok::annot_template_id)) {
1257      TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1258      if (TemplateId->Kind == TNK_Function_template) {
1259        Diag(Tok, diag::err_typename_refers_to_non_type_template)
1260          << Tok.getAnnotationRange();
1261        return true;
1262      }
1263
1264      ASTTemplateArgsPtr TemplateArgsPtr(Actions,
1265                                         TemplateId->getTemplateArgs(),
1266                                         TemplateId->NumArgs);
1267
1268      Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1269                                     TemplateId->TemplateKWLoc,
1270                                     TemplateId->Template,
1271                                     TemplateId->TemplateNameLoc,
1272                                     TemplateId->LAngleLoc,
1273                                     TemplateArgsPtr,
1274                                     TemplateId->RAngleLoc);
1275    } else {
1276      Diag(Tok, diag::err_expected_type_name_after_typename)
1277        << SS.getRange();
1278      return true;
1279    }
1280
1281    SourceLocation EndLoc = Tok.getLastLoc();
1282    Tok.setKind(tok::annot_typename);
1283    setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
1284    Tok.setAnnotationEndLoc(EndLoc);
1285    Tok.setLocation(TypenameLoc);
1286    PP.AnnotateCachedTokens(Tok);
1287    return false;
1288  }
1289
1290  // Remembers whether the token was originally a scope annotation.
1291  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1292
1293  CXXScopeSpec SS;
1294  if (getLangOpts().CPlusPlus)
1295    if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1296      return true;
1297
1298  if (Tok.is(tok::identifier)) {
1299    IdentifierInfo *CorrectedII = 0;
1300    // Determine whether the identifier is a type name.
1301    if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1302                                            Tok.getLocation(), getCurScope(),
1303                                            &SS, false,
1304                                            NextToken().is(tok::period),
1305                                            ParsedType(),
1306                                            /*IsCtorOrDtorName=*/false,
1307                                            /*NonTrivialTypeSourceInfo*/true,
1308                                            NeedType ? &CorrectedII : NULL)) {
1309      // A FixIt was applied as a result of typo correction
1310      if (CorrectedII)
1311        Tok.setIdentifierInfo(CorrectedII);
1312      // This is a typename. Replace the current token in-place with an
1313      // annotation type token.
1314      Tok.setKind(tok::annot_typename);
1315      setTypeAnnotation(Tok, Ty);
1316      Tok.setAnnotationEndLoc(Tok.getLocation());
1317      if (SS.isNotEmpty()) // it was a C++ qualified type name.
1318        Tok.setLocation(SS.getBeginLoc());
1319
1320      // In case the tokens were cached, have Preprocessor replace
1321      // them with the annotation token.
1322      PP.AnnotateCachedTokens(Tok);
1323      return false;
1324    }
1325
1326    if (!getLangOpts().CPlusPlus) {
1327      // If we're in C, we can't have :: tokens at all (the lexer won't return
1328      // them).  If the identifier is not a type, then it can't be scope either,
1329      // just early exit.
1330      return false;
1331    }
1332
1333    // If this is a template-id, annotate with a template-id or type token.
1334    if (NextToken().is(tok::less)) {
1335      TemplateTy Template;
1336      UnqualifiedId TemplateName;
1337      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1338      bool MemberOfUnknownSpecialization;
1339      if (TemplateNameKind TNK
1340          = Actions.isTemplateName(getCurScope(), SS,
1341                                   /*hasTemplateKeyword=*/false, TemplateName,
1342                                   /*ObjectType=*/ ParsedType(),
1343                                   EnteringContext,
1344                                   Template, MemberOfUnknownSpecialization)) {
1345        // Consume the identifier.
1346        ConsumeToken();
1347        if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1348                                    TemplateName)) {
1349          // If an unrecoverable error occurred, we need to return true here,
1350          // because the token stream is in a damaged state.  We may not return
1351          // a valid identifier.
1352          return true;
1353        }
1354      }
1355    }
1356
1357    // The current token, which is either an identifier or a
1358    // template-id, is not part of the annotation. Fall through to
1359    // push that token back into the stream and complete the C++ scope
1360    // specifier annotation.
1361  }
1362
1363  if (Tok.is(tok::annot_template_id)) {
1364    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1365    if (TemplateId->Kind == TNK_Type_template) {
1366      // A template-id that refers to a type was parsed into a
1367      // template-id annotation in a context where we weren't allowed
1368      // to produce a type annotation token. Update the template-id
1369      // annotation token to a type annotation token now.
1370      AnnotateTemplateIdTokenAsType();
1371      return false;
1372    }
1373  }
1374
1375  if (SS.isEmpty())
1376    return false;
1377
1378  // A C++ scope specifier that isn't followed by a typename.
1379  // Push the current token back into the token stream (or revert it if it is
1380  // cached) and use an annotation scope token for current token.
1381  if (PP.isBacktrackEnabled())
1382    PP.RevertCachedTokens(1);
1383  else
1384    PP.EnterToken(Tok);
1385  Tok.setKind(tok::annot_cxxscope);
1386  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
1387  Tok.setAnnotationRange(SS.getRange());
1388
1389  // In case the tokens were cached, have Preprocessor replace them
1390  // with the annotation token.  We don't need to do this if we've
1391  // just reverted back to the state we were in before being called.
1392  if (!wasScopeAnnotation)
1393    PP.AnnotateCachedTokens(Tok);
1394  return false;
1395}
1396
1397/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
1398/// annotates C++ scope specifiers and template-ids.  This returns
1399/// true if the token was annotated or there was an error that could not be
1400/// recovered from.
1401///
1402/// Note that this routine emits an error if you call it with ::new or ::delete
1403/// as the current tokens, so only call it in contexts where these are invalid.
1404bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
1405  assert(getLangOpts().CPlusPlus &&
1406         "Call sites of this function should be guarded by checking for C++");
1407  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1408          (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
1409         Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
1410
1411  CXXScopeSpec SS;
1412  if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1413    return true;
1414  if (SS.isEmpty())
1415    return false;
1416
1417  // Push the current token back into the token stream (or revert it if it is
1418  // cached) and use an annotation scope token for current token.
1419  if (PP.isBacktrackEnabled())
1420    PP.RevertCachedTokens(1);
1421  else
1422    PP.EnterToken(Tok);
1423  Tok.setKind(tok::annot_cxxscope);
1424  Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
1425  Tok.setAnnotationRange(SS.getRange());
1426
1427  // In case the tokens were cached, have Preprocessor replace them with the
1428  // annotation token.
1429  PP.AnnotateCachedTokens(Tok);
1430  return false;
1431}
1432
1433bool Parser::isTokenEqualOrEqualTypo() {
1434  tok::TokenKind Kind = Tok.getKind();
1435  switch (Kind) {
1436  default:
1437    return false;
1438  case tok::ampequal:            // &=
1439  case tok::starequal:           // *=
1440  case tok::plusequal:           // +=
1441  case tok::minusequal:          // -=
1442  case tok::exclaimequal:        // !=
1443  case tok::slashequal:          // /=
1444  case tok::percentequal:        // %=
1445  case tok::lessequal:           // <=
1446  case tok::lesslessequal:       // <<=
1447  case tok::greaterequal:        // >=
1448  case tok::greatergreaterequal: // >>=
1449  case tok::caretequal:          // ^=
1450  case tok::pipeequal:           // |=
1451  case tok::equalequal:          // ==
1452    Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1453      << getTokenSimpleSpelling(Kind)
1454      << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1455  case tok::equal:
1456    return true;
1457  }
1458}
1459
1460SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
1461  assert(Tok.is(tok::code_completion));
1462  PrevTokLocation = Tok.getLocation();
1463
1464  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1465    if (S->getFlags() & Scope::FnScope) {
1466      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
1467      cutOffParsing();
1468      return PrevTokLocation;
1469    }
1470
1471    if (S->getFlags() & Scope::ClassScope) {
1472      Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
1473      cutOffParsing();
1474      return PrevTokLocation;
1475    }
1476  }
1477
1478  Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1479  cutOffParsing();
1480  return PrevTokLocation;
1481}
1482
1483// Anchor the Parser::FieldCallback vtable to this translation unit.
1484// We use a spurious method instead of the destructor because
1485// destroying FieldCallbacks can actually be slightly
1486// performance-sensitive.
1487void Parser::FieldCallback::_anchor() {
1488}
1489
1490// Code-completion pass-through functions
1491
1492void Parser::CodeCompleteDirective(bool InConditional) {
1493  Actions.CodeCompletePreprocessorDirective(InConditional);
1494}
1495
1496void Parser::CodeCompleteInConditionalExclusion() {
1497  Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1498}
1499
1500void Parser::CodeCompleteMacroName(bool IsDefinition) {
1501  Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1502}
1503
1504void Parser::CodeCompletePreprocessorExpression() {
1505  Actions.CodeCompletePreprocessorExpression();
1506}
1507
1508void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1509                                       MacroInfo *MacroInfo,
1510                                       unsigned ArgumentIndex) {
1511  Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1512                                                ArgumentIndex);
1513}
1514
1515void Parser::CodeCompleteNaturalLanguage() {
1516  Actions.CodeCompleteNaturalLanguage();
1517}
1518
1519bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
1520  assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1521         "Expected '__if_exists' or '__if_not_exists'");
1522  Result.IsIfExists = Tok.is(tok::kw___if_exists);
1523  Result.KeywordLoc = ConsumeToken();
1524
1525  BalancedDelimiterTracker T(*this, tok::l_paren);
1526  if (T.consumeOpen()) {
1527    Diag(Tok, diag::err_expected_lparen_after)
1528      << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
1529    return true;
1530  }
1531
1532  // Parse nested-name-specifier.
1533  ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1534                                 /*EnteringContext=*/false);
1535
1536  // Check nested-name specifier.
1537  if (Result.SS.isInvalid()) {
1538    T.skipToEnd();
1539    return true;
1540  }
1541
1542  // Parse the unqualified-id.
1543  SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1544  if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1545                         TemplateKWLoc, Result.Name)) {
1546    T.skipToEnd();
1547    return true;
1548  }
1549
1550  if (T.consumeClose())
1551    return true;
1552
1553  // Check if the symbol exists.
1554  switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
1555                                               Result.IsIfExists, Result.SS,
1556                                               Result.Name)) {
1557  case Sema::IER_Exists:
1558    Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
1559    break;
1560
1561  case Sema::IER_DoesNotExist:
1562    Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
1563    break;
1564
1565  case Sema::IER_Dependent:
1566    Result.Behavior = IEB_Dependent;
1567    break;
1568
1569  case Sema::IER_Error:
1570    return true;
1571  }
1572
1573  return false;
1574}
1575
1576void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
1577  IfExistsCondition Result;
1578  if (ParseMicrosoftIfExistsCondition(Result))
1579    return;
1580
1581  BalancedDelimiterTracker Braces(*this, tok::l_brace);
1582  if (Braces.consumeOpen()) {
1583    Diag(Tok, diag::err_expected_lbrace);
1584    return;
1585  }
1586
1587  switch (Result.Behavior) {
1588  case IEB_Parse:
1589    // Parse declarations below.
1590    break;
1591
1592  case IEB_Dependent:
1593    llvm_unreachable("Cannot have a dependent external declaration");
1594
1595  case IEB_Skip:
1596    Braces.skipToEnd();
1597    return;
1598  }
1599
1600  // Parse the declarations.
1601  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1602    ParsedAttributesWithRange attrs(AttrFactory);
1603    MaybeParseCXX0XAttributes(attrs);
1604    MaybeParseMicrosoftAttributes(attrs);
1605    DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1606    if (Result && !getCurScope()->getParent())
1607      Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
1608  }
1609  Braces.consumeClose();
1610}
1611
1612Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
1613  assert(Tok.isObjCAtKeyword(tok::objc___experimental_modules_import) &&
1614         "Improper start to module import");
1615  SourceLocation ImportLoc = ConsumeToken();
1616
1617  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1618
1619  // Parse the module path.
1620  do {
1621    if (!Tok.is(tok::identifier)) {
1622      if (Tok.is(tok::code_completion)) {
1623        Actions.CodeCompleteModuleImport(ImportLoc, Path);
1624        ConsumeCodeCompletionToken();
1625        SkipUntil(tok::semi);
1626        return DeclGroupPtrTy();
1627      }
1628
1629      Diag(Tok, diag::err_module_expected_ident);
1630      SkipUntil(tok::semi);
1631      return DeclGroupPtrTy();
1632    }
1633
1634    // Record this part of the module path.
1635    Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
1636    ConsumeToken();
1637
1638    if (Tok.is(tok::period)) {
1639      ConsumeToken();
1640      continue;
1641    }
1642
1643    break;
1644  } while (true);
1645
1646  DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
1647  ExpectAndConsumeSemi(diag::err_module_expected_semi);
1648  if (Import.isInvalid())
1649    return DeclGroupPtrTy();
1650
1651  return Actions.ConvertDeclToDeclGroup(Import.get());
1652}
1653
1654bool Parser::BalancedDelimiterTracker::diagnoseOverflow() {
1655  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
1656  P.SkipUntil(tok::eof);
1657  return true;
1658}
1659
1660bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
1661                                            const char *Msg,
1662                                            tok::TokenKind SkipToToc ) {
1663  LOpen = P.Tok.getLocation();
1664  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1665    return true;
1666
1667  if (getDepth() < MaxDepth)
1668    return false;
1669
1670  return diagnoseOverflow();
1671}
1672
1673bool Parser::BalancedDelimiterTracker::diagnoseMissingClose() {
1674  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1675
1676  const char *LHSName = "unknown";
1677  diag::kind DID = diag::err_parse_error;
1678  switch (Close) {
1679  default: break;
1680  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1681  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1682  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
1683  }
1684  P.Diag(P.Tok, DID);
1685  P.Diag(LOpen, diag::note_matching) << LHSName;
1686  if (P.SkipUntil(Close))
1687    LClose = P.Tok.getLocation();
1688  return true;
1689}
1690
1691void Parser::BalancedDelimiterTracker::skipToEnd() {
1692  P.SkipUntil(Close, false);
1693}
1694