blob: 85b099ff0084a23fa5da9c6b8da3a9c3cf96abaa [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 Lattner289ab7b2006-11-08 06:54:53 +000015#include "clang/Parse/SemaDeclSpec.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 Lattner0663d2a2006-11-05 18:39:59 +000020Parser::Parser(Preprocessor &pp, MinimalAction &MinActions)
21 : PP(pp), Actions(MinActions), Diags(PP.getDiagnostics()) {
22 MinimalActions = &MinActions;
23 SemaActions = 0;
24 Tok.setKind(tok::eof);
25 CurScope = 0;
26
27 ParenCount = BracketCount = BraceCount = 0;
28}
29
30Parser::Parser(Preprocessor &pp, SemanticAction &SemanticActions)
31 : PP(pp), Actions(SemanticActions), Diags(PP.getDiagnostics()) {
32 MinimalActions = 0;
33 SemaActions = &SemanticActions;
Chris Lattner8c204872006-10-14 05:19:21 +000034 Tok.setKind(tok::eof);
Chris Lattnere4e38592006-08-14 00:15:05 +000035 CurScope = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000036
37 ParenCount = BracketCount = BraceCount = 0;
Chris Lattner971c6b62006-08-05 22:46:42 +000038}
39
Chris Lattner685ed1e2006-08-14 00:22:04 +000040/// Out-of-line virtual destructor to provide home for Action class.
41Action::~Action() {}
Chris Lattnere4e38592006-08-14 00:15:05 +000042
Chris Lattner0bb5f832006-07-31 01:59:18 +000043
Chris Lattnerb9093cd2006-08-04 04:39:53 +000044void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000045 const std::string &Msg) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000046 Diags.Report(Loc, DiagID, Msg);
Chris Lattner0bb5f832006-07-31 01:59:18 +000047}
48
Chris Lattner4564bc12006-08-10 23:14:52 +000049/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
50/// this helper function matches and consumes the specified RHS token if
51/// present. If not present, it emits the specified diagnostic indicating
52/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
53/// should be the name of the unmatched LHS token.
Chris Lattner71e23ce2006-11-04 20:18:38 +000054SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
55 SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000056
Chris Lattner71e23ce2006-11-04 20:18:38 +000057 if (Tok.getKind() == RHSTok)
58 return ConsumeAnyToken();
59
60 SourceLocation R = Tok.getLocation();
61 const char *LHSName = "unknown";
62 diag::kind DID = diag::err_parse_error;
63 switch (RHSTok) {
64 default: break;
65 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
66 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
67 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner4564bc12006-08-10 23:14:52 +000068 }
Chris Lattner71e23ce2006-11-04 20:18:38 +000069 Diag(Tok, DID);
70 Diag(LHSLoc, diag::err_matching, LHSName);
71 SkipUntil(RHSTok);
72 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +000073}
74
Chris Lattnerdbb2a462006-08-12 19:26:13 +000075/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
76/// input. If so, it is consumed and false is returned.
77///
78/// If the input is malformed, this emits the specified diagnostic. Next, if
79/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
80/// returned.
81bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000082 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000083 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000084 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000085 return false;
86 }
87
Chris Lattner6d7e6342006-08-15 03:41:14 +000088 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000089 if (SkipToTok != tok::unknown)
90 SkipUntil(SkipToTok);
91 return true;
92}
93
Chris Lattner70f32b72006-07-31 05:09:04 +000094//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000095// Error recovery.
96//===----------------------------------------------------------------------===//
97
98/// SkipUntil - Read tokens until we get to the specified token, then consume
99/// it (unless DontConsume is false). Because we cannot guarantee that the
100/// token will ever occur, this skips to the next token, or to some likely
101/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
102/// character.
103///
104/// If SkipUntil finds the specified token, it returns true, otherwise it
105/// returns false.
106bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +0000107 // We always want this function to skip at least one token if the first token
108 // isn't T and if not at EOF.
109 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000110 while (1) {
111 // If we found the token, stop and return true.
112 if (Tok.getKind() == T) {
113 if (DontConsume) {
114 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +0000115 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000116 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000117 }
118 return true;
119 }
120
121 switch (Tok.getKind()) {
122 case tok::eof:
123 // Ran out of tokens.
124 return false;
125
126 case tok::l_paren:
127 // Recursively skip properly-nested parens.
128 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000129 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000130 break;
131 case tok::l_square:
132 // Recursively skip properly-nested square brackets.
133 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000134 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000135 break;
136 case tok::l_brace:
137 // Recursively skip properly-nested braces.
138 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000139 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000140 break;
141
142 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
143 // Since the user wasn't looking for this token (if they were, it would
144 // already be handled), this isn't balanced. If there is a LHS token at a
145 // higher level, we will assume that this matches the unbalanced token
146 // and return it. Otherwise, this is a spurious RHS token, which we skip.
147 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000148 if (ParenCount && !isFirstTokenSkipped)
149 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000150 ConsumeParen();
151 break;
152 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000153 if (BracketCount && !isFirstTokenSkipped)
154 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000155 ConsumeBracket();
156 break;
157 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000158 if (BraceCount && !isFirstTokenSkipped)
159 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000160 ConsumeBrace();
161 break;
162
163 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000164 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000165 ConsumeStringToken();
166 break;
167 case tok::semi:
168 if (StopAtSemi)
169 return false;
170 // FALL THROUGH.
171 default:
172 // Skip this token.
173 ConsumeToken();
174 break;
175 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000176 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000177 }
178}
179
180//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000181// Scope manipulation
182//===----------------------------------------------------------------------===//
183
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000184/// ScopeCache - Cache scopes to avoid malloc traffic.
185static SmallVector<Scope*, 16> ScopeCache;
186
Chris Lattnere4e38592006-08-14 00:15:05 +0000187/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000188void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000189 if (!ScopeCache.empty()) {
190 Scope *N = ScopeCache.back();
191 ScopeCache.pop_back();
192 N->Init(CurScope, ScopeFlags);
193 CurScope = N;
194 } else {
195 CurScope = new Scope(CurScope, ScopeFlags);
196 }
Chris Lattnere4e38592006-08-14 00:15:05 +0000197}
198
199/// ExitScope - Pop a scope off the scope stack.
200void Parser::ExitScope() {
201 assert(CurScope && "Scope imbalance!");
202
203 // Inform the actions module that this scope is going away.
204 Actions.PopScope(Tok.getLocation(), CurScope);
205
206 Scope *Old = CurScope;
207 CurScope = Old->getParent();
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000208
209 if (ScopeCache.size() == 16)
210 delete Old;
211 else
212 ScopeCache.push_back(Old);
Chris Lattnere4e38592006-08-14 00:15:05 +0000213}
214
215
216
217
218//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000219// C99 6.9: External Definitions.
220//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000221
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000222Parser::~Parser() {
223 // If we still have scopes active, delete the scope tree.
224 delete CurScope;
225
226 // Free the scope cache.
227 while (!ScopeCache.empty()) {
228 delete ScopeCache.back();
229 ScopeCache.pop_back();
230 }
231}
232
Chris Lattner38ba3362006-08-17 07:04:37 +0000233/// Initialize - Warm up the parser.
234///
235void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000236 // Prime the lexer look-ahead.
237 ConsumeToken();
238
239 // Create the global scope, install it as the current scope.
240 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000241 EnterScope(0);
Chris Lattner38ba3362006-08-17 07:04:37 +0000242
Chris Lattner6d7e6342006-08-15 03:41:14 +0000243
244 // Install builtin types.
245 // TODO: Move this someplace more useful.
246 {
247 //__builtin_va_list
248 DeclSpec DS;
249 DS.StorageClassSpec = DeclSpec::SCS_typedef;
250
251 // TODO: add a 'TST_builtin' type?
252 DS.TypeSpecType = DeclSpec::TST_typedef;
Chris Lattner38ba3362006-08-17 07:04:37 +0000253
Chris Lattner6d7e6342006-08-15 03:41:14 +0000254 Declarator D(DS, Declarator::FileContext);
255 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000256 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000257 }
258
Chris Lattner0bb5f832006-07-31 01:59:18 +0000259 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
260 Diag(diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000261}
262
263/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
264/// action tells us to. This returns true if the EOF was encountered.
265bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
266 Result = 0;
267 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000268
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000269 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000270 return false;
271}
272
273/// Finalize - Shut down the parser.
274///
275void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000276 ExitScope();
277 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000278}
279
Chris Lattner38ba3362006-08-17 07:04:37 +0000280/// ParseTranslationUnit:
281/// translation-unit: [C99 6.9]
282/// external-declaration
283/// translation-unit external-declaration
284void Parser::ParseTranslationUnit() {
285 Initialize();
286
287 DeclTy *Res;
288 while (!ParseTopLevelDecl(Res))
289 /*parse them all*/;
290
291 Finalize();
292}
293
Chris Lattner0bb5f832006-07-31 01:59:18 +0000294/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000295/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000296/// function-definition [TODO]
297/// declaration [TODO]
298/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000299/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000300/// [GNU] __extension__ external-declaration [TODO]
Chris Lattner40f16b52006-11-05 02:05:37 +0000301/// [OBJC] objc-class-definition
302/// [OBJC] objc-class-declaration
303/// [OBJC] objc-alias-declaration
304/// [OBJC] objc-protocol-definition
305/// [OBJC] objc-method-definition
306/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000307///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000308/// [GNU] asm-definition:
309/// simple-asm-expr ';'
310///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000311Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000312 switch (Tok.getKind()) {
313 case tok::semi:
314 Diag(diag::ext_top_level_semi);
315 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000316 // TODO: Invoke action for top-level semicolon.
317 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000318 case tok::kw_asm:
319 ParseSimpleAsm();
320 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
321 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000322 // TODO: Invoke action for top-level asm.
323 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000324 case tok::at:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000325 ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000326 return 0;
327 case tok::minus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000328 ParseObjCInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000329 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000330 case tok::plus:
Chris Lattnerb26b6652006-11-08 06:10:32 +0000331 ParseObjCClassMethodDeclaration();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000332 return 0;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000333 default:
334 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000335 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000336 }
337}
338
339/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000340/// a declaration. We can't tell which we have until we read up to the
341/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000342///
Chris Lattner70f32b72006-07-31 05:09:04 +0000343/// function-definition: [C99 6.9.1]
344/// declaration-specifiers[opt] declarator declaration-list[opt]
345/// compound-statement [TODO]
346/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000347/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000348/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000349/// [OMP] threadprivate-directive [TODO]
350///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000351Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000352 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000353 DeclSpec DS;
354 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000355
356 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000357 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000358 if (Tok.getKind() == tok::semi) {
359 // TODO: emit error on 'int;' or 'const enum foo;'.
360 // if (!DS.isMissingDeclaratorOk()) Diag(...);
361
362 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000363 // TODO: Return type definition.
364 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000365 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000366
Chris Lattnerfff824f2006-08-07 06:31:38 +0000367 // Parse the first declarator.
368 Declarator DeclaratorInfo(DS, Declarator::FileContext);
369 ParseDeclarator(DeclaratorInfo);
370 // Error parsing the declarator?
371 if (DeclaratorInfo.getIdentifier() == 0) {
372 // If so, skip until the semi-colon or a }.
373 SkipUntil(tok::r_brace, true);
374 if (Tok.getKind() == tok::semi)
375 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000376 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000377 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000378
Chris Lattnerfff824f2006-08-07 06:31:38 +0000379 // If the declarator is the start of a function definition, handle it.
380 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
381 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000382 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000383 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
384 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
385 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000386 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000387 (Tok.getKind() == tok::l_brace || // int X() {}
388 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000389 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000390 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000391 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000392 Diag(Tok, diag::err_expected_fn_body);
393 else
394 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000395 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000396 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000397 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000398
Chris Lattner53361ac2006-08-10 05:19:57 +0000399 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000400 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000401}
402
Chris Lattnerfff824f2006-08-07 06:31:38 +0000403/// ParseFunctionDefinition - We parsed and verified that the specified
404/// Declarator is well formed. If this is a K&R-style function, read the
405/// parameters declaration-list, then start the compound-statement.
406///
407/// declaration-specifiers[opt] declarator declaration-list[opt]
408/// compound-statement [TODO]
409///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000410Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnerfff824f2006-08-07 06:31:38 +0000411 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
412 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
413 "This isn't a function declarator!");
Chris Lattner7014fb82006-11-05 07:36:23 +0000414
415 // FIXME: Enter a scope for the arguments.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000416 //EnterScope(Scope::FnScope);
Chris Lattner7014fb82006-11-05 07:36:23 +0000417
418
Chris Lattnerfff824f2006-08-07 06:31:38 +0000419
420 // If this declaration was formed with a K&R-style identifier list for the
421 // arguments, parse declarations for all of the args next.
422 // int foo(a,b) int a; float b; {}
423 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
424 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000425 while (isDeclarationSpecifier())
426 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000427
428 // Note, check that we got them all.
429 } else {
430 //if (isDeclarationSpecifier())
431 // Diag('k&r declspecs with prototype?');
432
Chris Lattner8693a512006-08-13 21:54:02 +0000433 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000434 }
435
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000436 // We should have an opening brace now.
437 if (Tok.getKind() != tok::l_brace) {
438 Diag(Tok, diag::err_expected_fn_body);
439
440 // Skip over garbage, until we get to '{'. Don't eat the '{'.
441 SkipUntil(tok::l_brace, true, true);
442
443 // If we didn't find the '{', bail out.
444 if (Tok.getKind() != tok::l_brace)
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000445 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000446 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000447
Chris Lattner30f910e2006-10-16 05:52:41 +0000448 // Parse the function body as a compound stmt.
449 StmtResult FnBody = ParseCompoundStatement();
450 if (FnBody.isInvalid) return 0;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000451
Chris Lattner7014fb82006-11-05 07:36:23 +0000452 // FIXME: Leave the argument scope.
453 // ExitScope();
454
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000455 // TODO: Pass argument information.
Chris Lattner30f910e2006-10-16 05:52:41 +0000456 return Actions.ParseFunctionDefinition(CurScope, D, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000457}
458
Chris Lattner0116c472006-08-15 06:03:28 +0000459/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
460/// allowed to be a wide string, and is not subject to character translation.
461///
462/// [GNU] asm-string-literal:
463/// string-literal
464///
465void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000466 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000467 Diag(Tok, diag::err_expected_string_literal);
468 return;
469 }
470
471 ExprResult Res = ParseStringLiteralExpression();
472 if (Res.isInvalid) return;
473
474 // TODO: Diagnose: wide string literal in 'asm'
475}
476
Chris Lattner6d7e6342006-08-15 03:41:14 +0000477/// ParseSimpleAsm
478///
479/// [GNU] simple-asm-expr:
480/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000481///
482void Parser::ParseSimpleAsm() {
483 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
484 ConsumeToken();
485
486 if (Tok.getKind() != tok::l_paren) {
487 Diag(Tok, diag::err_expected_lparen_after, "asm");
488 return;
489 }
490
Chris Lattner04132372006-10-16 06:12:55 +0000491 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000492
Chris Lattner0116c472006-08-15 06:03:28 +0000493 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000494
Chris Lattner04f80192006-08-15 04:55:54 +0000495 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000496}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000497