blob: fc5c4ae4f7143d91792fad713c10889fc38364d2 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Parser.cpp - C Language Family Parser ----------------------------===//
2//
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"
15#include "clang/Parse/DeclSpec.h"
16#include "clang/Parse/Scope.h"
17using namespace clang;
18
19Parser::Parser(Preprocessor &pp, Action &actions)
20 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
21 Tok.setKind(tok::eof);
22 CurScope = 0;
23 NumCachedScopes = 0;
24 ParenCount = BracketCount = BraceCount = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000025 ObjcImpDecl = 0;
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +000026 AllImplMethods.clear();
Chris Lattner4b009652007-07-25 00:24:17 +000027}
28
29/// Out-of-line virtual destructor to provide home for Action class.
30Action::~Action() {}
31
32
33void Parser::Diag(SourceLocation Loc, unsigned DiagID,
34 const std::string &Msg) {
35 Diags.Report(Loc, DiagID, &Msg, 1);
36}
37
38/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
39/// this helper function matches and consumes the specified RHS token if
40/// present. If not present, it emits the specified diagnostic indicating
41/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
42/// should be the name of the unmatched LHS token.
43SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
44 SourceLocation LHSLoc) {
45
Chris Lattner17a5fb62007-10-09 17:23:58 +000046 if (Tok.is(RHSTok))
Chris Lattner4b009652007-07-25 00:24:17 +000047 return ConsumeAnyToken();
48
49 SourceLocation R = Tok.getLocation();
50 const char *LHSName = "unknown";
51 diag::kind DID = diag::err_parse_error;
52 switch (RHSTok) {
53 default: break;
54 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
55 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
56 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
57 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
58 }
59 Diag(Tok, DID);
60 Diag(LHSLoc, diag::err_matching, LHSName);
61 SkipUntil(RHSTok);
62 return R;
63}
64
65/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
66/// input. If so, it is consumed and false is returned.
67///
68/// If the input is malformed, this emits the specified diagnostic. Next, if
69/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
70/// returned.
71bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
72 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattner17a5fb62007-10-09 17:23:58 +000073 if (Tok.is(ExpectedTok)) {
Chris Lattner4b009652007-07-25 00:24:17 +000074 ConsumeAnyToken();
75 return false;
76 }
77
78 Diag(Tok, DiagID, Msg);
79 if (SkipToTok != tok::unknown)
80 SkipUntil(SkipToTok);
81 return true;
82}
83
84//===----------------------------------------------------------------------===//
85// Error recovery.
86//===----------------------------------------------------------------------===//
87
88/// SkipUntil - Read tokens until we get to the specified token, then consume
89/// it (unless DontConsume is true). Because we cannot guarantee that the
90/// token will ever occur, this skips to the next token, or to some likely
91/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
92/// character.
93///
94/// If SkipUntil finds the specified token, it returns true, otherwise it
95/// returns false.
96bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
97 bool StopAtSemi, bool DontConsume) {
98 // We always want this function to skip at least one token if the first token
99 // isn't T and if not at EOF.
100 bool isFirstTokenSkipped = true;
101 while (1) {
102 // If we found one of the tokens, stop and return true.
103 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner17a5fb62007-10-09 17:23:58 +0000104 if (Tok.is(Toks[i])) {
Chris Lattner4b009652007-07-25 00:24:17 +0000105 if (DontConsume) {
106 // Noop, don't consume the token.
107 } else {
108 ConsumeAnyToken();
109 }
110 return true;
111 }
112 }
113
114 switch (Tok.getKind()) {
115 case tok::eof:
116 // Ran out of tokens.
117 return false;
118
119 case tok::l_paren:
120 // Recursively skip properly-nested parens.
121 ConsumeParen();
122 SkipUntil(tok::r_paren, false);
123 break;
124 case tok::l_square:
125 // Recursively skip properly-nested square brackets.
126 ConsumeBracket();
127 SkipUntil(tok::r_square, false);
128 break;
129 case tok::l_brace:
130 // Recursively skip properly-nested braces.
131 ConsumeBrace();
132 SkipUntil(tok::r_brace, false);
133 break;
134
135 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
136 // Since the user wasn't looking for this token (if they were, it would
137 // already be handled), this isn't balanced. If there is a LHS token at a
138 // higher level, we will assume that this matches the unbalanced token
139 // and return it. Otherwise, this is a spurious RHS token, which we skip.
140 case tok::r_paren:
141 if (ParenCount && !isFirstTokenSkipped)
142 return false; // Matches something.
143 ConsumeParen();
144 break;
145 case tok::r_square:
146 if (BracketCount && !isFirstTokenSkipped)
147 return false; // Matches something.
148 ConsumeBracket();
149 break;
150 case tok::r_brace:
151 if (BraceCount && !isFirstTokenSkipped)
152 return false; // Matches something.
153 ConsumeBrace();
154 break;
155
156 case tok::string_literal:
157 case tok::wide_string_literal:
158 ConsumeStringToken();
159 break;
160 case tok::semi:
161 if (StopAtSemi)
162 return false;
163 // FALL THROUGH.
164 default:
165 // Skip this token.
166 ConsumeToken();
167 break;
168 }
169 isFirstTokenSkipped = false;
170 }
171}
172
173//===----------------------------------------------------------------------===//
174// Scope manipulation
175//===----------------------------------------------------------------------===//
176
177/// EnterScope - Start a new scope.
178void Parser::EnterScope(unsigned ScopeFlags) {
179 if (NumCachedScopes) {
180 Scope *N = ScopeCache[--NumCachedScopes];
181 N->Init(CurScope, ScopeFlags);
182 CurScope = N;
183 } else {
184 CurScope = new Scope(CurScope, ScopeFlags);
185 }
186}
187
188/// ExitScope - Pop a scope off the scope stack.
189void Parser::ExitScope() {
190 assert(CurScope && "Scope imbalance!");
191
Chris Lattner62231492007-10-09 20:37:18 +0000192 // Inform the actions module that this scope is going away if there are any
193 // decls in it.
194 if (!CurScope->decl_empty())
Steve Naroff9637a9b2007-10-09 22:01:59 +0000195 Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000196
197 Scope *OldScope = CurScope;
198 CurScope = OldScope->getParent();
199
200 if (NumCachedScopes == ScopeCacheSize)
201 delete OldScope;
202 else
203 ScopeCache[NumCachedScopes++] = OldScope;
204}
205
206
207
208
209//===----------------------------------------------------------------------===//
210// C99 6.9: External Definitions.
211//===----------------------------------------------------------------------===//
212
213Parser::~Parser() {
214 // If we still have scopes active, delete the scope tree.
215 delete CurScope;
216
217 // Free the scope cache.
218 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
219 delete ScopeCache[i];
220}
221
222/// Initialize - Warm up the parser.
223///
224void Parser::Initialize() {
225 // Prime the lexer look-ahead.
226 ConsumeToken();
227
Chris Lattnera7549902007-08-26 06:24:45 +0000228 // Create the translation unit scope. Install it as the current scope.
Chris Lattner4b009652007-07-25 00:24:17 +0000229 assert(CurScope == 0 && "A scope is already active?");
Chris Lattnera7549902007-08-26 06:24:45 +0000230 EnterScope(Scope::DeclScope);
Steve Naroff9637a9b2007-10-09 22:01:59 +0000231 Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
232
Chris Lattner4b009652007-07-25 00:24:17 +0000233 // Install builtin types.
234 // TODO: Move this someplace more useful.
235 {
236 const char *Dummy;
237
238 //__builtin_va_list
239 DeclSpec DS;
240 bool Error = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, SourceLocation(),
241 Dummy);
242
243 // TODO: add a 'TST_builtin' type?
244 Error |= DS.SetTypeSpecType(DeclSpec::TST_int, SourceLocation(), Dummy);
245 assert(!Error && "Error setting up __builtin_va_list!");
246
247 Declarator D(DS, Declarator::FileContext);
248 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Steve Naroff0acc9c92007-09-15 18:49:24 +0000249 Actions.ActOnDeclarator(CurScope, D, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000250 }
251
Chris Lattner17a5fb62007-10-09 17:23:58 +0000252 if (Tok.is(tok::eof) &&
Chris Lattner7bdc85d2007-08-25 05:47:03 +0000253 !getLang().CPlusPlus) // Empty source file is an extension in C
Chris Lattner4b009652007-07-25 00:24:17 +0000254 Diag(Tok, diag::ext_empty_source_file);
Chris Lattner32352462007-08-29 22:54:08 +0000255
256 // Initialization for Objective-C context sensitive keywords recognition.
257 // Referenced in Parser::isObjCTypeQualifier.
258 if (getLang().ObjC1) {
259 ObjcTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
260 ObjcTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
261 ObjcTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
262 ObjcTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
263 ObjcTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
264 ObjcTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
265 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000266 if (getLang().ObjC2) {
267 ObjcPropertyAttrs[objc_readonly] = &PP.getIdentifierTable().get("readonly");
268 ObjcPropertyAttrs[objc_getter] = &PP.getIdentifierTable().get("getter");
269 ObjcPropertyAttrs[objc_setter] = &PP.getIdentifierTable().get("setter");
270 ObjcPropertyAttrs[objc_assign] = &PP.getIdentifierTable().get("assign");
271 ObjcPropertyAttrs[objc_readwrite] =
272 &PP.getIdentifierTable().get("readwrite");
273 ObjcPropertyAttrs[objc_retain] = &PP.getIdentifierTable().get("retain");
274 ObjcPropertyAttrs[objc_copy] = &PP.getIdentifierTable().get("copy");
275 ObjcPropertyAttrs[objc_nonatomic] =
276 &PP.getIdentifierTable().get("nonatomic");
277 }
Chris Lattner4b009652007-07-25 00:24:17 +0000278}
279
280/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
281/// action tells us to. This returns true if the EOF was encountered.
282bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
283 Result = 0;
Chris Lattner17a5fb62007-10-09 17:23:58 +0000284 if (Tok.is(tok::eof)) return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000285
286 Result = ParseExternalDeclaration();
287 return false;
288}
289
290/// Finalize - Shut down the parser.
291///
292void Parser::Finalize() {
293 ExitScope();
294 assert(CurScope == 0 && "Scope imbalance!");
295}
296
297/// ParseTranslationUnit:
298/// translation-unit: [C99 6.9]
299/// external-declaration
300/// translation-unit external-declaration
301void Parser::ParseTranslationUnit() {
302 Initialize();
303
304 DeclTy *Res;
305 while (!ParseTopLevelDecl(Res))
306 /*parse them all*/;
307
308 Finalize();
309}
310
311/// ParseExternalDeclaration:
312/// external-declaration: [C99 6.9]
Chris Lattner06f4e752007-08-10 20:57:02 +0000313/// function-definition
314/// declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000315/// [EXT] ';'
316/// [GNU] asm-definition
Chris Lattner06f4e752007-08-10 20:57:02 +0000317/// [GNU] __extension__ external-declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000318/// [OBJC] objc-class-definition
319/// [OBJC] objc-class-declaration
320/// [OBJC] objc-alias-declaration
321/// [OBJC] objc-protocol-definition
322/// [OBJC] objc-method-definition
323/// [OBJC] @end
324///
325/// [GNU] asm-definition:
326/// simple-asm-expr ';'
327///
328Parser::DeclTy *Parser::ParseExternalDeclaration() {
329 switch (Tok.getKind()) {
330 case tok::semi:
331 Diag(Tok, diag::ext_top_level_semi);
332 ConsumeToken();
333 // TODO: Invoke action for top-level semicolon.
334 return 0;
Chris Lattner06f4e752007-08-10 20:57:02 +0000335 case tok::kw___extension__: {
336 ConsumeToken();
337 // FIXME: Disable extension warnings.
338 DeclTy *RV = ParseExternalDeclaration();
339 // FIXME: Restore extension warnings.
340 return RV;
341 }
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case tok::kw_asm:
343 ParseSimpleAsm();
344 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
345 "top-level asm block");
346 // TODO: Invoke action for top-level asm.
347 return 0;
348 case tok::at:
349 // @ is not a legal token unless objc is enabled, no need to check.
Steve Narofffaed3bf2007-09-10 20:51:04 +0000350 return ParseObjCAtDirectives();
Chris Lattner4b009652007-07-25 00:24:17 +0000351 case tok::minus:
352 if (getLang().ObjC1) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000353 ParseObjCInstanceMethodDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000354 } else {
355 Diag(Tok, diag::err_expected_external_declaration);
356 ConsumeToken();
357 }
358 return 0;
359 case tok::plus:
360 if (getLang().ObjC1) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000361 ParseObjCClassMethodDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000362 } else {
363 Diag(Tok, diag::err_expected_external_declaration);
364 ConsumeToken();
365 }
366 return 0;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000367 case tok::kw_namespace:
Chris Lattner4b009652007-07-25 00:24:17 +0000368 case tok::kw_typedef:
Chris Lattner9c135722007-08-25 18:15:16 +0000369 // A function definition cannot start with a these keywords.
Chris Lattner4b009652007-07-25 00:24:17 +0000370 return ParseDeclaration(Declarator::FileContext);
371 default:
372 // We can't tell whether this is a function-definition or declaration yet.
373 return ParseDeclarationOrFunctionDefinition();
374 }
375}
376
377/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
378/// a declaration. We can't tell which we have until we read up to the
379/// compound-statement in function-definition.
380///
381/// function-definition: [C99 6.9.1]
382/// declaration-specifiers[opt] declarator declaration-list[opt]
Chris Lattneraac973e2007-08-22 06:06:56 +0000383/// compound-statement
Chris Lattner4b009652007-07-25 00:24:17 +0000384/// declaration: [C99 6.7]
Chris Lattneraac973e2007-08-22 06:06:56 +0000385/// declaration-specifiers init-declarator-list[opt] ';'
386/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner4b009652007-07-25 00:24:17 +0000387/// [OMP] threadprivate-directive [TODO]
388///
389Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
390 // Parse the common declaration-specifiers piece.
391 DeclSpec DS;
392 ParseDeclarationSpecifiers(DS);
393
394 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
395 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner17a5fb62007-10-09 17:23:58 +0000396 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000397 ConsumeToken();
398 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
399 }
400
Steve Naroffa7f62782007-08-23 19:56:30 +0000401 // ObjC2 allows prefix attributes on class interfaces.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000402 if (getLang().ObjC2 && Tok.is(tok::at)) {
Steve Narofffb367882007-08-20 21:31:48 +0000403 SourceLocation AtLoc = ConsumeToken(); // the "@"
404 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_interface)
405 return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
406 }
407
Chris Lattner4b009652007-07-25 00:24:17 +0000408 // Parse the first declarator.
409 Declarator DeclaratorInfo(DS, Declarator::FileContext);
410 ParseDeclarator(DeclaratorInfo);
411 // Error parsing the declarator?
412 if (DeclaratorInfo.getIdentifier() == 0) {
413 // If so, skip until the semi-colon or a }.
414 SkipUntil(tok::r_brace, true);
Chris Lattner17a5fb62007-10-09 17:23:58 +0000415 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000416 ConsumeToken();
417 return 0;
418 }
419
420 // If the declarator is the start of a function definition, handle it.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000421 if (Tok.is(tok::equal) || // int X()= -> not a function def
422 Tok.is(tok::comma) || // int X(), -> not a function def
423 Tok.is(tok::semi) || // int X(); -> not a function def
424 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
425 Tok.is(tok::kw___attribute)) { // int X() __attr__ -> not a function def
Chris Lattner4b009652007-07-25 00:24:17 +0000426 // FALL THROUGH.
427 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattner17a5fb62007-10-09 17:23:58 +0000428 (Tok.is(tok::l_brace) || // int X() {}
Chris Lattner4b009652007-07-25 00:24:17 +0000429 isDeclarationSpecifier())) { // int X(f) int f; {}
430 return ParseFunctionDefinition(DeclaratorInfo);
431 } else {
432 if (DeclaratorInfo.isFunctionDeclarator())
433 Diag(Tok, diag::err_expected_fn_body);
434 else
435 Diag(Tok, diag::err_expected_after_declarator);
436 SkipUntil(tok::semi);
437 return 0;
438 }
439
440 // Parse the init-declarator-list for a normal declaration.
441 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
442}
443
444/// ParseFunctionDefinition - We parsed and verified that the specified
445/// Declarator is well formed. If this is a K&R-style function, read the
446/// parameters declaration-list, then start the compound-statement.
447///
448/// declaration-specifiers[opt] declarator declaration-list[opt]
449/// compound-statement [TODO]
450///
451Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
452 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
453 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
454 "This isn't a function declarator!");
455 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
456
457 // If this declaration was formed with a K&R-style identifier list for the
458 // arguments, parse declarations for all of the args next.
459 // int foo(a,b) int a; float b; {}
460 if (!FTI.hasPrototype && FTI.NumArgs != 0)
461 ParseKNRParamDeclarations(D);
462
Chris Lattner4b009652007-07-25 00:24:17 +0000463 // We should have an opening brace now.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000464 if (Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000465 Diag(Tok, diag::err_expected_fn_body);
466
467 // Skip over garbage, until we get to '{'. Don't eat the '{'.
468 SkipUntil(tok::l_brace, true, true);
469
470 // If we didn't find the '{', bail out.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000471 if (Tok.isNot(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000472 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000473 }
474
Chris Lattnerea148702007-10-09 17:14:05 +0000475 SourceLocation BraceLoc = Tok.getLocation();
476
477 // Enter a scope for the function body.
478 EnterScope(Scope::FnScope|Scope::DeclScope);
479
480 // Tell the actions module that we have entered a function definition with the
481 // specified Declarator for the function.
482 DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
483
484
Chris Lattner4b009652007-07-25 00:24:17 +0000485 // Do not enter a scope for the brace, as the arguments are in the same scope
486 // (the function body) as the body itself. Instead, just read the statement
487 // list and put it into a CompoundStmt for safe keeping.
488 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattnerea148702007-10-09 17:14:05 +0000489
490 // If the function body could not be parsed, make a bogus compoundstmt.
491 if (FnBody.isInvalid)
492 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000493
494 // Leave the function body scope.
495 ExitScope();
496
497 // TODO: Pass argument information.
Chris Lattnerea148702007-10-09 17:14:05 +0000498 return Actions.ActOnFunctionDefBody(Res, FnBody.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000499}
500
501/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
502/// types for a function with a K&R-style identifier list for arguments.
503void Parser::ParseKNRParamDeclarations(Declarator &D) {
504 // We know that the top-level of this declarator is a function.
505 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
506
507 // Read all the argument declarations.
508 while (isDeclarationSpecifier()) {
509 SourceLocation DSStart = Tok.getLocation();
510
511 // Parse the common declaration-specifiers piece.
512 DeclSpec DS;
513 ParseDeclarationSpecifiers(DS);
514
515 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
516 // least one declarator'.
517 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
518 // the declarations though. It's trivial to ignore them, really hard to do
519 // anything else with them.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000520 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000521 Diag(DSStart, diag::err_declaration_does_not_declare_param);
522 ConsumeToken();
523 continue;
524 }
525
526 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
527 // than register.
528 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
529 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
530 Diag(DS.getStorageClassSpecLoc(),
531 diag::err_invalid_storage_class_in_func_decl);
532 DS.ClearStorageClassSpecs();
533 }
534 if (DS.isThreadSpecified()) {
535 Diag(DS.getThreadSpecLoc(),
536 diag::err_invalid_storage_class_in_func_decl);
537 DS.ClearStorageClassSpecs();
538 }
539
540 // Parse the first declarator attached to this declspec.
541 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
542 ParseDeclarator(ParmDeclarator);
543
544 // Handle the full declarator list.
545 while (1) {
546 DeclTy *AttrList;
547 // If attributes are present, parse them.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000548 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000549 // FIXME: attach attributes too.
550 AttrList = ParseAttributes();
551
552 // Ask the actions module to compute the type for this declarator.
553 Action::TypeResult TR =
Steve Naroff0acc9c92007-09-15 18:49:24 +0000554 Actions.ActOnParamDeclaratorType(CurScope, ParmDeclarator);
Steve Narofffaed3bf2007-09-10 20:51:04 +0000555
Chris Lattner4b009652007-07-25 00:24:17 +0000556 if (!TR.isInvalid &&
557 // A missing identifier has already been diagnosed.
558 ParmDeclarator.getIdentifier()) {
559
560 // Scan the argument list looking for the correct param to apply this
561 // type.
562 for (unsigned i = 0; ; ++i) {
563 // C99 6.9.1p6: those declarators shall declare only identifiers from
564 // the identifier list.
565 if (i == FTI.NumArgs) {
566 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
567 ParmDeclarator.getIdentifier()->getName());
568 break;
569 }
570
571 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
572 // Reject redefinitions of parameters.
573 if (FTI.ArgInfo[i].TypeInfo) {
574 Diag(ParmDeclarator.getIdentifierLoc(),
575 diag::err_param_redefinition,
576 ParmDeclarator.getIdentifier()->getName());
577 } else {
578 FTI.ArgInfo[i].TypeInfo = TR.Val;
579 }
580 break;
581 }
582 }
583 }
584
585 // If we don't have a comma, it is either the end of the list (a ';') or
586 // an error, bail out.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000587 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000588 break;
589
590 // Consume the comma.
591 ConsumeToken();
592
593 // Parse the next declarator.
594 ParmDeclarator.clear();
595 ParseDeclarator(ParmDeclarator);
596 }
597
Chris Lattner17a5fb62007-10-09 17:23:58 +0000598 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000599 ConsumeToken();
600 } else {
601 Diag(Tok, diag::err_parse_error);
602 // Skip to end of block or statement
603 SkipUntil(tok::semi, true);
Chris Lattner17a5fb62007-10-09 17:23:58 +0000604 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000605 ConsumeToken();
606 }
607 }
608
609 // The actions module must verify that all arguments were declared.
610}
611
612
613/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
614/// allowed to be a wide string, and is not subject to character translation.
615///
616/// [GNU] asm-string-literal:
617/// string-literal
618///
619void Parser::ParseAsmStringLiteral() {
620 if (!isTokenStringLiteral()) {
621 Diag(Tok, diag::err_expected_string_literal);
622 return;
623 }
624
625 ExprResult Res = ParseStringLiteralExpression();
626 if (Res.isInvalid) return;
627
628 // TODO: Diagnose: wide string literal in 'asm'
629}
630
631/// ParseSimpleAsm
632///
633/// [GNU] simple-asm-expr:
634/// 'asm' '(' asm-string-literal ')'
635///
636void Parser::ParseSimpleAsm() {
Chris Lattner17a5fb62007-10-09 17:23:58 +0000637 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Chris Lattner4b009652007-07-25 00:24:17 +0000638 ConsumeToken();
639
Chris Lattner17a5fb62007-10-09 17:23:58 +0000640 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000641 Diag(Tok, diag::err_expected_lparen_after, "asm");
642 return;
643 }
644
645 SourceLocation Loc = ConsumeParen();
646
647 ParseAsmStringLiteral();
648
649 MatchRHSPunctuation(tok::r_paren, Loc);
650}
651