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