blob: eb684474e203fd1ea815142d4d7fac6299a4ccc3 [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 Lattner04f80192006-08-15 04:55:54 +000047void Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc) {
Chris Lattner4564bc12006-08-10 23:14:52 +000048
49 if (Tok.getKind() == RHSTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000050 ConsumeAnyToken();
Chris Lattner4564bc12006-08-10 23:14:52 +000051 } else {
Chris Lattner04f80192006-08-15 04:55:54 +000052 const char *LHSName = "unknown";
53 diag::kind DID = diag::err_parse_error;
54 switch (RHSTok) {
55 default: break;
56 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
57 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
58 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
59 }
60 Diag(Tok, DID);
Chris Lattner4564bc12006-08-10 23:14:52 +000061 Diag(LHSLoc, diag::err_matching, LHSName);
62 SkipUntil(RHSTok);
63 }
64}
65
Chris Lattnerdbb2a462006-08-12 19:26:13 +000066/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
67/// input. If so, it is consumed and false is returned.
68///
69/// If the input is malformed, this emits the specified diagnostic. Next, if
70/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
71/// returned.
72bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +000073 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattnerdbb2a462006-08-12 19:26:13 +000074 if (Tok.getKind() == ExpectedTok) {
Chris Lattner15a00da2006-08-15 04:10:31 +000075 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +000076 return false;
77 }
78
Chris Lattner6d7e6342006-08-15 03:41:14 +000079 Diag(Tok, DiagID, Msg);
Chris Lattnerdbb2a462006-08-12 19:26:13 +000080 if (SkipToTok != tok::unknown)
81 SkipUntil(SkipToTok);
82 return true;
83}
84
Chris Lattner70f32b72006-07-31 05:09:04 +000085//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +000086// Error recovery.
87//===----------------------------------------------------------------------===//
88
89/// SkipUntil - Read tokens until we get to the specified token, then consume
90/// it (unless DontConsume is false). Because we cannot guarantee that the
91/// token will ever occur, this skips to the next token, or to some likely
92/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
93/// character.
94///
95/// If SkipUntil finds the specified token, it returns true, otherwise it
96/// returns false.
97bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) {
Chris Lattner5bd57e02006-08-11 06:40:25 +000098 // 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;
Chris Lattnereec40f92006-08-06 21:55:29 +0000101 while (1) {
102 // If we found the token, stop and return true.
103 if (Tok.getKind() == T) {
104 if (DontConsume) {
105 // Noop, don't consume the token.
Chris Lattnereec40f92006-08-06 21:55:29 +0000106 } else {
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000107 ConsumeAnyToken();
Chris Lattnereec40f92006-08-06 21:55:29 +0000108 }
109 return true;
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();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000120 SkipUntil(tok::r_paren, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000121 break;
122 case tok::l_square:
123 // Recursively skip properly-nested square brackets.
124 ConsumeBracket();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000125 SkipUntil(tok::r_square, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000126 break;
127 case tok::l_brace:
128 // Recursively skip properly-nested braces.
129 ConsumeBrace();
Chris Lattner5bd57e02006-08-11 06:40:25 +0000130 SkipUntil(tok::r_brace, false);
Chris Lattnereec40f92006-08-06 21:55:29 +0000131 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:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000139 if (ParenCount && !isFirstTokenSkipped)
140 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000141 ConsumeParen();
142 break;
143 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000144 if (BracketCount && !isFirstTokenSkipped)
145 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000146 ConsumeBracket();
147 break;
148 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000149 if (BraceCount && !isFirstTokenSkipped)
150 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000151 ConsumeBrace();
152 break;
153
154 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000155 case tok::wide_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000156 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 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000167 isFirstTokenSkipped = false;
Chris Lattnereec40f92006-08-06 21:55:29 +0000168 }
169}
170
171//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000172// Scope manipulation
173//===----------------------------------------------------------------------===//
174
175/// EnterScope - Start a new scope.
176void Parser::EnterScope() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000177 CurScope = new Scope(CurScope);
178}
179
180/// ExitScope - Pop a scope off the scope stack.
181void Parser::ExitScope() {
182 assert(CurScope && "Scope imbalance!");
183
184 // Inform the actions module that this scope is going away.
185 Actions.PopScope(Tok.getLocation(), CurScope);
186
187 Scope *Old = CurScope;
188 CurScope = Old->getParent();
189 delete Old;
190}
191
192
193
194
195//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000196// C99 6.9: External Definitions.
197//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000198
Chris Lattner38ba3362006-08-17 07:04:37 +0000199/// Initialize - Warm up the parser.
200///
201void Parser::Initialize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000202 // Prime the lexer look-ahead.
203 ConsumeToken();
204
205 // Create the global scope, install it as the current scope.
206 assert(CurScope == 0 && "A scope is already active?");
207 EnterScope();
Chris Lattner38ba3362006-08-17 07:04:37 +0000208
Chris Lattner6d7e6342006-08-15 03:41:14 +0000209
210 // Install builtin types.
211 // TODO: Move this someplace more useful.
212 {
213 //__builtin_va_list
214 DeclSpec DS;
215 DS.StorageClassSpec = DeclSpec::SCS_typedef;
216
217 // TODO: add a 'TST_builtin' type?
218 DS.TypeSpecType = DeclSpec::TST_typedef;
Chris Lattner38ba3362006-08-17 07:04:37 +0000219
Chris Lattner6d7e6342006-08-15 03:41:14 +0000220 Declarator D(DS, Declarator::FileContext);
221 D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation());
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000222 Actions.ParseDeclarator(CurScope, D, 0, 0);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000223 }
224
Chris Lattner0bb5f832006-07-31 01:59:18 +0000225 if (Tok.getKind() == tok::eof) // Empty source file is an extension.
226 Diag(diag::ext_empty_source_file);
Chris Lattner38ba3362006-08-17 07:04:37 +0000227}
228
229/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
230/// action tells us to. This returns true if the EOF was encountered.
231bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
232 Result = 0;
233 if (Tok.getKind() == tok::eof) return true;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000234
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000235 Result = ParseExternalDeclaration();
Chris Lattner38ba3362006-08-17 07:04:37 +0000236 return false;
237}
238
239/// Finalize - Shut down the parser.
240///
241void Parser::Finalize() {
Chris Lattnere4e38592006-08-14 00:15:05 +0000242 ExitScope();
243 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner0bb5f832006-07-31 01:59:18 +0000244}
245
Chris Lattner38ba3362006-08-17 07:04:37 +0000246/// ParseTranslationUnit:
247/// translation-unit: [C99 6.9]
248/// external-declaration
249/// translation-unit external-declaration
250void Parser::ParseTranslationUnit() {
251 Initialize();
252
253 DeclTy *Res;
254 while (!ParseTopLevelDecl(Res))
255 /*parse them all*/;
256
257 Finalize();
258}
259
Chris Lattner0bb5f832006-07-31 01:59:18 +0000260/// ParseExternalDeclaration:
Chris Lattner70f32b72006-07-31 05:09:04 +0000261/// external-declaration: [C99 6.9]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000262/// function-definition [TODO]
263/// declaration [TODO]
264/// [EXT] ';'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000265/// [GNU] asm-definition
Chris Lattner0bb5f832006-07-31 01:59:18 +0000266/// [GNU] __extension__ external-declaration [TODO]
267/// [OBJC] objc-class-definition [TODO]
268/// [OBJC] objc-class-declaration [TODO]
269/// [OBJC] objc-alias-declaration [TODO]
270/// [OBJC] objc-protocol-definition [TODO]
271/// [OBJC] objc-method-definition [TODO]
272/// [OBJC] @end [TODO]
273///
Chris Lattner6d7e6342006-08-15 03:41:14 +0000274/// [GNU] asm-definition:
275/// simple-asm-expr ';'
276///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000277Parser::DeclTy *Parser::ParseExternalDeclaration() {
Chris Lattner0bb5f832006-07-31 01:59:18 +0000278 switch (Tok.getKind()) {
279 case tok::semi:
280 Diag(diag::ext_top_level_semi);
281 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000282 // TODO: Invoke action for top-level semicolon.
283 return 0;
Chris Lattner6d7e6342006-08-15 03:41:14 +0000284 case tok::kw_asm:
285 ParseSimpleAsm();
286 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
287 "top-level asm block");
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000288 // TODO: Invoke action for top-level asm.
289 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000290 case tok::at:
291 ObjCParseAtDirectives();
292 return 0;
293 case tok::minus:
294 ObjCParseInstanceMethodDeclaration();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000295 return 0;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000296 case tok::plus:
297 ObjCParseClassMethodDeclaration();
298 return 0;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000299 default:
300 // We can't tell whether this is a function-definition or declaration yet.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000301 return ParseDeclarationOrFunctionDefinition();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000302 }
303}
304
305/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000306/// a declaration. We can't tell which we have until we read up to the
307/// compound-statement in function-definition.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000308///
Chris Lattner70f32b72006-07-31 05:09:04 +0000309/// function-definition: [C99 6.9.1]
310/// declaration-specifiers[opt] declarator declaration-list[opt]
311/// compound-statement [TODO]
312/// declaration: [C99 6.7]
Chris Lattner0bb5f832006-07-31 01:59:18 +0000313/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000314/// [!C99] init-declarator-list ';' [TODO]
Chris Lattner70f32b72006-07-31 05:09:04 +0000315/// [OMP] threadprivate-directive [TODO]
316///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000317Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
Chris Lattner70f32b72006-07-31 05:09:04 +0000318 // Parse the common declaration-specifiers piece.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000319 DeclSpec DS;
320 ParseDeclarationSpecifiers(DS);
Chris Lattnerd2864882006-08-05 08:09:44 +0000321
322 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000323 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0e894622006-08-13 19:58:17 +0000324 if (Tok.getKind() == tok::semi) {
325 // TODO: emit error on 'int;' or 'const enum foo;'.
326 // if (!DS.isMissingDeclaratorOk()) Diag(...);
327
328 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000329 // TODO: Return type definition.
330 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000331 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000332
Chris Lattnerfff824f2006-08-07 06:31:38 +0000333 // Parse the first declarator.
334 Declarator DeclaratorInfo(DS, Declarator::FileContext);
335 ParseDeclarator(DeclaratorInfo);
336 // Error parsing the declarator?
337 if (DeclaratorInfo.getIdentifier() == 0) {
338 // If so, skip until the semi-colon or a }.
339 SkipUntil(tok::r_brace, true);
340 if (Tok.getKind() == tok::semi)
341 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000342 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000343 }
Chris Lattner70f32b72006-07-31 05:09:04 +0000344
Chris Lattnerfff824f2006-08-07 06:31:38 +0000345 // If the declarator is the start of a function definition, handle it.
346 if (Tok.getKind() == tok::equal || // int X()= -> not a function def
347 Tok.getKind() == tok::comma || // int X(), -> not a function def
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000348 Tok.getKind() == tok::semi || // int X(); -> not a function def
Chris Lattnerfff824f2006-08-07 06:31:38 +0000349 Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def
350 Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def
351 // FALL THROUGH.
Chris Lattnera11999d2006-10-15 22:34:45 +0000352 } else if (DeclaratorInfo.isFunctionDeclarator() &&
Chris Lattnerfff824f2006-08-07 06:31:38 +0000353 (Tok.getKind() == tok::l_brace || // int X() {}
354 isDeclarationSpecifier())) { // int X(f) int f; {}
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000355 return ParseFunctionDefinition(DeclaratorInfo);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000356 } else {
Chris Lattnera11999d2006-10-15 22:34:45 +0000357 if (DeclaratorInfo.isFunctionDeclarator())
Chris Lattnerfff824f2006-08-07 06:31:38 +0000358 Diag(Tok, diag::err_expected_fn_body);
359 else
360 Diag(Tok, diag::err_expected_after_declarator);
Chris Lattnere4e38592006-08-14 00:15:05 +0000361 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000362 return 0;
Chris Lattnerfff824f2006-08-07 06:31:38 +0000363 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000364
Chris Lattner53361ac2006-08-10 05:19:57 +0000365 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000366 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner70f32b72006-07-31 05:09:04 +0000367}
368
Chris Lattnerfff824f2006-08-07 06:31:38 +0000369/// ParseFunctionDefinition - We parsed and verified that the specified
370/// Declarator is well formed. If this is a K&R-style function, read the
371/// parameters declaration-list, then start the compound-statement.
372///
373/// declaration-specifiers[opt] declarator declaration-list[opt]
374/// compound-statement [TODO]
375///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000376Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
Chris Lattnerfff824f2006-08-07 06:31:38 +0000377 const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0);
378 assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function &&
379 "This isn't a function declarator!");
380
381 // If this declaration was formed with a K&R-style identifier list for the
382 // arguments, parse declarations for all of the args next.
383 // int foo(a,b) int a; float b; {}
384 if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) {
385 // Read all the argument declarations.
Chris Lattner53361ac2006-08-10 05:19:57 +0000386 while (isDeclarationSpecifier())
387 ParseDeclaration(Declarator::KNRTypeListContext);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000388
389 // Note, check that we got them all.
390 } else {
391 //if (isDeclarationSpecifier())
392 // Diag('k&r declspecs with prototype?');
393
Chris Lattner8693a512006-08-13 21:54:02 +0000394 // TODO: Install the arguments into the current scope.
Chris Lattnerfff824f2006-08-07 06:31:38 +0000395 }
396
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000397 // We should have an opening brace now.
398 if (Tok.getKind() != tok::l_brace) {
399 Diag(Tok, diag::err_expected_fn_body);
400
401 // Skip over garbage, until we get to '{'. Don't eat the '{'.
402 SkipUntil(tok::l_brace, true, true);
403
404 // If we didn't find the '{', bail out.
405 if (Tok.getKind() != tok::l_brace)
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000406 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000407 }
Chris Lattnerfff824f2006-08-07 06:31:38 +0000408
Chris Lattner30f910e2006-10-16 05:52:41 +0000409 // Parse the function body as a compound stmt.
410 StmtResult FnBody = ParseCompoundStatement();
411 if (FnBody.isInvalid) return 0;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000412
413 // TODO: Pass argument information.
Chris Lattner30f910e2006-10-16 05:52:41 +0000414 return Actions.ParseFunctionDefinition(CurScope, D, FnBody.Val);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000415}
416
Chris Lattner0116c472006-08-15 06:03:28 +0000417/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
418/// allowed to be a wide string, and is not subject to character translation.
419///
420/// [GNU] asm-string-literal:
421/// string-literal
422///
423void Parser::ParseAsmStringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000424 if (!isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000425 Diag(Tok, diag::err_expected_string_literal);
426 return;
427 }
428
429 ExprResult Res = ParseStringLiteralExpression();
430 if (Res.isInvalid) return;
431
432 // TODO: Diagnose: wide string literal in 'asm'
433}
434
Chris Lattner6d7e6342006-08-15 03:41:14 +0000435/// ParseSimpleAsm
436///
437/// [GNU] simple-asm-expr:
438/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +0000439///
440void Parser::ParseSimpleAsm() {
441 assert(Tok.getKind() == tok::kw_asm && "Not an asm!");
442 ConsumeToken();
443
444 if (Tok.getKind() != tok::l_paren) {
445 Diag(Tok, diag::err_expected_lparen_after, "asm");
446 return;
447 }
448
Chris Lattner04132372006-10-16 06:12:55 +0000449 SourceLocation Loc = ConsumeParen();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000450
Chris Lattner0116c472006-08-15 06:03:28 +0000451 ParseAsmStringLiteral();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000452
Chris Lattner04f80192006-08-15 04:55:54 +0000453 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner6d7e6342006-08-15 03:41:14 +0000454}
Steve Naroffb419d3a2006-10-27 23:18:49 +0000455
456void Parser::ObjCParseAtDirectives() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000457 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroffb419d3a2006-10-27 23:18:49 +0000458
459 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattneraacc5af2006-11-03 07:21:07 +0000460 switch (II ? II->getObjCKeywordID() : tok::objc_not_keyword) {
461 case tok::objc_class:
462 return ObjCParseAtClassDeclaration(AtLoc);
463 case tok::objc_interface:
464 return ObjCParseAtInterfaceDeclaration();
465 case tok::objc_protocol:
466 return ObjCParseAtProtocolDeclaration();
467 case tok::objc_implementation:
468 return ObjCParseAtImplementationDeclaration();
469 case tok::objc_end:
470 return ObjCParseAtEndDeclaration();
471 case tok::objc_compatibility_alias:
472 return ObjCParseAtAliasDeclaration();
473 default:
474 Diag(AtLoc, diag::err_unexpected_at);
475 SkipUntil(tok::semi);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000476 }
477}
478
479///
480/// objc-class-declaration:
481/// @class identifier-list ;
482///
Chris Lattneraacc5af2006-11-03 07:21:07 +0000483void Parser::ObjCParseAtClassDeclaration(SourceLocation atLoc) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000484 ConsumeToken(); // the identifier "class"
Chris Lattner70058dd2006-11-03 07:32:21 +0000485 SmallVector<IdentifierInfo *, 8> ClassNames;
Steve Naroffb419d3a2006-10-27 23:18:49 +0000486
487 while (1) {
Chris Lattner70058dd2006-11-03 07:32:21 +0000488 if (Tok.getKind() != tok::identifier) {
489 Diag(diag::err_expected_ident);
490 SkipUntil(tok::semi);
491 return;
492 }
493
494 ClassNames.push_back(Tok.getIdentifierInfo());
495 ConsumeToken();
496
497 if (Tok.getKind() != tok::comma)
498 break;
499
500 ConsumeToken();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000501 }
Chris Lattner70058dd2006-11-03 07:32:21 +0000502
503 // Consume the ';'.
504 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
505 return;
506
507 Actions.ParsedClassDeclaration(CurScope, &ClassNames[0], ClassNames.size());
Steve Naroffb419d3a2006-10-27 23:18:49 +0000508}
Chris Lattner70058dd2006-11-03 07:32:21 +0000509
Chris Lattneraacc5af2006-11-03 07:21:07 +0000510void Parser::ObjCParseAtInterfaceDeclaration() {
511 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000512}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000513void Parser::ObjCParseAtProtocolDeclaration() {
514 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000515}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000516void Parser::ObjCParseAtImplementationDeclaration() {
517 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000518}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000519void Parser::ObjCParseAtEndDeclaration() {
520 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000521}
Chris Lattneraacc5af2006-11-03 07:21:07 +0000522void Parser::ObjCParseAtAliasDeclaration() {
523 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000524}
525
526void Parser::ObjCParseInstanceMethodDeclaration() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000527 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000528}
529
530void Parser::ObjCParseClassMethodDeclaration() {
Chris Lattneraacc5af2006-11-03 07:21:07 +0000531 assert(0 && "Unimp");
Steve Naroffb419d3a2006-10-27 23:18:49 +0000532}