Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 1 | //===--- Parse.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 | 971c6b6 | 2006-08-05 22:46:42 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | Parser::~Parser() { |
| 28 | delete CurScope; |
| 29 | } |
| 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 | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // C99 6.9: External Definitions. |
| 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 40 | |
| 41 | /// ParseTranslationUnit: |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 42 | /// translation-unit: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 43 | /// external-declaration |
| 44 | /// translation-unit external-declaration |
| 45 | void Parser::ParseTranslationUnit() { |
| 46 | |
| 47 | if (Tok.getKind() == tok::eof) // Empty source file is an extension. |
| 48 | Diag(diag::ext_empty_source_file); |
| 49 | |
| 50 | while (Tok.getKind() != tok::eof) |
| 51 | ParseExternalDeclaration(); |
| 52 | } |
| 53 | |
| 54 | /// ParseExternalDeclaration: |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 55 | /// external-declaration: [C99 6.9] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 56 | /// function-definition [TODO] |
| 57 | /// declaration [TODO] |
| 58 | /// [EXT] ';' |
| 59 | /// [GNU] asm-definition [TODO] |
| 60 | /// [GNU] __extension__ external-declaration [TODO] |
| 61 | /// [OBJC] objc-class-definition [TODO] |
| 62 | /// [OBJC] objc-class-declaration [TODO] |
| 63 | /// [OBJC] objc-alias-declaration [TODO] |
| 64 | /// [OBJC] objc-protocol-definition [TODO] |
| 65 | /// [OBJC] objc-method-definition [TODO] |
| 66 | /// [OBJC] @end [TODO] |
| 67 | /// |
| 68 | void Parser::ParseExternalDeclaration() { |
| 69 | switch (Tok.getKind()) { |
| 70 | case tok::semi: |
| 71 | Diag(diag::ext_top_level_semi); |
| 72 | ConsumeToken(); |
| 73 | break; |
| 74 | default: |
| 75 | // We can't tell whether this is a function-definition or declaration yet. |
| 76 | ParseDeclarationOrFunctionDefinition(); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 82 | /// a declaration. We can't tell which we have until we read up to the |
| 83 | /// compound-statement in function-definition. |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 84 | /// |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 85 | /// function-definition: [C99 6.9.1] |
| 86 | /// declaration-specifiers[opt] declarator declaration-list[opt] |
| 87 | /// compound-statement [TODO] |
| 88 | /// declaration: [C99 6.7] |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 89 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 90 | /// [!C99] init-declarator-list ';' [TODO] |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 91 | /// [OMP] threadprivate-directive [TODO] |
| 92 | /// |
| 93 | /// init-declarator-list: [C99 6.7] |
| 94 | /// init-declarator |
| 95 | /// init-declarator-list ',' init-declarator |
| 96 | /// init-declarator: [C99 6.7] |
| 97 | /// declarator |
| 98 | /// declarator '=' initializer |
| 99 | /// |
Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame] | 100 | void Parser::ParseDeclarationOrFunctionDefinition() { |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 101 | // Parse the common declaration-specifiers piece. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 102 | // NOTE: this can not be missing for C99 'declaration's. |
| 103 | DeclSpec DS; |
| 104 | ParseDeclarationSpecifiers(DS); |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 105 | |
| 106 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 107 | if (Tok.getKind() == tok::semi) |
| 108 | assert(0 && "Unimp!"); |
| 109 | |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 111 | // Parse the declarator. |
| 112 | { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 113 | Declarator DeclaratorInfo(DS, Declarator::FileContext); |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 114 | ParseDeclarator(DeclaratorInfo); |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 116 | // If the declarator was a function type... handle it. |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 118 | // must be: decl-spec[opt] declarator init-declarator-list |
| 119 | // Parse declarator '=' initializer. |
| 120 | if (Tok.getKind() == tok::equal) |
| 121 | assert(0 && "cannot handle initializer yet!"); |
| 122 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 123 | |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 124 | while (Tok.getKind() == tok::comma) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 125 | // Consume the comma. |
| 126 | ConsumeToken(); |
| 127 | |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 128 | // Parse the declarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 129 | Declarator DeclaratorInfo(DS, Declarator::FileContext); |
Chris Lattner | 15356a7 | 2006-08-06 00:02:28 +0000 | [diff] [blame] | 130 | ParseDeclarator(DeclaratorInfo); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 131 | |
| 132 | // declarator '=' initializer |
| 133 | if (Tok.getKind() == tok::equal) |
| 134 | assert(0 && "cannot handle initializer yet!"); |
| 135 | |
| 136 | |
| 137 | } |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 138 | |
| 139 | if (Tok.getKind() == tok::semi) { |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 140 | ConsumeToken(); |
Chris Lattner | d286488 | 2006-08-05 08:09:44 +0000 | [diff] [blame] | 141 | } else { |
| 142 | Diag(Tok, diag::err_parse_error); |
| 143 | // FIXME: skip to end of block or statement |
| 144 | while (Tok.getKind() != tok::semi && Tok.getKind() != tok::eof) |
| 145 | ConsumeToken(); |
| 146 | if (Tok.getKind() == tok::semi) |
| 147 | ConsumeToken(); |
Chris Lattner | 70f32b7 | 2006-07-31 05:09:04 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |