blob: 1057d93281899999e1dec0da04a0194327f8eda7 [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//
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"
16#include "Sema.h"
17#include "clang/Parse/Action.h"
18#include "clang/Parse/Parser.h"
19using namespace clang;
20
21namespace {
22 class ASTStreamer {
23 Parser P;
24 std::vector<Decl*> LastInGroupList;
25 public:
26 ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID)
27 : P(pp, *new Sema(pp, ctxt, LastInGroupList)) {
28 pp.EnterSourceFile(MainFileID, 0, true);
29
30 // Initialize the parser.
31 P.Initialize();
32 }
33
34 /// ReadTopLevelDecl - Parse and return the next top-level declaration.
35 Decl *ReadTopLevelDecl();
36
37 void PrintStats() const;
38
39 ~ASTStreamer() {
40 P.Finalize();
41 delete &P.getActions();
42 }
43 };
44}
45
46/// ReadTopLevelDecl - Parse and return the next top-level declaration.
47///
48Decl *ASTStreamer::ReadTopLevelDecl() {
49 Parser::DeclTy *Result;
50
51 /// If the previous time through we read something like 'int X, Y', return
52 /// the next declarator.
53 if (!LastInGroupList.empty()) {
54 Result = LastInGroupList.back();
55 LastInGroupList.pop_back();
56 return static_cast<Decl*>(Result);
57 }
58
59 do {
60 if (P.ParseTopLevelDecl(Result))
61 return 0; // End of file.
62
63 // If we got a null return and something *was* parsed, try again. This
64 // is due to a top-level semicolon, an action override, or a parse error
65 // skipping something.
66 } while (Result == 0);
67
68 // If we parsed a declspec with multiple declarators, reverse the list and
69 // return the first one.
70 if (!LastInGroupList.empty()) {
71 LastInGroupList.push_back((Decl*)Result);
72 std::reverse(LastInGroupList.begin(), LastInGroupList.end());
73 Result = LastInGroupList.back();
74 LastInGroupList.pop_back();
75 }
76
77 return static_cast<Decl*>(Result);
78}
79
80void ASTStreamer::PrintStats() const {
81}
82
83//===----------------------------------------------------------------------===//
84// Public interface to the file
85//===----------------------------------------------------------------------===//
86
87/// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
88/// and FileID.
89ASTStreamerTy *clang::ASTStreamer_Init(Preprocessor &pp, ASTContext &ctxt,
90 unsigned MainFileID) {
91 return new ASTStreamer(pp, ctxt, MainFileID);
92}
93
94/// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This
95/// returns null at end of file.
96Decl *clang::ASTStreamer_ReadTopLevelDecl(ASTStreamerTy *Streamer) {
97 return static_cast<ASTStreamer*>(Streamer)->ReadTopLevelDecl();
98}
99
100
101/// ASTStreamer_PrintStats - Emit statistic information to stderr.
102///
103void clang::ASTStreamer_PrintStats(ASTStreamerTy *Streamer) {
104 return static_cast<ASTStreamer*>(Streamer)->PrintStats();
105}
106
107/// ASTStreamer_Terminate - Gracefully shut down the streamer.
108///
109void clang::ASTStreamer_Terminate(ASTStreamerTy *Streamer) {
110 delete static_cast<ASTStreamer*>(Streamer);
111}