Fix a crash-on-invalid where we were trying to parse C++ constructs in
[clang.git] / lib / Parse / Parser.cpp
blob4b2bd0d89cff7f7eae2a9673355cbe05c4b6fab8
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.
12 //===----------------------------------------------------------------------===//
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 using namespace clang;
24 Parser::Parser(Preprocessor &pp, Sema &actions)
25 : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
26 GreaterThanIsOperator(true), ColonIsSacred(false),
27 InMessageExpression(false), TemplateParameterDepth(0) {
28 Tok.setKind(tok::eof);
29 Actions.CurScope = 0;
30 NumCachedScopes = 0;
31 ParenCount = BracketCount = BraceCount = 0;
32 ObjCImpDecl = 0;
34 // Add #pragma handlers. These are removed and destroyed in the
35 // destructor.
36 AlignHandler.reset(new PragmaAlignHandler(actions));
37 PP.AddPragmaHandler(AlignHandler.get());
39 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
40 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
42 OptionsHandler.reset(new PragmaOptionsHandler(actions));
43 PP.AddPragmaHandler(OptionsHandler.get());
45 PackHandler.reset(new PragmaPackHandler(actions));
46 PP.AddPragmaHandler(PackHandler.get());
48 UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
49 PP.AddPragmaHandler(UnusedHandler.get());
51 WeakHandler.reset(new PragmaWeakHandler(actions));
52 PP.AddPragmaHandler(WeakHandler.get());
54 PP.setCodeCompletionHandler(*this);
57 /// If a crash happens while the parser is active, print out a line indicating
58 /// what the current token is.
59 void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
60 const Token &Tok = P.getCurToken();
61 if (Tok.is(tok::eof)) {
62 OS << "<eof> parser at end of file\n";
63 return;
66 if (Tok.getLocation().isInvalid()) {
67 OS << "<unknown> parser at unknown location\n";
68 return;
71 const Preprocessor &PP = P.getPreprocessor();
72 Tok.getLocation().print(OS, PP.getSourceManager());
73 if (Tok.isAnnotation())
74 OS << ": at annotation token \n";
75 else
76 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
80 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
81 return Diags.Report(Loc, DiagID);
84 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
85 return Diag(Tok.getLocation(), DiagID);
88 /// \brief Emits a diagnostic suggesting parentheses surrounding a
89 /// given range.
90 ///
91 /// \param Loc The location where we'll emit the diagnostic.
92 /// \param Loc The kind of diagnostic to emit.
93 /// \param ParenRange Source range enclosing code that should be parenthesized.
94 void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
95 SourceRange ParenRange) {
96 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
97 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
98 // We can't display the parentheses, so just dig the
99 // warning/error and return.
100 Diag(Loc, DK);
101 return;
104 Diag(Loc, DK)
105 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
106 << FixItHint::CreateInsertion(EndLoc, ")");
109 /// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
110 /// this helper function matches and consumes the specified RHS token if
111 /// present. If not present, it emits the specified diagnostic indicating
112 /// that the parser failed to match the RHS of the token at LHSLoc. LHSName
113 /// should be the name of the unmatched LHS token.
114 SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
115 SourceLocation LHSLoc) {
117 if (Tok.is(RHSTok))
118 return ConsumeAnyToken();
120 SourceLocation R = Tok.getLocation();
121 const char *LHSName = "unknown";
122 diag::kind DID = diag::err_parse_error;
123 switch (RHSTok) {
124 default: break;
125 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
126 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
127 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
128 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
130 Diag(Tok, DID);
131 Diag(LHSLoc, diag::note_matching) << LHSName;
132 SkipUntil(RHSTok);
133 return R;
136 static 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;
143 /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
144 /// input. If so, it is consumed and false is returned.
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.
149 bool 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;
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();
165 // Pretend there wasn't a problem.
166 return false;
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;
180 if (SkipToTok != tok::unknown)
181 SkipUntil(SkipToTok);
182 return true;
185 bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
186 if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
187 ConsumeAnyToken();
188 return false;
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;
201 return ExpectAndConsume(tok::semi, DiagID);
204 //===----------------------------------------------------------------------===//
205 // Error recovery.
206 //===----------------------------------------------------------------------===//
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.
214 /// If SkipUntil finds the specified token, it returns true, otherwise it
215 /// returns false.
216 bool 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();
231 return true;
235 switch (Tok.getKind()) {
236 case tok::eof:
237 // Ran out of tokens.
238 return false;
240 case tok::code_completion:
241 if (!StopAtCodeCompletion)
242 ConsumeToken();
243 return false;
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;
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;
282 case tok::string_literal:
283 case tok::wide_string_literal:
284 ConsumeStringToken();
285 break;
286 case tok::semi:
287 if (StopAtSemi)
288 return false;
289 // FALL THROUGH.
290 default:
291 // Skip this token.
292 ConsumeToken();
293 break;
295 isFirstTokenSkipped = false;
299 //===----------------------------------------------------------------------===//
300 // Scope manipulation
301 //===----------------------------------------------------------------------===//
303 /// EnterScope - Start a new scope.
304 void Parser::EnterScope(unsigned ScopeFlags) {
305 if (NumCachedScopes) {
306 Scope *N = ScopeCache[--NumCachedScopes];
307 N->Init(getCurScope(), ScopeFlags);
308 Actions.CurScope = N;
309 } else {
310 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
314 /// ExitScope - Pop a scope off the scope stack.
315 void Parser::ExitScope() {
316 assert(getCurScope() && "Scope imbalance!");
318 // Inform the actions module that this scope is going away if there are any
319 // decls in it.
320 if (!getCurScope()->decl_empty())
321 Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
323 Scope *OldScope = getCurScope();
324 Actions.CurScope = OldScope->getParent();
326 if (NumCachedScopes == ScopeCacheSize)
327 delete OldScope;
328 else
329 ScopeCache[NumCachedScopes++] = OldScope;
335 //===----------------------------------------------------------------------===//
336 // C99 6.9: External Definitions.
337 //===----------------------------------------------------------------------===//
339 Parser::~Parser() {
340 // If we still have scopes active, delete the scope tree.
341 delete getCurScope();
342 Actions.CurScope = 0;
344 // Free the scope cache.
345 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
346 delete ScopeCache[i];
348 // Remove the pragma handlers we installed.
349 PP.RemovePragmaHandler(AlignHandler.get());
350 AlignHandler.reset();
351 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
352 GCCVisibilityHandler.reset();
353 PP.RemovePragmaHandler(OptionsHandler.get());
354 OptionsHandler.reset();
355 PP.RemovePragmaHandler(PackHandler.get());
356 PackHandler.reset();
357 PP.RemovePragmaHandler(UnusedHandler.get());
358 UnusedHandler.reset();
359 PP.RemovePragmaHandler(WeakHandler.get());
360 WeakHandler.reset();
361 PP.clearCodeCompletionHandler();
364 /// Initialize - Warm up the parser.
366 void Parser::Initialize() {
367 // Create the translation unit scope. Install it as the current scope.
368 assert(getCurScope() == 0 && "A scope is already active?");
369 EnterScope(Scope::DeclScope);
370 Actions.ActOnTranslationUnitScope(getCurScope());
372 // Prime the lexer look-ahead.
373 ConsumeToken();
375 if (Tok.is(tok::eof) &&
376 !getLang().CPlusPlus) // Empty source file is an extension in C
377 Diag(Tok, diag::ext_empty_source_file);
379 // Initialization for Objective-C context sensitive keywords recognition.
380 // Referenced in Parser::ParseObjCTypeQualifierList.
381 if (getLang().ObjC1) {
382 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
383 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
384 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
385 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
386 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
387 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
390 Ident_final = 0;
391 Ident_override = 0;
393 Ident_super = &PP.getIdentifierTable().get("super");
395 if (getLang().AltiVec) {
396 Ident_vector = &PP.getIdentifierTable().get("vector");
397 Ident_pixel = &PP.getIdentifierTable().get("pixel");
401 /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
402 /// action tells us to. This returns true if the EOF was encountered.
403 bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
405 while (Tok.is(tok::annot_pragma_unused))
406 HandlePragmaUnused();
408 Result = DeclGroupPtrTy();
409 if (Tok.is(tok::eof)) {
410 Actions.ActOnEndOfTranslationUnit();
411 return true;
414 ParsedAttributesWithRange attrs;
415 MaybeParseCXX0XAttributes(attrs);
416 MaybeParseMicrosoftAttributes(attrs);
418 Result = ParseExternalDeclaration(attrs);
419 return false;
422 /// ParseTranslationUnit:
423 /// translation-unit: [C99 6.9]
424 /// external-declaration
425 /// translation-unit external-declaration
426 void Parser::ParseTranslationUnit() {
427 Initialize();
429 DeclGroupPtrTy Res;
430 while (!ParseTopLevelDecl(Res))
431 /*parse them all*/;
433 ExitScope();
434 assert(getCurScope() == 0 && "Scope imbalance!");
437 /// ParseExternalDeclaration:
439 /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
440 /// function-definition
441 /// declaration
442 /// [C++0x] empty-declaration
443 /// [GNU] asm-definition
444 /// [GNU] __extension__ external-declaration
445 /// [OBJC] objc-class-definition
446 /// [OBJC] objc-class-declaration
447 /// [OBJC] objc-alias-declaration
448 /// [OBJC] objc-protocol-definition
449 /// [OBJC] objc-method-definition
450 /// [OBJC] @end
451 /// [C++] linkage-specification
452 /// [GNU] asm-definition:
453 /// simple-asm-expr ';'
455 /// [C++0x] empty-declaration:
456 /// ';'
458 /// [C++0x/GNU] 'extern' 'template' declaration
459 Parser::DeclGroupPtrTy
460 Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
461 ParsingDeclSpec *DS) {
462 ParenBraceBracketBalancer BalancerRAIIObj(*this);
464 Decl *SingleDecl = 0;
465 switch (Tok.getKind()) {
466 case tok::semi:
467 if (!getLang().CPlusPlus0x)
468 Diag(Tok, diag::ext_top_level_semi)
469 << FixItHint::CreateRemoval(Tok.getLocation());
471 ConsumeToken();
472 // TODO: Invoke action for top-level semicolon.
473 return DeclGroupPtrTy();
474 case tok::r_brace:
475 Diag(Tok, diag::err_expected_external_declaration);
476 ConsumeBrace();
477 return DeclGroupPtrTy();
478 case tok::eof:
479 Diag(Tok, diag::err_expected_external_declaration);
480 return DeclGroupPtrTy();
481 case tok::kw___extension__: {
482 // __extension__ silences extension warnings in the subexpression.
483 ExtensionRAIIObject O(Diags); // Use RAII to do this.
484 ConsumeToken();
485 return ParseExternalDeclaration(attrs);
487 case tok::kw_asm: {
488 ProhibitAttributes(attrs);
490 ExprResult Result(ParseSimpleAsm());
492 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
493 "top-level asm block");
495 if (Result.isInvalid())
496 return DeclGroupPtrTy();
497 SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), Result.get());
498 break;
500 case tok::at:
501 // @ is not a legal token unless objc is enabled, no need to check for ObjC.
502 /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
503 /// @class foo, bar;
504 SingleDecl = ParseObjCAtDirectives();
505 break;
506 case tok::minus:
507 case tok::plus:
508 if (!getLang().ObjC1) {
509 Diag(Tok, diag::err_expected_external_declaration);
510 ConsumeToken();
511 return DeclGroupPtrTy();
513 SingleDecl = ParseObjCMethodDefinition();
514 break;
515 case tok::code_completion:
516 Actions.CodeCompleteOrdinaryName(getCurScope(),
517 ObjCImpDecl? Sema::PCC_ObjCImplementation
518 : Sema::PCC_Namespace);
519 ConsumeCodeCompletionToken();
520 return ParseExternalDeclaration(attrs);
521 case tok::kw_using:
522 case tok::kw_namespace:
523 case tok::kw_typedef:
524 case tok::kw_template:
525 case tok::kw_export: // As in 'export template'
526 case tok::kw_static_assert:
527 // A function definition cannot start with a these keywords.
529 SourceLocation DeclEnd;
530 StmtVector Stmts(Actions);
531 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
534 case tok::kw_static:
535 // Parse (then ignore) 'static' prior to a template instantiation. This is
536 // a GCC extension that we intentionally do not support.
537 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
538 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
539 << 0;
540 SourceLocation DeclEnd;
541 StmtVector Stmts(Actions);
542 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
544 goto dont_know;
546 case tok::kw_inline:
547 if (getLang().CPlusPlus) {
548 tok::TokenKind NextKind = NextToken().getKind();
550 // Inline namespaces. Allowed as an extension even in C++03.
551 if (NextKind == tok::kw_namespace) {
552 SourceLocation DeclEnd;
553 StmtVector Stmts(Actions);
554 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
557 // Parse (then ignore) 'inline' prior to a template instantiation. This is
558 // a GCC extension that we intentionally do not support.
559 if (NextKind == tok::kw_template) {
560 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
561 << 1;
562 SourceLocation DeclEnd;
563 StmtVector Stmts(Actions);
564 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
567 goto dont_know;
569 case tok::kw_extern:
570 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
571 // Extern templates
572 SourceLocation ExternLoc = ConsumeToken();
573 SourceLocation TemplateLoc = ConsumeToken();
574 SourceLocation DeclEnd;
575 return Actions.ConvertDeclToDeclGroup(
576 ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
578 // FIXME: Detect C++ linkage specifications here?
579 goto dont_know;
581 default:
582 dont_know:
583 // We can't tell whether this is a function-definition or declaration yet.
584 if (DS) {
585 DS->takeAttributesFrom(attrs);
586 return ParseDeclarationOrFunctionDefinition(*DS);
587 } else {
588 return ParseDeclarationOrFunctionDefinition(attrs);
592 // This routine returns a DeclGroup, if the thing we parsed only contains a
593 // single decl, convert it now.
594 return Actions.ConvertDeclToDeclGroup(SingleDecl);
597 /// \brief Determine whether the current token, if it occurs after a
598 /// declarator, continues a declaration or declaration list.
599 bool Parser::isDeclarationAfterDeclarator() const {
600 return Tok.is(tok::equal) || // int X()= -> not a function def
601 Tok.is(tok::comma) || // int X(), -> not a function def
602 Tok.is(tok::semi) || // int X(); -> not a function def
603 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
604 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
605 (getLang().CPlusPlus &&
606 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
609 /// \brief Determine whether the current token, if it occurs after a
610 /// declarator, indicates the start of a function definition.
611 bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
612 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
613 if (Tok.is(tok::l_brace)) // int X() {}
614 return true;
616 // Handle K&R C argument lists: int X(f) int f; {}
617 if (!getLang().CPlusPlus &&
618 Declarator.getFunctionTypeInfo().isKNRPrototype())
619 return isDeclarationSpecifier();
621 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
622 Tok.is(tok::kw_try); // X() try { ... }
625 /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
626 /// a declaration. We can't tell which we have until we read up to the
627 /// compound-statement in function-definition. TemplateParams, if
628 /// non-NULL, provides the template parameters when we're parsing a
629 /// C++ template-declaration.
631 /// function-definition: [C99 6.9.1]
632 /// decl-specs declarator declaration-list[opt] compound-statement
633 /// [C90] function-definition: [C99 6.7.1] - implicit int result
634 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
636 /// declaration: [C99 6.7]
637 /// declaration-specifiers init-declarator-list[opt] ';'
638 /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
639 /// [OMP] threadprivate-directive [TODO]
641 Parser::DeclGroupPtrTy
642 Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
643 AccessSpecifier AS) {
644 // Parse the common declaration-specifiers piece.
645 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
647 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
648 // declaration-specifiers init-declarator-list[opt] ';'
649 if (Tok.is(tok::semi)) {
650 ConsumeToken();
651 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
652 DS.complete(TheDecl);
653 return Actions.ConvertDeclToDeclGroup(TheDecl);
656 // ObjC2 allows prefix attributes on class interfaces and protocols.
657 // FIXME: This still needs better diagnostics. We should only accept
658 // attributes here, no types, etc.
659 if (getLang().ObjC2 && Tok.is(tok::at)) {
660 SourceLocation AtLoc = ConsumeToken(); // the "@"
661 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
662 !Tok.isObjCAtKeyword(tok::objc_protocol)) {
663 Diag(Tok, diag::err_objc_unexpected_attr);
664 SkipUntil(tok::semi); // FIXME: better skip?
665 return DeclGroupPtrTy();
668 DS.abort();
670 const char *PrevSpec = 0;
671 unsigned DiagID;
672 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
673 Diag(AtLoc, DiagID) << PrevSpec;
675 Decl *TheDecl = 0;
676 if (Tok.isObjCAtKeyword(tok::objc_protocol))
677 TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
678 else
679 TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
680 return Actions.ConvertDeclToDeclGroup(TheDecl);
683 // If the declspec consisted only of 'extern' and we have a string
684 // literal following it, this must be a C++ linkage specifier like
685 // 'extern "C"'.
686 if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
687 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
688 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
689 Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
690 return Actions.ConvertDeclToDeclGroup(TheDecl);
693 return ParseDeclGroup(DS, Declarator::FileContext, true);
696 Parser::DeclGroupPtrTy
697 Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
698 AccessSpecifier AS) {
699 ParsingDeclSpec DS(*this);
700 DS.takeAttributesFrom(attrs);
701 return ParseDeclarationOrFunctionDefinition(DS, AS);
704 /// ParseFunctionDefinition - We parsed and verified that the specified
705 /// Declarator is well formed. If this is a K&R-style function, read the
706 /// parameters declaration-list, then start the compound-statement.
708 /// function-definition: [C99 6.9.1]
709 /// decl-specs declarator declaration-list[opt] compound-statement
710 /// [C90] function-definition: [C99 6.7.1] - implicit int result
711 /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
712 /// [C++] function-definition: [C++ 8.4]
713 /// decl-specifier-seq[opt] declarator ctor-initializer[opt]
714 /// function-body
715 /// [C++] function-definition: [C++ 8.4]
716 /// decl-specifier-seq[opt] declarator function-try-block
718 Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
719 const ParsedTemplateInfo &TemplateInfo) {
720 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
722 // If this is C90 and the declspecs were completely missing, fudge in an
723 // implicit int. We do this here because this is the only place where
724 // declaration-specifiers are completely optional in the grammar.
725 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
726 const char *PrevSpec;
727 unsigned DiagID;
728 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
729 D.getIdentifierLoc(),
730 PrevSpec, DiagID);
731 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
734 // If this declaration was formed with a K&R-style identifier list for the
735 // arguments, parse declarations for all of the args next.
736 // int foo(a,b) int a; float b; {}
737 if (FTI.isKNRPrototype())
738 ParseKNRParamDeclarations(D);
740 // We should have either an opening brace or, in a C++ constructor,
741 // we may have a colon.
742 if (Tok.isNot(tok::l_brace) &&
743 (!getLang().CPlusPlus ||
744 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try)))) {
745 Diag(Tok, diag::err_expected_fn_body);
747 // Skip over garbage, until we get to '{'. Don't eat the '{'.
748 SkipUntil(tok::l_brace, true, true);
750 // If we didn't find the '{', bail out.
751 if (Tok.isNot(tok::l_brace))
752 return 0;
755 // Enter a scope for the function body.
756 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
758 // Tell the actions module that we have entered a function definition with the
759 // specified Declarator for the function.
760 Decl *Res = TemplateInfo.TemplateParams?
761 Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
762 MultiTemplateParamsArg(Actions,
763 TemplateInfo.TemplateParams->data(),
764 TemplateInfo.TemplateParams->size()),
766 : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
768 // Break out of the ParsingDeclarator context before we parse the body.
769 D.complete(Res);
771 // Break out of the ParsingDeclSpec context, too. This const_cast is
772 // safe because we're always the sole owner.
773 D.getMutableDeclSpec().abort();
775 if (Tok.is(tok::kw_try))
776 return ParseFunctionTryBlock(Res);
778 // If we have a colon, then we're probably parsing a C++
779 // ctor-initializer.
780 if (Tok.is(tok::colon)) {
781 ParseConstructorInitializer(Res);
783 // Recover from error.
784 if (!Tok.is(tok::l_brace)) {
785 Actions.ActOnFinishFunctionBody(Res, 0);
786 return Res;
788 } else
789 Actions.ActOnDefaultCtorInitializers(Res);
791 return ParseFunctionStatementBody(Res);
794 /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
795 /// types for a function with a K&R-style identifier list for arguments.
796 void Parser::ParseKNRParamDeclarations(Declarator &D) {
797 // We know that the top-level of this declarator is a function.
798 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
800 // Enter function-declaration scope, limiting any declarators to the
801 // function prototype scope, including parameter declarators.
802 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
804 // Read all the argument declarations.
805 while (isDeclarationSpecifier()) {
806 SourceLocation DSStart = Tok.getLocation();
808 // Parse the common declaration-specifiers piece.
809 DeclSpec DS;
810 ParseDeclarationSpecifiers(DS);
812 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
813 // least one declarator'.
814 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
815 // the declarations though. It's trivial to ignore them, really hard to do
816 // anything else with them.
817 if (Tok.is(tok::semi)) {
818 Diag(DSStart, diag::err_declaration_does_not_declare_param);
819 ConsumeToken();
820 continue;
823 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
824 // than register.
825 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
826 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
827 Diag(DS.getStorageClassSpecLoc(),
828 diag::err_invalid_storage_class_in_func_decl);
829 DS.ClearStorageClassSpecs();
831 if (DS.isThreadSpecified()) {
832 Diag(DS.getThreadSpecLoc(),
833 diag::err_invalid_storage_class_in_func_decl);
834 DS.ClearStorageClassSpecs();
837 // Parse the first declarator attached to this declspec.
838 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
839 ParseDeclarator(ParmDeclarator);
841 // Handle the full declarator list.
842 while (1) {
843 // If attributes are present, parse them.
844 MaybeParseGNUAttributes(ParmDeclarator);
846 // Ask the actions module to compute the type for this declarator.
847 Decl *Param =
848 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
850 if (Param &&
851 // A missing identifier has already been diagnosed.
852 ParmDeclarator.getIdentifier()) {
854 // Scan the argument list looking for the correct param to apply this
855 // type.
856 for (unsigned i = 0; ; ++i) {
857 // C99 6.9.1p6: those declarators shall declare only identifiers from
858 // the identifier list.
859 if (i == FTI.NumArgs) {
860 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
861 << ParmDeclarator.getIdentifier();
862 break;
865 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
866 // Reject redefinitions of parameters.
867 if (FTI.ArgInfo[i].Param) {
868 Diag(ParmDeclarator.getIdentifierLoc(),
869 diag::err_param_redefinition)
870 << ParmDeclarator.getIdentifier();
871 } else {
872 FTI.ArgInfo[i].Param = Param;
874 break;
879 // If we don't have a comma, it is either the end of the list (a ';') or
880 // an error, bail out.
881 if (Tok.isNot(tok::comma))
882 break;
884 // Consume the comma.
885 ConsumeToken();
887 // Parse the next declarator.
888 ParmDeclarator.clear();
889 ParseDeclarator(ParmDeclarator);
892 if (Tok.is(tok::semi)) {
893 ConsumeToken();
894 } else {
895 Diag(Tok, diag::err_parse_error);
896 // Skip to end of block or statement
897 SkipUntil(tok::semi, true);
898 if (Tok.is(tok::semi))
899 ConsumeToken();
903 // The actions module must verify that all arguments were declared.
904 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
908 /// ParseAsmStringLiteral - This is just a normal string-literal, but is not
909 /// allowed to be a wide string, and is not subject to character translation.
911 /// [GNU] asm-string-literal:
912 /// string-literal
914 Parser::ExprResult Parser::ParseAsmStringLiteral() {
915 if (!isTokenStringLiteral()) {
916 Diag(Tok, diag::err_expected_string_literal);
917 return ExprError();
920 ExprResult Res(ParseStringLiteralExpression());
921 if (Res.isInvalid()) return move(Res);
923 // TODO: Diagnose: wide string literal in 'asm'
925 return move(Res);
928 /// ParseSimpleAsm
930 /// [GNU] simple-asm-expr:
931 /// 'asm' '(' asm-string-literal ')'
933 Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
934 assert(Tok.is(tok::kw_asm) && "Not an asm!");
935 SourceLocation Loc = ConsumeToken();
937 if (Tok.is(tok::kw_volatile)) {
938 // Remove from the end of 'asm' to the end of 'volatile'.
939 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
940 PP.getLocForEndOfToken(Tok.getLocation()));
942 Diag(Tok, diag::warn_file_asm_volatile)
943 << FixItHint::CreateRemoval(RemovalRange);
944 ConsumeToken();
947 if (Tok.isNot(tok::l_paren)) {
948 Diag(Tok, diag::err_expected_lparen_after) << "asm";
949 return ExprError();
952 Loc = ConsumeParen();
954 ExprResult Result(ParseAsmStringLiteral());
956 if (Result.isInvalid()) {
957 SkipUntil(tok::r_paren, true, true);
958 if (EndLoc)
959 *EndLoc = Tok.getLocation();
960 ConsumeAnyToken();
961 } else {
962 Loc = MatchRHSPunctuation(tok::r_paren, Loc);
963 if (EndLoc)
964 *EndLoc = Loc;
967 return move(Result);
970 /// TryAnnotateTypeOrScopeToken - If the current token position is on a
971 /// typename (possibly qualified in C++) or a C++ scope specifier not followed
972 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
973 /// with a single annotation token representing the typename or C++ scope
974 /// respectively.
975 /// This simplifies handling of C++ scope specifiers and allows efficient
976 /// backtracking without the need to re-parse and resolve nested-names and
977 /// typenames.
978 /// It will mainly be called when we expect to treat identifiers as typenames
979 /// (if they are typenames). For example, in C we do not expect identifiers
980 /// inside expressions to be treated as typenames so it will not be called
981 /// for expressions in C.
982 /// The benefit for C/ObjC is that a typename will be annotated and
983 /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
984 /// will not be called twice, once to check whether we have a declaration
985 /// specifier, and another one to get the actual type inside
986 /// ParseDeclarationSpecifiers).
988 /// This returns true if an error occurred.
990 /// Note that this routine emits an error if you call it with ::new or ::delete
991 /// as the current tokens, so only call it in contexts where these are invalid.
992 bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
993 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
994 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
995 "Cannot be a type or scope token!");
997 if (Tok.is(tok::kw_typename)) {
998 // Parse a C++ typename-specifier, e.g., "typename T::type".
1000 // typename-specifier:
1001 // 'typename' '::' [opt] nested-name-specifier identifier
1002 // 'typename' '::' [opt] nested-name-specifier template [opt]
1003 // simple-template-id
1004 SourceLocation TypenameLoc = ConsumeToken();
1005 CXXScopeSpec SS;
1006 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(), false))
1007 return true;
1008 if (!SS.isSet()) {
1009 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
1010 return true;
1013 TypeResult Ty;
1014 if (Tok.is(tok::identifier)) {
1015 // FIXME: check whether the next token is '<', first!
1016 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1017 *Tok.getIdentifierInfo(),
1018 Tok.getLocation());
1019 } else if (Tok.is(tok::annot_template_id)) {
1020 TemplateIdAnnotation *TemplateId
1021 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1022 if (TemplateId->Kind == TNK_Function_template) {
1023 Diag(Tok, diag::err_typename_refers_to_non_type_template)
1024 << Tok.getAnnotationRange();
1025 return true;
1028 AnnotateTemplateIdTokenAsType(0);
1029 assert(Tok.is(tok::annot_typename) &&
1030 "AnnotateTemplateIdTokenAsType isn't working properly");
1031 if (Tok.getAnnotationValue())
1032 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
1033 SourceLocation(),
1034 getTypeAnnotation(Tok));
1035 else
1036 Ty = true;
1037 } else {
1038 Diag(Tok, diag::err_expected_type_name_after_typename)
1039 << SS.getRange();
1040 return true;
1043 SourceLocation EndLoc = Tok.getLastLoc();
1044 Tok.setKind(tok::annot_typename);
1045 setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
1046 Tok.setAnnotationEndLoc(EndLoc);
1047 Tok.setLocation(TypenameLoc);
1048 PP.AnnotateCachedTokens(Tok);
1049 return false;
1052 // Remembers whether the token was originally a scope annotation.
1053 bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1055 CXXScopeSpec SS;
1056 if (getLang().CPlusPlus)
1057 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1058 return true;
1060 if (Tok.is(tok::identifier)) {
1061 // Determine whether the identifier is a type name.
1062 if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1063 Tok.getLocation(), getCurScope(),
1064 &SS)) {
1065 // This is a typename. Replace the current token in-place with an
1066 // annotation type token.
1067 Tok.setKind(tok::annot_typename);
1068 setTypeAnnotation(Tok, Ty);
1069 Tok.setAnnotationEndLoc(Tok.getLocation());
1070 if (SS.isNotEmpty()) // it was a C++ qualified type name.
1071 Tok.setLocation(SS.getBeginLoc());
1073 // In case the tokens were cached, have Preprocessor replace
1074 // them with the annotation token.
1075 PP.AnnotateCachedTokens(Tok);
1076 return false;
1079 if (!getLang().CPlusPlus) {
1080 // If we're in C, we can't have :: tokens at all (the lexer won't return
1081 // them). If the identifier is not a type, then it can't be scope either,
1082 // just early exit.
1083 return false;
1086 // If this is a template-id, annotate with a template-id or type token.
1087 if (NextToken().is(tok::less)) {
1088 TemplateTy Template;
1089 UnqualifiedId TemplateName;
1090 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1091 bool MemberOfUnknownSpecialization;
1092 if (TemplateNameKind TNK
1093 = Actions.isTemplateName(getCurScope(), SS,
1094 /*hasTemplateKeyword=*/false, TemplateName,
1095 /*ObjectType=*/ ParsedType(),
1096 EnteringContext,
1097 Template, MemberOfUnknownSpecialization)) {
1098 // Consume the identifier.
1099 ConsumeToken();
1100 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName)) {
1101 // If an unrecoverable error occurred, we need to return true here,
1102 // because the token stream is in a damaged state. We may not return
1103 // a valid identifier.
1104 return true;
1109 // The current token, which is either an identifier or a
1110 // template-id, is not part of the annotation. Fall through to
1111 // push that token back into the stream and complete the C++ scope
1112 // specifier annotation.
1115 if (Tok.is(tok::annot_template_id)) {
1116 TemplateIdAnnotation *TemplateId
1117 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1118 if (TemplateId->Kind == TNK_Type_template) {
1119 // A template-id that refers to a type was parsed into a
1120 // template-id annotation in a context where we weren't allowed
1121 // to produce a type annotation token. Update the template-id
1122 // annotation token to a type annotation token now.
1123 AnnotateTemplateIdTokenAsType(&SS);
1124 return false;
1128 if (SS.isEmpty())
1129 return false;
1131 // A C++ scope specifier that isn't followed by a typename.
1132 // Push the current token back into the token stream (or revert it if it is
1133 // cached) and use an annotation scope token for current token.
1134 if (PP.isBacktrackEnabled())
1135 PP.RevertCachedTokens(1);
1136 else
1137 PP.EnterToken(Tok);
1138 Tok.setKind(tok::annot_cxxscope);
1139 Tok.setAnnotationValue(SS.getScopeRep());
1140 Tok.setAnnotationRange(SS.getRange());
1142 // In case the tokens were cached, have Preprocessor replace them
1143 // with the annotation token. We don't need to do this if we've
1144 // just reverted back to the state we were in before being called.
1145 if (!wasScopeAnnotation)
1146 PP.AnnotateCachedTokens(Tok);
1147 return false;
1150 /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
1151 /// annotates C++ scope specifiers and template-ids. This returns
1152 /// true if the token was annotated or there was an error that could not be
1153 /// recovered from.
1155 /// Note that this routine emits an error if you call it with ::new or ::delete
1156 /// as the current tokens, so only call it in contexts where these are invalid.
1157 bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
1158 assert(getLang().CPlusPlus &&
1159 "Call sites of this function should be guarded by checking for C++");
1160 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1161 "Cannot be a type or scope token!");
1163 CXXScopeSpec SS;
1164 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
1165 return true;
1166 if (SS.isEmpty())
1167 return false;
1169 // Push the current token back into the token stream (or revert it if it is
1170 // cached) and use an annotation scope token for current token.
1171 if (PP.isBacktrackEnabled())
1172 PP.RevertCachedTokens(1);
1173 else
1174 PP.EnterToken(Tok);
1175 Tok.setKind(tok::annot_cxxscope);
1176 Tok.setAnnotationValue(SS.getScopeRep());
1177 Tok.setAnnotationRange(SS.getRange());
1179 // In case the tokens were cached, have Preprocessor replace them with the
1180 // annotation token.
1181 PP.AnnotateCachedTokens(Tok);
1182 return false;
1185 bool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) {
1186 if (Tok.is(tok::equalequal)) {
1187 // We have '==' in a context that we would expect a '='.
1188 // The user probably made a typo, intending to type '='. Emit diagnostic,
1189 // fixit hint to turn '==' -> '=' and continue as if the user typed '='.
1190 Diag(Tok, DiagID)
1191 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()),
1192 getTokenSimpleSpelling(tok::equal));
1193 return true;
1196 return Tok.is(tok::equal);
1199 void Parser::CodeCompletionRecovery() {
1200 for (Scope *S = getCurScope(); S; S = S->getParent()) {
1201 if (S->getFlags() & Scope::FnScope) {
1202 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
1203 return;
1206 if (S->getFlags() & Scope::ClassScope) {
1207 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
1208 return;
1212 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
1215 // Anchor the Parser::FieldCallback vtable to this translation unit.
1216 // We use a spurious method instead of the destructor because
1217 // destroying FieldCallbacks can actually be slightly
1218 // performance-sensitive.
1219 void Parser::FieldCallback::_anchor() {
1222 // Code-completion pass-through functions
1224 void Parser::CodeCompleteDirective(bool InConditional) {
1225 Actions.CodeCompletePreprocessorDirective(InConditional);
1228 void Parser::CodeCompleteInConditionalExclusion() {
1229 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1232 void Parser::CodeCompleteMacroName(bool IsDefinition) {
1233 Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1236 void Parser::CodeCompletePreprocessorExpression() {
1237 Actions.CodeCompletePreprocessorExpression();
1240 void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1241 MacroInfo *MacroInfo,
1242 unsigned ArgumentIndex) {
1243 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1244 ArgumentIndex);
1247 void Parser::CodeCompleteNaturalLanguage() {
1248 Actions.CodeCompleteNaturalLanguage();