blob: 6f4994236e19fe44bc16434d044d6f4a800d4fab [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 llvm;
18using namespace clang;
19
Chris Lattner697e5d62006-11-09 06:32:27 +000020Parser::Parser(Preprocessor &pp, Action &actions)
21 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
Chris Lattner8c204872006-10-14 05:19:21 +000022 Tok.setKind(tok::eof);
Chris Lattnere4e38592006-08-14 00:15:05 +000023 CurScope = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000024
25 ParenCount = BracketCount = BraceCount = 0;
Chris Lattner971c6b62006-08-05 22:46:42 +000026}
27
Chris Lattner685ed1e2006-08-14 00:22:04 +000028/// Out-of-line virtual destructor to provide home for Action class.
29Action::~Action() {}
Chris Lattnere4e38592006-08-14 00:15:05 +000030
Chris Lattner0bb5f832006-07-31 01:59:18 +000031
Chris Lattnerb9093cd2006-08-04 04:39:53 +000032void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000033 const std::string &Msg) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000034 Diags.Report(Loc, DiagID, Msg);
Chris Lattner0bb5f832006-07-31 01:59:18 +000035}
36
Chris Lattner4564bc12006-08-10 23:14:52 +000037/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
38/// this helper function matches and consumes the specified RHS token if
39/// present. If not present, it emits the specified diagnostic indicating
40/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
41/// should be the name of the unmatched LHS token.
Chris Lattner71e23ce2006-11-04 20:18:38 +000042SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
43 SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000044
Chris Lattner71e23ce2006-11-04 20:18:38 +000045 if (Tok.getKind() == RHSTok)
46 return ConsumeAnyToken();
47
48 SourceLocation R = Tok.getLocation();
49 const char *LHSName = "unknown";
50 diag::kind DID = diag::err_parse_error;
51 switch (RHSTok) {
52 default: break;
53 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
54 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
55 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner29375652006-12-04 18:06:35 +000056 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
Chris Lattner4564bc12006-08-10 23:14:52 +000057 }
Chris Lattner71e23ce2006-11-04 20:18:38 +000058 Diag(Tok, DID);
59 Diag(LHSLoc, diag::err_matching, LHSName);
60 SkipUntil(RHSTok);
61 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +000062}
63
Chris Lattnerdbb2a462006-08-12 19:26:13 +000064/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
65/// input. If so, it is consumed and false is returned.
66///
67/// If the input is malformed, this emits the specified diagnostic. Next, if
68/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
69/// returned.
70bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000071 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000072 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000073 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000074 return false;
75 }
76
Chris Lattner6d7e6342006-08-15 03:41:14 +000077 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000078 if (SkipToTok != tok::unknown)
79 SkipUntil(SkipToTok);
80 return true;
81}
82
Chris Lattner70f32b72006-07-31 05:09:04 +000083//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000084// Error recovery.
85//===----------------------------------------------------------------------===//
86
87/// SkipUntil - Read tokens until we get to the specified token, then consume
88/// it (unless DontConsume is false). Because we cannot guarantee that the
89/// token will ever occur, this skips to the next token, or to some likely
90/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
91/// character.
92///
93/// If SkipUntil finds the specified token, it returns true, otherwise it
94/// returns false.
Chris Lattner83b94e02007-04-27 19:12:15 +000095bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
96 bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000097 // We always want this function to skip at least one token if the first token
98 // isn't T and if not at EOF.
99 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000100 while (1) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000101 // If we found one of the tokens, stop and return true.
102 for (unsigned i = 0; i != NumToks; ++i) {
103 if (Tok.getKind() == Toks[i]) {
104 if (DontConsume) {
105 // Noop, don't consume the token.
106 } else {
107 ConsumeAnyToken();
108 }
109 return true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000110 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000111 }
112
113 switch (Tok.getKind()) {
114 case tok::eof:
115 // Ran out of tokens.
116 return false;
117
118 case tok::l_paren:
119 // Recursively skip properly-nested parens.
120 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000121 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000122 break;
123 case tok::l_square:
124 // Recursively skip properly-nested square brackets.
125 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000126 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000127 break;
128 case tok::l_brace:
129 // Recursively skip properly-nested braces.
130 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000131 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000132 break;
133
134 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
135 // Since the user wasn't looking for this token (if they were, it would
136 // already be handled), this isn't balanced. If there is a LHS token at a
137 // higher level, we will assume that this matches the unbalanced token
138 // and return it. Otherwise, this is a spurious RHS token, which we skip.
139 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000140 if (ParenCount && !isFirstTokenSkipped)
141 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000142 ConsumeParen();
143 break;
144 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000145 if (BracketCount && !isFirstTokenSkipped)
146 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000147 ConsumeBracket();
148 break;
149 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000150 if (BraceCount && !isFirstTokenSkipped)
151 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000152 ConsumeBrace();
153 break;
154
155 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000156 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000157 ConsumeStringToken();
158 break;
159 case tok::semi:
160 if (StopAtSemi)
161 return false;
162 // FALL THROUGH.
163 default:
164 // Skip this token.
165 ConsumeToken();
166 break;
167 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000168 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000169 }
170}
171
172//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000173// Scope manipulation
174//===----------------------------------------------------------------------===//
175
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000176/// ScopeCache - Cache scopes to avoid malloc traffic.
177static SmallVector<Scope*, 16> ScopeCache;
178
Chris Lattnere4e38592006-08-14 00:15:05 +0000179/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000180void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000181 if (!ScopeCache.empty()) {
182 Scope *N = ScopeCache.back();
183 ScopeCache.pop_back();
184 N->Init(CurScope, ScopeFlags);
185 CurScope = N;
186 } else {
187 CurScope = new Scope(CurScope, ScopeFlags);
188 }
Chris Lattnere4e38592006-08-14 00:15:05 +0000189}
190
191/// ExitScope - Pop a scope off the scope stack.
192void Parser::ExitScope() {
193 assert(CurScope && "Scope imbalance!");
194
195 // Inform the actions module that this scope is going away.
196 Actions.PopScope(Tok.getLocation(), CurScope);
197
198 Scope *Old = CurScope;
199 CurScope = Old->getParent();
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000200
201 if (ScopeCache.size() == 16)
202 delete Old;
203 else
204 ScopeCache.push_back(Old);
Chris Lattnere4e38592006-08-14 00:15:05 +0000205}
206
207
208
209
210//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000211// C99 6.9: External Definitions.
212//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000213
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000214Parser::~Parser() {
215 // If we still have scopes active, delete the scope tree.
216 delete CurScope;
217
218 // Free the scope cache.
219 while (!ScopeCache.empty()) {
220 delete ScopeCache.back();
221 ScopeCache.pop_back();
222 }
223}
224
Chris Lattner38ba3362006-08-17 07:04:37 +0000225/// Initialize - Warm up the parser.
226///
227void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000228 // Prime the lexer look-ahead.
229 ConsumeToken();
230
231 // Create the global scope, install it as the current scope.
232 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000233 EnterScope(0);
Chris Lattner38ba3362006-08-17 07:04:37 +0000234
Chris Lattner6d7e6342006-08-15 03:41:14 +0000235
236 // Install builtin types.
237 // TODO: Move this someplace more useful.
238 {
Chris Lattner353f5742006-11-28 04:50:12 +0000239 const char *Dummy;
240
Chris Lattner6d7e6342006-08-15 03:41:14 +0000241 //__builtin_va_list
242 DeclSpec DS;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000243 bool Error = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, SourceLocation(),
244 Dummy);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000245
246 // TODO: add a 'TST_builtin' type?
Chris Lattnerb20e8942006-11-28 05:30:29 +0000247 Error |= DS.SetTypeSpecType(DeclSpec::TST_int, SourceLocation(), Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000248 assert(!Error && "Error setting up __builtin_va_list!");
Chris Lattner38ba3362006-08-17 07:04:37 +0000249
Chris Lattner6d7e6342006-08-15 03:41:14 +0000250 Declarator D(DS, Declarator::FileContext);
251 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000252 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000253 }
254
Chris Lattner0bb5f832006-07-31 01:59:18 +0000255 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
Chris Lattnerbd638922006-11-10 05:19:25 +0000256 Diag(Tok, diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000257}
258
259/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
260/// action tells us to. This returns true if the EOF was encountered.
261bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
262 Result = 0;
263 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000264
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000265 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000266 return false;
267}
268
269/// Finalize - Shut down the parser.
270///
271void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000272 ExitScope();
273 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000274}
275
Chris Lattner38ba3362006-08-17 07:04:37 +0000276/// ParseTranslationUnit:
277/// translation-unit: [C99 6.9]
278/// external-declaration
279/// translation-unit external-declaration
280void Parser::ParseTranslationUnit() {
281 Initialize();
282
283 DeclTy *Res;
284 while (!ParseTopLevelDecl(Res))
285 /*parse them all*/;
286
287 Finalize();
288}
289
Chris Lattner0bb5f832006-07-31 01:59:18 +0000290/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000291/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000292/// function-definition [TODO]
293/// declaration [TODO]
294/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000295/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000296/// [GNU] __extension__ external-declaration [TODO]
Chris Lattner40f16b52006-11-05 02:05:37 +0000297/// [OBJC] objc-class-definition
298/// [OBJC] objc-class-declaration
299/// [OBJC] objc-alias-declaration
300/// [OBJC] objc-protocol-definition
301/// [OBJC] objc-method-definition
302/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000303///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000304/// [GNU] asm-definition:
305/// simple-asm-expr ';'
306///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000307Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000308 switch (Tok.getKind()) {
309 case tok::semi:
Chris Lattnerbd638922006-11-10 05:19:25 +0000310 Diag(Tok, diag::ext_top_level_semi);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000311 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000312 // TODO: Invoke action for top-level semicolon.
313 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000314 case tok::kw_asm:
315 ParseSimpleAsm();
316 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
317 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000318 // TODO: Invoke action for top-level asm.
319 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000320 case tok::at:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000321 ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000322 return 0;
323 case tok::minus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000324 ParseObjCInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000325 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000326 case tok::plus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000327 ParseObjCClassMethodDeclaration();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000328 return 0;
Chris Lattner302b4be2006-11-19 02:31:38 +0000329 case tok::kw_typedef:
330 // A function definition cannot start with a 'typedef' keyword.
331 return ParseDeclaration(Declarator::FileContext);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000332 default:
333 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000334 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000335 }
336}
337
338/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000339/// a declaration. We can't tell which we have until we read up to the
340/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000341///
Chris Lattner70f32b72006-07-31 05:09:04 +0000342/// function-definition: [C99 6.9.1]
343/// declaration-specifiers[opt] declarator declaration-list[opt]
344/// compound-statement [TODO]
345/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000346/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000347/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000348/// [OMP] threadprivate-directive [TODO]
349///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000350Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000351 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000352 DeclSpec DS;
353 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000354
355 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000356 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000357 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000358 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000359 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000360 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000361
Chris Lattnerfff824f2006-08-07 06:31:38 +0000362 // Parse the first declarator.
363 Declarator DeclaratorInfo(DS, Declarator::FileContext);
364 ParseDeclarator(DeclaratorInfo);
365 // Error parsing the declarator?
366 if (DeclaratorInfo.getIdentifier() == 0) {
367 // If so, skip until the semi-colon or a }.
368 SkipUntil(tok::r_brace, true);
369 if (Tok.getKind() == tok::semi)
370 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000371 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000372 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000373
Chris Lattnerfff824f2006-08-07 06:31:38 +0000374 // If the declarator is the start of a function definition, handle it.
375 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
376 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000377 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000378 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
379 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
380 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000381 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000382 (Tok.getKind() == tok::l_brace || // int X() {}
383 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000384 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000385 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000386 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000387 Diag(Tok, diag::err_expected_fn_body);
388 else
389 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000390 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000391 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000392 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000393
Chris Lattner53361ac2006-08-10 05:19:57 +0000394 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000395 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000396}
397
Chris Lattnerfff824f2006-08-07 06:31:38 +0000398/// ParseFunctionDefinition - We parsed and verified that the specified
399/// Declarator is well formed. If this is a K&R-style function, read the
400/// parameters declaration-list, then start the compound-statement.
401///
402/// declaration-specifiers[opt] declarator declaration-list[opt]
403/// compound-statement [TODO]
404///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000405Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnercbc426d2006-12-02 06:43:02 +0000406 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
407 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000408 "This isn't a function declarator!");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000409 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000410
411 // If this declaration was formed with a K&R-style identifier list for the
412 // arguments, parse declarations for all of the args next.
413 // int foo(a,b) int a; float b; {}
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000414 if (!FTI.hasPrototype && FTI.NumArgs != 0)
415 ParseKNRParamDeclarations(D);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000416
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000417 // Enter a scope for the function body.
418 EnterScope(Scope::FnScope);
419
Chris Lattner2114d5e2006-12-04 07:40:24 +0000420 // Tell the actions module that we have entered a function definition with the
421 // specified Declarator for the function.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000422 DeclTy *Res = Actions.ParseStartOfFunctionDef(CurScope, D);
423
424
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000425 // We should have an opening brace now.
426 if (Tok.getKind() != tok::l_brace) {
427 Diag(Tok, diag::err_expected_fn_body);
428
429 // Skip over garbage, until we get to '{'. Don't eat the '{'.
430 SkipUntil(tok::l_brace, true, true);
431
432 // If we didn't find the '{', bail out.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000433 if (Tok.getKind() != tok::l_brace) {
434 ExitScope();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000435 return 0;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000436 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000437 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000438
Chris Lattner7f58c3d2007-01-21 06:56:16 +0000439 // Do not enter a scope for the brace, as the arguments are in the same scope
440 // (the function body) as the body itself. Instead, just read the statement
441 // list and put it into a CompoundStmt for safe keeping.
442 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000443 if (FnBody.isInvalid) {
444 ExitScope();
445 return 0;
446 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000447
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000448 // Leave the function body scope.
449 ExitScope();
Chris Lattner7014fb82006-11-05 07:36:23 +0000450
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000451 // TODO: Pass argument information.
Chris Lattner229ce602006-11-21 01:21:07 +0000452 return Actions.ParseFunctionDefBody(Res, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000453}
454
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000455/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
456/// types for a function with a K&R-style identifier list for arguments.
457void Parser::ParseKNRParamDeclarations(Declarator &D) {
458 // We know that the top-level of this declarator is a function.
459 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
460
461 // Read all the argument declarations.
462 while (isDeclarationSpecifier()) {
463 SourceLocation DSStart = Tok.getLocation();
464
465 // Parse the common declaration-specifiers piece.
466 DeclSpec DS;
467 ParseDeclarationSpecifiers(DS);
468
469 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
470 // least one declarator'.
471 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
472 // the declarations though. It's trivial to ignore them, really hard to do
473 // anything else with them.
474 if (Tok.getKind() == tok::semi) {
475 Diag(DSStart, diag::err_declaration_does_not_declare_param);
476 ConsumeToken();
477 continue;
478 }
479
480 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
481 // than register.
482 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
483 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
484 Diag(DS.getStorageClassSpecLoc(),
485 diag::err_invalid_storage_class_in_func_decl);
486 DS.ClearStorageClassSpecs();
487 }
488 if (DS.isThreadSpecified()) {
489 Diag(DS.getThreadSpecLoc(),
490 diag::err_invalid_storage_class_in_func_decl);
491 DS.ClearStorageClassSpecs();
492 }
493
494 // Parse the first declarator attached to this declspec.
495 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
496 ParseDeclarator(ParmDeclarator);
497
498 // Handle the full declarator list.
499 while (1) {
500 // If attributes are present, parse them.
501 if (Tok.getKind() == tok::kw___attribute)
502 // FIXME: attach attributes too.
503 ParseAttributes();
504
505 // Ask the actions module to compute the type for this declarator.
506 Action::TypeResult TR =
507 Actions.ParseParamDeclaratorType(CurScope, ParmDeclarator);
508 if (!TR.isInvalid &&
509 // A missing identifier has already been diagnosed.
510 ParmDeclarator.getIdentifier()) {
511
512 // Scan the argument list looking for the correct param to apply this
513 // type.
514 for (unsigned i = 0; ; ++i) {
515 // C99 6.9.1p6: those declarators shall declare only identifiers from
516 // the identifier list.
517 if (i == FTI.NumArgs) {
518 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
519 ParmDeclarator.getIdentifier()->getName());
520 break;
521 }
522
523 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
524 // Reject redefinitions of parameters.
525 if (FTI.ArgInfo[i].TypeInfo) {
526 Diag(ParmDeclarator.getIdentifierLoc(),
527 diag::err_param_redefinition,
528 ParmDeclarator.getIdentifier()->getName());
529 } else {
530 FTI.ArgInfo[i].TypeInfo = TR.Val;
531 }
532 break;
533 }
534 }
535 }
536
537 // If we don't have a comma, it is either the end of the list (a ';') or
538 // an error, bail out.
539 if (Tok.getKind() != tok::comma)
540 break;
541
542 // Consume the comma.
543 ConsumeToken();
544
545 // Parse the next declarator.
546 ParmDeclarator.clear();
547 ParseDeclarator(ParmDeclarator);
548 }
549
550 if (Tok.getKind() == tok::semi) {
551 ConsumeToken();
552 } else {
553 Diag(Tok, diag::err_parse_error);
554 // Skip to end of block or statement
555 SkipUntil(tok::semi, true);
556 if (Tok.getKind() == tok::semi)
557 ConsumeToken();
558 }
559 }
560
561 // The actions module must verify that all arguments were declared.
562}
563
564
Chris Lattner0116c472006-08-15 06:03:28 +0000565/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
566/// allowed to be a wide string, and is not subject to character translation.
567///
568/// [GNU] asm-string-literal:
569/// string-literal
570///
571void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000572 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000573 Diag(Tok, diag::err_expected_string_literal);
574 return;
575 }
576
577 ExprResult Res = ParseStringLiteralExpression();
578 if (Res.isInvalid) return;
579
580 // TODO: Diagnose: wide string literal in 'asm'
581}
582
Chris Lattner6d7e6342006-08-15 03:41:14 +0000583/// ParseSimpleAsm
584///
585/// [GNU] simple-asm-expr:
586/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000587///
588void Parser::ParseSimpleAsm() {
589 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
590 ConsumeToken();
591
592 if (Tok.getKind() != tok::l_paren) {
593 Diag(Tok, diag::err_expected_lparen_after, "asm");
594 return;
595 }
596
Chris Lattner04132372006-10-16 06:12:55 +0000597 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000598
Chris Lattner0116c472006-08-15 06:03:28 +0000599 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000600
Chris Lattner04f80192006-08-15 04:55:54 +0000601 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000602}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000603