blob: 11ec72737e64de3ec004ea3badd70307bbb63add [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTStreamer.cpp - Provide streaming interface to ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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 Lattner1cc01712007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "Sema.h"
18#include "clang/Parse/Action.h"
19#include "clang/Parse/Parser.h"
20using namespace clang;
21
Chris Lattner1cc01712007-09-15 22:56:56 +000022ASTConsumer::~ASTConsumer() {}
23
Chris Lattner4b009652007-07-25 00:24:17 +000024namespace {
25 class ASTStreamer {
26 Parser P;
Chris Lattner4b009652007-07-25 00:24:17 +000027 public:
Ted Kremenek17861c52007-12-19 22:51:13 +000028 ASTStreamer(Preprocessor &pp, ASTContext &ctxt)
Steve Naroffca44ffd2007-11-29 23:05:20 +000029 : P(pp, *new Sema(pp, ctxt)) {
Ted Kremenek17861c52007-12-19 22:51:13 +000030
31 pp.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +000032
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///
51Decl *ASTStreamer::ReadTopLevelDecl() {
52 Parser::DeclTy *Result;
53
Chris Lattner4b009652007-07-25 00:24:17 +000054 do {
Steve Naroffca44ffd2007-11-29 23:05:20 +000055 if (P.ParseTopLevelDecl(Result))
Chris Lattner4b009652007-07-25 00:24:17 +000056 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 Naroffca44ffd2007-11-29 23:05:20 +000061 } while (Result == 0);
Chris Lattner4b009652007-07-25 00:24:17 +000062
63 return static_cast<Decl*>(Result);
64}
65
66void ASTStreamer::PrintStats() const {
Steve Naroff8255b042007-10-14 00:58:41 +000067 P.getActions().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
70//===----------------------------------------------------------------------===//
71// Public interface to the file
72//===----------------------------------------------------------------------===//
73
Chris Lattner1cc01712007-09-15 22:56:56 +000074/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner8593cbf2007-11-03 06:24:16 +000075/// the file is parsed. This takes ownership of the ASTConsumer and
76/// ultimately deletes it.
Ted Kremenek17861c52007-12-19 22:51:13 +000077void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner1cc01712007-09-15 22:56:56 +000078 // 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 Naroff4ed9d662007-09-27 14:38:14 +000085 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner1cc01712007-09-15 22:56:56 +000086
Ted Kremenek17861c52007-12-19 22:51:13 +000087 ASTStreamer Streamer(PP, Context);
Chris Lattner1cc01712007-09-15 22:56:56 +000088
Ted Kremenek17861c52007-12-19 22:51:13 +000089 Consumer->Initialize(Context);
Chris Lattner1cc01712007-09-15 22:56:56 +000090
91 while (Decl *D = Streamer.ReadTopLevelDecl())
Chris Lattner8593cbf2007-11-03 06:24:16 +000092 Consumer->HandleTopLevelDecl(D);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000093
Chris Lattner1cc01712007-09-15 22:56:56 +000094 if (PrintStats) {
95 fprintf(stderr, "\nSTATISTICS:\n");
96 Streamer.PrintStats();
97 Context.PrintStats();
98 Decl::PrintStats();
99 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +0000100 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +0000101
102 Decl::CollectingStats(false);
103 Stmt::CollectingStats(false);
104 }
Chris Lattner8593cbf2007-11-03 06:24:16 +0000105
106 delete Consumer;
Chris Lattner1cc01712007-09-15 22:56:56 +0000107}