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