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) { |
| 48 | if (isTokenParen()) |
| 49 | ConsumeParen(); |
| 50 | else if (isTokenBracket()) |
| 51 | ConsumeBracket(); |
| 52 | else if (isTokenBrace()) |
| 53 | ConsumeBrace(); |
| 54 | else |
| 55 | ConsumeParen(); |
| 56 | } else { |
| 57 | Diag(Tok, DiagID); |
| 58 | Diag(LHSLoc, diag::err_matching, LHSName); |
| 59 | SkipUntil(RHSTok); |
| 60 | } |
| 61 | } |
| 62 | |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 64 | // Error recovery. |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | /// SkipUntil - Read tokens until we get to the specified token, then consume |
| 68 | /// it (unless DontConsume is false). Because we cannot guarantee that the |
| 69 | /// token will ever occur, this skips to the next token, or to some likely |
| 70 | /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' |
| 71 | /// character. |
| 72 | /// |
| 73 | /// If SkipUntil finds the specified token, it returns true, otherwise it |
| 74 | /// returns false. |
| 75 | bool Parser::SkipUntil(tok::TokenKind T, bool StopAtSemi, bool DontConsume) { |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 76 | // We always want this function to skip at least one token if the first token |
| 77 | // isn't T and if not at EOF. |
| 78 | bool isFirstTokenSkipped = true; |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 79 | while (1) { |
| 80 | // If we found the token, stop and return true. |
| 81 | if (Tok.getKind() == T) { |
| 82 | if (DontConsume) { |
| 83 | // Noop, don't consume the token. |
| 84 | } else if (isTokenParen()) { |
| 85 | ConsumeParen(); |
| 86 | } else if (isTokenBracket()) { |
| 87 | ConsumeBracket(); |
| 88 | } else if (isTokenBrace()) { |
| 89 | ConsumeBrace(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 90 | } else if (isTokenStringLiteral()) { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 91 | ConsumeStringToken(); |
| 92 | } else { |
| 93 | ConsumeToken(); |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | switch (Tok.getKind()) { |
| 99 | case tok::eof: |
| 100 | // Ran out of tokens. |
| 101 | return false; |
| 102 | |
| 103 | case tok::l_paren: |
| 104 | // Recursively skip properly-nested parens. |
| 105 | ConsumeParen(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 106 | SkipUntil(tok::r_paren, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 107 | break; |
| 108 | case tok::l_square: |
| 109 | // Recursively skip properly-nested square brackets. |
| 110 | ConsumeBracket(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 111 | SkipUntil(tok::r_square, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 112 | break; |
| 113 | case tok::l_brace: |
| 114 | // Recursively skip properly-nested braces. |
| 115 | ConsumeBrace(); |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 116 | SkipUntil(tok::r_brace, false); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 117 | break; |
| 118 | |
| 119 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 120 | // Since the user wasn't looking for this token (if they were, it would |
| 121 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 122 | // higher level, we will assume that this matches the unbalanced token |
| 123 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 124 | case tok::r_paren: |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 125 | if (ParenCount && !isFirstTokenSkipped) |
| 126 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 127 | ConsumeParen(); |
| 128 | break; |
| 129 | case tok::r_square: |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 130 | if (BracketCount && !isFirstTokenSkipped) |
| 131 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 132 | ConsumeBracket(); |
| 133 | break; |
| 134 | case tok::r_brace: |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 135 | if (BraceCount && !isFirstTokenSkipped) |
| 136 | return false; // Matches something. |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 137 | ConsumeBrace(); |
| 138 | break; |
| 139 | |
| 140 | case tok::string_literal: |
| 141 | ConsumeStringToken(); |
| 142 | break; |
| 143 | case tok::semi: |
| 144 | if (StopAtSemi) |
| 145 | return false; |
| 146 | // FALL THROUGH. |
| 147 | default: |
| 148 | // Skip this token. |
| 149 | ConsumeToken(); |
| 150 | break; |
| 151 | } |
Chris Lattner | 5bd57e0 | 2006-08-11 06:40:25 +0000 | [diff] [blame^] | 152 | isFirstTokenSkipped = false; |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | //===----------------------------------------------------------------------===// |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 157 | // C99 6.9: External Definitions. |
| 158 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 159 | |
| 160 | /// ParseTranslationUnit: |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 161 | /// translation-unit: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 162 | /// external-declaration |
| 163 | /// translation-unit external-declaration |
| 164 | void Parser::ParseTranslationUnit() { |
| 165 | |
| 166 | if (Tok.getKind() == tok::eof) // Empty source file is an extension. |
| 167 | Diag(diag::ext_empty_source_file); |
| 168 | |
| 169 | while (Tok.getKind() != tok::eof) |
| 170 | ParseExternalDeclaration(); |
| 171 | } |
| 172 | |
| 173 | /// ParseExternalDeclaration: |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 174 | /// external-declaration: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 175 | /// function-definition [TODO] |
| 176 | /// declaration [TODO] |
| 177 | /// [EXT] ';' |
| 178 | /// [GNU] asm-definition [TODO] |
| 179 | /// [GNU] __extension__ external-declaration [TODO] |
| 180 | /// [OBJC] objc-class-definition [TODO] |
| 181 | /// [OBJC] objc-class-declaration [TODO] |
| 182 | /// [OBJC] objc-alias-declaration [TODO] |
| 183 | /// [OBJC] objc-protocol-definition [TODO] |
| 184 | /// [OBJC] objc-method-definition [TODO] |
| 185 | /// [OBJC] @end [TODO] |
| 186 | /// |
| 187 | void Parser::ParseExternalDeclaration() { |
| 188 | switch (Tok.getKind()) { |
| 189 | case tok::semi: |
| 190 | Diag(diag::ext_top_level_semi); |
| 191 | ConsumeToken(); |
| 192 | break; |
| 193 | default: |
| 194 | // We can't tell whether this is a function-definition or declaration yet. |
| 195 | ParseDeclarationOrFunctionDefinition(); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 201 | /// a declaration. We can't tell which we have until we read up to the |
| 202 | /// compound-statement in function-definition. |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 203 | /// |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 204 | /// function-definition: [C99 6.9.1] |
| 205 | /// declaration-specifiers[opt] declarator declaration-list[opt] |
| 206 | /// compound-statement [TODO] |
| 207 | /// declaration: [C99 6.7] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 208 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 209 | /// [!C99] init-declarator-list ';' [TODO] |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 210 | /// [OMP] threadprivate-directive [TODO] |
| 211 | /// |
| 212 | /// init-declarator-list: [C99 6.7] |
| 213 | /// init-declarator |
| 214 | /// init-declarator-list ',' init-declarator |
| 215 | /// init-declarator: [C99 6.7] |
| 216 | /// declarator |
| 217 | /// declarator '=' initializer |
| 218 | /// |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 219 | void Parser::ParseDeclarationOrFunctionDefinition() { |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 220 | // Parse the common declaration-specifiers piece. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 221 | DeclSpec DS; |
| 222 | ParseDeclarationSpecifiers(DS); |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 223 | |
| 224 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 225 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 226 | if (Tok.getKind() == tok::semi) |
| 227 | assert(0 && "Unimp!"); |
| 228 | |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 229 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 230 | // Parse the first declarator. |
| 231 | Declarator DeclaratorInfo(DS, Declarator::FileContext); |
| 232 | ParseDeclarator(DeclaratorInfo); |
| 233 | // Error parsing the declarator? |
| 234 | if (DeclaratorInfo.getIdentifier() == 0) { |
| 235 | // If so, skip until the semi-colon or a }. |
| 236 | SkipUntil(tok::r_brace, true); |
| 237 | if (Tok.getKind() == tok::semi) |
| 238 | ConsumeToken(); |
| 239 | return; |
| 240 | } |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 241 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 242 | // If the declarator is the start of a function definition, handle it. |
| 243 | if (Tok.getKind() == tok::equal || // int X()= -> not a function def |
| 244 | Tok.getKind() == tok::comma || // int X(), -> not a function def |
| 245 | Tok.getKind() == tok::semi || // int X(); -> not a function def |
| 246 | Tok.getKind() == tok::kw_asm || // int X() __asm__ -> not a fn def |
| 247 | Tok.getKind() == tok::kw___attribute) {// int X() __attr__ -> not a fn def |
| 248 | // FALL THROUGH. |
| 249 | } else if (DeclaratorInfo.isInnermostFunctionType() && |
| 250 | (Tok.getKind() == tok::l_brace || // int X() {} |
| 251 | isDeclarationSpecifier())) { // int X(f) int f; {} |
| 252 | ParseFunctionDefinition(DeclaratorInfo); |
| 253 | return; |
| 254 | } else { |
| 255 | if (DeclaratorInfo.isInnermostFunctionType()) |
| 256 | Diag(Tok, diag::err_expected_fn_body); |
| 257 | else |
| 258 | Diag(Tok, diag::err_expected_after_declarator); |
| 259 | SkipUntil(tok::r_brace, true); |
| 260 | if (Tok.getKind() == tok::semi) |
| 261 | ConsumeToken(); |
| 262 | return; |
| 263 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 265 | // Parse the init-declarator-list for a normal declaration. |
| 266 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 269 | /// ParseFunctionDefinition - We parsed and verified that the specified |
| 270 | /// Declarator is well formed. If this is a K&R-style function, read the |
| 271 | /// parameters declaration-list, then start the compound-statement. |
| 272 | /// |
| 273 | /// declaration-specifiers[opt] declarator declaration-list[opt] |
| 274 | /// compound-statement [TODO] |
| 275 | /// |
| 276 | void Parser::ParseFunctionDefinition(Declarator &D) { |
| 277 | const DeclaratorTypeInfo &FnTypeInfo = D.getTypeObject(0); |
| 278 | assert(FnTypeInfo.Kind == DeclaratorTypeInfo::Function && |
| 279 | "This isn't a function declarator!"); |
| 280 | |
| 281 | // If this declaration was formed with a K&R-style identifier list for the |
| 282 | // arguments, parse declarations for all of the args next. |
| 283 | // int foo(a,b) int a; float b; {} |
| 284 | if (!FnTypeInfo.Fun.hasPrototype && !FnTypeInfo.Fun.isEmpty) { |
| 285 | // Read all the argument declarations. |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 286 | while (isDeclarationSpecifier()) |
| 287 | ParseDeclaration(Declarator::KNRTypeListContext); |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 288 | |
| 289 | // Note, check that we got them all. |
| 290 | } else { |
| 291 | //if (isDeclarationSpecifier()) |
| 292 | // Diag('k&r declspecs with prototype?'); |
| 293 | |
| 294 | // FIXME: Install the arguments into the current scope. |
| 295 | } |
| 296 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 297 | // We should have an opening brace now. |
| 298 | if (Tok.getKind() != tok::l_brace) { |
| 299 | Diag(Tok, diag::err_expected_fn_body); |
| 300 | |
| 301 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 302 | SkipUntil(tok::l_brace, true, true); |
| 303 | |
| 304 | // If we didn't find the '{', bail out. |
| 305 | if (Tok.getKind() != tok::l_brace) |
| 306 | return; |
| 307 | } |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 309 | ParseCompoundStatement(); |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |