blob: 1efdccdd85f8001d0f883ea1de4233d936fc20ca [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
20Parser::Parser(Preprocessor &pp, ParserActions &actions)
Chris Lattner971c6b62006-08-05 22:46:42 +000021 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
22 // Create the global scope, install it as the current scope.
23 CurScope = new Scope(0);
Chris Lattneracd58a32006-08-06 17:24:14 +000024 Tok.SetKind(tok::eof);
Chris Lattnereec40f92006-08-06 21:55:29 +000025
26 ParenCount = BracketCount = BraceCount = 0;
Chris Lattner971c6b62006-08-05 22:46:42 +000027}
28
29Parser::~Parser() {
30 delete CurScope;
31}
32
Chris Lattner0bb5f832006-07-31 01:59:18 +000033
Chris Lattnerb9093cd2006-08-04 04:39:53 +000034void Parser::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner0bb5f832006-07-31 01:59:18 +000035 const std::string &Msg) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000036 Diags.Report(Loc, DiagID, Msg);
Chris Lattner0bb5f832006-07-31 01:59:18 +000037}
38
Chris Lattner4564bc12006-08-10 23:14:52 +000039/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
40/// this helper function matches and consumes the specified RHS token if
41/// present. If not present, it emits the specified diagnostic indicating
42/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
43/// should be the name of the unmatched LHS token.
44void Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc,
45 const char *LHSName, unsigned DiagID) {
46
47 if (Tok.getKind() == RHSTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000048 ConsumeAnyToken();
Chris Lattner4564bc12006-08-10 23:14:52 +000049 } else {
50 Diag(Tok, DiagID);
51 Diag(LHSLoc, diag::err_matching, LHSName);
52 SkipUntil(RHSTok);
53 }
54}
55
Chris Lattnerdbb2a462006-08-12 19:26:13 +000056/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
57/// input. If so, it is consumed and false is returned.
58///
59/// If the input is malformed, this emits the specified diagnostic. Next, if
60/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
61/// returned.
62bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
63 tok::TokenKind SkipToTok) {
64 if (Tok.getKind() == ExpectedTok) {
65 ConsumeToken();
66 return false;
67 }
68
69 Diag(Tok, DiagID);
70 if (SkipToTok != tok::unknown)
71 SkipUntil(SkipToTok);
72 return true;
73}
74
Chris Lattner70f32b72006-07-31 05:09:04 +000075//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000076// Error recovery.
77//===----------------------------------------------------------------------===//
78
79/// SkipUntil - Read tokens until we get to the specified token, then consume
80/// it (unless DontConsume is false). Because we cannot guarantee that the
81/// token will ever occur, this skips to the next token, or to some likely
82/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
83/// character.
84///
85/// If SkipUntil finds the specified token, it returns true, otherwise it
86/// returns false.
87bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000088 // We always want this function to skip at least one token if the first token
89 // isn't T and if not at EOF.
90 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +000091 while (1) {
92 // If we found the token, stop and return true.
93 if (Tok.getKind() == T) {
94 if (DontConsume) {
95 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +000096 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000097 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +000098 }
99 return true;
100 }
101
102 switch (Tok.getKind()) {
103 case tok::eof:
104 // Ran out of tokens.
105 return false;
106
107 case tok::l_paren:
108 // Recursively skip properly-nested parens.
109 ConsumeParen();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000110 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000111 break;
112 case tok::l_square:
113 // Recursively skip properly-nested square brackets.
114 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000115 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000116 break;
117 case tok::l_brace:
118 // Recursively skip properly-nested braces.
119 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000120 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000121 break;
122
123 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
124 // Since the user wasn't looking for this token (if they were, it would
125 // already be handled), this isn't balanced. If there is a LHS token at a
126 // higher level, we will assume that this matches the unbalanced token
127 // and return it. Otherwise, this is a spurious RHS token, which we skip.
128 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000129 if (ParenCount && !isFirstTokenSkipped)
130 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000131 ConsumeParen();
132 break;
133 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000134 if (BracketCount && !isFirstTokenSkipped)
135 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000136 ConsumeBracket();
137 break;
138 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000139 if (BraceCount && !isFirstTokenSkipped)
140 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000141 ConsumeBrace();
142 break;
143
144 case tok::string_literal:
145 ConsumeStringToken();
146 break;
147 case tok::semi:
148 if (StopAtSemi)
149 return false;
150 // FALL THROUGH.
151 default:
152 // Skip this token.
153 ConsumeToken();
154 break;
155 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000156 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000157 }
158}
159
160//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000161// C99 6.9: External Definitions.
162//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000163
164/// ParseTranslationUnit:
Chris Lattner70f32b72006-07-31 05:09:04 +0000165/// translation-unit: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000166/// external-declaration
167/// translation-unit external-declaration
168void Parser::ParseTranslationUnit() {
169
170 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
171 Diag(diag::ext_empty_source_file);
172
173 while (Tok.getKind() != tok::eof)
174 ParseExternalDeclaration();
175}
176
177/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000178/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000179/// function-definition [TODO]
180/// declaration [TODO]
181/// [EXT] ';'
182/// [GNU] asm-definition [TODO]
183/// [GNU] __extension__ external-declaration [TODO]
184/// [OBJC] objc-class-definition [TODO]
185/// [OBJC] objc-class-declaration [TODO]
186/// [OBJC] objc-alias-declaration [TODO]
187/// [OBJC] objc-protocol-definition [TODO]
188/// [OBJC] objc-method-definition [TODO]
189/// [OBJC] @end [TODO]
190///
191void Parser::ParseExternalDeclaration() {
192 switch (Tok.getKind()) {
193 case tok::semi:
194 Diag(diag::ext_top_level_semi);
195 ConsumeToken();
196 break;
197 default:
198 // We can't tell whether this is a function-definition or declaration yet.
199 ParseDeclarationOrFunctionDefinition();
200 break;
201 }
202}
203
204/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000205/// a declaration. We can't tell which we have until we read up to the
206/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000207///
Chris Lattner70f32b72006-07-31 05:09:04 +0000208/// function-definition: [C99 6.9.1]
209/// declaration-specifiers[opt] declarator declaration-list[opt]
210/// compound-statement [TODO]
211/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000212/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000213/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000214/// [OMP] threadprivate-directive [TODO]
215///
216/// init-declarator-list: [C99 6.7]
217/// init-declarator
218/// init-declarator-list ',' init-declarator
219/// init-declarator: [C99 6.7]
220/// declarator
221/// declarator '=' initializer
222///
Chris Lattner0bb5f832006-07-31 01:59:18 +0000223void Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000224 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000225 DeclSpec DS;
226 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000227
228 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000229 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000230 if (Tok.getKind() == tok::semi) {
231 // TODO: emit error on 'int;' or 'const enum foo;'.
232 // if (!DS.isMissingDeclaratorOk()) Diag(...);
233
234 ConsumeToken();
235 return;
236 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000237
Chris Lattnerfff824f2006-08-07 06:31:38 +0000238 // Parse the first declarator.
239 Declarator DeclaratorInfo(DS, Declarator::FileContext);
240 ParseDeclarator(DeclaratorInfo);
241 // Error parsing the declarator?
242 if (DeclaratorInfo.getIdentifier() == 0) {
243 // If so, skip until the semi-colon or a }.
244 SkipUntil(tok::r_brace, true);
245 if (Tok.getKind() == tok::semi)
246 ConsumeToken();
247 return;
248 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000249
Chris Lattnerfff824f2006-08-07 06:31:38 +0000250 // If the declarator is the start of a function definition, handle it.
251 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
252 Tok.getKind() == tok::comma || // int X(), -> not a function def
253 Tok.getKind() == tok::semi || // int X(); -> not a function def
254 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
255 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
256 // FALL THROUGH.
257 } else if (DeclaratorInfo.isInnermostFunctionType() &&
258 (Tok.getKind() == tok::l_brace || // int X() {}
259 isDeclarationSpecifier())) { // int X(f) int f; {}
260 ParseFunctionDefinition(DeclaratorInfo);
261 return;
262 } else {
263 if (DeclaratorInfo.isInnermostFunctionType())
264 Diag(Tok, diag::err_expected_fn_body);
265 else
266 Diag(Tok, diag::err_expected_after_declarator);
267 SkipUntil(tok::r_brace, true);
268 if (Tok.getKind() == tok::semi)
269 ConsumeToken();
270 return;
271 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000272
Chris Lattner53361ac2006-08-10 05:19:57 +0000273 // Parse the init-declarator-list for a normal declaration.
274 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000275}
276
Chris Lattnerfff824f2006-08-07 06:31:38 +0000277/// ParseFunctionDefinition - We parsed and verified that the specified
278/// Declarator is well formed. If this is a K&R-style function, read the
279/// parameters declaration-list, then start the compound-statement.
280///
281/// declaration-specifiers[opt] declarator declaration-list[opt]
282/// compound-statement [TODO]
283///
284void Parser::ParseFunctionDefinition(Declarator &D) {
285 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
286 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
287 "This isn't a function declarator!");
288
289 // If this declaration was formed with a K&R-style identifier list for the
290 // arguments, parse declarations for all of the args next.
291 // int foo(a,b) int a; float b; {}
292 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
293 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000294 while (isDeclarationSpecifier())
295 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000296
297 // Note, check that we got them all.
298 } else {
299 //if (isDeclarationSpecifier())
300 // Diag('k&r declspecs with prototype?');
301
Chris Lattner8693a512006-08-13 21:54:02 +0000302 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000303 }
304
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000305 // We should have an opening brace now.
306 if (Tok.getKind() != tok::l_brace) {
307 Diag(Tok, diag::err_expected_fn_body);
308
309 // Skip over garbage, until we get to '{'. Don't eat the '{'.
310 SkipUntil(tok::l_brace, true, true);
311
312 // If we didn't find the '{', bail out.
313 if (Tok.getKind() != tok::l_brace)
314 return;
315 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000316
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000317 ParseCompoundStatement();
Chris Lattnerfff824f2006-08-07 06:31:38 +0000318}
319