blob: 1f8e016bfe686ab1a3dbb27f6dbbbfbfc3fc9794 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner971c6b62006-08-05 22:46:42 +000016#include "clang/Parse/Scope.h"
Chris Lattner0bb5f832006-07-31 01:59:18 +000017using namespace clang;
18
Chris Lattner697e5d62006-11-09 06:32:27 +000019Parser::Parser(Preprocessor &pp, Action &actions)
20 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
Chris Lattner8c204872006-10-14 05:19:21 +000021 Tok.setKind(tok::eof);
Chris Lattnere4e38592006-08-14 00:15:05 +000022 CurScope = 0;
Chris Lattner03928c72007-07-15 00:04:39 +000023 NumCachedScopes = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000024 ParenCount = BracketCount = BraceCount = 0;
Steve Naroff09bf8152007-09-06 21:24:23 +000025 ObjcImpDecl = 0;
Fariborz Jahanianf6546b32007-09-27 18:57:03 +000026 AllImplMethods.clear();
Chris Lattner971c6b62006-08-05 22:46:42 +000027}
28
Chris Lattner685ed1e2006-08-14 00:22:04 +000029/// Out-of-line virtual destructor to provide home for Action class.
30Action::~Action() {}
Chris Lattnere4e38592006-08-14 00:15:05 +000031
Chris Lattner0bb5f832006-07-31 01:59:18 +000032
Chris Lattnerb9093cd2006-08-04 04:39:53 +000033void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000034 const std::string &Msg) {
Chris Lattner36982e42007-05-16 17:49:37 +000035 Diags.Report(Loc, DiagID, &Msg, 1);
Chris Lattner0bb5f832006-07-31 01:59:18 +000036}
37
Chris Lattner4564bc12006-08-10 23:14:52 +000038/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
39/// this helper function matches and consumes the specified RHS token if
40/// present. If not present, it emits the specified diagnostic indicating
41/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
42/// should be the name of the unmatched LHS token.
Chris Lattner71e23ce2006-11-04 20:18:38 +000043SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
44 SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000045
Chris Lattner0ab032a2007-10-09 17:23:58 +000046 if (Tok.is(RHSTok))
Chris Lattner71e23ce2006-11-04 20:18:38 +000047 return ConsumeAnyToken();
48
49 SourceLocation R = Tok.getLocation();
50 const char *LHSName = "unknown";
51 diag::kind DID = diag::err_parse_error;
52 switch (RHSTok) {
53 default: break;
54 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
55 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
56 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner29375652006-12-04 18:06:35 +000057 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
Chris Lattner4564bc12006-08-10 23:14:52 +000058 }
Chris Lattner71e23ce2006-11-04 20:18:38 +000059 Diag(Tok, DID);
60 Diag(LHSLoc, diag::err_matching, LHSName);
61 SkipUntil(RHSTok);
62 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +000063}
64
Chris Lattnerdbb2a462006-08-12 19:26:13 +000065/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
66/// input. If so, it is consumed and false is returned.
67///
68/// If the input is malformed, this emits the specified diagnostic. Next, if
69/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
70/// returned.
71bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000072 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattner0ab032a2007-10-09 17:23:58 +000073 if (Tok.is(ExpectedTok)) {
Chris Lattner15a00da2006-08-15 04:10:31 +000074 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000075 return false;
76 }
77
Chris Lattner6d7e6342006-08-15 03:41:14 +000078 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000079 if (SkipToTok != tok::unknown)
80 SkipUntil(SkipToTok);
81 return true;
82}
83
Chris Lattner70f32b72006-07-31 05:09:04 +000084//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000085// Error recovery.
86//===----------------------------------------------------------------------===//
87
88/// SkipUntil - Read tokens until we get to the specified token, then consume
Chris Lattner01e4b242007-07-24 17:03:04 +000089/// it (unless DontConsume is true). Because we cannot guarantee that the
Chris Lattnereec40f92006-08-06 21:55:29 +000090/// token will ever occur, this skips to the next token, or to some likely
91/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
92/// character.
93///
94/// If SkipUntil finds the specified token, it returns true, otherwise it
95/// returns false.
Chris Lattner83b94e02007-04-27 19:12:15 +000096bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
97 bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000098 // We always want this function to skip at least one token if the first token
99 // isn't T and if not at EOF.
100 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000101 while (1) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000102 // If we found one of the tokens, stop and return true.
103 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000104 if (Tok.is(Toks[i])) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000105 if (DontConsume) {
106 // Noop, don't consume the token.
107 } else {
108 ConsumeAnyToken();
109 }
110 return true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000111 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000112 }
113
114 switch (Tok.getKind()) {
115 case tok::eof:
116 // Ran out of tokens.
117 return false;
118
119 case tok::l_paren:
120 // Recursively skip properly-nested parens.
121 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000122 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000123 break;
124 case tok::l_square:
125 // Recursively skip properly-nested square brackets.
126 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000127 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000128 break;
129 case tok::l_brace:
130 // Recursively skip properly-nested braces.
131 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000132 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000133 break;
134
135 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
136 // Since the user wasn't looking for this token (if they were, it would
137 // already be handled), this isn't balanced. If there is a LHS token at a
138 // higher level, we will assume that this matches the unbalanced token
139 // and return it. Otherwise, this is a spurious RHS token, which we skip.
140 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000141 if (ParenCount && !isFirstTokenSkipped)
142 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000143 ConsumeParen();
144 break;
145 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000146 if (BracketCount && !isFirstTokenSkipped)
147 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000148 ConsumeBracket();
149 break;
150 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000151 if (BraceCount && !isFirstTokenSkipped)
152 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000153 ConsumeBrace();
154 break;
155
156 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000157 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000158 ConsumeStringToken();
159 break;
160 case tok::semi:
161 if (StopAtSemi)
162 return false;
163 // FALL THROUGH.
164 default:
165 // Skip this token.
166 ConsumeToken();
167 break;
168 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000169 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000170 }
171}
172
173//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000174// Scope manipulation
175//===----------------------------------------------------------------------===//
176
177/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000178void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattner03928c72007-07-15 00:04:39 +0000179 if (NumCachedScopes) {
180 Scope *N = ScopeCache[--NumCachedScopes];
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000181 N->Init(CurScope, ScopeFlags);
182 CurScope = N;
183 } else {
184 CurScope = new Scope(CurScope, ScopeFlags);
185 }
Chris Lattnere4e38592006-08-14 00:15:05 +0000186}
187
188/// ExitScope - Pop a scope off the scope stack.
189void Parser::ExitScope() {
190 assert(CurScope && "Scope imbalance!");
191
Chris Lattner87547e62007-10-09 20:37:18 +0000192 // Inform the actions module that this scope is going away if there are any
193 // decls in it.
194 if (!CurScope->decl_empty())
Steve Naroffc62adb62007-10-09 22:01:59 +0000195 Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Chris Lattnere4e38592006-08-14 00:15:05 +0000196
Chris Lattner03928c72007-07-15 00:04:39 +0000197 Scope *OldScope = CurScope;
198 CurScope = OldScope->getParent();
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000199
Chris Lattner03928c72007-07-15 00:04:39 +0000200 if (NumCachedScopes == ScopeCacheSize)
201 delete OldScope;
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000202 else
Chris Lattner03928c72007-07-15 00:04:39 +0000203 ScopeCache[NumCachedScopes++] = OldScope;
Chris Lattnere4e38592006-08-14 00:15:05 +0000204}
205
206
207
208
209//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000210// C99 6.9: External Definitions.
211//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000212
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000213Parser::~Parser() {
214 // If we still have scopes active, delete the scope tree.
215 delete CurScope;
216
217 // Free the scope cache.
Chris Lattner03928c72007-07-15 00:04:39 +0000218 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
219 delete ScopeCache[i];
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000220}
221
Chris Lattner38ba3362006-08-17 07:04:37 +0000222/// Initialize - Warm up the parser.
223///
224void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000225 // Prime the lexer look-ahead.
226 ConsumeToken();
227
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000228 // Create the translation unit scope. Install it as the current scope.
Chris Lattnere4e38592006-08-14 00:15:05 +0000229 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000230 EnterScope(Scope::DeclScope);
Steve Naroffc62adb62007-10-09 22:01:59 +0000231 Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
232
Chris Lattner0ab032a2007-10-09 17:23:58 +0000233 if (Tok.is(tok::eof) &&
Chris Lattner66b67ef2007-08-25 05:47:03 +0000234 !getLang().CPlusPlus) // Empty source file is an extension in C
Chris Lattnerbd638922006-11-10 05:19:25 +0000235 Diag(Tok, diag::ext_empty_source_file);
Chris Lattner66782842007-08-29 22:54:08 +0000236
237 // Initialization for Objective-C context sensitive keywords recognition.
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000238 // Referenced in Parser::ParseObjcTypeQualifierList.
Chris Lattner66782842007-08-29 22:54:08 +0000239 if (getLang().ObjC1) {
240 ObjcTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
241 ObjcTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
242 ObjcTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
243 ObjcTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
244 ObjcTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
245 ObjcTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
246 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000247 if (getLang().ObjC2) {
248 ObjcPropertyAttrs[objc_readonly] = &PP.getIdentifierTable().get("readonly");
249 ObjcPropertyAttrs[objc_getter] = &PP.getIdentifierTable().get("getter");
250 ObjcPropertyAttrs[objc_setter] = &PP.getIdentifierTable().get("setter");
251 ObjcPropertyAttrs[objc_assign] = &PP.getIdentifierTable().get("assign");
252 ObjcPropertyAttrs[objc_readwrite] =
253 &PP.getIdentifierTable().get("readwrite");
254 ObjcPropertyAttrs[objc_retain] = &PP.getIdentifierTable().get("retain");
255 ObjcPropertyAttrs[objc_copy] = &PP.getIdentifierTable().get("copy");
256 ObjcPropertyAttrs[objc_nonatomic] =
257 &PP.getIdentifierTable().get("nonatomic");
258 }
Chris Lattner38ba3362006-08-17 07:04:37 +0000259}
260
261/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
262/// action tells us to. This returns true if the EOF was encountered.
263bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
264 Result = 0;
Chris Lattner0ab032a2007-10-09 17:23:58 +0000265 if (Tok.is(tok::eof)) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000266
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000267 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000268 return false;
269}
270
271/// Finalize - Shut down the parser.
272///
273void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000274 ExitScope();
275 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000276}
277
Chris Lattner38ba3362006-08-17 07:04:37 +0000278/// ParseTranslationUnit:
279/// translation-unit: [C99 6.9]
280/// external-declaration
281/// translation-unit external-declaration
282void Parser::ParseTranslationUnit() {
283 Initialize();
284
285 DeclTy *Res;
286 while (!ParseTopLevelDecl(Res))
287 /*parse them all*/;
288
289 Finalize();
290}
291
Chris Lattner0bb5f832006-07-31 01:59:18 +0000292/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000293/// external-declaration: [C99 6.9]
Chris Lattnercccc3112007-08-10 20:57:02 +0000294/// function-definition
295/// declaration
Chris Lattner0bb5f832006-07-31 01:59:18 +0000296/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000297/// [GNU] asm-definition
Chris Lattnercccc3112007-08-10 20:57:02 +0000298/// [GNU] __extension__ external-declaration
Chris Lattner40f16b52006-11-05 02:05:37 +0000299/// [OBJC] objc-class-definition
300/// [OBJC] objc-class-declaration
301/// [OBJC] objc-alias-declaration
302/// [OBJC] objc-protocol-definition
303/// [OBJC] objc-method-definition
304/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000305///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000306/// [GNU] asm-definition:
307/// simple-asm-expr ';'
308///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000309Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000310 switch (Tok.getKind()) {
311 case tok::semi:
Chris Lattnerbd638922006-11-10 05:19:25 +0000312 Diag(Tok, diag::ext_top_level_semi);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000313 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000314 // TODO: Invoke action for top-level semicolon.
315 return 0;
Chris Lattnercccc3112007-08-10 20:57:02 +0000316 case tok::kw___extension__: {
317 ConsumeToken();
318 // FIXME: Disable extension warnings.
319 DeclTy *RV = ParseExternalDeclaration();
320 // FIXME: Restore extension warnings.
321 return RV;
322 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000323 case tok::kw_asm:
324 ParseSimpleAsm();
325 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
326 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000327 // TODO: Invoke action for top-level asm.
328 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000329 case tok::at:
Chris Lattnerc24278d2007-05-02 23:45:06 +0000330 // @ is not a legal token unless objc is enabled, no need to check.
Steve Naroffacb1e742007-09-10 20:51:04 +0000331 return ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000332 case tok::minus:
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000333 if (getLang().ObjC1)
334 ParseObjCInstanceMethodDefinition();
335 else {
Chris Lattnerc24278d2007-05-02 23:45:06 +0000336 Diag(Tok, diag::err_expected_external_declaration);
337 ConsumeToken();
338 }
Chris Lattneraacc5af2006-11-03 07:21:07 +0000339 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000340 case tok::plus:
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000341 if (getLang().ObjC1)
342 ParseObjCClassMethodDefinition();
343 else {
Chris Lattnerc24278d2007-05-02 23:45:06 +0000344 Diag(Tok, diag::err_expected_external_declaration);
345 ConsumeToken();
346 }
Steve Naroffb419d3a2006-10-27 23:18:49 +0000347 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000348 case tok::kw_namespace:
Chris Lattner302b4be2006-11-19 02:31:38 +0000349 case tok::kw_typedef:
Chris Lattner479ed3a2007-08-25 18:15:16 +0000350 // A function definition cannot start with a these keywords.
Chris Lattner302b4be2006-11-19 02:31:38 +0000351 return ParseDeclaration(Declarator::FileContext);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000352 default:
353 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000354 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000355 }
356}
357
358/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000359/// a declaration. We can't tell which we have until we read up to the
360/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000361///
Chris Lattner70f32b72006-07-31 05:09:04 +0000362/// function-definition: [C99 6.9.1]
363/// declaration-specifiers[opt] declarator declaration-list[opt]
Chris Lattnerf2659392007-08-22 06:06:56 +0000364/// compound-statement
Chris Lattner70f32b72006-07-31 05:09:04 +0000365/// declaration: [C99 6.7]
Chris Lattnerf2659392007-08-22 06:06:56 +0000366/// declaration-specifiers init-declarator-list[opt] ';'
367/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner70f32b72006-07-31 05:09:04 +0000368/// [OMP] threadprivate-directive [TODO]
369///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000370Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000371 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000372 DeclSpec DS;
373 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000374
375 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000376 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0ab032a2007-10-09 17:23:58 +0000377 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000378 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000379 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000380 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000381
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000382 // ObjC2 allows prefix attributes on class interfaces.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000383 if (getLang().ObjC2 && Tok.is(tok::at)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000384 SourceLocation AtLoc = ConsumeToken(); // the "@"
385 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_interface)
386 return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
387 }
388
Chris Lattnerfff824f2006-08-07 06:31:38 +0000389 // Parse the first declarator.
390 Declarator DeclaratorInfo(DS, Declarator::FileContext);
391 ParseDeclarator(DeclaratorInfo);
392 // Error parsing the declarator?
393 if (DeclaratorInfo.getIdentifier() == 0) {
394 // If so, skip until the semi-colon or a }.
395 SkipUntil(tok::r_brace, true);
Chris Lattner0ab032a2007-10-09 17:23:58 +0000396 if (Tok.is(tok::semi))
Chris Lattnerfff824f2006-08-07 06:31:38 +0000397 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000398 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000399 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000400
Chris Lattnerfff824f2006-08-07 06:31:38 +0000401 // If the declarator is the start of a function definition, handle it.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000402 if (Tok.is(tok::equal) || // int X()= -> not a function def
403 Tok.is(tok::comma) || // int X(), -> not a function def
404 Tok.is(tok::semi) || // int X(); -> not a function def
405 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
406 Tok.is(tok::kw___attribute)) { // int X() __attr__ -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000407 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000408 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattner0ab032a2007-10-09 17:23:58 +0000409 (Tok.is(tok::l_brace) || // int X() {}
Chris Lattnerfff824f2006-08-07 06:31:38 +0000410 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000411 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000412 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000413 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000414 Diag(Tok, diag::err_expected_fn_body);
415 else
416 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000417 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000418 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000419 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000420
Chris Lattner53361ac2006-08-10 05:19:57 +0000421 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000422 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000423}
424
Chris Lattnerfff824f2006-08-07 06:31:38 +0000425/// ParseFunctionDefinition - We parsed and verified that the specified
426/// Declarator is well formed. If this is a K&R-style function, read the
427/// parameters declaration-list, then start the compound-statement.
428///
429/// declaration-specifiers[opt] declarator declaration-list[opt]
430/// compound-statement [TODO]
431///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000432Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnercbc426d2006-12-02 06:43:02 +0000433 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
434 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000435 "This isn't a function declarator!");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000436 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000437
438 // If this declaration was formed with a K&R-style identifier list for the
439 // arguments, parse declarations for all of the args next.
440 // int foo(a,b) int a; float b; {}
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000441 if (!FTI.hasPrototype && FTI.NumArgs != 0)
442 ParseKNRParamDeclarations(D);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000443
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000444 // We should have an opening brace now.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000445 if (Tok.isNot(tok::l_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000446 Diag(Tok, diag::err_expected_fn_body);
447
448 // Skip over garbage, until we get to '{'. Don't eat the '{'.
449 SkipUntil(tok::l_brace, true, true);
450
451 // If we didn't find the '{', bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000452 if (Tok.isNot(tok::l_brace))
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000453 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000454 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000455
Chris Lattnera55a2cc2007-10-09 17:14:05 +0000456 SourceLocation BraceLoc = Tok.getLocation();
457
458 // Enter a scope for the function body.
459 EnterScope(Scope::FnScope|Scope::DeclScope);
460
461 // Tell the actions module that we have entered a function definition with the
462 // specified Declarator for the function.
463 DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
464
Fariborz Jahanian8e632942007-11-08 19:01:26 +0000465 return ParseFunctionStatementBody(Res, BraceLoc, BraceLoc);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000466}
467
Fariborz Jahanian7a017212007-11-09 22:27:59 +0000468/// ObjcParseMethodDefinition - This routine parses a method definition and
469/// returns its AST.
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000470void Parser::ObjcParseMethodDefinition(DeclTy *D) {
Fariborz Jahanian56ff1462007-11-08 23:49:49 +0000471 // We should have an opening brace now.
472 if (Tok.isNot(tok::l_brace)) {
473 Diag(Tok, diag::err_expected_fn_body);
474
475 // Skip over garbage, until we get to '{'. Don't eat the '{'.
476 SkipUntil(tok::l_brace, true, true);
477
478 // If we didn't find the '{', bail out.
479 if (Tok.isNot(tok::l_brace))
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000480 return;
Fariborz Jahanian56ff1462007-11-08 23:49:49 +0000481 }
482
483 SourceLocation BraceLoc = Tok.getLocation();
484
Fariborz Jahanian7a017212007-11-09 22:27:59 +0000485 // Enter a scope for the method body.
Fariborz Jahanian56ff1462007-11-08 23:49:49 +0000486 EnterScope(Scope::FnScope|Scope::DeclScope);
487
Fariborz Jahanian7a017212007-11-09 22:27:59 +0000488 // Tell the actions module that we have entered a method definition with the
489 // specified Declarator for the method.
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000490 Actions.ObjcActOnStartOfMethodDef(CurScope, D);
Fariborz Jahanian56ff1462007-11-08 23:49:49 +0000491
Fariborz Jahanian85e1d0d2007-11-10 16:31:34 +0000492 StmtResult FnBody = ParseCompoundStatementBody();
493
494 // If the function body could not be parsed, make a bogus compoundstmt.
495 if (FnBody.isInvalid)
496 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
497
498 // Leave the function body scope.
499 ExitScope();
500
501 // TODO: Pass argument information.
502 Actions.ActOnMethodDefBody(D, FnBody.Val);
Fariborz Jahanian56ff1462007-11-08 23:49:49 +0000503}
504
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000505/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
506/// types for a function with a K&R-style identifier list for arguments.
507void Parser::ParseKNRParamDeclarations(Declarator &D) {
508 // We know that the top-level of this declarator is a function.
509 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
510
511 // Read all the argument declarations.
512 while (isDeclarationSpecifier()) {
513 SourceLocation DSStart = Tok.getLocation();
514
515 // Parse the common declaration-specifiers piece.
516 DeclSpec DS;
517 ParseDeclarationSpecifiers(DS);
518
519 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
520 // least one declarator'.
521 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
522 // the declarations though. It's trivial to ignore them, really hard to do
523 // anything else with them.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000524 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000525 Diag(DSStart, diag::err_declaration_does_not_declare_param);
526 ConsumeToken();
527 continue;
528 }
529
530 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
531 // than register.
532 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
533 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
534 Diag(DS.getStorageClassSpecLoc(),
535 diag::err_invalid_storage_class_in_func_decl);
536 DS.ClearStorageClassSpecs();
537 }
538 if (DS.isThreadSpecified()) {
539 Diag(DS.getThreadSpecLoc(),
540 diag::err_invalid_storage_class_in_func_decl);
541 DS.ClearStorageClassSpecs();
542 }
543
544 // Parse the first declarator attached to this declspec.
545 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
546 ParseDeclarator(ParmDeclarator);
547
548 // Handle the full declarator list.
549 while (1) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000550 DeclTy *AttrList;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000551 // If attributes are present, parse them.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000552 if (Tok.is(tok::kw___attribute))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000553 // FIXME: attach attributes too.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000554 AttrList = ParseAttributes();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000555
556 // Ask the actions module to compute the type for this declarator.
557 Action::TypeResult TR =
Steve Naroff30d242c2007-09-15 18:49:24 +0000558 Actions.ActOnParamDeclaratorType(CurScope, ParmDeclarator);
Steve Naroffacb1e742007-09-10 20:51:04 +0000559
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000560 if (!TR.isInvalid &&
561 // A missing identifier has already been diagnosed.
562 ParmDeclarator.getIdentifier()) {
563
564 // Scan the argument list looking for the correct param to apply this
565 // type.
566 for (unsigned i = 0; ; ++i) {
567 // C99 6.9.1p6: those declarators shall declare only identifiers from
568 // the identifier list.
569 if (i == FTI.NumArgs) {
570 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
571 ParmDeclarator.getIdentifier()->getName());
572 break;
573 }
574
575 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
576 // Reject redefinitions of parameters.
577 if (FTI.ArgInfo[i].TypeInfo) {
578 Diag(ParmDeclarator.getIdentifierLoc(),
579 diag::err_param_redefinition,
580 ParmDeclarator.getIdentifier()->getName());
581 } else {
582 FTI.ArgInfo[i].TypeInfo = TR.Val;
583 }
584 break;
585 }
586 }
587 }
588
589 // If we don't have a comma, it is either the end of the list (a ';') or
590 // an error, bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000591 if (Tok.isNot(tok::comma))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000592 break;
593
594 // Consume the comma.
595 ConsumeToken();
596
597 // Parse the next declarator.
598 ParmDeclarator.clear();
599 ParseDeclarator(ParmDeclarator);
600 }
601
Chris Lattner0ab032a2007-10-09 17:23:58 +0000602 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000603 ConsumeToken();
604 } else {
605 Diag(Tok, diag::err_parse_error);
606 // Skip to end of block or statement
607 SkipUntil(tok::semi, true);
Chris Lattner0ab032a2007-10-09 17:23:58 +0000608 if (Tok.is(tok::semi))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000609 ConsumeToken();
610 }
611 }
612
613 // The actions module must verify that all arguments were declared.
614}
615
616
Chris Lattner0116c472006-08-15 06:03:28 +0000617/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
618/// allowed to be a wide string, and is not subject to character translation.
619///
620/// [GNU] asm-string-literal:
621/// string-literal
622///
623void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000624 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000625 Diag(Tok, diag::err_expected_string_literal);
626 return;
627 }
628
629 ExprResult Res = ParseStringLiteralExpression();
630 if (Res.isInvalid) return;
631
632 // TODO: Diagnose: wide string literal in 'asm'
633}
634
Chris Lattner6d7e6342006-08-15 03:41:14 +0000635/// ParseSimpleAsm
636///
637/// [GNU] simple-asm-expr:
638/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000639///
640void Parser::ParseSimpleAsm() {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000641 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Chris Lattner6d7e6342006-08-15 03:41:14 +0000642 ConsumeToken();
643
Chris Lattner0ab032a2007-10-09 17:23:58 +0000644 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000645 Diag(Tok, diag::err_expected_lparen_after, "asm");
646 return;
647 }
648
Chris Lattner04132372006-10-16 06:12:55 +0000649 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000650
Chris Lattner0116c472006-08-15 06:03:28 +0000651 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000652
Chris Lattner04f80192006-08-15 04:55:54 +0000653 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000654}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000655