blob: e1d952fd024ab43178aad462cce8c1873dbecc4e [file] [log] [blame]
Chris Lattner73709ed2006-08-17 06:28:25 +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/AST/ASTStreamer.h"
15#include "clang/Parse/Action.h"
16#include "clang/Parse/Parser.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000017using namespace llvm;
18using namespace clang;
19
Chris Lattnerc11438c2006-08-18 05:17:52 +000020/// Interface to the Builder.cpp file.
21///
22Action *CreateASTBuilderActions();
Chris Lattner73709ed2006-08-17 06:28:25 +000023
24
25namespace {
26 class ASTStreamer {
Chris Lattner73709ed2006-08-17 06:28:25 +000027 Parser P;
28 public:
29 ASTStreamer(Preprocessor &PP, unsigned MainFileID)
Chris Lattnerc11438c2006-08-18 05:17:52 +000030 : P(PP, *CreateASTBuilderActions()) {
Chris Lattner73709ed2006-08-17 06:28:25 +000031 PP.EnterSourceFile(MainFileID, 0, true);
32
Chris Lattner38ba3362006-08-17 07:04:37 +000033 // Initialize the parser.
34 P.Initialize();
Chris Lattner73709ed2006-08-17 06:28:25 +000035 }
36
37 /// ReadTopLevelDecl - Parse and return the next top-level declaration.
38 Decl *ReadTopLevelDecl() {
Chris Lattner38ba3362006-08-17 07:04:37 +000039 Parser::DeclTy *Result;
40 if (P.ParseTopLevelDecl(Result))
41 return 0;
42 Result = (Decl*)1; // FIXME!
43 return (Decl*)Result;
44 }
45
46 ~ASTStreamer() {
47 P.Finalize();
Chris Lattnerc11438c2006-08-18 05:17:52 +000048 delete &P.getActions();
Chris Lattner73709ed2006-08-17 06:28:25 +000049 }
50 };
51}
52
53
54
55//===----------------------------------------------------------------------===//
56// Public interface to the file
57//===----------------------------------------------------------------------===//
58
59/// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
60/// and FileID.
61ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &PP,
62 unsigned MainFileID) {
63 return new ASTStreamer(PP, MainFileID);
64}
65
66/// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This
67/// returns null at end of file.
68Decl *llvm::clang::ASTStreamer_ReadTopLevelDecl(ASTStreamerTy *Streamer) {
69 return static_cast<ASTStreamer*>(Streamer)->ReadTopLevelDecl();
70}
71
72/// ASTStreamer_Terminate - Gracefully shut down the streamer.
73///
74void llvm::clang::ASTStreamer_Terminate(ASTStreamerTy *Streamer) {
75 delete static_cast<ASTStreamer*>(Streamer);
76}