blob: 49689e9c2c84996562eefdfeddc705d9fca969a3 [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"
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000016#include "clang/Parse/Action.h"
17#include "clang/Parse/Parser.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000018using namespace llvm;
19using namespace clang;
20
Chris Lattner73709ed2006-08-17 06:28:25 +000021namespace {
22 class ASTStreamer {
Chris Lattner73709ed2006-08-17 06:28:25 +000023 Parser P;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000024 std::vector<Decl*> LastInGroupList;
Chris Lattner73709ed2006-08-17 06:28:25 +000025 public:
Chris Lattner72b7d392006-11-04 06:37:16 +000026 ASTStreamer(Preprocessor &PP, unsigned MainFileID)
Chris Lattnercc67ec12006-11-09 06:54:47 +000027 : P(PP, *new Sema(PP, LastInGroupList)) {
Chris Lattner73709ed2006-08-17 06:28:25 +000028 PP.EnterSourceFile(MainFileID, 0, true);
29
Chris Lattner38ba3362006-08-17 07:04:37 +000030 // Initialize the parser.
31 P.Initialize();
Chris Lattner73709ed2006-08-17 06:28:25 +000032 }
33
34 /// ReadTopLevelDecl - Parse and return the next top-level declaration.
35 Decl *ReadTopLevelDecl() {
Chris Lattner38ba3362006-08-17 07:04:37 +000036 Parser::DeclTy *Result;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000037
38 /// If the previous time through we read something like 'int X, Y', return
39 /// the next declarator.
40 if (!LastInGroupList.empty()) {
41 Result = LastInGroupList.back();
42 LastInGroupList.pop_back();
43 return (Decl*)Result;
44 }
45
46 do {
47 if (P.ParseTopLevelDecl(Result))
48 return 0; // End of file.
49
50 // If we got a null return and something *was* parsed, try again. This
51 // is due to a top-level semicolon, an action override, or a parse error
52 // skipping something.
53 } while (Result == 0);
54
55 // If we parsed a declspec with multiple declarators, reverse the list and
56 // return the first one.
57 if (!LastInGroupList.empty()) {
58 LastInGroupList.push_back((Decl*)Result);
59 std::reverse(LastInGroupList.begin(), LastInGroupList.end());
60 Result = LastInGroupList.back();
61 LastInGroupList.pop_back();
62 }
63
Chris Lattner38ba3362006-08-17 07:04:37 +000064 return (Decl*)Result;
65 }
66
67 ~ASTStreamer() {
68 P.Finalize();
Chris Lattnerc11438c2006-08-18 05:17:52 +000069 delete &P.getActions();
Chris Lattner73709ed2006-08-17 06:28:25 +000070 }
71 };
72}
73
74
75
76//===----------------------------------------------------------------------===//
77// Public interface to the file
78//===----------------------------------------------------------------------===//
79
80/// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
81/// and FileID.
82ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &PP,
Chris Lattner72b7d392006-11-04 06:37:16 +000083 unsigned MainFileID) {
84 return new ASTStreamer(PP, MainFileID);
Chris Lattner73709ed2006-08-17 06:28:25 +000085}
86
87/// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This
88/// returns null at end of file.
89Decl *llvm::clang::ASTStreamer_ReadTopLevelDecl(ASTStreamerTy *Streamer) {
90 return static_cast<ASTStreamer*>(Streamer)->ReadTopLevelDecl();
91}
92
93/// ASTStreamer_Terminate - Gracefully shut down the streamer.
94///
95void llvm::clang::ASTStreamer_Terminate(ASTStreamerTy *Streamer) {
96 delete static_cast<ASTStreamer*>(Streamer);
97}