blob: 43089f788bd72f6622016758f449c89d2cdca119 [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 Lattner7ad0fbe2006-11-05 07:46:30 +000015#include "clang/Parse/SemaDecl.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
40Parser::~Parser() {
Chris Lattnere4e38592006-08-14 00:15:05 +000041 // If we still have scopes active, delete the scope tree.
Chris Lattner971c6b62006-08-05 22:46:42 +000042 delete CurScope;
43}
44
Chris Lattner685ed1e2006-08-14 00:22:04 +000045/// Out-of-line virtual destructor to provide home for Action class.
46Action::~Action() {}
Chris Lattnere4e38592006-08-14 00:15:05 +000047
Chris Lattner0bb5f832006-07-31 01:59:18 +000048
Chris Lattnerb9093cd2006-08-04 04:39:53 +000049void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000050 const std::string &Msg) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000051 Diags.Report(Loc, DiagID, Msg);
Chris Lattner0bb5f832006-07-31 01:59:18 +000052}
53
Chris Lattner4564bc12006-08-10 23:14:52 +000054/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
55/// this helper function matches and consumes the specified RHS token if
56/// present. If not present, it emits the specified diagnostic indicating
57/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
58/// should be the name of the unmatched LHS token.
Chris Lattner71e23ce2006-11-04 20:18:38 +000059SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
60 SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000061
Chris Lattner71e23ce2006-11-04 20:18:38 +000062 if (Tok.getKind() == RHSTok)
63 return ConsumeAnyToken();
64
65 SourceLocation R = Tok.getLocation();
66 const char *LHSName = "unknown";
67 diag::kind DID = diag::err_parse_error;
68 switch (RHSTok) {
69 default: break;
70 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
71 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
72 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner4564bc12006-08-10 23:14:52 +000073 }
Chris Lattner71e23ce2006-11-04 20:18:38 +000074 Diag(Tok, DID);
75 Diag(LHSLoc, diag::err_matching, LHSName);
76 SkipUntil(RHSTok);
77 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +000078}
79
Chris Lattnerdbb2a462006-08-12 19:26:13 +000080/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
81/// input. If so, it is consumed and false is returned.
82///
83/// If the input is malformed, this emits the specified diagnostic. Next, if
84/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
85/// returned.
86bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000087 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000088 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000089 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000090 return false;
91 }
92
Chris Lattner6d7e6342006-08-15 03:41:14 +000093 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000094 if (SkipToTok != tok::unknown)
95 SkipUntil(SkipToTok);
96 return true;
97}
98
Chris Lattner70f32b72006-07-31 05:09:04 +000099//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +0000100// Error recovery.
101//===----------------------------------------------------------------------===//
102
103/// SkipUntil - Read tokens until we get to the specified token, then consume
104/// it (unless DontConsume is false). Because we cannot guarantee that the
105/// token will ever occur, this skips to the next token, or to some likely
106/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
107/// character.
108///
109/// If SkipUntil finds the specified token, it returns true, otherwise it
110/// returns false.
111bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +0000112 // We always want this function to skip at least one token if the first token
113 // isn't T and if not at EOF.
114 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000115 while (1) {
116 // If we found the token, stop and return true.
117 if (Tok.getKind() == T) {
118 if (DontConsume) {
119 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +0000120 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000121 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000122 }
123 return true;
124 }
125
126 switch (Tok.getKind()) {
127 case tok::eof:
128 // Ran out of tokens.
129 return false;
130
131 case tok::l_paren:
132 // Recursively skip properly-nested parens.
133 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000134 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000135 break;
136 case tok::l_square:
137 // Recursively skip properly-nested square brackets.
138 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000139 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000140 break;
141 case tok::l_brace:
142 // Recursively skip properly-nested braces.
143 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000144 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000145 break;
146
147 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
148 // Since the user wasn't looking for this token (if they were, it would
149 // already be handled), this isn't balanced. If there is a LHS token at a
150 // higher level, we will assume that this matches the unbalanced token
151 // and return it. Otherwise, this is a spurious RHS token, which we skip.
152 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000153 if (ParenCount && !isFirstTokenSkipped)
154 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000155 ConsumeParen();
156 break;
157 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000158 if (BracketCount && !isFirstTokenSkipped)
159 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000160 ConsumeBracket();
161 break;
162 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000163 if (BraceCount && !isFirstTokenSkipped)
164 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000165 ConsumeBrace();
166 break;
167
168 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000169 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000170 ConsumeStringToken();
171 break;
172 case tok::semi:
173 if (StopAtSemi)
174 return false;
175 // FALL THROUGH.
176 default:
177 // Skip this token.
178 ConsumeToken();
179 break;
180 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000181 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000182 }
183}
184
185//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000186// Scope manipulation
187//===----------------------------------------------------------------------===//
188
189/// EnterScope - Start a new scope.
190void Parser::EnterScope() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000191 CurScope = new Scope(CurScope);
192}
193
194/// ExitScope - Pop a scope off the scope stack.
195void Parser::ExitScope() {
196 assert(CurScope && "Scope imbalance!");
197
198 // Inform the actions module that this scope is going away.
199 Actions.PopScope(Tok.getLocation(), CurScope);
200
201 Scope *Old = CurScope;
202 CurScope = Old->getParent();
203 delete Old;
204}
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 Lattner38ba3362006-08-17 07:04:37 +0000213/// Initialize - Warm up the parser.
214///
215void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000216 // Prime the lexer look-ahead.
217 ConsumeToken();
218
219 // Create the global scope, install it as the current scope.
220 assert(CurScope == 0 && "A scope is already active?");
221 EnterScope();
Chris Lattner38ba3362006-08-17 07:04:37 +0000222
Chris Lattner6d7e6342006-08-15 03:41:14 +0000223
224 // Install builtin types.
225 // TODO: Move this someplace more useful.
226 {
227 //__builtin_va_list
228 DeclSpec DS;
229 DS.StorageClassSpec = DeclSpec::SCS_typedef;
230
231 // TODO: add a 'TST_builtin' type?
232 DS.TypeSpecType = DeclSpec::TST_typedef;
Chris Lattner38ba3362006-08-17 07:04:37 +0000233
Chris Lattner6d7e6342006-08-15 03:41:14 +0000234 Declarator D(DS, Declarator::FileContext);
235 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000236 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000237 }
238
Chris Lattner0bb5f832006-07-31 01:59:18 +0000239 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
240 Diag(diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000241}
242
243/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
244/// action tells us to. This returns true if the EOF was encountered.
245bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
246 Result = 0;
247 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000248
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000249 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000250 return false;
251}
252
253/// Finalize - Shut down the parser.
254///
255void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000256 ExitScope();
257 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000258}
259
Chris Lattner38ba3362006-08-17 07:04:37 +0000260/// ParseTranslationUnit:
261/// translation-unit: [C99 6.9]
262/// external-declaration
263/// translation-unit external-declaration
264void Parser::ParseTranslationUnit() {
265 Initialize();
266
267 DeclTy *Res;
268 while (!ParseTopLevelDecl(Res))
269 /*parse them all*/;
270
271 Finalize();
272}
273
Chris Lattner0bb5f832006-07-31 01:59:18 +0000274/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000275/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000276/// function-definition [TODO]
277/// declaration [TODO]
278/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000279/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000280/// [GNU] __extension__ external-declaration [TODO]
Chris Lattner40f16b52006-11-05 02:05:37 +0000281/// [OBJC] objc-class-definition
282/// [OBJC] objc-class-declaration
283/// [OBJC] objc-alias-declaration
284/// [OBJC] objc-protocol-definition
285/// [OBJC] objc-method-definition
286/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000287///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000288/// [GNU] asm-definition:
289/// simple-asm-expr ';'
290///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000291Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000292 switch (Tok.getKind()) {
293 case tok::semi:
294 Diag(diag::ext_top_level_semi);
295 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000296 // TODO: Invoke action for top-level semicolon.
297 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000298 case tok::kw_asm:
299 ParseSimpleAsm();
300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
301 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000302 // TODO: Invoke action for top-level asm.
303 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000304 case tok::at:
305 ObjCParseAtDirectives();
306 return 0;
307 case tok::minus:
308 ObjCParseInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000309 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000310 case tok::plus:
311 ObjCParseClassMethodDeclaration();
312 return 0;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000313 default:
314 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000315 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000316 }
317}
318
319/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000320/// a declaration. We can't tell which we have until we read up to the
321/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000322///
Chris Lattner70f32b72006-07-31 05:09:04 +0000323/// function-definition: [C99 6.9.1]
324/// declaration-specifiers[opt] declarator declaration-list[opt]
325/// compound-statement [TODO]
326/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000327/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000328/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000329/// [OMP] threadprivate-directive [TODO]
330///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000331Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000332 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000333 DeclSpec DS;
334 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000335
336 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000337 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000338 if (Tok.getKind() == tok::semi) {
339 // TODO: emit error on 'int;' or 'const enum foo;'.
340 // if (!DS.isMissingDeclaratorOk()) Diag(...);
341
342 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000343 // TODO: Return type definition.
344 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000345 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000346
Chris Lattnerfff824f2006-08-07 06:31:38 +0000347 // Parse the first declarator.
348 Declarator DeclaratorInfo(DS, Declarator::FileContext);
349 ParseDeclarator(DeclaratorInfo);
350 // Error parsing the declarator?
351 if (DeclaratorInfo.getIdentifier() == 0) {
352 // If so, skip until the semi-colon or a }.
353 SkipUntil(tok::r_brace, true);
354 if (Tok.getKind() == tok::semi)
355 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000356 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000357 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000358
Chris Lattnerfff824f2006-08-07 06:31:38 +0000359 // If the declarator is the start of a function definition, handle it.
360 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
361 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000362 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000363 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
364 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
365 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000366 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000367 (Tok.getKind() == tok::l_brace || // int X() {}
368 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000369 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000370 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000371 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000372 Diag(Tok, diag::err_expected_fn_body);
373 else
374 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000375 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000376 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000377 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000378
Chris Lattner53361ac2006-08-10 05:19:57 +0000379 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000380 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000381}
382
Chris Lattnerfff824f2006-08-07 06:31:38 +0000383/// ParseFunctionDefinition - We parsed and verified that the specified
384/// Declarator is well formed. If this is a K&R-style function, read the
385/// parameters declaration-list, then start the compound-statement.
386///
387/// declaration-specifiers[opt] declarator declaration-list[opt]
388/// compound-statement [TODO]
389///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000390Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnerfff824f2006-08-07 06:31:38 +0000391 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
392 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
393 "This isn't a function declarator!");
Chris Lattner7014fb82006-11-05 07:36:23 +0000394
395 // FIXME: Enter a scope for the arguments.
396 //EnterScope();
397
398
Chris Lattnerfff824f2006-08-07 06:31:38 +0000399
400 // If this declaration was formed with a K&R-style identifier list for the
401 // arguments, parse declarations for all of the args next.
402 // int foo(a,b) int a; float b; {}
403 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
404 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000405 while (isDeclarationSpecifier())
406 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000407
408 // Note, check that we got them all.
409 } else {
410 //if (isDeclarationSpecifier())
411 // Diag('k&r declspecs with prototype?');
412
Chris Lattner8693a512006-08-13 21:54:02 +0000413 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000414 }
415
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000416 // We should have an opening brace now.
417 if (Tok.getKind() != tok::l_brace) {
418 Diag(Tok, diag::err_expected_fn_body);
419
420 // Skip over garbage, until we get to '{'. Don't eat the '{'.
421 SkipUntil(tok::l_brace, true, true);
422
423 // If we didn't find the '{', bail out.
424 if (Tok.getKind() != tok::l_brace)
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000425 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000426 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000427
Chris Lattner30f910e2006-10-16 05:52:41 +0000428 // Parse the function body as a compound stmt.
429 StmtResult FnBody = ParseCompoundStatement();
430 if (FnBody.isInvalid) return 0;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000431
Chris Lattner7014fb82006-11-05 07:36:23 +0000432 // FIXME: Leave the argument scope.
433 // ExitScope();
434
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000435 // TODO: Pass argument information.
Chris Lattner30f910e2006-10-16 05:52:41 +0000436 return Actions.ParseFunctionDefinition(CurScope, D, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000437}
438
Chris Lattner0116c472006-08-15 06:03:28 +0000439/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
440/// allowed to be a wide string, and is not subject to character translation.
441///
442/// [GNU] asm-string-literal:
443/// string-literal
444///
445void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000446 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000447 Diag(Tok, diag::err_expected_string_literal);
448 return;
449 }
450
451 ExprResult Res = ParseStringLiteralExpression();
452 if (Res.isInvalid) return;
453
454 // TODO: Diagnose: wide string literal in 'asm'
455}
456
Chris Lattner6d7e6342006-08-15 03:41:14 +0000457/// ParseSimpleAsm
458///
459/// [GNU] simple-asm-expr:
460/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000461///
462void Parser::ParseSimpleAsm() {
463 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
464 ConsumeToken();
465
466 if (Tok.getKind() != tok::l_paren) {
467 Diag(Tok, diag::err_expected_lparen_after, "asm");
468 return;
469 }
470
Chris Lattner04132372006-10-16 06:12:55 +0000471 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000472
Chris Lattner0116c472006-08-15 06:03:28 +0000473 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000474
Chris Lattner04f80192006-08-15 04:55:54 +0000475 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000476}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000477