blob: bfe6ee27fbe5bdb543080faecd5d1ae9713a7b2b [file] [log] [blame]
Ted Kremenekcc927092007-12-13 17:54:02 +00001//===--- TranslationUnit.cpp - Abstraction for Translation Units ----------===//
Ted Kremenek2f743592007-12-05 21:36:08 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek2f743592007-12-05 21:36:08 +00007//
8// FIXME: This should eventually be moved out of the driver, or replaced
9// with its eventual successor.
10//
11//===----------------------------------------------------------------------===//
12
Ted Kremenek525fdbc2007-12-18 21:36:21 +000013#include "clang/AST/TranslationUnit.h"
Ted Kremenek2f743592007-12-05 21:36:08 +000014
15#include "clang/Basic/TargetInfo.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/AST/AST.h"
18
19#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/System/Path.h"
Ted Kremenek2f743592007-12-05 21:36:08 +000023
Ted Kremenek27f8a282008-05-20 00:43:19 +000024#include "llvm/ADT/OwningPtr.h"
25#include "llvm/ADT/DenseSet.h"
Ted Kremenek2f743592007-12-05 21:36:08 +000026
Ted Kremenek2f743592007-12-05 21:36:08 +000027using namespace clang;
28
Ted Kremenek63ea8632007-12-19 19:27:38 +000029enum { BasicMetadataBlock = 1,
30 ASTContextBlock = 2,
31 DeclsBlock = 3 };
32
Sam Bishop0f9c72f2008-04-03 14:20:28 +000033TranslationUnit::~TranslationUnit() {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000034 if (OwnsMetaData && Context) {
Eli Friedman1108e7d2008-05-21 05:01:55 +000035 llvm::DenseSet<Decl*> Killed;
36 for (iterator I=begin(), E=end(); I!=E; ++I) {
37 if (Killed.count(*I)) continue;
38
39 Killed.insert(*I);
40 (*I)->Destroy(*Context);
41 }
42
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000043 // The ASTContext object has the sole references to the IdentifierTable
44 // Selectors, and the Target information. Go and delete them, since
45 // the TranslationUnit effectively owns them.
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000046 delete &(Context->Idents);
47 delete &(Context->Selectors);
48 delete &(Context->Target);
49 delete Context;
50 }
Sam Bishop71de20e2008-04-03 05:35:20 +000051}
Ted Kremenek63ea8632007-12-19 19:27:38 +000052
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000053bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
54 const llvm::sys::Path& Filename) {
55
56 return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
57}
58
Ted Kremenekdca29272007-12-18 21:44:50 +000059bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
60 const llvm::sys::Path& Filename) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000061
Ted Kremenek2f743592007-12-05 21:36:08 +000062 // Reserve 256K for bitstream buffer.
63 std::vector<unsigned char> Buffer;
64 Buffer.reserve(256*1024);
65
66 // Create bitstream.
67 llvm::BitstreamWriter Stream(Buffer);
68
69 // Emit the preamble.
70 Stream.Emit((unsigned)'B', 8);
71 Stream.Emit((unsigned)'C', 8);
72 Stream.Emit(0xC, 4);
73 Stream.Emit(0xF, 4);
74 Stream.Emit(0xE, 4);
75 Stream.Emit(0x0, 4);
76
77 {
78 // Create serializer. Placing it in its own scope assures any necessary
79 // finalization of bits to the buffer in the serializer's dstor.
80 llvm::Serializer Sezr(Stream);
81
82 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +000083 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +000084 }
85
86 // Write the bits to disk.
87 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
88 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
89 fclose(fp);
90 return true;
91 }
92
93 return false;
94}
95
96void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
97
98 // ===---------------------------------------------------===/
99 // Serialize the top-level decls.
100 // ===---------------------------------------------------===/
101
102 Sezr.EnterBlock(DeclsBlock);
103
104 // Only serialize the head of a decl chain. The ASTConsumer interfaces
105 // provides us with each top-level decl, including those nested in
106 // a decl chain, so we may be passed decls that are already serialized.
107 for (const_iterator I=begin(), E=end(); I!=E; ++I)
108 if (!Sezr.isRegistered(*I))
109 Sezr.EmitOwnedPtr(*I);
110
111 Sezr.ExitBlock();
112
113 // ===---------------------------------------------------===/
114 // Serialize the "Translation Unit" metadata.
115 // ===---------------------------------------------------===/
116
117 // Emit ASTContext.
118 Sezr.EnterBlock(ASTContextBlock);
119 Sezr.EmitOwnedPtr(Context);
120 Sezr.ExitBlock();
121
122 Sezr.EnterBlock(BasicMetadataBlock);
123
124 // Block for SourceManager, LangOptions, and Target. Allows easy skipping
125 // around to the block for the Selectors during deserialization.
126 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000127
Ted Kremenek2f743592007-12-05 21:36:08 +0000128 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000129 Sezr.Emit(Context->getSourceManager());
Ted Kremenek2f743592007-12-05 21:36:08 +0000130
131 // Emit the LangOptions.
132 Sezr.Emit(LangOpts);
133
134 // Emit the Target.
135 Sezr.EmitPtr(&Context->Target);
136 Sezr.EmitCStr(Context->Target.getTargetTriple());
137
138 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
139
140 // Emit the Selectors.
141 Sezr.Emit(Context->Selectors);
142
143 // Emit the Identifier Table.
144 Sezr.Emit(Context->Idents);
145
146 Sezr.ExitBlock(); // exit "ASTContextBlock"
147}
148
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000149TranslationUnit*
Ted Kremenekdca29272007-12-18 21:44:50 +0000150clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000151
152 // Create the memory buffer that contains the contents of the file.
Ted Kremenekee533642007-12-20 19:47:16 +0000153 llvm::OwningPtr<llvm::MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000154 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
Ted Kremenek2f743592007-12-05 21:36:08 +0000155
156 if (!MBuffer) {
157 // FIXME: Provide diagnostic.
158 return NULL;
159 }
160
161 // Check if the file is of the proper length.
162 if (MBuffer->getBufferSize() & 0x3) {
163 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
164 return NULL;
165 }
166
167 // Create the bitstream reader.
168 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
169 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
170
171 if (Stream.Read(8) != 'B' ||
172 Stream.Read(8) != 'C' ||
173 Stream.Read(4) != 0xC ||
174 Stream.Read(4) != 0xF ||
175 Stream.Read(4) != 0xE ||
176 Stream.Read(4) != 0x0) {
177 // FIXME: Provide diagnostic.
178 return NULL;
179 }
180
181 // Create the deserializer.
182 llvm::Deserializer Dezr(Stream);
183
Ted Kremenekdca29272007-12-18 21:44:50 +0000184 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000185}
186
187TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
188 FileManager& FMgr) {
189
190 // Create the translation unit object.
191 TranslationUnit* TU = new TranslationUnit();
192
193 // ===---------------------------------------------------===/
194 // Deserialize the "Translation Unit" metadata.
195 // ===---------------------------------------------------===/
196
197 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
198 // (which will appear earlier) and record its location.
199
200 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
201 assert (FoundBlock);
202
203 llvm::Deserializer::Location ASTContextBlockLoc =
204 Dezr.getCurrentBlockLocation();
205
206 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
207 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000208
Ted Kremenek2f743592007-12-05 21:36:08 +0000209 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000210 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000211
212 // Read the LangOptions.
213 TU->LangOpts.Read(Dezr);
214
Ted Kremenekbbced582007-12-12 18:05:32 +0000215 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000216 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
217 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000218 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000219 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000220 }
221
222 // For Selectors, we must read the identifier table first because the
223 // SelectorTable depends on the identifiers being already deserialized.
224 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
225 Dezr.SkipBlock();
226
227 // Read the identifier table.
228 IdentifierTable::CreateAndRegister(Dezr);
229
230 // Now jump back and read the selectors.
231 Dezr.JumpTo(SelectorBlkLoc);
232 SelectorTable::CreateAndRegister(Dezr);
233
234 // Now jump back to ASTContextBlock and read the ASTContext.
235 Dezr.JumpTo(ASTContextBlockLoc);
236 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
237
238 // "Rewind" the stream. Find the block with the serialized top-level decls.
239 Dezr.Rewind();
240 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
241 assert (FoundBlock);
242 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
243
244 while (!Dezr.FinishedBlock(DeclBlockLoc))
Sam Bishope2563ca2008-04-07 21:55:54 +0000245 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context));
Ted Kremenek2f743592007-12-05 21:36:08 +0000246
247 return TU;
248}
249