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 | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Declarations.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 | |
| 20 | Parser::Parser(Preprocessor &pp, ParserActions &actions) |
Chris Lattner | 971c6b6 | 2006-08-05 22:46:42 +0000 | [diff] [blame] | 21 | : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) { |
| 22 | // Create the global scope, install it as the current scope. |
| 23 | CurScope = new Scope(0); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 24 | Tok.SetKind(tok::eof); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 25 | |
| 26 | ParenCount = BracketCount = BraceCount = 0; |
Chris Lattner | 971c6b6 | 2006-08-05 22:46:42 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | Parser::~Parser() { |
| 30 | delete CurScope; |
| 31 | } |
| 32 | |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 33 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 34 | void Parser::Diag(SourceLocation Loc, unsigned DiagID, |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 35 | const std::string &Msg) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 36 | Diags.Report(Loc, DiagID, Msg); |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 39 | /// 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. |
| 44 | void Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc, |
| 45 | const char *LHSName, unsigned DiagID) { |
| 46 | |
| 47 | if (Tok.getKind() == RHSTok) { |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 48 | ConsumeAnyToken(); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 49 | } else { |
| 50 | Diag(Tok, DiagID); |
| 51 | Diag(LHSLoc, diag::err_matching, LHSName); |
| 52 | SkipUntil(RHSTok); |
| 53 | } |
| 54 | } |
| 55 | |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 56 | /// 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. |
| 62 | bool 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 Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 76 | // 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. |
| 87 | bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) { |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 88 | // 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 Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 91 | 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 Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 96 | } else { |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 97 | ConsumeAnyToken(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 98 | } |
| 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 Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 110 | SkipUntil(tok::r_paren, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 111 | break; |
| 112 | case tok::l_square: |
| 113 | // Recursively skip properly-nested square brackets. |
| 114 | ConsumeBracket(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 115 | SkipUntil(tok::r_square, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 116 | break; |
| 117 | case tok::l_brace: |
| 118 | // Recursively skip properly-nested braces. |
| 119 | ConsumeBrace(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 120 | SkipUntil(tok::r_brace, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 121 | 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 Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 129 | if (ParenCount && !isFirstTokenSkipped) |
| 130 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 131 | ConsumeParen(); |
| 132 | break; |
| 133 | case tok::r_square: |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 134 | if (BracketCount && !isFirstTokenSkipped) |
| 135 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 136 | ConsumeBracket(); |
| 137 | break; |
| 138 | case tok::r_brace: |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 139 | if (BraceCount && !isFirstTokenSkipped) |
| 140 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 141 | 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 Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame] | 156 | isFirstTokenSkipped = false; |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | //===----------------------------------------------------------------------===// |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 161 | // C99 6.9: External Definitions. |
| 162 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 163 | |
| 164 | /// ParseTranslationUnit: |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 165 | /// translation-unit: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 166 | /// external-declaration |
| 167 | /// translation-unit external-declaration |
| 168 | void 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 Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 178 | /// external-declaration: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 179 | /// 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 | /// |
| 191 | void 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 Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 205 | /// a declaration. We can't tell which we have until we read up to the |
| 206 | /// compound-statement in function-definition. |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 207 | /// |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 208 | /// function-definition: [C99 6.9.1] |
| 209 | /// declaration-specifiers[opt] declarator declaration-list[opt] |
| 210 | /// compound-statement [TODO] |
| 211 | /// declaration: [C99 6.7] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 212 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 213 | /// [!C99] init-declarator-list ';' [TODO] |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 214 | /// [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 Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 223 | void Parser::ParseDeclarationOrFunctionDefinition() { |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 224 | // Parse the common declaration-specifiers piece. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 225 | DeclSpec DS; |
| 226 | ParseDeclarationSpecifiers(DS); |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 227 | |
| 228 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 229 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 230 | 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 Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 237 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 238 | // 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 Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 249 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 250 | // 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 Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 273 | // Parse the init-declarator-list for a normal declaration. |
| 274 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 277 | /// 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 | /// |
| 284 | void 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 Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 294 | while (isDeclarationSpecifier()) |
| 295 | ParseDeclaration(Declarator::KNRTypeListContext); |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 296 | |
| 297 | // Note, check that we got them all. |
| 298 | } else { |
| 299 | //if (isDeclarationSpecifier()) |
| 300 | // Diag('k&r declspecs with prototype?'); |
| 301 | |
Chris Lattner | 8693a51 | 2006-08-13 21:54:02 +0000 | [diff] [blame^] | 302 | // TODO: Install the arguments into the current scope. |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 305 | // 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 Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 317 | ParseCompoundStatement(); |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 318 | } |
| 319 | |