blob: fa7710a5cd605ccc05e0f34d819c47dce5e11f6c [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 Lattnerb9093cd2006-08-04 04:39:53 +000015#include "clang/Parse/Declarations.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 Lattner685ed1e2006-08-14 00:22:04 +000020Parser::Parser(Preprocessor &pp, Action &actions)
Chris Lattner971c6b62006-08-05 22:46:42 +000021 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
Chris Lattneracd58a32006-08-06 17:24:14 +000022 Tok.SetKind(tok::eof);
Chris Lattnere4e38592006-08-14 00:15:05 +000023 CurScope = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000024
25 ParenCount = BracketCount = BraceCount = 0;
Chris Lattner971c6b62006-08-05 22:46:42 +000026}
27
28Parser::~Parser() {
Chris Lattnere4e38592006-08-14 00:15:05 +000029 // If we still have scopes active, delete the scope tree.
Chris Lattner971c6b62006-08-05 22:46:42 +000030 delete CurScope;
31}
32
Chris Lattner685ed1e2006-08-14 00:22:04 +000033/// Out-of-line virtual destructor to provide home for Action class.
34Action::~Action() {}
Chris Lattnere4e38592006-08-14 00:15:05 +000035
Chris Lattner0bb5f832006-07-31 01:59:18 +000036
Chris Lattnerb9093cd2006-08-04 04:39:53 +000037void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000038 const std::string &Msg) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000039 Diags.Report(Loc, DiagID, Msg);
Chris Lattner0bb5f832006-07-31 01:59:18 +000040}
41
Chris Lattner4564bc12006-08-10 23:14:52 +000042/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
43/// this helper function matches and consumes the specified RHS token if
44/// present. If not present, it emits the specified diagnostic indicating
45/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
46/// should be the name of the unmatched LHS token.
47void Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc,
48 const char *LHSName, unsigned DiagID) {
49
50 if (Tok.getKind() == RHSTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000051 ConsumeAnyToken();
Chris Lattner4564bc12006-08-10 23:14:52 +000052 } else {
53 Diag(Tok, DiagID);
54 Diag(LHSLoc, diag::err_matching, LHSName);
55 SkipUntil(RHSTok);
56 }
57}
58
Chris Lattnerdbb2a462006-08-12 19:26:13 +000059/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
60/// input. If so, it is consumed and false is returned.
61///
62/// If the input is malformed, this emits the specified diagnostic. Next, if
63/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
64/// returned.
65bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000066 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000067 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000068 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000069 return false;
70 }
71
Chris Lattner6d7e6342006-08-15 03:41:14 +000072 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000073 if (SkipToTok != tok::unknown)
74 SkipUntil(SkipToTok);
75 return true;
76}
77
Chris Lattner70f32b72006-07-31 05:09:04 +000078//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000079// Error recovery.
80//===----------------------------------------------------------------------===//
81
82/// SkipUntil - Read tokens until we get to the specified token, then consume
83/// it (unless DontConsume is false). Because we cannot guarantee that the
84/// token will ever occur, this skips to the next token, or to some likely
85/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
86/// character.
87///
88/// If SkipUntil finds the specified token, it returns true, otherwise it
89/// returns false.
90bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000091 // We always want this function to skip at least one token if the first token
92 // isn't T and if not at EOF.
93 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +000094 while (1) {
95 // If we found the token, stop and return true.
96 if (Tok.getKind() == T) {
97 if (DontConsume) {
98 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +000099 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000100 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000101 }
102 return true;
103 }
104
105 switch (Tok.getKind()) {
106 case tok::eof:
107 // Ran out of tokens.
108 return false;
109
110 case tok::l_paren:
111 // Recursively skip properly-nested parens.
112 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000113 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000114 break;
115 case tok::l_square:
116 // Recursively skip properly-nested square brackets.
117 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000118 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000119 break;
120 case tok::l_brace:
121 // Recursively skip properly-nested braces.
122 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000123 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000124 break;
125
126 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
127 // Since the user wasn't looking for this token (if they were, it would
128 // already be handled), this isn't balanced. If there is a LHS token at a
129 // higher level, we will assume that this matches the unbalanced token
130 // and return it. Otherwise, this is a spurious RHS token, which we skip.
131 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000132 if (ParenCount && !isFirstTokenSkipped)
133 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000134 ConsumeParen();
135 break;
136 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000137 if (BracketCount && !isFirstTokenSkipped)
138 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000139 ConsumeBracket();
140 break;
141 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000142 if (BraceCount && !isFirstTokenSkipped)
143 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000144 ConsumeBrace();
145 break;
146
147 case tok::string_literal:
148 ConsumeStringToken();
149 break;
150 case tok::semi:
151 if (StopAtSemi)
152 return false;
153 // FALL THROUGH.
154 default:
155 // Skip this token.
156 ConsumeToken();
157 break;
158 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000159 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000160 }
161}
162
163//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000164// Scope manipulation
165//===----------------------------------------------------------------------===//
166
167/// EnterScope - Start a new scope.
168void Parser::EnterScope() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000169 CurScope = new Scope(CurScope);
170}
171
172/// ExitScope - Pop a scope off the scope stack.
173void Parser::ExitScope() {
174 assert(CurScope && "Scope imbalance!");
175
176 // Inform the actions module that this scope is going away.
177 Actions.PopScope(Tok.getLocation(), CurScope);
178
179 Scope *Old = CurScope;
180 CurScope = Old->getParent();
181 delete Old;
182}
183
184
185
186
187//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000188// C99 6.9: External Definitions.
189//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000190
191/// ParseTranslationUnit:
Chris Lattner70f32b72006-07-31 05:09:04 +0000192/// translation-unit: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000193/// external-declaration
194/// translation-unit external-declaration
195void Parser::ParseTranslationUnit() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000196 // Prime the lexer look-ahead.
197 ConsumeToken();
198
199 // Create the global scope, install it as the current scope.
200 assert(CurScope == 0 && "A scope is already active?");
201 EnterScope();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000202
Chris Lattner6d7e6342006-08-15 03:41:14 +0000203
204 // Install builtin types.
205 // TODO: Move this someplace more useful.
206 {
207 //__builtin_va_list
208 DeclSpec DS;
209 DS.StorageClassSpec = DeclSpec::SCS_typedef;
210
211 // TODO: add a 'TST_builtin' type?
212 DS.TypeSpecType = DeclSpec::TST_typedef;
213
214 Declarator D(DS, Declarator::FileContext);
215 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
216 Actions.ParseDeclarator(SourceLocation(), CurScope, D, 0);
217 }
218
219
Chris Lattner0bb5f832006-07-31 01:59:18 +0000220 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
221 Diag(diag::ext_empty_source_file);
222
223 while (Tok.getKind() != tok::eof)
224 ParseExternalDeclaration();
Chris Lattnere4e38592006-08-14 00:15:05 +0000225
226 ExitScope();
227 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000228}
229
230/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000231/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000232/// function-definition [TODO]
233/// declaration [TODO]
234/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000235/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000236/// [GNU] __extension__ external-declaration [TODO]
237/// [OBJC] objc-class-definition [TODO]
238/// [OBJC] objc-class-declaration [TODO]
239/// [OBJC] objc-alias-declaration [TODO]
240/// [OBJC] objc-protocol-definition [TODO]
241/// [OBJC] objc-method-definition [TODO]
242/// [OBJC] @end [TODO]
243///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000244/// [GNU] asm-definition:
245/// simple-asm-expr ';'
246///
Chris Lattner0bb5f832006-07-31 01:59:18 +0000247void Parser::ParseExternalDeclaration() {
248 switch (Tok.getKind()) {
249 case tok::semi:
250 Diag(diag::ext_top_level_semi);
251 ConsumeToken();
252 break;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000253 case tok::kw_asm:
254 ParseSimpleAsm();
255 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
256 "top-level asm block");
257 break;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000258 default:
259 // We can't tell whether this is a function-definition or declaration yet.
260 ParseDeclarationOrFunctionDefinition();
261 break;
262 }
263}
264
265/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000266/// a declaration. We can't tell which we have until we read up to the
267/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000268///
Chris Lattner70f32b72006-07-31 05:09:04 +0000269/// function-definition: [C99 6.9.1]
270/// declaration-specifiers[opt] declarator declaration-list[opt]
271/// compound-statement [TODO]
272/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000273/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000274/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000275/// [OMP] threadprivate-directive [TODO]
276///
Chris Lattner0bb5f832006-07-31 01:59:18 +0000277void Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000278 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000279 DeclSpec DS;
280 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000281
282 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000283 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000284 if (Tok.getKind() == tok::semi) {
285 // TODO: emit error on 'int;' or 'const enum foo;'.
286 // if (!DS.isMissingDeclaratorOk()) Diag(...);
287
288 ConsumeToken();
289 return;
290 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000291
Chris Lattnerfff824f2006-08-07 06:31:38 +0000292 // Parse the first declarator.
293 Declarator DeclaratorInfo(DS, Declarator::FileContext);
294 ParseDeclarator(DeclaratorInfo);
295 // Error parsing the declarator?
296 if (DeclaratorInfo.getIdentifier() == 0) {
297 // If so, skip until the semi-colon or a }.
298 SkipUntil(tok::r_brace, true);
299 if (Tok.getKind() == tok::semi)
300 ConsumeToken();
301 return;
302 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000303
Chris Lattnerfff824f2006-08-07 06:31:38 +0000304 // If the declarator is the start of a function definition, handle it.
305 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
306 Tok.getKind() == tok::comma || // int X(), -> not a function def
307 Tok.getKind() == tok::semi || // int X(); -> not a function def
308 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
309 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
310 // FALL THROUGH.
311 } else if (DeclaratorInfo.isInnermostFunctionType() &&
312 (Tok.getKind() == tok::l_brace || // int X() {}
313 isDeclarationSpecifier())) { // int X(f) int f; {}
314 ParseFunctionDefinition(DeclaratorInfo);
315 return;
316 } else {
317 if (DeclaratorInfo.isInnermostFunctionType())
318 Diag(Tok, diag::err_expected_fn_body);
319 else
320 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000321 SkipUntil(tok::semi);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000322 return;
323 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000324
Chris Lattner53361ac2006-08-10 05:19:57 +0000325 // Parse the init-declarator-list for a normal declaration.
326 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000327}
328
Chris Lattnerfff824f2006-08-07 06:31:38 +0000329/// ParseFunctionDefinition - We parsed and verified that the specified
330/// Declarator is well formed. If this is a K&R-style function, read the
331/// parameters declaration-list, then start the compound-statement.
332///
333/// declaration-specifiers[opt] declarator declaration-list[opt]
334/// compound-statement [TODO]
335///
336void Parser::ParseFunctionDefinition(Declarator &D) {
337 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
338 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
339 "This isn't a function declarator!");
340
341 // If this declaration was formed with a K&R-style identifier list for the
342 // arguments, parse declarations for all of the args next.
343 // int foo(a,b) int a; float b; {}
344 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
345 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000346 while (isDeclarationSpecifier())
347 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000348
349 // Note, check that we got them all.
350 } else {
351 //if (isDeclarationSpecifier())
352 // Diag('k&r declspecs with prototype?');
353
Chris Lattner8693a512006-08-13 21:54:02 +0000354 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000355 }
356
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000357 // We should have an opening brace now.
358 if (Tok.getKind() != tok::l_brace) {
359 Diag(Tok, diag::err_expected_fn_body);
360
361 // Skip over garbage, until we get to '{'. Don't eat the '{'.
362 SkipUntil(tok::l_brace, true, true);
363
364 // If we didn't find the '{', bail out.
365 if (Tok.getKind() != tok::l_brace)
366 return;
367 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000368
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000369 ParseCompoundStatement();
Chris Lattnerfff824f2006-08-07 06:31:38 +0000370}
371
Chris Lattner6d7e6342006-08-15 03:41:14 +0000372/// ParseSimpleAsm
373///
374/// [GNU] simple-asm-expr:
375/// 'asm' '(' asm-string-literal ')'
376/// [GNU] asm-string-literal:
377/// string-literal
378///
379void Parser::ParseSimpleAsm() {
380 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
381 ConsumeToken();
382
383 if (Tok.getKind() != tok::l_paren) {
384 Diag(Tok, diag::err_expected_lparen_after, "asm");
385 return;
386 }
387
388 SourceLocation Loc = Tok.getLocation();
389 ConsumeParen();
390
391 if (Tok.getKind() != tok::string_literal) {
392 Diag(Tok, diag::err_expected_string_literal);
393 SkipUntil(tok::r_paren);
394 return;
395 }
396
397 ExprResult Res = ParseStringLiteralExpression();
398 if (Res.isInvalid) {
399 Diag(Tok, diag::err_expected_string_literal);
400 SkipUntil(tok::r_paren);
401 return;
402 }
403
404 // TODO: Diagnose: wide string literal in 'asm'
405
406 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
407}