Parser.cpp revision 1fd6d44d7ca97631497551bbf98866263143d706
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/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
18#include "clang/Parse/Template.h"
19#include "llvm/Support/raw_ostream.h"
20#include "RAIIObjectsForParser.h"
21#include "ParsePragma.h"
22using namespace clang;
23
24Parser::Parser(Preprocessor &pp, Action &actions)
25  : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
26    GreaterThanIsOperator(true), ColonIsSacred(false),
27    TemplateParameterDepth(0) {
28  Tok.setKind(tok::eof);
29  CurScope = 0;
30  NumCachedScopes = 0;
31  ParenCount = BracketCount = BraceCount = 0;
32  ObjCImpDecl = DeclPtrTy();
33
34  // Add #pragma handlers. These are removed and destroyed in the
35  // destructor.
36  PackHandler.reset(new
37          PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions));
38  PP.AddPragmaHandler(0, PackHandler.get());
39
40  UnusedHandler.reset(new
41          PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions,
42                              *this));
43  PP.AddPragmaHandler(0, UnusedHandler.get());
44
45  WeakHandler.reset(new
46          PragmaWeakHandler(&PP.getIdentifierTable().get("weak"), actions));
47  PP.AddPragmaHandler(0, WeakHandler.get());
48}
49
50/// If a crash happens while the parser is active, print out a line indicating
51/// what the current token is.
52void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
53  const Token &Tok = P.getCurToken();
54  if (Tok.is(tok::eof)) {
55    OS << "<eof> parser at end of file\n";
56    return;
57  }
58
59  if (Tok.getLocation().isInvalid()) {
60    OS << "<unknown> parser at unknown location\n";
61    return;
62  }
63
64  const Preprocessor &PP = P.getPreprocessor();
65  Tok.getLocation().print(OS, PP.getSourceManager());
66  if (Tok.isAnnotation())
67    OS << ": at annotation token \n";
68  else
69    OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
70}
71
72
73DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
74  return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
75}
76
77DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
78  return Diag(Tok.getLocation(), DiagID);
79}
80
81/// \brief Emits a diagnostic suggesting parentheses surrounding a
82/// given range.
83///
84/// \param Loc The location where we'll emit the diagnostic.
85/// \param Loc The kind of diagnostic to emit.
86/// \param ParenRange Source range enclosing code that should be parenthesized.
87void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
88                                SourceRange ParenRange) {
89  SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
90  if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
91    // We can't display the parentheses, so just dig the
92    // warning/error and return.
93    Diag(Loc, DK);
94    return;
95  }
96
97  Diag(Loc, DK)
98    << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
99    << FixItHint::CreateInsertion(EndLoc, ")");
100}
101
102/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
103/// this helper function matches and consumes the specified RHS token if
104/// present.  If not present, it emits the specified diagnostic indicating
105/// that the parser failed to match the RHS of the token at LHSLoc.  LHSName
106/// should be the name of the unmatched LHS token.
107SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
108                                           SourceLocation LHSLoc) {
109
110  if (Tok.is(RHSTok))
111    return ConsumeAnyToken();
112
113  SourceLocation R = Tok.getLocation();
114  const char *LHSName = "unknown";
115  diag::kind DID = diag::err_parse_error;
116  switch (RHSTok) {
117  default: break;
118  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
119  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
120  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
121  case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
122  }
123  Diag(Tok, DID);
124  Diag(LHSLoc, diag::note_matching) << LHSName;
125  SkipUntil(RHSTok);
126  return R;
127}
128
129/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
130/// input.  If so, it is consumed and false is returned.
131///
132/// If the input is malformed, this emits the specified diagnostic.  Next, if
133/// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
134/// returned.
135bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
136                              const char *Msg, tok::TokenKind SkipToTok) {
137  if (Tok.is(ExpectedTok)) {
138    ConsumeAnyToken();
139    return false;
140  }
141
142  const char *Spelling = 0;
143  SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
144  if (EndLoc.isValid() &&
145      (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
146    // Show what code to insert to fix this problem.
147    Diag(EndLoc, DiagID)
148      << Msg
149      << FixItHint::CreateInsertion(EndLoc, Spelling);
150  } else
151    Diag(Tok, DiagID) << Msg;
152
153  if (SkipToTok != tok::unknown)
154    SkipUntil(SkipToTok);
155  return true;
156}
157
158//===----------------------------------------------------------------------===//
159// Error recovery.
160//===----------------------------------------------------------------------===//
161
162/// SkipUntil - Read tokens until we get to the specified token, then consume
163/// it (unless DontConsume is true).  Because we cannot guarantee that the
164/// token will ever occur, this skips to the next token, or to some likely
165/// good stopping point.  If StopAtSemi is true, skipping will stop at a ';'
166/// character.
167///
168/// If SkipUntil finds the specified token, it returns true, otherwise it
169/// returns false.
170bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
171                       bool StopAtSemi, bool DontConsume) {
172  // We always want this function to skip at least one token if the first token
173  // isn't T and if not at EOF.
174  bool isFirstTokenSkipped = true;
175  while (1) {
176    // If we found one of the tokens, stop and return true.
177    for (unsigned i = 0; i != NumToks; ++i) {
178      if (Tok.is(Toks[i])) {
179        if (DontConsume) {
180          // Noop, don't consume the token.
181        } else {
182          ConsumeAnyToken();
183        }
184        return true;
185      }
186    }
187
188    switch (Tok.getKind()) {
189    case tok::eof:
190      // Ran out of tokens.
191      return false;
192
193    case tok::l_paren:
194      // Recursively skip properly-nested parens.
195      ConsumeParen();
196      SkipUntil(tok::r_paren, false);
197      break;
198    case tok::l_square:
199      // Recursively skip properly-nested square brackets.
200      ConsumeBracket();
201      SkipUntil(tok::r_square, false);
202      break;
203    case tok::l_brace:
204      // Recursively skip properly-nested braces.
205      ConsumeBrace();
206      SkipUntil(tok::r_brace, false);
207      break;
208
209    // Okay, we found a ']' or '}' or ')', which we think should be balanced.
210    // Since the user wasn't looking for this token (if they were, it would
211    // already be handled), this isn't balanced.  If there is a LHS token at a
212    // higher level, we will assume that this matches the unbalanced token
213    // and return it.  Otherwise, this is a spurious RHS token, which we skip.
214    case tok::r_paren:
215      if (ParenCount && !isFirstTokenSkipped)
216        return false;  // Matches something.
217      ConsumeParen();
218      break;
219    case tok::r_square:
220      if (BracketCount && !isFirstTokenSkipped)
221        return false;  // Matches something.
222      ConsumeBracket();
223      break;
224    case tok::r_brace:
225      if (BraceCount && !isFirstTokenSkipped)
226        return false;  // Matches something.
227      ConsumeBrace();
228      break;
229
230    case tok::string_literal:
231    case tok::wide_string_literal:
232      ConsumeStringToken();
233      break;
234    case tok::semi:
235      if (StopAtSemi)
236        return false;
237      // FALL THROUGH.
238    default:
239      // Skip this token.
240      ConsumeToken();
241      break;
242    }
243    isFirstTokenSkipped = false;
244  }
245}
246
247//===----------------------------------------------------------------------===//
248// Scope manipulation
249//===----------------------------------------------------------------------===//
250
251/// EnterScope - Start a new scope.
252void Parser::EnterScope(unsigned ScopeFlags) {
253  if (NumCachedScopes) {
254    Scope *N = ScopeCache[--NumCachedScopes];
255    N->Init(CurScope, ScopeFlags);
256    CurScope = N;
257  } else {
258    CurScope = new Scope(CurScope, ScopeFlags);
259  }
260  CurScope->setNumErrorsAtStart(Diags.getNumErrors());
261}
262
263/// ExitScope - Pop a scope off the scope stack.
264void Parser::ExitScope() {
265  assert(CurScope && "Scope imbalance!");
266
267  // Inform the actions module that this scope is going away if there are any
268  // decls in it.
269  if (!CurScope->decl_empty())
270    Actions.ActOnPopScope(Tok.getLocation(), CurScope);
271
272  Scope *OldScope = CurScope;
273  CurScope = OldScope->getParent();
274
275  if (NumCachedScopes == ScopeCacheSize)
276    delete OldScope;
277  else
278    ScopeCache[NumCachedScopes++] = OldScope;
279}
280
281
282
283
284//===----------------------------------------------------------------------===//
285// C99 6.9: External Definitions.
286//===----------------------------------------------------------------------===//
287
288Parser::~Parser() {
289  // If we still have scopes active, delete the scope tree.
290  delete CurScope;
291
292  // Free the scope cache.
293  for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
294    delete ScopeCache[i];
295
296  // Remove the pragma handlers we installed.
297  PP.RemovePragmaHandler(0, PackHandler.get());
298  PackHandler.reset();
299  PP.RemovePragmaHandler(0, UnusedHandler.get());
300  UnusedHandler.reset();
301  PP.RemovePragmaHandler(0, WeakHandler.get());
302  WeakHandler.reset();
303}
304
305/// Initialize - Warm up the parser.
306///
307void Parser::Initialize() {
308  // Prime the lexer look-ahead.
309  ConsumeToken();
310
311  // Create the translation unit scope.  Install it as the current scope.
312  assert(CurScope == 0 && "A scope is already active?");
313  EnterScope(Scope::DeclScope);
314  Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
315
316  if (Tok.is(tok::eof) &&
317      !getLang().CPlusPlus)  // Empty source file is an extension in C
318    Diag(Tok, diag::ext_empty_source_file);
319
320  // Initialization for Objective-C context sensitive keywords recognition.
321  // Referenced in Parser::ParseObjCTypeQualifierList.
322  if (getLang().ObjC1) {
323    ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
324    ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
325    ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
326    ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
327    ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
328    ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
329  }
330
331  Ident_super = &PP.getIdentifierTable().get("super");
332
333  if (getLang().AltiVec) {
334    Ident_vector = &PP.getIdentifierTable().get("vector");
335    Ident_pixel = &PP.getIdentifierTable().get("pixel");
336  }
337}
338
339/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
340/// action tells us to.  This returns true if the EOF was encountered.
341bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
342  Result = DeclGroupPtrTy();
343  if (Tok.is(tok::eof)) {
344    Actions.ActOnEndOfTranslationUnit();
345    return true;
346  }
347
348  CXX0XAttributeList Attr;
349  if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
350    Attr = ParseCXX0XAttributes();
351  Result = ParseExternalDeclaration(Attr);
352  return false;
353}
354
355/// ParseTranslationUnit:
356///       translation-unit: [C99 6.9]
357///         external-declaration
358///         translation-unit external-declaration
359void Parser::ParseTranslationUnit() {
360  Initialize();
361
362  DeclGroupPtrTy Res;
363  while (!ParseTopLevelDecl(Res))
364    /*parse them all*/;
365
366  ExitScope();
367  assert(CurScope == 0 && "Scope imbalance!");
368}
369
370/// ParseExternalDeclaration:
371///
372///       external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
373///         function-definition
374///         declaration
375/// [C++0x] empty-declaration
376/// [GNU]   asm-definition
377/// [GNU]   __extension__ external-declaration
378/// [OBJC]  objc-class-definition
379/// [OBJC]  objc-class-declaration
380/// [OBJC]  objc-alias-declaration
381/// [OBJC]  objc-protocol-definition
382/// [OBJC]  objc-method-definition
383/// [OBJC]  @end
384/// [C++]   linkage-specification
385/// [GNU] asm-definition:
386///         simple-asm-expr ';'
387///
388/// [C++0x] empty-declaration:
389///           ';'
390///
391/// [C++0x/GNU] 'extern' 'template' declaration
392Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(CXX0XAttributeList Attr) {
393  DeclPtrTy SingleDecl;
394  switch (Tok.getKind()) {
395  case tok::semi:
396    if (!getLang().CPlusPlus0x)
397      Diag(Tok, diag::ext_top_level_semi)
398        << FixItHint::CreateRemoval(Tok.getLocation());
399
400    ConsumeToken();
401    // TODO: Invoke action for top-level semicolon.
402    return DeclGroupPtrTy();
403  case tok::r_brace:
404    Diag(Tok, diag::err_expected_external_declaration);
405    ConsumeBrace();
406    return DeclGroupPtrTy();
407  case tok::eof:
408    Diag(Tok, diag::err_expected_external_declaration);
409    return DeclGroupPtrTy();
410  case tok::kw___extension__: {
411    // __extension__ silences extension warnings in the subexpression.
412    ExtensionRAIIObject O(Diags);  // Use RAII to do this.
413    ConsumeToken();
414    return ParseExternalDeclaration(Attr);
415  }
416  case tok::kw_asm: {
417    if (Attr.HasAttr)
418      Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
419        << Attr.Range;
420
421    OwningExprResult Result(ParseSimpleAsm());
422
423    ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
424                     "top-level asm block");
425
426    if (Result.isInvalid())
427      return DeclGroupPtrTy();
428    SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
429    break;
430  }
431  case tok::at:
432    // @ is not a legal token unless objc is enabled, no need to check for ObjC.
433    /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
434    /// @class foo, bar;
435    SingleDecl = ParseObjCAtDirectives();
436    break;
437  case tok::minus:
438  case tok::plus:
439    if (!getLang().ObjC1) {
440      Diag(Tok, diag::err_expected_external_declaration);
441      ConsumeToken();
442      return DeclGroupPtrTy();
443    }
444    SingleDecl = ParseObjCMethodDefinition();
445    break;
446  case tok::code_completion:
447      Actions.CodeCompleteOrdinaryName(CurScope,
448                                   ObjCImpDecl? Action::CCC_ObjCImplementation
449                                              : Action::CCC_Namespace);
450    ConsumeToken();
451    return ParseExternalDeclaration(Attr);
452  case tok::kw_using:
453  case tok::kw_namespace:
454  case tok::kw_typedef:
455  case tok::kw_template:
456  case tok::kw_export:    // As in 'export template'
457  case tok::kw_static_assert:
458    // A function definition cannot start with a these keywords.
459    {
460      SourceLocation DeclEnd;
461      return ParseDeclaration(Declarator::FileContext, DeclEnd, Attr);
462    }
463  case tok::kw_extern:
464    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
465      // Extern templates
466      SourceLocation ExternLoc = ConsumeToken();
467      SourceLocation TemplateLoc = ConsumeToken();
468      SourceLocation DeclEnd;
469      return Actions.ConvertDeclToDeclGroup(
470                  ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
471    }
472
473    // FIXME: Detect C++ linkage specifications here?
474
475    // Fall through to handle other declarations or function definitions.
476
477  default:
478    // We can't tell whether this is a function-definition or declaration yet.
479    return ParseDeclarationOrFunctionDefinition(Attr.AttrList);
480  }
481
482  // This routine returns a DeclGroup, if the thing we parsed only contains a
483  // single decl, convert it now.
484  return Actions.ConvertDeclToDeclGroup(SingleDecl);
485}
486
487/// \brief Determine whether the current token, if it occurs after a
488/// declarator, continues a declaration or declaration list.
489bool Parser::isDeclarationAfterDeclarator() {
490  return Tok.is(tok::equal) ||      // int X()=  -> not a function def
491    Tok.is(tok::comma) ||           // int X(),  -> not a function def
492    Tok.is(tok::semi)  ||           // int X();  -> not a function def
493    Tok.is(tok::kw_asm) ||          // int X() __asm__ -> not a function def
494    Tok.is(tok::kw___attribute) ||  // int X() __attr__ -> not a function def
495    (getLang().CPlusPlus &&
496     Tok.is(tok::l_paren));         // int X(0) -> not a function def [C++]
497}
498
499/// \brief Determine whether the current token, if it occurs after a
500/// declarator, indicates the start of a function definition.
501bool Parser::isStartOfFunctionDefinition() {
502  if (Tok.is(tok::l_brace))   // int X() {}
503    return true;
504
505  if (!getLang().CPlusPlus)
506    return isDeclarationSpecifier();   // int X(f) int f; {}
507  return Tok.is(tok::colon) ||         // X() : Base() {} (used for ctors)
508         Tok.is(tok::kw_try);          // X() try { ... }
509}
510
511/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
512/// a declaration.  We can't tell which we have until we read up to the
513/// compound-statement in function-definition. TemplateParams, if
514/// non-NULL, provides the template parameters when we're parsing a
515/// C++ template-declaration.
516///
517///       function-definition: [C99 6.9.1]
518///         decl-specs      declarator declaration-list[opt] compound-statement
519/// [C90] function-definition: [C99 6.7.1] - implicit int result
520/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
521///
522///       declaration: [C99 6.7]
523///         declaration-specifiers init-declarator-list[opt] ';'
524/// [!C99]  init-declarator-list ';'                   [TODO: warn in c99 mode]
525/// [OMP]   threadprivate-directive                              [TODO]
526///
527Parser::DeclGroupPtrTy
528Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
529                                             AttributeList *Attr,
530                                             AccessSpecifier AS) {
531  // Parse the common declaration-specifiers piece.
532  if (Attr)
533    DS.AddAttributes(Attr);
534
535  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
536
537  // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
538  // declaration-specifiers init-declarator-list[opt] ';'
539  if (Tok.is(tok::semi)) {
540    ConsumeToken();
541    DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, AS, DS);
542    DS.complete(TheDecl);
543    return Actions.ConvertDeclToDeclGroup(TheDecl);
544  }
545
546  // ObjC2 allows prefix attributes on class interfaces and protocols.
547  // FIXME: This still needs better diagnostics. We should only accept
548  // attributes here, no types, etc.
549  if (getLang().ObjC2 && Tok.is(tok::at)) {
550    SourceLocation AtLoc = ConsumeToken(); // the "@"
551    if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
552        !Tok.isObjCAtKeyword(tok::objc_protocol)) {
553      Diag(Tok, diag::err_objc_unexpected_attr);
554      SkipUntil(tok::semi); // FIXME: better skip?
555      return DeclGroupPtrTy();
556    }
557
558    DS.abort();
559
560    const char *PrevSpec = 0;
561    unsigned DiagID;
562    if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
563      Diag(AtLoc, DiagID) << PrevSpec;
564
565    DeclPtrTy TheDecl;
566    if (Tok.isObjCAtKeyword(tok::objc_protocol))
567      TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
568    else
569      TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
570    return Actions.ConvertDeclToDeclGroup(TheDecl);
571  }
572
573  // If the declspec consisted only of 'extern' and we have a string
574  // literal following it, this must be a C++ linkage specifier like
575  // 'extern "C"'.
576  if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
577      DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
578      DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
579    DeclPtrTy TheDecl = ParseLinkage(DS, Declarator::FileContext);
580    return Actions.ConvertDeclToDeclGroup(TheDecl);
581  }
582
583  return ParseDeclGroup(DS, Declarator::FileContext, true);
584}
585
586Parser::DeclGroupPtrTy
587Parser::ParseDeclarationOrFunctionDefinition(AttributeList *Attr,
588                                             AccessSpecifier AS) {
589  ParsingDeclSpec DS(*this);
590  return ParseDeclarationOrFunctionDefinition(DS, Attr, AS);
591}
592
593/// ParseFunctionDefinition - We parsed and verified that the specified
594/// Declarator is well formed.  If this is a K&R-style function, read the
595/// parameters declaration-list, then start the compound-statement.
596///
597///       function-definition: [C99 6.9.1]
598///         decl-specs      declarator declaration-list[opt] compound-statement
599/// [C90] function-definition: [C99 6.7.1] - implicit int result
600/// [C90]   decl-specs[opt] declarator declaration-list[opt] compound-statement
601/// [C++] function-definition: [C++ 8.4]
602///         decl-specifier-seq[opt] declarator ctor-initializer[opt]
603///         function-body
604/// [C++] function-definition: [C++ 8.4]
605///         decl-specifier-seq[opt] declarator function-try-block
606///
607Parser::DeclPtrTy Parser::ParseFunctionDefinition(ParsingDeclarator &D,
608                                     const ParsedTemplateInfo &TemplateInfo) {
609  const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
610  assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
611         "This isn't a function declarator!");
612  const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
613
614  // If this is C90 and the declspecs were completely missing, fudge in an
615  // implicit int.  We do this here because this is the only place where
616  // declaration-specifiers are completely optional in the grammar.
617  if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
618    const char *PrevSpec;
619    unsigned DiagID;
620    D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
621                                           D.getIdentifierLoc(),
622                                           PrevSpec, DiagID);
623    D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
624  }
625
626  // If this declaration was formed with a K&R-style identifier list for the
627  // arguments, parse declarations for all of the args next.
628  // int foo(a,b) int a; float b; {}
629  if (!FTI.hasPrototype && FTI.NumArgs != 0)
630    ParseKNRParamDeclarations(D);
631
632  // We should have either an opening brace or, in a C++ constructor,
633  // we may have a colon.
634  if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) &&
635      Tok.isNot(tok::kw_try)) {
636    Diag(Tok, diag::err_expected_fn_body);
637
638    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
639    SkipUntil(tok::l_brace, true, true);
640
641    // If we didn't find the '{', bail out.
642    if (Tok.isNot(tok::l_brace))
643      return DeclPtrTy();
644  }
645
646  // Enter a scope for the function body.
647  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
648
649  // Tell the actions module that we have entered a function definition with the
650  // specified Declarator for the function.
651  DeclPtrTy Res = TemplateInfo.TemplateParams?
652      Actions.ActOnStartOfFunctionTemplateDef(CurScope,
653                              Action::MultiTemplateParamsArg(Actions,
654                                          TemplateInfo.TemplateParams->data(),
655                                         TemplateInfo.TemplateParams->size()),
656                                              D)
657    : Actions.ActOnStartOfFunctionDef(CurScope, D);
658
659  // Break out of the ParsingDeclarator context before we parse the body.
660  D.complete(Res);
661
662  // Break out of the ParsingDeclSpec context, too.  This const_cast is
663  // safe because we're always the sole owner.
664  D.getMutableDeclSpec().abort();
665
666  if (Tok.is(tok::kw_try))
667    return ParseFunctionTryBlock(Res);
668
669  // If we have a colon, then we're probably parsing a C++
670  // ctor-initializer.
671  if (Tok.is(tok::colon)) {
672    ParseConstructorInitializer(Res);
673
674    // Recover from error.
675    if (!Tok.is(tok::l_brace)) {
676      Actions.ActOnFinishFunctionBody(Res, Action::StmtArg(Actions));
677      return Res;
678    }
679  } else
680    Actions.ActOnDefaultCtorInitializers(Res);
681
682  return ParseFunctionStatementBody(Res);
683}
684
685/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
686/// types for a function with a K&R-style identifier list for arguments.
687void Parser::ParseKNRParamDeclarations(Declarator &D) {
688  // We know that the top-level of this declarator is a function.
689  DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
690
691  // Enter function-declaration scope, limiting any declarators to the
692  // function prototype scope, including parameter declarators.
693  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
694
695  // Read all the argument declarations.
696  while (isDeclarationSpecifier()) {
697    SourceLocation DSStart = Tok.getLocation();
698
699    // Parse the common declaration-specifiers piece.
700    DeclSpec DS;
701    ParseDeclarationSpecifiers(DS);
702
703    // C99 6.9.1p6: 'each declaration in the declaration list shall have at
704    // least one declarator'.
705    // NOTE: GCC just makes this an ext-warn.  It's not clear what it does with
706    // the declarations though.  It's trivial to ignore them, really hard to do
707    // anything else with them.
708    if (Tok.is(tok::semi)) {
709      Diag(DSStart, diag::err_declaration_does_not_declare_param);
710      ConsumeToken();
711      continue;
712    }
713
714    // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
715    // than register.
716    if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
717        DS.getStorageClassSpec() != DeclSpec::SCS_register) {
718      Diag(DS.getStorageClassSpecLoc(),
719           diag::err_invalid_storage_class_in_func_decl);
720      DS.ClearStorageClassSpecs();
721    }
722    if (DS.isThreadSpecified()) {
723      Diag(DS.getThreadSpecLoc(),
724           diag::err_invalid_storage_class_in_func_decl);
725      DS.ClearStorageClassSpecs();
726    }
727
728    // Parse the first declarator attached to this declspec.
729    Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
730    ParseDeclarator(ParmDeclarator);
731
732    // Handle the full declarator list.
733    while (1) {
734      // If attributes are present, parse them.
735      if (Tok.is(tok::kw___attribute)) {
736        SourceLocation Loc;
737        AttributeList *AttrList = ParseGNUAttributes(&Loc);
738        ParmDeclarator.AddAttributes(AttrList, Loc);
739      }
740
741      // Ask the actions module to compute the type for this declarator.
742      Action::DeclPtrTy Param =
743        Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
744
745      if (Param &&
746          // A missing identifier has already been diagnosed.
747          ParmDeclarator.getIdentifier()) {
748
749        // Scan the argument list looking for the correct param to apply this
750        // type.
751        for (unsigned i = 0; ; ++i) {
752          // C99 6.9.1p6: those declarators shall declare only identifiers from
753          // the identifier list.
754          if (i == FTI.NumArgs) {
755            Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
756              << ParmDeclarator.getIdentifier();
757            break;
758          }
759
760          if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
761            // Reject redefinitions of parameters.
762            if (FTI.ArgInfo[i].Param) {
763              Diag(ParmDeclarator.getIdentifierLoc(),
764                   diag::err_param_redefinition)
765                 << ParmDeclarator.getIdentifier();
766            } else {
767              FTI.ArgInfo[i].Param = Param;
768            }
769            break;
770          }
771        }
772      }
773
774      // If we don't have a comma, it is either the end of the list (a ';') or
775      // an error, bail out.
776      if (Tok.isNot(tok::comma))
777        break;
778
779      // Consume the comma.
780      ConsumeToken();
781
782      // Parse the next declarator.
783      ParmDeclarator.clear();
784      ParseDeclarator(ParmDeclarator);
785    }
786
787    if (Tok.is(tok::semi)) {
788      ConsumeToken();
789    } else {
790      Diag(Tok, diag::err_parse_error);
791      // Skip to end of block or statement
792      SkipUntil(tok::semi, true);
793      if (Tok.is(tok::semi))
794        ConsumeToken();
795    }
796  }
797
798  // The actions module must verify that all arguments were declared.
799  Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation());
800}
801
802
803/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
804/// allowed to be a wide string, and is not subject to character translation.
805///
806/// [GNU] asm-string-literal:
807///         string-literal
808///
809Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
810  if (!isTokenStringLiteral()) {
811    Diag(Tok, diag::err_expected_string_literal);
812    return ExprError();
813  }
814
815  OwningExprResult Res(ParseStringLiteralExpression());
816  if (Res.isInvalid()) return move(Res);
817
818  // TODO: Diagnose: wide string literal in 'asm'
819
820  return move(Res);
821}
822
823/// ParseSimpleAsm
824///
825/// [GNU] simple-asm-expr:
826///         'asm' '(' asm-string-literal ')'
827///
828Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
829  assert(Tok.is(tok::kw_asm) && "Not an asm!");
830  SourceLocation Loc = ConsumeToken();
831
832  if (Tok.is(tok::kw_volatile)) {
833    // Remove from the end of 'asm' to the end of 'volatile'.
834    SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
835                             PP.getLocForEndOfToken(Tok.getLocation()));
836
837    Diag(Tok, diag::warn_file_asm_volatile)
838      << FixItHint::CreateRemoval(RemovalRange);
839    ConsumeToken();
840  }
841
842  if (Tok.isNot(tok::l_paren)) {
843    Diag(Tok, diag::err_expected_lparen_after) << "asm";
844    return ExprError();
845  }
846
847  Loc = ConsumeParen();
848
849  OwningExprResult Result(ParseAsmStringLiteral());
850
851  if (Result.isInvalid()) {
852    SkipUntil(tok::r_paren, true, true);
853    if (EndLoc)
854      *EndLoc = Tok.getLocation();
855    ConsumeAnyToken();
856  } else {
857    Loc = MatchRHSPunctuation(tok::r_paren, Loc);
858    if (EndLoc)
859      *EndLoc = Loc;
860  }
861
862  return move(Result);
863}
864
865/// TryAnnotateTypeOrScopeToken - If the current token position is on a
866/// typename (possibly qualified in C++) or a C++ scope specifier not followed
867/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
868/// with a single annotation token representing the typename or C++ scope
869/// respectively.
870/// This simplifies handling of C++ scope specifiers and allows efficient
871/// backtracking without the need to re-parse and resolve nested-names and
872/// typenames.
873/// It will mainly be called when we expect to treat identifiers as typenames
874/// (if they are typenames). For example, in C we do not expect identifiers
875/// inside expressions to be treated as typenames so it will not be called
876/// for expressions in C.
877/// The benefit for C/ObjC is that a typename will be annotated and
878/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
879/// will not be called twice, once to check whether we have a declaration
880/// specifier, and another one to get the actual type inside
881/// ParseDeclarationSpecifiers).
882///
883/// This returns true if an error occurred.
884///
885/// Note that this routine emits an error if you call it with ::new or ::delete
886/// as the current tokens, so only call it in contexts where these are invalid.
887bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
888  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
889          || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
890         "Cannot be a type or scope token!");
891
892  if (Tok.is(tok::kw_typename)) {
893    // Parse a C++ typename-specifier, e.g., "typename T::type".
894    //
895    //   typename-specifier:
896    //     'typename' '::' [opt] nested-name-specifier identifier
897    //     'typename' '::' [opt] nested-name-specifier template [opt]
898    //            simple-template-id
899    SourceLocation TypenameLoc = ConsumeToken();
900    CXXScopeSpec SS;
901    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false))
902      return true;
903    if (!SS.isSet()) {
904      Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
905      return true;
906    }
907
908    TypeResult Ty;
909    if (Tok.is(tok::identifier)) {
910      // FIXME: check whether the next token is '<', first!
911      Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(),
912                                     Tok.getLocation());
913    } else if (Tok.is(tok::annot_template_id)) {
914      TemplateIdAnnotation *TemplateId
915        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
916      if (TemplateId->Kind == TNK_Function_template) {
917        Diag(Tok, diag::err_typename_refers_to_non_type_template)
918          << Tok.getAnnotationRange();
919        return true;
920      }
921
922      AnnotateTemplateIdTokenAsType(0);
923      assert(Tok.is(tok::annot_typename) &&
924             "AnnotateTemplateIdTokenAsType isn't working properly");
925      if (Tok.getAnnotationValue())
926        Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
927                                       Tok.getAnnotationValue());
928      else
929        Ty = true;
930    } else {
931      Diag(Tok, diag::err_expected_type_name_after_typename)
932        << SS.getRange();
933      return true;
934    }
935
936    SourceLocation EndLoc = Tok.getLastLoc();
937    Tok.setKind(tok::annot_typename);
938    Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get());
939    Tok.setAnnotationEndLoc(EndLoc);
940    Tok.setLocation(TypenameLoc);
941    PP.AnnotateCachedTokens(Tok);
942    return false;
943  }
944
945  // Remembers whether the token was originally a scope annotation.
946  bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
947
948  CXXScopeSpec SS;
949  if (getLang().CPlusPlus)
950    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
951      return true;
952
953  if (Tok.is(tok::identifier)) {
954    // Determine whether the identifier is a type name.
955    if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
956                                         Tok.getLocation(), CurScope, &SS)) {
957      // This is a typename. Replace the current token in-place with an
958      // annotation type token.
959      Tok.setKind(tok::annot_typename);
960      Tok.setAnnotationValue(Ty);
961      Tok.setAnnotationEndLoc(Tok.getLocation());
962      if (SS.isNotEmpty()) // it was a C++ qualified type name.
963        Tok.setLocation(SS.getBeginLoc());
964
965      // In case the tokens were cached, have Preprocessor replace
966      // them with the annotation token.
967      PP.AnnotateCachedTokens(Tok);
968      return false;
969    }
970
971    if (!getLang().CPlusPlus) {
972      // If we're in C, we can't have :: tokens at all (the lexer won't return
973      // them).  If the identifier is not a type, then it can't be scope either,
974      // just early exit.
975      return false;
976    }
977
978    // If this is a template-id, annotate with a template-id or type token.
979    if (NextToken().is(tok::less)) {
980      TemplateTy Template;
981      UnqualifiedId TemplateName;
982      TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
983      bool MemberOfUnknownSpecialization;
984      if (TemplateNameKind TNK
985            = Actions.isTemplateName(CurScope, SS, TemplateName,
986                                     /*ObjectType=*/0, EnteringContext,
987                                     Template, MemberOfUnknownSpecialization)) {
988        // Consume the identifier.
989        ConsumeToken();
990        if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName)) {
991          // If an unrecoverable error occurred, we need to return true here,
992          // because the token stream is in a damaged state.  We may not return
993          // a valid identifier.
994          return true;
995        }
996      }
997    }
998
999    // The current token, which is either an identifier or a
1000    // template-id, is not part of the annotation. Fall through to
1001    // push that token back into the stream and complete the C++ scope
1002    // specifier annotation.
1003  }
1004
1005  if (Tok.is(tok::annot_template_id)) {
1006    TemplateIdAnnotation *TemplateId
1007      = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1008    if (TemplateId->Kind == TNK_Type_template) {
1009      // A template-id that refers to a type was parsed into a
1010      // template-id annotation in a context where we weren't allowed
1011      // to produce a type annotation token. Update the template-id
1012      // annotation token to a type annotation token now.
1013      AnnotateTemplateIdTokenAsType(&SS);
1014      return false;
1015    }
1016  }
1017
1018  if (SS.isEmpty())
1019    return false;
1020
1021  // A C++ scope specifier that isn't followed by a typename.
1022  // Push the current token back into the token stream (or revert it if it is
1023  // cached) and use an annotation scope token for current token.
1024  if (PP.isBacktrackEnabled())
1025    PP.RevertCachedTokens(1);
1026  else
1027    PP.EnterToken(Tok);
1028  Tok.setKind(tok::annot_cxxscope);
1029  Tok.setAnnotationValue(SS.getScopeRep());
1030  Tok.setAnnotationRange(SS.getRange());
1031
1032  // In case the tokens were cached, have Preprocessor replace them
1033  // with the annotation token.  We don't need to do this if we've
1034  // just reverted back to the state we were in before being called.
1035  if (!wasScopeAnnotation)
1036    PP.AnnotateCachedTokens(Tok);
1037  return false;
1038}
1039
1040/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
1041/// annotates C++ scope specifiers and template-ids.  This returns
1042/// true if the token was annotated or there was an error that could not be
1043/// recovered from.
1044///
1045/// Note that this routine emits an error if you call it with ::new or ::delete
1046/// as the current tokens, so only call it in contexts where these are invalid.
1047bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
1048  assert(getLang().CPlusPlus &&
1049         "Call sites of this function should be guarded by checking for C++");
1050  assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1051         "Cannot be a type or scope token!");
1052
1053  CXXScopeSpec SS;
1054  if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
1055    return true;
1056  if (SS.isEmpty())
1057    return false;
1058
1059  // Push the current token back into the token stream (or revert it if it is
1060  // cached) and use an annotation scope token for current token.
1061  if (PP.isBacktrackEnabled())
1062    PP.RevertCachedTokens(1);
1063  else
1064    PP.EnterToken(Tok);
1065  Tok.setKind(tok::annot_cxxscope);
1066  Tok.setAnnotationValue(SS.getScopeRep());
1067  Tok.setAnnotationRange(SS.getRange());
1068
1069  // In case the tokens were cached, have Preprocessor replace them with the
1070  // annotation token.
1071  PP.AnnotateCachedTokens(Tok);
1072  return false;
1073}
1074
1075// Anchor the Parser::FieldCallback vtable to this translation unit.
1076// We use a spurious method instead of the destructor because
1077// destroying FieldCallbacks can actually be slightly
1078// performance-sensitive.
1079void Parser::FieldCallback::_anchor() {
1080}
1081