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 | |
Chris Lattner | a8a2c82 | 2007-05-21 17:38:36 +0000 | [diff] [blame] | 14 | #include "clang/Sema/ASTStreamer.h" |
Steve Naroff | 2c055d2 | 2007-02-28 19:32:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 17 | #include "Sema.h" |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Action.h" |
| 19 | #include "clang/Parse/Parser.h" |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 22 | ASTConsumer::~ASTConsumer() {} |
| 23 | |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class ASTStreamer { |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 26 | Parser P; |
| 27 | public: |
Ted Kremenek | 230bd91 | 2007-12-19 22:51:13 +0000 | [diff] [blame^] | 28 | ASTStreamer(Preprocessor &pp, ASTContext &ctxt) |
Steve Naroff | 205ec3d | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 29 | : P(pp, *new Sema(pp, ctxt)) { |
Ted Kremenek | 230bd91 | 2007-12-19 22:51:13 +0000 | [diff] [blame^] | 30 | |
| 31 | pp.EnterMainSourceFile(); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 33 | // Initialize the parser. |
| 34 | P.Initialize(); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 38 | Decl *ReadTopLevelDecl(); |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 40 | void PrintStats() const; |
| 41 | |
Chris Lattner | 38ba336 | 2006-08-17 07:04:37 +0000 | [diff] [blame] | 42 | ~ASTStreamer() { |
| 43 | P.Finalize(); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 44 | delete &P.getActions(); |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 49 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 50 | /// |
| 51 | Decl *ASTStreamer::ReadTopLevelDecl() { |
| 52 | Parser::DeclTy *Result; |
| 53 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 54 | do { |
Steve Naroff | 205ec3d | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 55 | if (P.ParseTopLevelDecl(Result)) |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 56 | return 0; // End of file. |
| 57 | |
| 58 | // If we got a null return and something *was* parsed, try again. This |
| 59 | // is due to a top-level semicolon, an action override, or a parse error |
| 60 | // skipping something. |
Steve Naroff | 205ec3d | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 61 | } while (Result == 0); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 62 | |
| 63 | return static_cast<Decl*>(Result); |
| 64 | } |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 66 | void ASTStreamer::PrintStats() const { |
Steve Naroff | 5811baf | 2007-10-14 00:58:41 +0000 | [diff] [blame] | 67 | P.getActions().PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 73709ed | 2006-08-17 06:28:25 +0000 | [diff] [blame] | 69 | |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | // Public interface to the file |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 74 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 376cdaf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 75 | /// the file is parsed. This takes ownership of the ASTConsumer and |
| 76 | /// ultimately deletes it. |
Ted Kremenek | 230bd91 | 2007-12-19 22:51:13 +0000 | [diff] [blame^] | 77 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 78 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 79 | if (PrintStats) { |
| 80 | Decl::CollectingStats(true); |
| 81 | Stmt::CollectingStats(true); |
| 82 | } |
| 83 | |
| 84 | ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(), |
Steve Naroff | f73590d | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 85 | PP.getIdentifierTable(), PP.getSelectorTable()); |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 230bd91 | 2007-12-19 22:51:13 +0000 | [diff] [blame^] | 87 | ASTStreamer Streamer(PP, Context); |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 230bd91 | 2007-12-19 22:51:13 +0000 | [diff] [blame^] | 89 | Consumer->Initialize(Context); |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 90 | |
| 91 | while (Decl *D = Streamer.ReadTopLevelDecl()) |
Chris Lattner | 376cdaf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 92 | Consumer->HandleTopLevelDecl(D); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 94 | if (PrintStats) { |
| 95 | fprintf(stderr, "\nSTATISTICS:\n"); |
| 96 | Streamer.PrintStats(); |
| 97 | Context.PrintStats(); |
| 98 | Decl::PrintStats(); |
| 99 | Stmt::PrintStats(); |
Chris Lattner | 376cdaf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 100 | Consumer->PrintStats(); |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 101 | |
| 102 | Decl::CollectingStats(false); |
| 103 | Stmt::CollectingStats(false); |
| 104 | } |
Chris Lattner | 376cdaf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 105 | |
| 106 | delete Consumer; |
Chris Lattner | 75e0c8c | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 107 | } |