Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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/Sema/ASTStreamer.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "Sema.h" |
| 18 | #include "clang/Parse/Action.h" |
| 19 | #include "clang/Parse/Parser.h" |
| 20 | using namespace clang; |
| 21 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 22 | ASTConsumer::~ASTConsumer() {} |
| 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class ASTStreamer { |
| 26 | Parser P; |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 27 | std::vector<Decl*> TopLevelDeclList; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | public: |
| 29 | ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID) |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 30 | : P(pp, *new Sema(pp, ctxt, TopLevelDeclList)) { |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 31 | pp.EnterMainSourceFile(MainFileID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | |
| 33 | // Initialize the parser. |
| 34 | P.Initialize(); |
| 35 | } |
| 36 | |
| 37 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 38 | Decl *ReadTopLevelDecl(); |
| 39 | |
| 40 | void PrintStats() const; |
| 41 | |
| 42 | ~ASTStreamer() { |
| 43 | P.Finalize(); |
| 44 | delete &P.getActions(); |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 50 | /// |
| 51 | Decl *ASTStreamer::ReadTopLevelDecl() { |
| 52 | Parser::DeclTy *Result; |
| 53 | |
| 54 | /// If the previous time through we read something like 'int X, Y', return |
| 55 | /// the next declarator. |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 56 | if (!TopLevelDeclList.empty()) { |
| 57 | Result = TopLevelDeclList.back(); |
| 58 | TopLevelDeclList.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | return static_cast<Decl*>(Result); |
| 60 | } |
| 61 | |
| 62 | do { |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 63 | if (P.ParseTopLevelDecl()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | return 0; // End of file. |
| 65 | |
| 66 | // If we got a null return and something *was* parsed, try again. This |
| 67 | // is due to a top-level semicolon, an action override, or a parse error |
| 68 | // skipping something. |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 69 | } while (TopLevelDeclList.size() == 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | |
| 71 | // If we parsed a declspec with multiple declarators, reverse the list and |
| 72 | // return the first one. |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 73 | if (TopLevelDeclList.size() > 1) |
| 74 | std::reverse(TopLevelDeclList.begin(), TopLevelDeclList.end()); |
| 75 | |
| 76 | Result = TopLevelDeclList.back(); |
| 77 | TopLevelDeclList.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | |
| 79 | return static_cast<Decl*>(Result); |
| 80 | } |
| 81 | |
| 82 | void ASTStreamer::PrintStats() const { |
Steve Naroff | 58ff9e8 | 2007-10-14 00:58:41 +0000 | [diff] [blame] | 83 | P.getActions().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | // Public interface to the file |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 90 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 91 | /// the file is parsed. This takes ownership of the ASTConsumer and |
| 92 | /// ultimately deletes it. |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 93 | void clang::ParseAST(Preprocessor &PP, unsigned MainFileID, |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 94 | ASTConsumer *Consumer, bool PrintStats) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 95 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 96 | if (PrintStats) { |
| 97 | Decl::CollectingStats(true); |
| 98 | Stmt::CollectingStats(true); |
| 99 | } |
| 100 | |
| 101 | ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(), |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 102 | PP.getIdentifierTable(), PP.getSelectorTable()); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 103 | |
| 104 | ASTStreamer Streamer(PP, Context, MainFileID); |
| 105 | |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 106 | Consumer->Initialize(Context, MainFileID); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 107 | |
| 108 | while (Decl *D = Streamer.ReadTopLevelDecl()) |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 109 | Consumer->HandleTopLevelDecl(D); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 111 | if (PrintStats) { |
| 112 | fprintf(stderr, "\nSTATISTICS:\n"); |
| 113 | Streamer.PrintStats(); |
| 114 | Context.PrintStats(); |
| 115 | Decl::PrintStats(); |
| 116 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 117 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 118 | |
| 119 | Decl::CollectingStats(false); |
| 120 | Stmt::CollectingStats(false); |
| 121 | } |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 122 | |
| 123 | delete Consumer; |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 124 | } |