blob: 255b48a7ca62a35b64c332fe78538dc9b0121a85 [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;
25}
26
27/// Out-of-line virtual destructor to provide home for Action class.
28Action::~Action() {}
29
30
31void Parser::Diag(SourceLocation Loc, unsigned DiagID,
32 const std::string &Msg) {
33 Diags.Report(Loc, DiagID, &Msg, 1);
34}
35
36/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
37/// this helper function matches and consumes the specified RHS token if
38/// present. If not present, it emits the specified diagnostic indicating
39/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
40/// should be the name of the unmatched LHS token.
41SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
42 SourceLocation LHSLoc) {
43
44 if (Tok.getKind() == RHSTok)
45 return ConsumeAnyToken();
46
47 SourceLocation R = Tok.getLocation();
48 const char *LHSName = "unknown";
49 diag::kind DID = diag::err_parse_error;
50 switch (RHSTok) {
51 default: break;
52 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
53 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
54 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
55 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
56 }
57 Diag(Tok, DID);
58 Diag(LHSLoc, diag::err_matching, LHSName);
59 SkipUntil(RHSTok);
60 return R;
61}
62
63/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
64/// input. If so, it is consumed and false is returned.
65///
66/// If the input is malformed, this emits the specified diagnostic. Next, if
67/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
68/// returned.
69bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
70 const char *Msg, tok::TokenKind SkipToTok) {
71 if (Tok.getKind() == ExpectedTok) {
72 ConsumeAnyToken();
73 return false;
74 }
75
76 Diag(Tok, DiagID, Msg);
77 if (SkipToTok != tok::unknown)
78 SkipUntil(SkipToTok);
79 return true;
80}
81
82//===----------------------------------------------------------------------===//
83// Error recovery.
84//===----------------------------------------------------------------------===//
85
86/// SkipUntil - Read tokens until we get to the specified token, then consume
87/// it (unless DontConsume is true). Because we cannot guarantee that the
88/// token will ever occur, this skips to the next token, or to some likely
89/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
90/// character.
91///
92/// If SkipUntil finds the specified token, it returns true, otherwise it
93/// returns false.
94bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
95 bool StopAtSemi, bool DontConsume) {
96 // 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;
99 while (1) {
100 // If we found one of the tokens, stop and return true.
101 for (unsigned i = 0; i != NumToks; ++i) {
102 if (Tok.getKind() == Toks[i]) {
103 if (DontConsume) {
104 // Noop, don't consume the token.
105 } else {
106 ConsumeAnyToken();
107 }
108 return true;
109 }
110 }
111
112 switch (Tok.getKind()) {
113 case tok::eof:
114 // Ran out of tokens.
115 return false;
116
117 case tok::l_paren:
118 // Recursively skip properly-nested parens.
119 ConsumeParen();
120 SkipUntil(tok::r_paren, false);
121 break;
122 case tok::l_square:
123 // Recursively skip properly-nested square brackets.
124 ConsumeBracket();
125 SkipUntil(tok::r_square, false);
126 break;
127 case tok::l_brace:
128 // Recursively skip properly-nested braces.
129 ConsumeBrace();
130 SkipUntil(tok::r_brace, false);
131 break;
132
133 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
134 // Since the user wasn't looking for this token (if they were, it would
135 // already be handled), this isn't balanced. If there is a LHS token at a
136 // higher level, we will assume that this matches the unbalanced token
137 // and return it. Otherwise, this is a spurious RHS token, which we skip.
138 case tok::r_paren:
139 if (ParenCount && !isFirstTokenSkipped)
140 return false; // Matches something.
141 ConsumeParen();
142 break;
143 case tok::r_square:
144 if (BracketCount && !isFirstTokenSkipped)
145 return false; // Matches something.
146 ConsumeBracket();
147 break;
148 case tok::r_brace:
149 if (BraceCount && !isFirstTokenSkipped)
150 return false; // Matches something.
151 ConsumeBrace();
152 break;
153
154 case tok::string_literal:
155 case tok::wide_string_literal:
156 ConsumeStringToken();
157 break;
158 case tok::semi:
159 if (StopAtSemi)
160 return false;
161 // FALL THROUGH.
162 default:
163 // Skip this token.
164 ConsumeToken();
165 break;
166 }
167 isFirstTokenSkipped = false;
168 }
169}
170
171//===----------------------------------------------------------------------===//
172// Scope manipulation
173//===----------------------------------------------------------------------===//
174
175/// EnterScope - Start a new scope.
176void Parser::EnterScope(unsigned ScopeFlags) {
177 if (NumCachedScopes) {
178 Scope *N = ScopeCache[--NumCachedScopes];
179 N->Init(CurScope, ScopeFlags);
180 CurScope = N;
181 } else {
182 CurScope = new Scope(CurScope, ScopeFlags);
183 }
184}
185
186/// ExitScope - Pop a scope off the scope stack.
187void Parser::ExitScope() {
188 assert(CurScope && "Scope imbalance!");
189
190 // Inform the actions module that this scope is going away.
191 Actions.PopScope(Tok.getLocation(), CurScope);
192
193 Scope *OldScope = CurScope;
194 CurScope = OldScope->getParent();
195
196 if (NumCachedScopes == ScopeCacheSize)
197 delete OldScope;
198 else
199 ScopeCache[NumCachedScopes++] = OldScope;
200}
201
202
203
204
205//===----------------------------------------------------------------------===//
206// C99 6.9: External Definitions.
207//===----------------------------------------------------------------------===//
208
209Parser::~Parser() {
210 // If we still have scopes active, delete the scope tree.
211 delete CurScope;
212
213 // Free the scope cache.
214 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
215 delete ScopeCache[i];
216}
217
218/// Initialize - Warm up the parser.
219///
220void Parser::Initialize() {
221 // Prime the lexer look-ahead.
222 ConsumeToken();
223
224 // Create the global scope, install it as the current scope.
225 assert(CurScope == 0 && "A scope is already active?");
226 EnterScope(0);
227
228
229 // Install builtin types.
230 // TODO: Move this someplace more useful.
231 {
232 const char *Dummy;
233
234 //__builtin_va_list
235 DeclSpec DS;
236 bool Error = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, SourceLocation(),
237 Dummy);
238
239 // TODO: add a 'TST_builtin' type?
240 Error |= DS.SetTypeSpecType(DeclSpec::TST_int, SourceLocation(), Dummy);
241 assert(!Error && "Error setting up __builtin_va_list!");
242
243 Declarator D(DS, Declarator::FileContext);
244 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
245 Actions.ParseDeclarator(CurScope, D, 0, 0);
246 }
247
Chris Lattner7bdc85d2007-08-25 05:47:03 +0000248 if (Tok.getKind() == tok::eof &&
249 !getLang().CPlusPlus) // Empty source file is an extension in C
Chris Lattner4b009652007-07-25 00:24:17 +0000250 Diag(Tok, diag::ext_empty_source_file);
251}
252
253/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
254/// action tells us to. This returns true if the EOF was encountered.
255bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
256 Result = 0;
257 if (Tok.getKind() == tok::eof) return true;
258
259 Result = ParseExternalDeclaration();
260 return false;
261}
262
263/// Finalize - Shut down the parser.
264///
265void Parser::Finalize() {
266 ExitScope();
267 assert(CurScope == 0 && "Scope imbalance!");
268}
269
270/// ParseTranslationUnit:
271/// translation-unit: [C99 6.9]
272/// external-declaration
273/// translation-unit external-declaration
274void Parser::ParseTranslationUnit() {
275 Initialize();
276
277 DeclTy *Res;
278 while (!ParseTopLevelDecl(Res))
279 /*parse them all*/;
280
281 Finalize();
282}
283
284/// ParseExternalDeclaration:
285/// external-declaration: [C99 6.9]
Chris Lattner06f4e752007-08-10 20:57:02 +0000286/// function-definition
287/// declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000288/// [EXT] ';'
289/// [GNU] asm-definition
Chris Lattner06f4e752007-08-10 20:57:02 +0000290/// [GNU] __extension__ external-declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000291/// [OBJC] objc-class-definition
292/// [OBJC] objc-class-declaration
293/// [OBJC] objc-alias-declaration
294/// [OBJC] objc-protocol-definition
295/// [OBJC] objc-method-definition
296/// [OBJC] @end
297///
298/// [GNU] asm-definition:
299/// simple-asm-expr ';'
300///
301Parser::DeclTy *Parser::ParseExternalDeclaration() {
302 switch (Tok.getKind()) {
303 case tok::semi:
304 Diag(Tok, diag::ext_top_level_semi);
305 ConsumeToken();
306 // TODO: Invoke action for top-level semicolon.
307 return 0;
Chris Lattner06f4e752007-08-10 20:57:02 +0000308 case tok::kw___extension__: {
309 ConsumeToken();
310 // FIXME: Disable extension warnings.
311 DeclTy *RV = ParseExternalDeclaration();
312 // FIXME: Restore extension warnings.
313 return RV;
314 }
Chris Lattner4b009652007-07-25 00:24:17 +0000315 case tok::kw_asm:
316 ParseSimpleAsm();
317 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
318 "top-level asm block");
319 // TODO: Invoke action for top-level asm.
320 return 0;
321 case tok::at:
322 // @ is not a legal token unless objc is enabled, no need to check.
323 ParseObjCAtDirectives();
324 return 0;
325 case tok::minus:
326 if (getLang().ObjC1) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000327 ParseObjCInstanceMethodDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000328 } else {
329 Diag(Tok, diag::err_expected_external_declaration);
330 ConsumeToken();
331 }
332 return 0;
333 case tok::plus:
334 if (getLang().ObjC1) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000335 ParseObjCClassMethodDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000336 } else {
337 Diag(Tok, diag::err_expected_external_declaration);
338 ConsumeToken();
339 }
340 return 0;
341 case tok::kw_typedef:
342 // A function definition cannot start with a 'typedef' keyword.
343 return ParseDeclaration(Declarator::FileContext);
344 default:
345 // We can't tell whether this is a function-definition or declaration yet.
346 return ParseDeclarationOrFunctionDefinition();
347 }
348}
349
350/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
351/// a declaration. We can't tell which we have until we read up to the
352/// compound-statement in function-definition.
353///
354/// function-definition: [C99 6.9.1]
355/// declaration-specifiers[opt] declarator declaration-list[opt]
Chris Lattneraac973e2007-08-22 06:06:56 +0000356/// compound-statement
Chris Lattner4b009652007-07-25 00:24:17 +0000357/// declaration: [C99 6.7]
Chris Lattneraac973e2007-08-22 06:06:56 +0000358/// declaration-specifiers init-declarator-list[opt] ';'
359/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner4b009652007-07-25 00:24:17 +0000360/// [OMP] threadprivate-directive [TODO]
361///
362Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
363 // Parse the common declaration-specifiers piece.
364 DeclSpec DS;
365 ParseDeclarationSpecifiers(DS);
366
367 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
368 // declaration-specifiers init-declarator-list[opt] ';'
369 if (Tok.getKind() == tok::semi) {
370 ConsumeToken();
371 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
372 }
373
Steve Naroffa7f62782007-08-23 19:56:30 +0000374 // ObjC2 allows prefix attributes on class interfaces.
375 if (getLang().ObjC2 && Tok.getKind() == tok::at) {
Steve Narofffb367882007-08-20 21:31:48 +0000376 SourceLocation AtLoc = ConsumeToken(); // the "@"
377 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_interface)
378 return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
379 }
380
Chris Lattner4b009652007-07-25 00:24:17 +0000381 // Parse the first declarator.
382 Declarator DeclaratorInfo(DS, Declarator::FileContext);
383 ParseDeclarator(DeclaratorInfo);
384 // Error parsing the declarator?
385 if (DeclaratorInfo.getIdentifier() == 0) {
386 // If so, skip until the semi-colon or a }.
387 SkipUntil(tok::r_brace, true);
388 if (Tok.getKind() == tok::semi)
389 ConsumeToken();
390 return 0;
391 }
392
393 // If the declarator is the start of a function definition, handle it.
394 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
395 Tok.getKind() == tok::comma || // int X(), -> not a function def
396 Tok.getKind() == tok::semi || // int X(); -> not a function def
397 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
398 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
399 // FALL THROUGH.
400 } else if (DeclaratorInfo.isFunctionDeclarator() &&
401 (Tok.getKind() == tok::l_brace || // int X() {}
402 isDeclarationSpecifier())) { // int X(f) int f; {}
403 return ParseFunctionDefinition(DeclaratorInfo);
404 } else {
405 if (DeclaratorInfo.isFunctionDeclarator())
406 Diag(Tok, diag::err_expected_fn_body);
407 else
408 Diag(Tok, diag::err_expected_after_declarator);
409 SkipUntil(tok::semi);
410 return 0;
411 }
412
413 // Parse the init-declarator-list for a normal declaration.
414 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
415}
416
417/// ParseFunctionDefinition - We parsed and verified that the specified
418/// Declarator is well formed. If this is a K&R-style function, read the
419/// parameters declaration-list, then start the compound-statement.
420///
421/// declaration-specifiers[opt] declarator declaration-list[opt]
422/// compound-statement [TODO]
423///
424Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
425 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
426 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
427 "This isn't a function declarator!");
428 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
429
430 // If this declaration was formed with a K&R-style identifier list for the
431 // arguments, parse declarations for all of the args next.
432 // int foo(a,b) int a; float b; {}
433 if (!FTI.hasPrototype && FTI.NumArgs != 0)
434 ParseKNRParamDeclarations(D);
435
436 // Enter a scope for the function body.
437 EnterScope(Scope::FnScope);
438
439 // Tell the actions module that we have entered a function definition with the
440 // specified Declarator for the function.
441 DeclTy *Res = Actions.ParseStartOfFunctionDef(CurScope, D);
442
443
444 // We should have an opening brace now.
445 if (Tok.getKind() != tok::l_brace) {
446 Diag(Tok, diag::err_expected_fn_body);
447
448 // Skip over garbage, until we get to '{'. Don't eat the '{'.
449 SkipUntil(tok::l_brace, true, true);
450
451 // If we didn't find the '{', bail out.
452 if (Tok.getKind() != tok::l_brace) {
453 ExitScope();
454 return 0;
455 }
456 }
457
458 // Do not enter a scope for the brace, as the arguments are in the same scope
459 // (the function body) as the body itself. Instead, just read the statement
460 // list and put it into a CompoundStmt for safe keeping.
461 StmtResult FnBody = ParseCompoundStatementBody();
462 if (FnBody.isInvalid) {
463 ExitScope();
464 return 0;
465 }
466
467 // Leave the function body scope.
468 ExitScope();
469
470 // TODO: Pass argument information.
471 return Actions.ParseFunctionDefBody(Res, FnBody.Val);
472}
473
474/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
475/// types for a function with a K&R-style identifier list for arguments.
476void Parser::ParseKNRParamDeclarations(Declarator &D) {
477 // We know that the top-level of this declarator is a function.
478 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
479
480 // Read all the argument declarations.
481 while (isDeclarationSpecifier()) {
482 SourceLocation DSStart = Tok.getLocation();
483
484 // Parse the common declaration-specifiers piece.
485 DeclSpec DS;
486 ParseDeclarationSpecifiers(DS);
487
488 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
489 // least one declarator'.
490 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
491 // the declarations though. It's trivial to ignore them, really hard to do
492 // anything else with them.
493 if (Tok.getKind() == tok::semi) {
494 Diag(DSStart, diag::err_declaration_does_not_declare_param);
495 ConsumeToken();
496 continue;
497 }
498
499 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
500 // than register.
501 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
502 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
503 Diag(DS.getStorageClassSpecLoc(),
504 diag::err_invalid_storage_class_in_func_decl);
505 DS.ClearStorageClassSpecs();
506 }
507 if (DS.isThreadSpecified()) {
508 Diag(DS.getThreadSpecLoc(),
509 diag::err_invalid_storage_class_in_func_decl);
510 DS.ClearStorageClassSpecs();
511 }
512
513 // Parse the first declarator attached to this declspec.
514 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
515 ParseDeclarator(ParmDeclarator);
516
517 // Handle the full declarator list.
518 while (1) {
519 DeclTy *AttrList;
520 // If attributes are present, parse them.
521 if (Tok.getKind() == tok::kw___attribute)
522 // FIXME: attach attributes too.
523 AttrList = ParseAttributes();
524
525 // Ask the actions module to compute the type for this declarator.
526 Action::TypeResult TR =
527 Actions.ParseParamDeclaratorType(CurScope, ParmDeclarator);
528 if (!TR.isInvalid &&
529 // A missing identifier has already been diagnosed.
530 ParmDeclarator.getIdentifier()) {
531
532 // Scan the argument list looking for the correct param to apply this
533 // type.
534 for (unsigned i = 0; ; ++i) {
535 // C99 6.9.1p6: those declarators shall declare only identifiers from
536 // the identifier list.
537 if (i == FTI.NumArgs) {
538 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
539 ParmDeclarator.getIdentifier()->getName());
540 break;
541 }
542
543 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
544 // Reject redefinitions of parameters.
545 if (FTI.ArgInfo[i].TypeInfo) {
546 Diag(ParmDeclarator.getIdentifierLoc(),
547 diag::err_param_redefinition,
548 ParmDeclarator.getIdentifier()->getName());
549 } else {
550 FTI.ArgInfo[i].TypeInfo = TR.Val;
551 }
552 break;
553 }
554 }
555 }
556
557 // If we don't have a comma, it is either the end of the list (a ';') or
558 // an error, bail out.
559 if (Tok.getKind() != tok::comma)
560 break;
561
562 // Consume the comma.
563 ConsumeToken();
564
565 // Parse the next declarator.
566 ParmDeclarator.clear();
567 ParseDeclarator(ParmDeclarator);
568 }
569
570 if (Tok.getKind() == tok::semi) {
571 ConsumeToken();
572 } else {
573 Diag(Tok, diag::err_parse_error);
574 // Skip to end of block or statement
575 SkipUntil(tok::semi, true);
576 if (Tok.getKind() == tok::semi)
577 ConsumeToken();
578 }
579 }
580
581 // The actions module must verify that all arguments were declared.
582}
583
584
585/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
586/// allowed to be a wide string, and is not subject to character translation.
587///
588/// [GNU] asm-string-literal:
589/// string-literal
590///
591void Parser::ParseAsmStringLiteral() {
592 if (!isTokenStringLiteral()) {
593 Diag(Tok, diag::err_expected_string_literal);
594 return;
595 }
596
597 ExprResult Res = ParseStringLiteralExpression();
598 if (Res.isInvalid) return;
599
600 // TODO: Diagnose: wide string literal in 'asm'
601}
602
603/// ParseSimpleAsm
604///
605/// [GNU] simple-asm-expr:
606/// 'asm' '(' asm-string-literal ')'
607///
608void Parser::ParseSimpleAsm() {
609 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
610 ConsumeToken();
611
612 if (Tok.getKind() != tok::l_paren) {
613 Diag(Tok, diag::err_expected_lparen_after, "asm");
614 return;
615 }
616
617 SourceLocation Loc = ConsumeParen();
618
619 ParseAsmStringLiteral();
620
621 MatchRHSPunctuation(tok::r_paren, Loc);
622}
623