blob: 70a336678cdac40ab0c54a73667d67859d2d9a51 [file] [log] [blame]
Chris Lattnereb8a28f2006-08-10 18:43:39 +00001//===--- Parser.cpp - C Language Family Parser ----------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0bb5f832006-07-31 01:59:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner971c6b62006-08-05 22:46:42 +000017#include "clang/Parse/Scope.h"
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000019#include "ExtensionRAIIObject.h"
Daniel Dunbar921b9682008-10-04 19:21:03 +000020#include "ParsePragma.h"
Chris Lattner0bb5f832006-07-31 01:59:18 +000021using namespace clang;
22
Chris Lattner697e5d62006-11-09 06:32:27 +000023Parser::Parser(Preprocessor &pp, Action &actions)
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000024 : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
Douglas Gregor8bf42052009-02-09 18:46:07 +000025 GreaterThanIsOperator(true) {
Chris Lattner8c204872006-10-14 05:19:21 +000026 Tok.setKind(tok::eof);
Chris Lattnere4e38592006-08-14 00:15:05 +000027 CurScope = 0;
Chris Lattner03928c72007-07-15 00:04:39 +000028 NumCachedScopes = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000029 ParenCount = BracketCount = BraceCount = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +000030 ObjCImpDecl = 0;
Daniel Dunbar921b9682008-10-04 19:21:03 +000031
32 // Add #pragma handlers. These are removed and destroyed in the
33 // destructor.
34 PackHandler =
35 new PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions);
36 PP.AddPragmaHandler(0, PackHandler);
37
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000038 // Instantiate a LexedMethodsForTopClass for all the non-nested classes.
39 PushTopClassStack();
Chris Lattner971c6b62006-08-05 22:46:42 +000040}
41
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000042/// If a crash happens while the parser is active, print out a line indicating
43/// what the current token is.
44void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
45 const Token &Tok = P.getCurToken();
46 if (Tok.getLocation().isInvalid()) {
47 OS << "<eof> parser at end of file\n";
48 return;
49 }
50
51 const Preprocessor &PP = P.getPreprocessor();
52 Tok.getLocation().print(OS, PP.getSourceManager());
53 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
Douglas Gregord7c4d982008-12-30 03:27:21 +000054}
Chris Lattner0bb5f832006-07-31 01:59:18 +000055
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000056
Chris Lattner427c9c12008-11-22 00:59:29 +000057DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000058 return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +000059}
60
Chris Lattner427c9c12008-11-22 00:59:29 +000061DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
Chris Lattner6d29c102008-11-18 07:48:38 +000062 return Diag(Tok.getLocation(), DiagID);
Chris Lattner0bb5f832006-07-31 01:59:18 +000063}
64
Douglas Gregor87f95b02009-02-26 21:00:50 +000065/// \brief Emits a diagnostic suggesting parentheses surrounding a
66/// given range.
67///
68/// \param Loc The location where we'll emit the diagnostic.
69/// \param Loc The kind of diagnostic to emit.
70/// \param ParenRange Source range enclosing code that should be parenthesized.
71void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
72 SourceRange ParenRange) {
Douglas Gregor96977da2009-02-27 17:53:17 +000073 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
74 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
Douglas Gregor87f95b02009-02-26 21:00:50 +000075 // We can't display the parentheses, so just dig the
76 // warning/error and return.
77 Diag(Loc, DK);
78 return;
79 }
80
Douglas Gregor87f95b02009-02-26 21:00:50 +000081 Diag(Loc, DK)
Douglas Gregor96977da2009-02-27 17:53:17 +000082 << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(")
83 << CodeModificationHint::CreateInsertion(EndLoc, ")");
Douglas Gregor87f95b02009-02-26 21:00:50 +000084}
85
Chris Lattner4564bc12006-08-10 23:14:52 +000086/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
87/// this helper function matches and consumes the specified RHS token if
88/// present. If not present, it emits the specified diagnostic indicating
89/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
90/// should be the name of the unmatched LHS token.
Chris Lattner71e23ce2006-11-04 20:18:38 +000091SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
92 SourceLocation LHSLoc) {
Mike Stump01e07652008-06-19 19:28:49 +000093
Chris Lattner0ab032a2007-10-09 17:23:58 +000094 if (Tok.is(RHSTok))
Chris Lattner71e23ce2006-11-04 20:18:38 +000095 return ConsumeAnyToken();
Mike Stump01e07652008-06-19 19:28:49 +000096
Chris Lattner71e23ce2006-11-04 20:18:38 +000097 SourceLocation R = Tok.getLocation();
98 const char *LHSName = "unknown";
99 diag::kind DID = diag::err_parse_error;
100 switch (RHSTok) {
101 default: break;
102 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
103 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
104 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner29375652006-12-04 18:06:35 +0000105 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
Chris Lattner4564bc12006-08-10 23:14:52 +0000106 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000107 Diag(Tok, DID);
Chris Lattner03c40412008-11-23 23:17:07 +0000108 Diag(LHSLoc, diag::note_matching) << LHSName;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000109 SkipUntil(RHSTok);
110 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +0000111}
112
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000113/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
114/// input. If so, it is consumed and false is returned.
115///
116/// If the input is malformed, this emits the specified diagnostic. Next, if
117/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
118/// returned.
119bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +0000120 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000121 if (Tok.is(ExpectedTok)) {
Chris Lattner15a00da2006-08-15 04:10:31 +0000122 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000123 return false;
124 }
Mike Stump01e07652008-06-19 19:28:49 +0000125
Douglas Gregor87f95b02009-02-26 21:00:50 +0000126 const char *Spelling = 0;
Douglas Gregor96977da2009-02-27 17:53:17 +0000127 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
128 if (EndLoc.isValid() &&
129 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000130 // Show what code to insert to fix this problem.
Douglas Gregor96977da2009-02-27 17:53:17 +0000131 Diag(EndLoc, DiagID)
Douglas Gregor87f95b02009-02-26 21:00:50 +0000132 << Msg
Douglas Gregor96977da2009-02-27 17:53:17 +0000133 << CodeModificationHint::CreateInsertion(EndLoc, Spelling);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000134 } else
135 Diag(Tok, DiagID) << Msg;
136
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000137 if (SkipToTok != tok::unknown)
138 SkipUntil(SkipToTok);
139 return true;
140}
141
Chris Lattner70f32b72006-07-31 05:09:04 +0000142//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +0000143// Error recovery.
144//===----------------------------------------------------------------------===//
145
146/// SkipUntil - Read tokens until we get to the specified token, then consume
Chris Lattner01e4b242007-07-24 17:03:04 +0000147/// it (unless DontConsume is true). Because we cannot guarantee that the
Chris Lattnereec40f92006-08-06 21:55:29 +0000148/// token will ever occur, this skips to the next token, or to some likely
149/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
150/// character.
Mike Stump01e07652008-06-19 19:28:49 +0000151///
Chris Lattnereec40f92006-08-06 21:55:29 +0000152/// If SkipUntil finds the specified token, it returns true, otherwise it
Mike Stump01e07652008-06-19 19:28:49 +0000153/// returns false.
Chris Lattner83b94e02007-04-27 19:12:15 +0000154bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
155 bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +0000156 // We always want this function to skip at least one token if the first token
157 // isn't T and if not at EOF.
158 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000159 while (1) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000160 // If we found one of the tokens, stop and return true.
161 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000162 if (Tok.is(Toks[i])) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000163 if (DontConsume) {
164 // Noop, don't consume the token.
165 } else {
166 ConsumeAnyToken();
167 }
168 return true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000169 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000170 }
Mike Stump01e07652008-06-19 19:28:49 +0000171
Chris Lattnereec40f92006-08-06 21:55:29 +0000172 switch (Tok.getKind()) {
173 case tok::eof:
174 // Ran out of tokens.
175 return false;
Mike Stump01e07652008-06-19 19:28:49 +0000176
Chris Lattnereec40f92006-08-06 21:55:29 +0000177 case tok::l_paren:
178 // Recursively skip properly-nested parens.
179 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000180 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000181 break;
182 case tok::l_square:
183 // Recursively skip properly-nested square brackets.
184 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000185 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000186 break;
187 case tok::l_brace:
188 // Recursively skip properly-nested braces.
189 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000190 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000191 break;
Mike Stump01e07652008-06-19 19:28:49 +0000192
Chris Lattnereec40f92006-08-06 21:55:29 +0000193 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
194 // Since the user wasn't looking for this token (if they were, it would
195 // already be handled), this isn't balanced. If there is a LHS token at a
196 // higher level, we will assume that this matches the unbalanced token
197 // and return it. Otherwise, this is a spurious RHS token, which we skip.
198 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000199 if (ParenCount && !isFirstTokenSkipped)
200 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000201 ConsumeParen();
202 break;
203 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000204 if (BracketCount && !isFirstTokenSkipped)
205 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000206 ConsumeBracket();
207 break;
208 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000209 if (BraceCount && !isFirstTokenSkipped)
210 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000211 ConsumeBrace();
212 break;
Mike Stump01e07652008-06-19 19:28:49 +0000213
Chris Lattnereec40f92006-08-06 21:55:29 +0000214 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000215 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000216 ConsumeStringToken();
217 break;
218 case tok::semi:
219 if (StopAtSemi)
220 return false;
221 // FALL THROUGH.
222 default:
223 // Skip this token.
224 ConsumeToken();
225 break;
226 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000227 isFirstTokenSkipped = false;
Mike Stump01e07652008-06-19 19:28:49 +0000228 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000229}
230
231//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000232// Scope manipulation
233//===----------------------------------------------------------------------===//
234
235/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000236void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattner03928c72007-07-15 00:04:39 +0000237 if (NumCachedScopes) {
238 Scope *N = ScopeCache[--NumCachedScopes];
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000239 N->Init(CurScope, ScopeFlags);
240 CurScope = N;
241 } else {
242 CurScope = new Scope(CurScope, ScopeFlags);
243 }
Chris Lattnere4e38592006-08-14 00:15:05 +0000244}
245
246/// ExitScope - Pop a scope off the scope stack.
247void Parser::ExitScope() {
248 assert(CurScope && "Scope imbalance!");
249
Chris Lattner87547e62007-10-09 20:37:18 +0000250 // Inform the actions module that this scope is going away if there are any
251 // decls in it.
252 if (!CurScope->decl_empty())
Steve Naroffc62adb62007-10-09 22:01:59 +0000253 Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Mike Stump01e07652008-06-19 19:28:49 +0000254
Chris Lattner03928c72007-07-15 00:04:39 +0000255 Scope *OldScope = CurScope;
256 CurScope = OldScope->getParent();
Mike Stump01e07652008-06-19 19:28:49 +0000257
Chris Lattner03928c72007-07-15 00:04:39 +0000258 if (NumCachedScopes == ScopeCacheSize)
259 delete OldScope;
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000260 else
Chris Lattner03928c72007-07-15 00:04:39 +0000261 ScopeCache[NumCachedScopes++] = OldScope;
Chris Lattnere4e38592006-08-14 00:15:05 +0000262}
263
264
265
266
267//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000268// C99 6.9: External Definitions.
269//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000270
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000271Parser::~Parser() {
272 // If we still have scopes active, delete the scope tree.
273 delete CurScope;
Mike Stump01e07652008-06-19 19:28:49 +0000274
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000275 // Free the scope cache.
Chris Lattner03928c72007-07-15 00:04:39 +0000276 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
277 delete ScopeCache[i];
Daniel Dunbar921b9682008-10-04 19:21:03 +0000278
279 // Remove the pragma handlers we installed.
280 PP.RemovePragmaHandler(0, PackHandler);
281 delete PackHandler;
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000282}
283
Chris Lattner38ba3362006-08-17 07:04:37 +0000284/// Initialize - Warm up the parser.
285///
286void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000287 // Prime the lexer look-ahead.
288 ConsumeToken();
Mike Stump01e07652008-06-19 19:28:49 +0000289
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000290 // Create the translation unit scope. Install it as the current scope.
Chris Lattnere4e38592006-08-14 00:15:05 +0000291 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000292 EnterScope(Scope::DeclScope);
Steve Naroffc62adb62007-10-09 22:01:59 +0000293 Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
Mike Stump01e07652008-06-19 19:28:49 +0000294
Chris Lattner0ab032a2007-10-09 17:23:58 +0000295 if (Tok.is(tok::eof) &&
Chris Lattner66b67ef2007-08-25 05:47:03 +0000296 !getLang().CPlusPlus) // Empty source file is an extension in C
Chris Lattnerbd638922006-11-10 05:19:25 +0000297 Diag(Tok, diag::ext_empty_source_file);
Mike Stump01e07652008-06-19 19:28:49 +0000298
Chris Lattner66782842007-08-29 22:54:08 +0000299 // Initialization for Objective-C context sensitive keywords recognition.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000300 // Referenced in Parser::ParseObjCTypeQualifierList.
Chris Lattner66782842007-08-29 22:54:08 +0000301 if (getLang().ObjC1) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000302 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
303 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
304 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
305 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
306 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
307 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
Chris Lattner66782842007-08-29 22:54:08 +0000308 }
Daniel Dunbar12c9ddc2008-08-14 22:04:54 +0000309
310 Ident_super = &PP.getIdentifierTable().get("super");
Chris Lattner38ba3362006-08-17 07:04:37 +0000311}
312
313/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
314/// action tells us to. This returns true if the EOF was encountered.
Steve Naroff205ec3d2007-11-29 23:05:20 +0000315bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
316 Result = 0;
Chris Lattnerf4404402008-08-23 03:19:52 +0000317 if (Tok.is(tok::eof)) {
318 Actions.ActOnEndOfTranslationUnit();
319 return true;
320 }
Mike Stump01e07652008-06-19 19:28:49 +0000321
Steve Naroff205ec3d2007-11-29 23:05:20 +0000322 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000323 return false;
324}
325
Chris Lattner38ba3362006-08-17 07:04:37 +0000326/// ParseTranslationUnit:
327/// translation-unit: [C99 6.9]
Mike Stump01e07652008-06-19 19:28:49 +0000328/// external-declaration
329/// translation-unit external-declaration
Chris Lattner38ba3362006-08-17 07:04:37 +0000330void Parser::ParseTranslationUnit() {
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000331 Initialize();
Mike Stump01e07652008-06-19 19:28:49 +0000332
Steve Naroff205ec3d2007-11-29 23:05:20 +0000333 DeclTy *Res;
334 while (!ParseTopLevelDecl(Res))
Chris Lattner38ba3362006-08-17 07:04:37 +0000335 /*parse them all*/;
Chris Lattner2cc35ae2008-08-23 02:00:52 +0000336
337 ExitScope();
338 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner38ba3362006-08-17 07:04:37 +0000339}
340
Chris Lattner0bb5f832006-07-31 01:59:18 +0000341/// ParseExternalDeclaration:
Chris Lattner46415262008-12-08 21:59:01 +0000342///
Douglas Gregor15799fd2008-11-21 16:10:08 +0000343/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
Chris Lattnercccc3112007-08-10 20:57:02 +0000344/// function-definition
345/// declaration
Chris Lattner0bb5f832006-07-31 01:59:18 +0000346/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000347/// [GNU] asm-definition
Chris Lattnercccc3112007-08-10 20:57:02 +0000348/// [GNU] __extension__ external-declaration
Chris Lattner40f16b52006-11-05 02:05:37 +0000349/// [OBJC] objc-class-definition
350/// [OBJC] objc-class-declaration
351/// [OBJC] objc-alias-declaration
352/// [OBJC] objc-protocol-definition
353/// [OBJC] objc-method-definition
354/// [OBJC] @end
Douglas Gregor15799fd2008-11-21 16:10:08 +0000355/// [C++] linkage-specification
Chris Lattner6d7e6342006-08-15 03:41:14 +0000356/// [GNU] asm-definition:
357/// simple-asm-expr ';'
358///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000359Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000360 switch (Tok.getKind()) {
361 case tok::semi:
Chris Lattnerbd638922006-11-10 05:19:25 +0000362 Diag(Tok, diag::ext_top_level_semi);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000363 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000364 // TODO: Invoke action for top-level semicolon.
365 return 0;
Chris Lattner46415262008-12-08 21:59:01 +0000366 case tok::r_brace:
367 Diag(Tok, diag::err_expected_external_declaration);
368 ConsumeBrace();
369 return 0;
370 case tok::eof:
371 Diag(Tok, diag::err_expected_external_declaration);
372 return 0;
Chris Lattnercccc3112007-08-10 20:57:02 +0000373 case tok::kw___extension__: {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000374 // __extension__ silences extension warnings in the subexpression.
375 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner1ff6e732008-10-20 06:51:33 +0000376 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000377 return ParseExternalDeclaration();
Chris Lattnercccc3112007-08-10 20:57:02 +0000378 }
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000379 case tok::kw_asm: {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000380 OwningExprResult Result(ParseSimpleAsm());
Mike Stump01e07652008-06-19 19:28:49 +0000381
Anders Carlsson0fae4f52008-02-08 00:23:11 +0000382 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
383 "top-level asm block");
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000384
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000385 if (!Result.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000386 return Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
Chris Lattnera120a522008-05-27 23:32:43 +0000387 return 0;
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000388 }
Steve Naroffb419d3a2006-10-27 23:18:49 +0000389 case tok::at:
Chris Lattnerc24278d2007-05-02 23:45:06 +0000390 // @ is not a legal token unless objc is enabled, no need to check.
Steve Naroffacb1e742007-09-10 20:51:04 +0000391 return ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000392 case tok::minus:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000393 case tok::plus:
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000394 if (getLang().ObjC1)
Steve Naroff7b8fa472007-11-13 23:01:27 +0000395 return ParseObjCMethodDefinition();
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000396 else {
Chris Lattnerc24278d2007-05-02 23:45:06 +0000397 Diag(Tok, diag::err_expected_external_declaration);
398 ConsumeToken();
399 }
Steve Naroffb419d3a2006-10-27 23:18:49 +0000400 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000401 case tok::kw_using:
Chris Lattnera5235172007-08-25 06:57:03 +0000402 case tok::kw_namespace:
Chris Lattner302b4be2006-11-19 02:31:38 +0000403 case tok::kw_typedef:
Douglas Gregoreb31f392008-12-01 23:54:00 +0000404 case tok::kw_template:
405 case tok::kw_export: // As in 'export template'
Chris Lattner479ed3a2007-08-25 18:15:16 +0000406 // A function definition cannot start with a these keywords.
Chris Lattner302b4be2006-11-19 02:31:38 +0000407 return ParseDeclaration(Declarator::FileContext);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000408 default:
409 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000410 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000411 }
412}
413
414/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000415/// a declaration. We can't tell which we have until we read up to the
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000416/// compound-statement in function-definition. TemplateParams, if
417/// non-NULL, provides the template parameters when we're parsing a
418/// C++ template-declaration.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000419///
Chris Lattner70f32b72006-07-31 05:09:04 +0000420/// function-definition: [C99 6.9.1]
Chris Lattner94fc8062008-04-05 05:52:15 +0000421/// decl-specs declarator declaration-list[opt] compound-statement
422/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stump01e07652008-06-19 19:28:49 +0000423/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Chris Lattner94fc8062008-04-05 05:52:15 +0000424///
Chris Lattner70f32b72006-07-31 05:09:04 +0000425/// declaration: [C99 6.7]
Chris Lattnerf2659392007-08-22 06:06:56 +0000426/// declaration-specifiers init-declarator-list[opt] ';'
427/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner70f32b72006-07-31 05:09:04 +0000428/// [OMP] threadprivate-directive [TODO]
429///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000430Parser::DeclTy *
431Parser::ParseDeclarationOrFunctionDefinition(
432 TemplateParameterLists *TemplateParams) {
Chris Lattner70f32b72006-07-31 05:09:04 +0000433 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000434 DeclSpec DS;
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000435 ParseDeclarationSpecifiers(DS, TemplateParams);
Mike Stump01e07652008-06-19 19:28:49 +0000436
Chris Lattnerd2864882006-08-05 08:09:44 +0000437 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000438 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0ab032a2007-10-09 17:23:58 +0000439 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000440 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000441 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000442 }
Mike Stump01e07652008-06-19 19:28:49 +0000443
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000444 // ObjC2 allows prefix attributes on class interfaces and protocols.
445 // FIXME: This still needs better diagnostics. We should only accept
446 // attributes here, no types, etc.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000447 if (getLang().ObjC2 && Tok.is(tok::at)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000448 SourceLocation AtLoc = ConsumeToken(); // the "@"
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000449 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
450 !Tok.isObjCAtKeyword(tok::objc_protocol)) {
451 Diag(Tok, diag::err_objc_unexpected_attr);
Chris Lattner5e530bc2007-12-27 19:57:00 +0000452 SkipUntil(tok::semi); // FIXME: better skip?
453 return 0;
454 }
Fariborz Jahanian056e3a42008-01-02 19:17:38 +0000455 const char *PrevSpec = 0;
456 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec))
Chris Lattner6d29c102008-11-18 07:48:38 +0000457 Diag(AtLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000458 if (Tok.isObjCAtKeyword(tok::objc_protocol))
459 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
Mike Stump01e07652008-06-19 19:28:49 +0000460 return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000461 }
Mike Stump01e07652008-06-19 19:28:49 +0000462
Chris Lattner38376f12008-01-12 07:05:38 +0000463 // If the declspec consisted only of 'extern' and we have a string
464 // literal following it, this must be a C++ linkage specifier like
465 // 'extern "C"'.
Chris Lattner65531e82008-01-12 07:08:43 +0000466 if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
Chris Lattner38376f12008-01-12 07:05:38 +0000467 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
468 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier)
469 return ParseLinkage(Declarator::FileContext);
470
Chris Lattnerfff824f2006-08-07 06:31:38 +0000471 // Parse the first declarator.
472 Declarator DeclaratorInfo(DS, Declarator::FileContext);
473 ParseDeclarator(DeclaratorInfo);
474 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +0000475 if (!DeclaratorInfo.hasName()) {
Chris Lattnerfff824f2006-08-07 06:31:38 +0000476 // If so, skip until the semi-colon or a }.
Douglas Gregorda747ba2008-12-01 23:03:32 +0000477 SkipUntil(tok::r_brace, true, true);
Chris Lattner0ab032a2007-10-09 17:23:58 +0000478 if (Tok.is(tok::semi))
Chris Lattnerfff824f2006-08-07 06:31:38 +0000479 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000480 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000481 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000482
Chris Lattnerfff824f2006-08-07 06:31:38 +0000483 // If the declarator is the start of a function definition, handle it.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000484 if (Tok.is(tok::equal) || // int X()= -> not a function def
485 Tok.is(tok::comma) || // int X(), -> not a function def
486 Tok.is(tok::semi) || // int X(); -> not a function def
487 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000488 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
489 (getLang().CPlusPlus &&
490 Tok.is(tok::l_paren)) ) { // int X(0) -> not a function def [C++]
Chris Lattnerfff824f2006-08-07 06:31:38 +0000491 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000492 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Argyrios Kyrtzidise6aff3d2008-06-21 10:00:56 +0000493 (Tok.is(tok::l_brace) || // int X() {}
Chris Lattner7094c152009-02-27 17:15:01 +0000494 (!getLang().CPlusPlus &&
495 isDeclarationSpecifier()))) { // int X(f) int f; {}
Steve Narofff6319972008-02-14 02:58:32 +0000496 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
497 Diag(Tok, diag::err_function_declared_typedef);
Mike Stump01e07652008-06-19 19:28:49 +0000498
Steve Narofff6319972008-02-14 02:58:32 +0000499 if (Tok.is(tok::l_brace)) {
500 // This recovery skips the entire function body. It would be nice
Douglas Gregor5101c242008-12-05 18:15:24 +0000501 // to simply call ParseFunctionDefinition() below, however Sema
Steve Narofff6319972008-02-14 02:58:32 +0000502 // assumes the declarator represents a function, not a typedef.
503 ConsumeBrace();
504 SkipUntil(tok::r_brace, true);
505 } else {
506 SkipUntil(tok::semi);
507 }
508 return 0;
509 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000510 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000511 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000512 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000513 Diag(Tok, diag::err_expected_fn_body);
514 else
Chris Lattner7094c152009-02-27 17:15:01 +0000515 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000516 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000517 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000518 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000519
Chris Lattner53361ac2006-08-10 05:19:57 +0000520 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000521 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000522}
523
Chris Lattnerfff824f2006-08-07 06:31:38 +0000524/// ParseFunctionDefinition - We parsed and verified that the specified
525/// Declarator is well formed. If this is a K&R-style function, read the
526/// parameters declaration-list, then start the compound-statement.
527///
Chris Lattner94fc8062008-04-05 05:52:15 +0000528/// function-definition: [C99 6.9.1]
529/// decl-specs declarator declaration-list[opt] compound-statement
530/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stump01e07652008-06-19 19:28:49 +0000531/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Douglas Gregore8381c02008-11-05 04:29:56 +0000532/// [C++] function-definition: [C++ 8.4]
533/// decl-specifier-seq[opt] declarator ctor-initializer[opt] function-body
534/// [C++] function-definition: [C++ 8.4]
535/// decl-specifier-seq[opt] declarator function-try-block [TODO]
Chris Lattnerfff824f2006-08-07 06:31:38 +0000536///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000537Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnercbc426d2006-12-02 06:43:02 +0000538 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
539 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000540 "This isn't a function declarator!");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000541 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Mike Stump01e07652008-06-19 19:28:49 +0000542
Chris Lattner94fc8062008-04-05 05:52:15 +0000543 // If this is C90 and the declspecs were completely missing, fudge in an
544 // implicit int. We do this here because this is the only place where
545 // declaration-specifiers are completely optional in the grammar.
Chris Lattnere0c51162009-02-27 18:35:46 +0000546 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
Chris Lattner94fc8062008-04-05 05:52:15 +0000547 const char *PrevSpec;
Chris Lattnerfcc390a2008-10-20 02:01:34 +0000548 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
549 D.getIdentifierLoc(),
550 PrevSpec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000551 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
Chris Lattner94fc8062008-04-05 05:52:15 +0000552 }
Mike Stump01e07652008-06-19 19:28:49 +0000553
Chris Lattnerfff824f2006-08-07 06:31:38 +0000554 // If this declaration was formed with a K&R-style identifier list for the
555 // arguments, parse declarations for all of the args next.
556 // int foo(a,b) int a; float b; {}
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000557 if (!FTI.hasPrototype && FTI.NumArgs != 0)
558 ParseKNRParamDeclarations(D);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000559
Douglas Gregore8381c02008-11-05 04:29:56 +0000560 // We should have either an opening brace or, in a C++ constructor,
561 // we may have a colon.
Sebastian Redl198a5832008-11-24 21:45:59 +0000562 // FIXME: In C++, we might also find the 'try' keyword.
Douglas Gregore8381c02008-11-05 04:29:56 +0000563 if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000564 Diag(Tok, diag::err_expected_fn_body);
565
566 // Skip over garbage, until we get to '{'. Don't eat the '{'.
567 SkipUntil(tok::l_brace, true, true);
Mike Stump01e07652008-06-19 19:28:49 +0000568
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000569 // If we didn't find the '{', bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000570 if (Tok.isNot(tok::l_brace))
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000571 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000572 }
Mike Stump01e07652008-06-19 19:28:49 +0000573
Chris Lattnera55a2cc2007-10-09 17:14:05 +0000574 // Enter a scope for the function body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000575 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump01e07652008-06-19 19:28:49 +0000576
Chris Lattnera55a2cc2007-10-09 17:14:05 +0000577 // Tell the actions module that we have entered a function definition with the
578 // specified Declarator for the function.
579 DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
Mike Stump01e07652008-06-19 19:28:49 +0000580
Douglas Gregore8381c02008-11-05 04:29:56 +0000581 // If we have a colon, then we're probably parsing a C++
582 // ctor-initializer.
583 if (Tok.is(tok::colon))
584 ParseConstructorInitializer(Res);
585
586 SourceLocation BraceLoc = Tok.getLocation();
Chris Lattner12f2ea52009-03-05 00:49:17 +0000587 return ParseFunctionStatementBody(Res);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000588}
589
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000590/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
591/// types for a function with a K&R-style identifier list for arguments.
592void Parser::ParseKNRParamDeclarations(Declarator &D) {
593 // We know that the top-level of this declarator is a function.
594 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
595
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000596 // Enter function-declaration scope, limiting any declarators to the
597 // function prototype scope, including parameter declarators.
Douglas Gregor658b9552009-01-09 22:42:13 +0000598 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000599
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000600 // Read all the argument declarations.
601 while (isDeclarationSpecifier()) {
602 SourceLocation DSStart = Tok.getLocation();
Mike Stump01e07652008-06-19 19:28:49 +0000603
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000604 // Parse the common declaration-specifiers piece.
605 DeclSpec DS;
606 ParseDeclarationSpecifiers(DS);
Mike Stump01e07652008-06-19 19:28:49 +0000607
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000608 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
609 // least one declarator'.
610 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
611 // the declarations though. It's trivial to ignore them, really hard to do
612 // anything else with them.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000613 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000614 Diag(DSStart, diag::err_declaration_does_not_declare_param);
615 ConsumeToken();
616 continue;
617 }
Mike Stump01e07652008-06-19 19:28:49 +0000618
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000619 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
620 // than register.
621 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
622 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
623 Diag(DS.getStorageClassSpecLoc(),
624 diag::err_invalid_storage_class_in_func_decl);
625 DS.ClearStorageClassSpecs();
626 }
627 if (DS.isThreadSpecified()) {
628 Diag(DS.getThreadSpecLoc(),
629 diag::err_invalid_storage_class_in_func_decl);
630 DS.ClearStorageClassSpecs();
631 }
Mike Stump01e07652008-06-19 19:28:49 +0000632
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000633 // Parse the first declarator attached to this declspec.
634 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
635 ParseDeclarator(ParmDeclarator);
636
637 // Handle the full declarator list.
638 while (1) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000639 DeclTy *AttrList;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000640 // If attributes are present, parse them.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000641 if (Tok.is(tok::kw___attribute))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000642 // FIXME: attach attributes too.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000643 AttrList = ParseAttributes();
Mike Stump01e07652008-06-19 19:28:49 +0000644
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000645 // Ask the actions module to compute the type for this declarator.
Mike Stump01e07652008-06-19 19:28:49 +0000646 Action::DeclTy *Param =
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000647 Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
Steve Naroffacb1e742007-09-10 20:51:04 +0000648
Mike Stump01e07652008-06-19 19:28:49 +0000649 if (Param &&
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000650 // A missing identifier has already been diagnosed.
651 ParmDeclarator.getIdentifier()) {
652
653 // Scan the argument list looking for the correct param to apply this
654 // type.
655 for (unsigned i = 0; ; ++i) {
656 // C99 6.9.1p6: those declarators shall declare only identifiers from
657 // the identifier list.
658 if (i == FTI.NumArgs) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000659 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
Chris Lattner760d19ad2008-11-19 07:51:13 +0000660 << ParmDeclarator.getIdentifier();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000661 break;
662 }
Mike Stump01e07652008-06-19 19:28:49 +0000663
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000664 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
665 // Reject redefinitions of parameters.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000666 if (FTI.ArgInfo[i].Param) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000667 Diag(ParmDeclarator.getIdentifierLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +0000668 diag::err_param_redefinition)
Chris Lattner760d19ad2008-11-19 07:51:13 +0000669 << ParmDeclarator.getIdentifier();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000670 } else {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000671 FTI.ArgInfo[i].Param = Param;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000672 }
673 break;
674 }
675 }
676 }
677
678 // If we don't have a comma, it is either the end of the list (a ';') or
679 // an error, bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000680 if (Tok.isNot(tok::comma))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000681 break;
Mike Stump01e07652008-06-19 19:28:49 +0000682
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000683 // Consume the comma.
684 ConsumeToken();
Mike Stump01e07652008-06-19 19:28:49 +0000685
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000686 // Parse the next declarator.
687 ParmDeclarator.clear();
688 ParseDeclarator(ParmDeclarator);
689 }
Mike Stump01e07652008-06-19 19:28:49 +0000690
Chris Lattner0ab032a2007-10-09 17:23:58 +0000691 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000692 ConsumeToken();
693 } else {
694 Diag(Tok, diag::err_parse_error);
695 // Skip to end of block or statement
696 SkipUntil(tok::semi, true);
Chris Lattner0ab032a2007-10-09 17:23:58 +0000697 if (Tok.is(tok::semi))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000698 ConsumeToken();
699 }
700 }
Mike Stump01e07652008-06-19 19:28:49 +0000701
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000702 // The actions module must verify that all arguments were declared.
Douglas Gregor9aa89042009-01-23 16:23:13 +0000703 Actions.ActOnFinishKNRParamDeclarations(CurScope, D);
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000704}
705
706
Chris Lattner0116c472006-08-15 06:03:28 +0000707/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
708/// allowed to be a wide string, and is not subject to character translation.
709///
710/// [GNU] asm-string-literal:
711/// string-literal
712///
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000713Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000714 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000715 Diag(Tok, diag::err_expected_string_literal);
Sebastian Redl042ad952008-12-11 19:30:53 +0000716 return ExprError();
Chris Lattner0116c472006-08-15 06:03:28 +0000717 }
Mike Stump01e07652008-06-19 19:28:49 +0000718
Sebastian Redld65cea82008-12-11 22:51:44 +0000719 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000720 if (Res.isInvalid()) return move(Res);
Mike Stump01e07652008-06-19 19:28:49 +0000721
Chris Lattner0116c472006-08-15 06:03:28 +0000722 // TODO: Diagnose: wide string literal in 'asm'
Mike Stump01e07652008-06-19 19:28:49 +0000723
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000724 return move(Res);
Chris Lattner0116c472006-08-15 06:03:28 +0000725}
726
Chris Lattner6d7e6342006-08-15 03:41:14 +0000727/// ParseSimpleAsm
728///
729/// [GNU] simple-asm-expr:
730/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000731///
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000732Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000733 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000734 SourceLocation Loc = ConsumeToken();
Mike Stump01e07652008-06-19 19:28:49 +0000735
Chris Lattner0ab032a2007-10-09 17:23:58 +0000736 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000737 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Sebastian Redl042ad952008-12-11 19:30:53 +0000738 return ExprError();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000739 }
Mike Stump01e07652008-06-19 19:28:49 +0000740
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000741 Loc = ConsumeParen();
Mike Stump01e07652008-06-19 19:28:49 +0000742
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000743 OwningExprResult Result(ParseAsmStringLiteral());
Mike Stump01e07652008-06-19 19:28:49 +0000744
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000745 if (Result.isInvalid()) {
746 SkipUntil(tok::r_paren, true, true);
747 if (EndLoc)
748 *EndLoc = Tok.getLocation();
749 ConsumeAnyToken();
750 } else {
751 Loc = MatchRHSPunctuation(tok::r_paren, Loc);
752 if (EndLoc)
753 *EndLoc = Loc;
754 }
Mike Stump01e07652008-06-19 19:28:49 +0000755
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000756 return move(Result);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000757}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000758
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000759/// TryAnnotateTypeOrScopeToken - If the current token position is on a
760/// typename (possibly qualified in C++) or a C++ scope specifier not followed
761/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
762/// with a single annotation token representing the typename or C++ scope
763/// respectively.
764/// This simplifies handling of C++ scope specifiers and allows efficient
765/// backtracking without the need to re-parse and resolve nested-names and
766/// typenames.
Argyrios Kyrtzidis0c4162a2008-11-26 21:51:07 +0000767/// It will mainly be called when we expect to treat identifiers as typenames
768/// (if they are typenames). For example, in C we do not expect identifiers
769/// inside expressions to be treated as typenames so it will not be called
770/// for expressions in C.
771/// The benefit for C/ObjC is that a typename will be annotated and
Steve Naroff16c8e592009-01-28 19:39:02 +0000772/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
Argyrios Kyrtzidis0c4162a2008-11-26 21:51:07 +0000773/// will not be called twice, once to check whether we have a declaration
774/// specifier, and another one to get the actual type inside
775/// ParseDeclarationSpecifiers).
Chris Lattner9a8968b2009-01-04 23:23:14 +0000776///
777/// This returns true if the token was annotated.
Chris Lattner45ddec32009-01-05 00:13:00 +0000778///
779/// Note that this routine emits an error if you call it with ::new or ::delete
780/// as the current tokens, so only call it in contexts where these are invalid.
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000781bool Parser::TryAnnotateTypeOrScopeToken() {
Chris Lattnerb5134c02009-01-05 01:24:05 +0000782 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
783 "Cannot be a type or scope token!");
784
785 // FIXME: Implement template-ids
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000786 CXXScopeSpec SS;
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +0000787 if (getLang().CPlusPlus)
Chris Lattnera448d752009-01-06 06:59:53 +0000788 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000789
790 if (Tok.is(tok::identifier)) {
Chris Lattnerda030082009-01-05 01:49:50 +0000791 // Determine whether the identifier is a type name.
Steve Naroff16c8e592009-01-28 19:39:02 +0000792 if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000793 Tok.getLocation(), CurScope, &SS)) {
Chris Lattnerda030082009-01-05 01:49:50 +0000794 // This is a typename. Replace the current token in-place with an
795 // annotation type token.
Chris Lattnera8a3f732009-01-06 05:06:21 +0000796 Tok.setKind(tok::annot_typename);
Chris Lattnerda030082009-01-05 01:49:50 +0000797 Tok.setAnnotationValue(Ty);
798 Tok.setAnnotationEndLoc(Tok.getLocation());
799 if (SS.isNotEmpty()) // it was a C++ qualified type name.
800 Tok.setLocation(SS.getBeginLoc());
801
802 // In case the tokens were cached, have Preprocessor replace
803 // them with the annotation token.
804 PP.AnnotateCachedTokens(Tok);
805 return true;
Douglas Gregor7f741122009-02-25 19:37:18 +0000806 }
807
808 if (!getLang().CPlusPlus) {
Chris Lattnerda030082009-01-05 01:49:50 +0000809 // If we're in C, we can't have :: tokens at all (the lexer won't return
810 // them). If the identifier is not a type, then it can't be scope either,
811 // just early exit.
812 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000813 }
Chris Lattnerda030082009-01-05 01:49:50 +0000814
Douglas Gregor7f741122009-02-25 19:37:18 +0000815 // If this is a template-id, annotate with a template-id or type token.
Douglas Gregor8bf42052009-02-09 18:46:07 +0000816 if (NextToken().is(tok::less)) {
817 DeclTy *Template;
818 if (TemplateNameKind TNK
819 = Actions.isTemplateName(*Tok.getIdentifierInfo(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000820 CurScope, Template, &SS))
Douglas Gregor8bf42052009-02-09 18:46:07 +0000821 AnnotateTemplateIdToken(Template, TNK, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000822 }
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000823
Douglas Gregor7f741122009-02-25 19:37:18 +0000824 // The current token, which is either an identifier or a
825 // template-id, is not part of the annotation. Fall through to
826 // push that token back into the stream and complete the C++ scope
827 // specifier annotation.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000828 }
829
Douglas Gregor7f741122009-02-25 19:37:18 +0000830 if (Tok.is(tok::annot_template_id)) {
831 TemplateIdAnnotation *TemplateId
832 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
833 if (TemplateId->Kind == TNK_Class_template) {
834 // A template-id that refers to a type was parsed into a
835 // template-id annotation in a context where we weren't allowed
836 // to produce a type annotation token. Update the template-id
837 // annotation token to a type annotation token now.
838 return !AnnotateTemplateIdTokenAsType(&SS);
839 }
840 }
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000841
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000842 if (SS.isEmpty())
Chris Lattner9a8968b2009-01-04 23:23:14 +0000843 return false;
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000844
845 // A C++ scope specifier that isn't followed by a typename.
846 // Push the current token back into the token stream (or revert it if it is
847 // cached) and use an annotation scope token for current token.
848 if (PP.isBacktrackEnabled())
849 PP.RevertCachedTokens(1);
850 else
851 PP.EnterToken(Tok);
852 Tok.setKind(tok::annot_cxxscope);
853 Tok.setAnnotationValue(SS.getScopeRep());
854 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000855
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000856 // In case the tokens were cached, have Preprocessor replace them with the
857 // annotation token.
858 PP.AnnotateCachedTokens(Tok);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000859 return true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000860}
861
862/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
Douglas Gregor7f741122009-02-25 19:37:18 +0000863/// annotates C++ scope specifiers and template-ids. This returns
864/// true if the token was annotated.
Chris Lattner45ddec32009-01-05 00:13:00 +0000865///
866/// Note that this routine emits an error if you call it with ::new or ::delete
867/// as the current tokens, so only call it in contexts where these are invalid.
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000868bool Parser::TryAnnotateCXXScopeToken() {
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +0000869 assert(getLang().CPlusPlus &&
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000870 "Call sites of this function should be guarded by checking for C++");
Chris Lattnerb5134c02009-01-05 01:24:05 +0000871 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
872 "Cannot be a type or scope token!");
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000873
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +0000874 CXXScopeSpec SS;
Chris Lattnera448d752009-01-06 06:59:53 +0000875 if (!ParseOptionalCXXScopeSpecifier(SS))
Douglas Gregor7f741122009-02-25 19:37:18 +0000876 return Tok.is(tok::annot_template_id);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000877
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000878 // Push the current token back into the token stream (or revert it if it is
879 // cached) and use an annotation scope token for current token.
880 if (PP.isBacktrackEnabled())
881 PP.RevertCachedTokens(1);
882 else
883 PP.EnterToken(Tok);
884 Tok.setKind(tok::annot_cxxscope);
885 Tok.setAnnotationValue(SS.getScopeRep());
886 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000887
Chris Lattnerdfa1a452009-01-04 22:32:19 +0000888 // In case the tokens were cached, have Preprocessor replace them with the
889 // annotation token.
890 PP.AnnotateCachedTokens(Tok);
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000891 return true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000892}