blob: 7310f697a22ea060fad2cbdf8f06829b446a276e [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 Lattner8c204872006-10-14 05:19:21 +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.
Chris Lattner71e23ce2006-11-04 20:18:38 +000047SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
48 SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000049
Chris Lattner71e23ce2006-11-04 20:18:38 +000050 if (Tok.getKind() == RHSTok)
51 return ConsumeAnyToken();
52
53 SourceLocation R = Tok.getLocation();
54 const char *LHSName = "unknown";
55 diag::kind DID = diag::err_parse_error;
56 switch (RHSTok) {
57 default: break;
58 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
59 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
60 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Chris Lattner4564bc12006-08-10 23:14:52 +000061 }
Chris Lattner71e23ce2006-11-04 20:18:38 +000062 Diag(Tok, DID);
63 Diag(LHSLoc, diag::err_matching, LHSName);
64 SkipUntil(RHSTok);
65 return R;
Chris Lattner4564bc12006-08-10 23:14:52 +000066}
67
Chris Lattnerdbb2a462006-08-12 19:26:13 +000068/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
69/// input. If so, it is consumed and false is returned.
70///
71/// If the input is malformed, this emits the specified diagnostic. Next, if
72/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
73/// returned.
74bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000075 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000076 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000077 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000078 return false;
79 }
80
Chris Lattner6d7e6342006-08-15 03:41:14 +000081 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000082 if (SkipToTok != tok::unknown)
83 SkipUntil(SkipToTok);
84 return true;
85}
86
Chris Lattner70f32b72006-07-31 05:09:04 +000087//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000088// Error recovery.
89//===----------------------------------------------------------------------===//
90
91/// SkipUntil - Read tokens until we get to the specified token, then consume
92/// it (unless DontConsume is false). Because we cannot guarantee that the
93/// token will ever occur, this skips to the next token, or to some likely
94/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
95/// character.
96///
97/// If SkipUntil finds the specified token, it returns true, otherwise it
98/// returns false.
99bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +0000100 // We always want this function to skip at least one token if the first token
101 // isn't T and if not at EOF.
102 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000103 while (1) {
104 // If we found the token, stop and return true.
105 if (Tok.getKind() == T) {
106 if (DontConsume) {
107 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +0000108 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000109 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000110 }
111 return true;
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();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000122 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000123 break;
124 case tok::l_square:
125 // Recursively skip properly-nested square brackets.
126 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000127 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000128 break;
129 case tok::l_brace:
130 // Recursively skip properly-nested braces.
131 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000132 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000133 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:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000141 if (ParenCount && !isFirstTokenSkipped)
142 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000143 ConsumeParen();
144 break;
145 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000146 if (BracketCount && !isFirstTokenSkipped)
147 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000148 ConsumeBracket();
149 break;
150 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000151 if (BraceCount && !isFirstTokenSkipped)
152 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000153 ConsumeBrace();
154 break;
155
156 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000157 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000158 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 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000169 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000170 }
171}
172
173//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000174// Scope manipulation
175//===----------------------------------------------------------------------===//
176
177/// EnterScope - Start a new scope.
178void Parser::EnterScope() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000179 CurScope = new Scope(CurScope);
180}
181
182/// ExitScope - Pop a scope off the scope stack.
183void Parser::ExitScope() {
184 assert(CurScope && "Scope imbalance!");
185
186 // Inform the actions module that this scope is going away.
187 Actions.PopScope(Tok.getLocation(), CurScope);
188
189 Scope *Old = CurScope;
190 CurScope = Old->getParent();
191 delete Old;
192}
193
194
195
196
197//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000198// C99 6.9: External Definitions.
199//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000200
Chris Lattner38ba3362006-08-17 07:04:37 +0000201/// Initialize - Warm up the parser.
202///
203void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000204 // Prime the lexer look-ahead.
205 ConsumeToken();
206
207 // Create the global scope, install it as the current scope.
208 assert(CurScope == 0 && "A scope is already active?");
209 EnterScope();
Chris Lattner38ba3362006-08-17 07:04:37 +0000210
Chris Lattner6d7e6342006-08-15 03:41:14 +0000211
212 // Install builtin types.
213 // TODO: Move this someplace more useful.
214 {
215 //__builtin_va_list
216 DeclSpec DS;
217 DS.StorageClassSpec = DeclSpec::SCS_typedef;
218
219 // TODO: add a 'TST_builtin' type?
220 DS.TypeSpecType = DeclSpec::TST_typedef;
Chris Lattner38ba3362006-08-17 07:04:37 +0000221
Chris Lattner6d7e6342006-08-15 03:41:14 +0000222 Declarator D(DS, Declarator::FileContext);
223 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000224 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000225 }
226
Chris Lattner0bb5f832006-07-31 01:59:18 +0000227 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
228 Diag(diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000229}
230
231/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
232/// action tells us to. This returns true if the EOF was encountered.
233bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
234 Result = 0;
235 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000236
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000237 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000238 return false;
239}
240
241/// Finalize - Shut down the parser.
242///
243void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000244 ExitScope();
245 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000246}
247
Chris Lattner38ba3362006-08-17 07:04:37 +0000248/// ParseTranslationUnit:
249/// translation-unit: [C99 6.9]
250/// external-declaration
251/// translation-unit external-declaration
252void Parser::ParseTranslationUnit() {
253 Initialize();
254
255 DeclTy *Res;
256 while (!ParseTopLevelDecl(Res))
257 /*parse them all*/;
258
259 Finalize();
260}
261
Chris Lattner0bb5f832006-07-31 01:59:18 +0000262/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000263/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000264/// function-definition [TODO]
265/// declaration [TODO]
266/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000267/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000268/// [GNU] __extension__ external-declaration [TODO]
Chris Lattner40f16b52006-11-05 02:05:37 +0000269/// [OBJC] objc-class-definition
270/// [OBJC] objc-class-declaration
271/// [OBJC] objc-alias-declaration
272/// [OBJC] objc-protocol-definition
273/// [OBJC] objc-method-definition
274/// [OBJC] @end
Chris Lattner0bb5f832006-07-31 01:59:18 +0000275///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000276/// [GNU] asm-definition:
277/// simple-asm-expr ';'
278///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000279Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000280 switch (Tok.getKind()) {
281 case tok::semi:
282 Diag(diag::ext_top_level_semi);
283 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000284 // TODO: Invoke action for top-level semicolon.
285 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000286 case tok::kw_asm:
287 ParseSimpleAsm();
288 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
289 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000290 // TODO: Invoke action for top-level asm.
291 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000292 case tok::at:
293 ObjCParseAtDirectives();
294 return 0;
295 case tok::minus:
296 ObjCParseInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000297 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000298 case tok::plus:
299 ObjCParseClassMethodDeclaration();
300 return 0;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000301 default:
302 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000303 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000304 }
305}
306
307/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000308/// a declaration. We can't tell which we have until we read up to the
309/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000310///
Chris Lattner70f32b72006-07-31 05:09:04 +0000311/// function-definition: [C99 6.9.1]
312/// declaration-specifiers[opt] declarator declaration-list[opt]
313/// compound-statement [TODO]
314/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000315/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000316/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000317/// [OMP] threadprivate-directive [TODO]
318///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000319Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000320 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000321 DeclSpec DS;
322 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000323
324 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000325 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000326 if (Tok.getKind() == tok::semi) {
327 // TODO: emit error on 'int;' or 'const enum foo;'.
328 // if (!DS.isMissingDeclaratorOk()) Diag(...);
329
330 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000331 // TODO: Return type definition.
332 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000333 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000334
Chris Lattnerfff824f2006-08-07 06:31:38 +0000335 // Parse the first declarator.
336 Declarator DeclaratorInfo(DS, Declarator::FileContext);
337 ParseDeclarator(DeclaratorInfo);
338 // Error parsing the declarator?
339 if (DeclaratorInfo.getIdentifier() == 0) {
340 // If so, skip until the semi-colon or a }.
341 SkipUntil(tok::r_brace, true);
342 if (Tok.getKind() == tok::semi)
343 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000344 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000345 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000346
Chris Lattnerfff824f2006-08-07 06:31:38 +0000347 // If the declarator is the start of a function definition, handle it.
348 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
349 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000350 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000351 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
352 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
353 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000354 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000355 (Tok.getKind() == tok::l_brace || // int X() {}
356 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000357 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000358 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000359 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000360 Diag(Tok, diag::err_expected_fn_body);
361 else
362 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000363 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000364 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000365 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000366
Chris Lattner53361ac2006-08-10 05:19:57 +0000367 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000368 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000369}
370
Chris Lattnerfff824f2006-08-07 06:31:38 +0000371/// ParseFunctionDefinition - We parsed and verified that the specified
372/// Declarator is well formed. If this is a K&R-style function, read the
373/// parameters declaration-list, then start the compound-statement.
374///
375/// declaration-specifiers[opt] declarator declaration-list[opt]
376/// compound-statement [TODO]
377///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000378Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnerfff824f2006-08-07 06:31:38 +0000379 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
380 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
381 "This isn't a function declarator!");
382
383 // If this declaration was formed with a K&R-style identifier list for the
384 // arguments, parse declarations for all of the args next.
385 // int foo(a,b) int a; float b; {}
386 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
387 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000388 while (isDeclarationSpecifier())
389 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000390
391 // Note, check that we got them all.
392 } else {
393 //if (isDeclarationSpecifier())
394 // Diag('k&r declspecs with prototype?');
395
Chris Lattner8693a512006-08-13 21:54:02 +0000396 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000397 }
398
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000399 // We should have an opening brace now.
400 if (Tok.getKind() != tok::l_brace) {
401 Diag(Tok, diag::err_expected_fn_body);
402
403 // Skip over garbage, until we get to '{'. Don't eat the '{'.
404 SkipUntil(tok::l_brace, true, true);
405
406 // If we didn't find the '{', bail out.
407 if (Tok.getKind() != tok::l_brace)
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000408 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000409 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000410
Chris Lattner30f910e2006-10-16 05:52:41 +0000411 // Parse the function body as a compound stmt.
412 StmtResult FnBody = ParseCompoundStatement();
413 if (FnBody.isInvalid) return 0;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000414
415 // TODO: Pass argument information.
Chris Lattner30f910e2006-10-16 05:52:41 +0000416 return Actions.ParseFunctionDefinition(CurScope, D, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000417}
418
Chris Lattner0116c472006-08-15 06:03:28 +0000419/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
420/// allowed to be a wide string, and is not subject to character translation.
421///
422/// [GNU] asm-string-literal:
423/// string-literal
424///
425void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000426 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000427 Diag(Tok, diag::err_expected_string_literal);
428 return;
429 }
430
431 ExprResult Res = ParseStringLiteralExpression();
432 if (Res.isInvalid) return;
433
434 // TODO: Diagnose: wide string literal in 'asm'
435}
436
Chris Lattner6d7e6342006-08-15 03:41:14 +0000437/// ParseSimpleAsm
438///
439/// [GNU] simple-asm-expr:
440/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000441///
442void Parser::ParseSimpleAsm() {
443 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
444 ConsumeToken();
445
446 if (Tok.getKind() != tok::l_paren) {
447 Diag(Tok, diag::err_expected_lparen_after, "asm");
448 return;
449 }
450
Chris Lattner04132372006-10-16 06:12:55 +0000451 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000452
Chris Lattner0116c472006-08-15 06:03:28 +0000453 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000454
Chris Lattner04f80192006-08-15 04:55:54 +0000455 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000456}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000457
Chris Lattner40f16b52006-11-05 02:05:37 +0000458/// ParseExternalDeclaration:
459/// external-declaration: [C99 6.9]
460/// [OBJC] objc-class-definition
461/// [OBJC] objc-class-declaration [TODO]
462/// [OBJC] objc-alias-declaration [TODO]
463/// [OBJC] objc-protocol-definition [TODO]
464/// [OBJC] objc-method-definition [TODO]
465/// [OBJC] '@' 'end' [TODO]
Steve Naroffb419d3a2006-10-27 23:18:49 +0000466void Parser::ObjCParseAtDirectives() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000467 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroffb419d3a2006-10-27 23:18:49 +0000468
469 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000470 switch (II ? II->getObjCKeywordID() : tok::objc_not_keyword) {
471 case tok::objc_class:
472 return ObjCParseAtClassDeclaration(AtLoc);
473 case tok::objc_interface:
474 return ObjCParseAtInterfaceDeclaration();
475 case tok::objc_protocol:
476 return ObjCParseAtProtocolDeclaration();
477 case tok::objc_implementation:
478 return ObjCParseAtImplementationDeclaration();
479 case tok::objc_end:
480 return ObjCParseAtEndDeclaration();
481 case tok::objc_compatibility_alias:
482 return ObjCParseAtAliasDeclaration();
483 default:
484 Diag(AtLoc, diag::err_unexpected_at);
485 SkipUntil(tok::semi);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000486 }
487}
488
489///
490/// objc-class-declaration:
Chris Lattner40f16b52006-11-05 02:05:37 +0000491/// '@' 'class' identifier-list ';'
Steve Naroffb419d3a2006-10-27 23:18:49 +0000492///
Chris Lattneraacc5af2006-11-03 07:21:07 +0000493void Parser::ObjCParseAtClassDeclaration(SourceLocation atLoc) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000494 ConsumeToken(); // the identifier "class"
Chris Lattner70058dd2006-11-03 07:32:21 +0000495 SmallVector<IdentifierInfo *, 8> ClassNames;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000496
497 while (1) {
Chris Lattner70058dd2006-11-03 07:32:21 +0000498 if (Tok.getKind() != tok::identifier) {
499 Diag(diag::err_expected_ident);
500 SkipUntil(tok::semi);
501 return;
502 }
503
504 ClassNames.push_back(Tok.getIdentifierInfo());
505 ConsumeToken();
506
507 if (Tok.getKind() != tok::comma)
508 break;
509
510 ConsumeToken();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000511 }
Chris Lattner70058dd2006-11-03 07:32:21 +0000512
513 // Consume the ';'.
514 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
515 return;
516
517 Actions.ParsedClassDeclaration(CurScope, &ClassNames[0], ClassNames.size());
Steve Naroffb419d3a2006-10-27 23:18:49 +0000518}
Chris Lattner70058dd2006-11-03 07:32:21 +0000519
Chris Lattneraacc5af2006-11-03 07:21:07 +0000520void Parser::ObjCParseAtInterfaceDeclaration() {
521 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000522}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000523void Parser::ObjCParseAtProtocolDeclaration() {
524 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000525}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000526void Parser::ObjCParseAtImplementationDeclaration() {
527 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000528}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000529void Parser::ObjCParseAtEndDeclaration() {
530 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000531}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000532void Parser::ObjCParseAtAliasDeclaration() {
533 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000534}
535
536void Parser::ObjCParseInstanceMethodDeclaration() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000537 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000538}
539
540void Parser::ObjCParseClassMethodDeclaration() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000541 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000542}