blob: cf440cfdb6477a8e9ffc0e2070c1055a7a1ba755 [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.
95bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000096 // We always want this function to skip at least one token if the first token
97 // isn't T and if not at EOF.
98 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +000099 while (1) {
100 // If we found the token, stop and return true.
101 if (Tok.getKind() == T) {
102 if (DontConsume) {
103 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +0000104 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000105 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000106 }
107 return true;
108 }
109
110 switch (Tok.getKind()) {
111 case tok::eof:
112 // Ran out of tokens.
113 return false;
114
115 case tok::l_paren:
116 // Recursively skip properly-nested parens.
117 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000118 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000119 break;
120 case tok::l_square:
121 // Recursively skip properly-nested square brackets.
122 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000123 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000124 break;
125 case tok::l_brace:
126 // Recursively skip properly-nested braces.
127 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000128 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000129 break;
130
131 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
132 // Since the user wasn't looking for this token (if they were, it would
133 // already be handled), this isn't balanced. If there is a LHS token at a
134 // higher level, we will assume that this matches the unbalanced token
135 // and return it. Otherwise, this is a spurious RHS token, which we skip.
136 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000137 if (ParenCount && !isFirstTokenSkipped)
138 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000139 ConsumeParen();
140 break;
141 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000142 if (BracketCount && !isFirstTokenSkipped)
143 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000144 ConsumeBracket();
145 break;
146 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000147 if (BraceCount && !isFirstTokenSkipped)
148 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000149 ConsumeBrace();
150 break;
151
152 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000153 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000154 ConsumeStringToken();
155 break;
156 case tok::semi:
157 if (StopAtSemi)
158 return false;
159 // FALL THROUGH.
160 default:
161 // Skip this token.
162 ConsumeToken();
163 break;
164 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000165 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000166 }
167}
168
169//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000170// Scope manipulation
171//===----------------------------------------------------------------------===//
172
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000173/// ScopeCache - Cache scopes to avoid malloc traffic.
174static SmallVector<Scope*, 16> ScopeCache;
175
Chris Lattnere4e38592006-08-14 00:15:05 +0000176/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000177void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000178 if (!ScopeCache.empty()) {
179 Scope *N = ScopeCache.back();
180 ScopeCache.pop_back();
181 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
192 // Inform the actions module that this scope is going away.
193 Actions.PopScope(Tok.getLocation(), CurScope);
194
195 Scope *Old = CurScope;
196 CurScope = Old->getParent();
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000197
198 if (ScopeCache.size() == 16)
199 delete Old;
200 else
201 ScopeCache.push_back(Old);
Chris Lattnere4e38592006-08-14 00:15:05 +0000202}
203
204
205
206
207//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000208// C99 6.9: External Definitions.
209//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000210
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000211Parser::~Parser() {
212 // If we still have scopes active, delete the scope tree.
213 delete CurScope;
214
215 // Free the scope cache.
216 while (!ScopeCache.empty()) {
217 delete ScopeCache.back();
218 ScopeCache.pop_back();
219 }
220}
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
228 // Create the global scope, install it as the current scope.
229 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000230 EnterScope(0);
Chris Lattner38ba3362006-08-17 07:04:37 +0000231
Chris Lattner6d7e6342006-08-15 03:41:14 +0000232
233 // Install builtin types.
234 // TODO: Move this someplace more useful.
235 {
Chris Lattner353f5742006-11-28 04:50:12 +0000236 const char *Dummy;
237
Chris Lattner6d7e6342006-08-15 03:41:14 +0000238 //__builtin_va_list
239 DeclSpec DS;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000240 bool Error = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, SourceLocation(),
241 Dummy);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000242
243 // TODO: add a 'TST_builtin' type?
Chris Lattnerb20e8942006-11-28 05:30:29 +0000244 Error |= DS.SetTypeSpecType(DeclSpec::TST_int, SourceLocation(), Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000245 assert(!Error && "Error setting up __builtin_va_list!");
Chris Lattner38ba3362006-08-17 07:04:37 +0000246
Chris Lattner6d7e6342006-08-15 03:41:14 +0000247 Declarator D(DS, Declarator::FileContext);
248 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000249 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000250 }
251
Chris Lattner0bb5f832006-07-31 01:59:18 +0000252 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
Chris Lattnerbd638922006-11-10 05:19:25 +0000253 Diag(Tok, diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000254}
255
256/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
257/// action tells us to. This returns true if the EOF was encountered.
258bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
259 Result = 0;
260 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000261
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000262 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000263 return false;
264}
265
266/// Finalize - Shut down the parser.
267///
268void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000269 ExitScope();
270 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000271}
272
Chris Lattner38ba3362006-08-17 07:04:37 +0000273/// ParseTranslationUnit:
274/// translation-unit: [C99 6.9]
275/// external-declaration
276/// translation-unit external-declaration
277void Parser::ParseTranslationUnit() {
278 Initialize();
279
280 DeclTy *Res;
281 while (!ParseTopLevelDecl(Res))
282 /*parse them all*/;
283
284 Finalize();
285}
286
Chris Lattner0bb5f832006-07-31 01:59:18 +0000287/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000288/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000289/// function-definition [TODO]
290/// declaration [TODO]
291/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000292/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000293/// [GNU] __extension__ external-declaration [TODO]
Chris Lattner40f16b52006-11-05 02:05:37 +0000294/// [OBJC] objc-class-definition
295/// [OBJC] objc-class-declaration
296/// [OBJC] objc-alias-declaration
297/// [OBJC] objc-protocol-definition
298/// [OBJC] objc-method-definition
299/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000300///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000301/// [GNU] asm-definition:
302/// simple-asm-expr ';'
303///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000304Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000305 switch (Tok.getKind()) {
306 case tok::semi:
Chris Lattnerbd638922006-11-10 05:19:25 +0000307 Diag(Tok, diag::ext_top_level_semi);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000308 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000309 // TODO: Invoke action for top-level semicolon.
310 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000311 case tok::kw_asm:
312 ParseSimpleAsm();
313 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
314 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000315 // TODO: Invoke action for top-level asm.
316 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000317 case tok::at:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000318 ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000319 return 0;
320 case tok::minus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000321 ParseObjCInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000322 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000323 case tok::plus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000324 ParseObjCClassMethodDeclaration();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000325 return 0;
Chris Lattner302b4be2006-11-19 02:31:38 +0000326 case tok::kw_typedef:
327 // A function definition cannot start with a 'typedef' keyword.
328 return ParseDeclaration(Declarator::FileContext);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000329 default:
330 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000331 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000332 }
333}
334
335/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000336/// a declaration. We can't tell which we have until we read up to the
337/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000338///
Chris Lattner70f32b72006-07-31 05:09:04 +0000339/// function-definition: [C99 6.9.1]
340/// declaration-specifiers[opt] declarator declaration-list[opt]
341/// compound-statement [TODO]
342/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000343/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000344/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000345/// [OMP] threadprivate-directive [TODO]
346///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000347Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000348 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000349 DeclSpec DS;
350 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000351
352 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000353 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000354 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000355 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000356 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000357 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000358
Chris Lattnerfff824f2006-08-07 06:31:38 +0000359 // Parse the first declarator.
360 Declarator DeclaratorInfo(DS, Declarator::FileContext);
361 ParseDeclarator(DeclaratorInfo);
362 // Error parsing the declarator?
363 if (DeclaratorInfo.getIdentifier() == 0) {
364 // If so, skip until the semi-colon or a }.
365 SkipUntil(tok::r_brace, true);
366 if (Tok.getKind() == tok::semi)
367 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000368 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000369 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000370
Chris Lattnerfff824f2006-08-07 06:31:38 +0000371 // If the declarator is the start of a function definition, handle it.
372 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
373 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000374 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000375 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
376 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
377 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000378 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000379 (Tok.getKind() == tok::l_brace || // int X() {}
380 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000381 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000382 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000383 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000384 Diag(Tok, diag::err_expected_fn_body);
385 else
386 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000387 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000388 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000389 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000390
Chris Lattner53361ac2006-08-10 05:19:57 +0000391 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000392 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000393}
394
Chris Lattnerfff824f2006-08-07 06:31:38 +0000395/// ParseFunctionDefinition - We parsed and verified that the specified
396/// Declarator is well formed. If this is a K&R-style function, read the
397/// parameters declaration-list, then start the compound-statement.
398///
399/// declaration-specifiers[opt] declarator declaration-list[opt]
400/// compound-statement [TODO]
401///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000402Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnercbc426d2006-12-02 06:43:02 +0000403 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
404 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000405 "This isn't a function declarator!");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000406 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000407
408 // If this declaration was formed with a K&R-style identifier list for the
409 // arguments, parse declarations for all of the args next.
410 // int foo(a,b) int a; float b; {}
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000411 if (!FTI.hasPrototype && FTI.NumArgs != 0)
412 ParseKNRParamDeclarations(D);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000413
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000414 // Enter a scope for the function body.
415 EnterScope(Scope::FnScope);
416
Chris Lattner2114d5e2006-12-04 07:40:24 +0000417 // Tell the actions module that we have entered a function definition with the
418 // specified Declarator for the function.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000419 DeclTy *Res = Actions.ParseStartOfFunctionDef(CurScope, D);
420
421
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000422 // We should have an opening brace now.
423 if (Tok.getKind() != tok::l_brace) {
424 Diag(Tok, diag::err_expected_fn_body);
425
426 // Skip over garbage, until we get to '{'. Don't eat the '{'.
427 SkipUntil(tok::l_brace, true, true);
428
429 // If we didn't find the '{', bail out.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000430 if (Tok.getKind() != tok::l_brace) {
431 ExitScope();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000432 return 0;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000433 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000434 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000435
Chris Lattner7f58c3d2007-01-21 06:56:16 +0000436 // Do not enter a scope for the brace, as the arguments are in the same scope
437 // (the function body) as the body itself. Instead, just read the statement
438 // list and put it into a CompoundStmt for safe keeping.
439 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000440 if (FnBody.isInvalid) {
441 ExitScope();
442 return 0;
443 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000444
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000445 // Leave the function body scope.
446 ExitScope();
Chris Lattner7014fb82006-11-05 07:36:23 +0000447
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000448 // TODO: Pass argument information.
Chris Lattner229ce602006-11-21 01:21:07 +0000449 return Actions.ParseFunctionDefBody(Res, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000450}
451
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000452/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
453/// types for a function with a K&R-style identifier list for arguments.
454void Parser::ParseKNRParamDeclarations(Declarator &D) {
455 // We know that the top-level of this declarator is a function.
456 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
457
458 // Read all the argument declarations.
459 while (isDeclarationSpecifier()) {
460 SourceLocation DSStart = Tok.getLocation();
461
462 // Parse the common declaration-specifiers piece.
463 DeclSpec DS;
464 ParseDeclarationSpecifiers(DS);
465
466 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
467 // least one declarator'.
468 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
469 // the declarations though. It's trivial to ignore them, really hard to do
470 // anything else with them.
471 if (Tok.getKind() == tok::semi) {
472 Diag(DSStart, diag::err_declaration_does_not_declare_param);
473 ConsumeToken();
474 continue;
475 }
476
477 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
478 // than register.
479 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
480 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
481 Diag(DS.getStorageClassSpecLoc(),
482 diag::err_invalid_storage_class_in_func_decl);
483 DS.ClearStorageClassSpecs();
484 }
485 if (DS.isThreadSpecified()) {
486 Diag(DS.getThreadSpecLoc(),
487 diag::err_invalid_storage_class_in_func_decl);
488 DS.ClearStorageClassSpecs();
489 }
490
491 // Parse the first declarator attached to this declspec.
492 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
493 ParseDeclarator(ParmDeclarator);
494
495 // Handle the full declarator list.
496 while (1) {
497 // If attributes are present, parse them.
498 if (Tok.getKind() == tok::kw___attribute)
499 // FIXME: attach attributes too.
500 ParseAttributes();
501
502 // Ask the actions module to compute the type for this declarator.
503 Action::TypeResult TR =
504 Actions.ParseParamDeclaratorType(CurScope, ParmDeclarator);
505 if (!TR.isInvalid &&
506 // A missing identifier has already been diagnosed.
507 ParmDeclarator.getIdentifier()) {
508
509 // Scan the argument list looking for the correct param to apply this
510 // type.
511 for (unsigned i = 0; ; ++i) {
512 // C99 6.9.1p6: those declarators shall declare only identifiers from
513 // the identifier list.
514 if (i == FTI.NumArgs) {
515 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
516 ParmDeclarator.getIdentifier()->getName());
517 break;
518 }
519
520 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
521 // Reject redefinitions of parameters.
522 if (FTI.ArgInfo[i].TypeInfo) {
523 Diag(ParmDeclarator.getIdentifierLoc(),
524 diag::err_param_redefinition,
525 ParmDeclarator.getIdentifier()->getName());
526 } else {
527 FTI.ArgInfo[i].TypeInfo = TR.Val;
528 }
529 break;
530 }
531 }
532 }
533
534 // If we don't have a comma, it is either the end of the list (a ';') or
535 // an error, bail out.
536 if (Tok.getKind() != tok::comma)
537 break;
538
539 // Consume the comma.
540 ConsumeToken();
541
542 // Parse the next declarator.
543 ParmDeclarator.clear();
544 ParseDeclarator(ParmDeclarator);
545 }
546
547 if (Tok.getKind() == tok::semi) {
548 ConsumeToken();
549 } else {
550 Diag(Tok, diag::err_parse_error);
551 // Skip to end of block or statement
552 SkipUntil(tok::semi, true);
553 if (Tok.getKind() == tok::semi)
554 ConsumeToken();
555 }
556 }
557
558 // The actions module must verify that all arguments were declared.
559}
560
561
Chris Lattner0116c472006-08-15 06:03:28 +0000562/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
563/// allowed to be a wide string, and is not subject to character translation.
564///
565/// [GNU] asm-string-literal:
566/// string-literal
567///
568void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000569 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000570 Diag(Tok, diag::err_expected_string_literal);
571 return;
572 }
573
574 ExprResult Res = ParseStringLiteralExpression();
575 if (Res.isInvalid) return;
576
577 // TODO: Diagnose: wide string literal in 'asm'
578}
579
Chris Lattner6d7e6342006-08-15 03:41:14 +0000580/// ParseSimpleAsm
581///
582/// [GNU] simple-asm-expr:
583/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000584///
585void Parser::ParseSimpleAsm() {
586 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
587 ConsumeToken();
588
589 if (Tok.getKind() != tok::l_paren) {
590 Diag(Tok, diag::err_expected_lparen_after, "asm");
591 return;
592 }
593
Chris Lattner04132372006-10-16 06:12:55 +0000594 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000595
Chris Lattner0116c472006-08-15 06:03:28 +0000596 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000597
Chris Lattner04f80192006-08-15 04:55:54 +0000598 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000599}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000600