blob: b51874ca354c76a8b76c3a009142fa1a1f66f5ee [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- 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 Lattner556beb72007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "Sema.h"
18#include "clang/Parse/Action.h"
19#include "clang/Parse/Parser.h"
20using namespace clang;
21
Chris Lattner556beb72007-09-15 22:56:56 +000022ASTConsumer::~ASTConsumer() {}
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024namespace {
25 class ASTStreamer {
26 Parser P;
Reid Spencer5f016e22007-07-11 17:01:13 +000027 public:
28 ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID)
Steve Naroff89307ff2007-11-29 23:05:20 +000029 : P(pp, *new Sema(pp, ctxt)) {
Chris Lattner53b0dab2007-10-09 22:10:18 +000030 pp.EnterMainSourceFile(MainFileID);
Reid Spencer5f016e22007-07-11 17:01:13 +000031
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///
50Decl *ASTStreamer::ReadTopLevelDecl() {
51 Parser::DeclTy *Result;
52
Reid Spencer5f016e22007-07-11 17:01:13 +000053 do {
Steve Naroff89307ff2007-11-29 23:05:20 +000054 if (P.ParseTopLevelDecl(Result))
Reid Spencer5f016e22007-07-11 17:01:13 +000055 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 Naroff89307ff2007-11-29 23:05:20 +000060 } while (Result == 0);
Reid Spencer5f016e22007-07-11 17:01:13 +000061
62 return static_cast<Decl*>(Result);
63}
64
65void ASTStreamer::PrintStats() const {
Steve Naroff58ff9e82007-10-14 00:58:41 +000066 P.getActions().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +000067}
68
69//===----------------------------------------------------------------------===//
70// Public interface to the file
71//===----------------------------------------------------------------------===//
72
Chris Lattner556beb72007-09-15 22:56:56 +000073/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner31e6c7d2007-11-03 06:24:16 +000074/// the file is parsed. This takes ownership of the ASTConsumer and
75/// ultimately deletes it.
Chris Lattner556beb72007-09-15 22:56:56 +000076void clang::ParseAST(Preprocessor &PP, unsigned MainFileID,
Chris Lattner31e6c7d2007-11-03 06:24:16 +000077 ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner556beb72007-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 Naroff68d331a2007-09-27 14:38:14 +000085 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner556beb72007-09-15 22:56:56 +000086
87 ASTStreamer Streamer(PP, Context, MainFileID);
88
Chris Lattner31e6c7d2007-11-03 06:24:16 +000089 Consumer->Initialize(Context, MainFileID);
Chris Lattner556beb72007-09-15 22:56:56 +000090
91 while (Decl *D = Streamer.ReadTopLevelDecl())
Chris Lattner31e6c7d2007-11-03 06:24:16 +000092 Consumer->HandleTopLevelDecl(D);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000093
Chris Lattner556beb72007-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 Lattner31e6c7d2007-11-03 06:24:16 +0000100 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000101
102 Decl::CollectingStats(false);
103 Stmt::CollectingStats(false);
104 }
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000105
106 delete Consumer;
Chris Lattner556beb72007-09-15 22:56:56 +0000107}