blob: 2e6c7f6b790b08099ed5ba1a89ffe03bf2c5be06 [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///
Chris Lattner2dacc3f2006-10-16 00:33:54 +000022Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo,
23 std::vector<Decl*> &LastInGroupList);
Chris Lattner73709ed2006-08-17 06:28:25 +000024
25
26namespace {
27 class ASTStreamer {
Chris Lattner73709ed2006-08-17 06:28:25 +000028 Parser P;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000029 std::vector<Decl*> LastInGroupList;
Chris Lattner73709ed2006-08-17 06:28:25 +000030 public:
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000031 ASTStreamer(Preprocessor &PP, unsigned MainFileID, bool FullLocInfo)
Chris Lattner2dacc3f2006-10-16 00:33:54 +000032 : P(PP, *CreateASTBuilderActions(PP, FullLocInfo, LastInGroupList)) {
Chris Lattner73709ed2006-08-17 06:28:25 +000033 PP.EnterSourceFile(MainFileID, 0, true);
34
Chris Lattner38ba3362006-08-17 07:04:37 +000035 // Initialize the parser.
36 P.Initialize();
Chris Lattner73709ed2006-08-17 06:28:25 +000037 }
38
39 /// ReadTopLevelDecl - Parse and return the next top-level declaration.
40 Decl *ReadTopLevelDecl() {
Chris Lattner38ba3362006-08-17 07:04:37 +000041 Parser::DeclTy *Result;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000042
43 /// If the previous time through we read something like 'int X, Y', return
44 /// the next declarator.
45 if (!LastInGroupList.empty()) {
46 Result = LastInGroupList.back();
47 LastInGroupList.pop_back();
48 return (Decl*)Result;
49 }
50
51 do {
52 if (P.ParseTopLevelDecl(Result))
53 return 0; // End of file.
54
55 // If we got a null return and something *was* parsed, try again. This
56 // is due to a top-level semicolon, an action override, or a parse error
57 // skipping something.
58 } while (Result == 0);
59
60 // If we parsed a declspec with multiple declarators, reverse the list and
61 // return the first one.
62 if (!LastInGroupList.empty()) {
63 LastInGroupList.push_back((Decl*)Result);
64 std::reverse(LastInGroupList.begin(), LastInGroupList.end());
65 Result = LastInGroupList.back();
66 LastInGroupList.pop_back();
67 }
68
Chris Lattner38ba3362006-08-17 07:04:37 +000069 return (Decl*)Result;
70 }
71
72 ~ASTStreamer() {
73 P.Finalize();
Chris Lattnerc11438c2006-08-18 05:17:52 +000074 delete &P.getActions();
Chris Lattner73709ed2006-08-17 06:28:25 +000075 }
76 };
77}
78
79
80
81//===----------------------------------------------------------------------===//
82// Public interface to the file
83//===----------------------------------------------------------------------===//
84
85/// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
86/// and FileID.
87ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &PP,
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000088 unsigned MainFileID,
89 bool FullLocInfo) {
90 return new ASTStreamer(PP, MainFileID, FullLocInfo);
Chris Lattner73709ed2006-08-17 06:28:25 +000091}
92
93/// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This
94/// returns null at end of file.
95Decl *llvm::clang::ASTStreamer_ReadTopLevelDecl(ASTStreamerTy *Streamer) {
96 return static_cast<ASTStreamer*>(Streamer)->ReadTopLevelDecl();
97}
98
99/// ASTStreamer_Terminate - Gracefully shut down the streamer.
100///
101void llvm::clang::ASTStreamer_Terminate(ASTStreamerTy *Streamer) {
102 delete static_cast<ASTStreamer*>(Streamer);
103}