Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 1 | //===--- ASTStreamer.cpp - Provide streaming interface to ASTs ------------===// |
| 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 ASTStreamer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTStreamer.h" |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame^] | 15 | #include "Sema.h" |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Action.h" |
| 17 | #include "clang/Parse/Parser.h" |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | class ASTStreamer { |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 23 | Parser P; |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 24 | std::vector<Decl*> LastInGroupList; |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 25 | public: |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 26 | ASTStreamer(Preprocessor &PP, unsigned MainFileID) |
Chris Lattner | cc67ec1 | 2006-11-09 06:54:47 +0000 | [diff] [blame] | 27 | : P(PP, *new Sema(PP, LastInGroupList)) { |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 28 | PP.EnterSourceFile(MainFileID, 0, true); |
| 29 | |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 30 | // Initialize the parser. |
| 31 | P.Initialize(); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 35 | Decl *ReadTopLevelDecl() { |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 36 | Parser::DeclTy *Result; |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 37 | |
| 38 | /// If the previous time through we read something like 'int X, Y', return |
| 39 | /// the next declarator. |
| 40 | if (!LastInGroupList.empty()) { |
| 41 | Result = LastInGroupList.back(); |
| 42 | LastInGroupList.pop_back(); |
| 43 | return (Decl*)Result; |
| 44 | } |
| 45 | |
| 46 | do { |
| 47 | if (P.ParseTopLevelDecl(Result)) |
| 48 | return 0; // End of file. |
| 49 | |
| 50 | // If we got a null return and something *was* parsed, try again. This |
| 51 | // is due to a top-level semicolon, an action override, or a parse error |
| 52 | // skipping something. |
| 53 | } while (Result == 0); |
| 54 | |
| 55 | // If we parsed a declspec with multiple declarators, reverse the list and |
| 56 | // return the first one. |
| 57 | if (!LastInGroupList.empty()) { |
| 58 | LastInGroupList.push_back((Decl*)Result); |
| 59 | std::reverse(LastInGroupList.begin(), LastInGroupList.end()); |
| 60 | Result = LastInGroupList.back(); |
| 61 | LastInGroupList.pop_back(); |
| 62 | } |
| 63 | |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 64 | return (Decl*)Result; |
| 65 | } |
| 66 | |
| 67 | ~ASTStreamer() { |
| 68 | P.Finalize(); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 69 | delete &P.getActions(); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // Public interface to the file |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor |
| 81 | /// and FileID. |
| 82 | ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &PP, |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 83 | unsigned MainFileID) { |
| 84 | return new ASTStreamer(PP, MainFileID); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This |
| 88 | /// returns null at end of file. |
| 89 | Decl *llvm::clang::ASTStreamer_ReadTopLevelDecl(ASTStreamerTy *Streamer) { |
| 90 | return static_cast<ASTStreamer*>(Streamer)->ReadTopLevelDecl(); |
| 91 | } |
| 92 | |
| 93 | /// ASTStreamer_Terminate - Gracefully shut down the streamer. |
| 94 | /// |
| 95 | void llvm::clang::ASTStreamer_Terminate(ASTStreamerTy *Streamer) { |
| 96 | delete static_cast<ASTStreamer*>(Streamer); |
| 97 | } |