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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | public: |
| 28 | ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID) |
Steve Naroff | 89307ff | 2007-11-29 23:05:20 +0000 | [diff] [blame^] | 29 | : P(pp, *new Sema(pp, ctxt)) { |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 30 | pp.EnterMainSourceFile(MainFileID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | |
| 32 | // Initialize the parser. |
| 33 | P.Initialize(); |
| 34 | } |
| 35 | |
| 36 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 37 | Decl *ReadTopLevelDecl(); |
| 38 | |
| 39 | void PrintStats() const; |
| 40 | |
| 41 | ~ASTStreamer() { |
| 42 | P.Finalize(); |
| 43 | delete &P.getActions(); |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | /// ReadTopLevelDecl - Parse and return the next top-level declaration. |
| 49 | /// |
| 50 | Decl *ASTStreamer::ReadTopLevelDecl() { |
| 51 | Parser::DeclTy *Result; |
| 52 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | do { |
Steve Naroff | 89307ff | 2007-11-29 23:05:20 +0000 | [diff] [blame^] | 54 | if (P.ParseTopLevelDecl(Result)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | return 0; // End of file. |
| 56 | |
| 57 | // If we got a null return and something *was* parsed, try again. This |
| 58 | // is due to a top-level semicolon, an action override, or a parse error |
| 59 | // skipping something. |
Steve Naroff | 89307ff | 2007-11-29 23:05:20 +0000 | [diff] [blame^] | 60 | } while (Result == 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | |
| 62 | return static_cast<Decl*>(Result); |
| 63 | } |
| 64 | |
| 65 | void ASTStreamer::PrintStats() const { |
Steve Naroff | 58ff9e8 | 2007-10-14 00:58:41 +0000 | [diff] [blame] | 66 | P.getActions().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // Public interface to the file |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 73 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 74 | /// the file is parsed. This takes ownership of the ASTConsumer and |
| 75 | /// ultimately deletes it. |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 76 | void clang::ParseAST(Preprocessor &PP, unsigned MainFileID, |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 77 | ASTConsumer *Consumer, bool PrintStats) { |
Chris Lattner | 556beb7 | 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 | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 85 | PP.getIdentifierTable(), PP.getSelectorTable()); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 86 | |
| 87 | ASTStreamer Streamer(PP, Context, MainFileID); |
| 88 | |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 89 | Consumer->Initialize(Context, MainFileID); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 90 | |
| 91 | while (Decl *D = Streamer.ReadTopLevelDecl()) |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 92 | Consumer->HandleTopLevelDecl(D); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 556beb7 | 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 | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 100 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 101 | |
| 102 | Decl::CollectingStats(false); |
| 103 | Stmt::CollectingStats(false); |
| 104 | } |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 105 | |
| 106 | delete Consumer; |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 107 | } |