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