Chris Lattner | 0bb5f83 | 2006-07-31 01:59:18 +0000 | [diff] [blame^] | 1 | //===--- Parser.cpp - C Language Family Parser ----------------------------===// |
| 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" |
| 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | using namespace llvm; |
| 17 | using namespace clang; |
| 18 | |
| 19 | Parser::Parser(Preprocessor &pp, ParserActions &actions) |
| 20 | : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {} |
| 21 | |
| 22 | void Parser::Diag(const LexerToken &Tok, unsigned DiagID, |
| 23 | const std::string &Msg) { |
| 24 | Diags.Report(Tok.getLocation(), DiagID, Msg); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | /// ParseTranslationUnit: |
| 30 | /// translation-unit: [C99 6.9p1] |
| 31 | /// external-declaration |
| 32 | /// translation-unit external-declaration |
| 33 | void Parser::ParseTranslationUnit() { |
| 34 | |
| 35 | if (Tok.getKind() == tok::eof) // Empty source file is an extension. |
| 36 | Diag(diag::ext_empty_source_file); |
| 37 | |
| 38 | while (Tok.getKind() != tok::eof) |
| 39 | ParseExternalDeclaration(); |
| 40 | } |
| 41 | |
| 42 | /// ParseExternalDeclaration: |
| 43 | /// external-declaration: [C99 6.9p1] |
| 44 | /// function-definition [TODO] |
| 45 | /// declaration [TODO] |
| 46 | /// [EXT] ';' |
| 47 | /// [GNU] asm-definition [TODO] |
| 48 | /// [GNU] __extension__ external-declaration [TODO] |
| 49 | /// [OBJC] objc-class-definition [TODO] |
| 50 | /// [OBJC] objc-class-declaration [TODO] |
| 51 | /// [OBJC] objc-alias-declaration [TODO] |
| 52 | /// [OBJC] objc-protocol-definition [TODO] |
| 53 | /// [OBJC] objc-method-definition [TODO] |
| 54 | /// [OBJC] @end [TODO] |
| 55 | /// |
| 56 | void Parser::ParseExternalDeclaration() { |
| 57 | switch (Tok.getKind()) { |
| 58 | case tok::semi: |
| 59 | Diag(diag::ext_top_level_semi); |
| 60 | ConsumeToken(); |
| 61 | break; |
| 62 | default: |
| 63 | // We can't tell whether this is a function-definition or declaration yet. |
| 64 | ParseDeclarationOrFunctionDefinition(); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or |
| 70 | /// a declaration. We can't tell which we have until we read the common |
| 71 | /// declaration-specifiers portion. |
| 72 | /// |
| 73 | /// function-definition: [C99 6.9.1p1] |
| 74 | /// declaration-specifiers declarator declaration-list[opt] |
| 75 | /// compound-statement [TODO] |
| 76 | /// declaration: [C99 6.7p1] |
| 77 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
| 78 | /// [OMP] threadprivate-directive [TODO] |
| 79 | void Parser::ParseDeclarationOrFunctionDefinition() { |
| 80 | ConsumeToken(); |
| 81 | } |