blob: 659aa064de142edda52dfd77185d781462824e02 [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() {
Eli Friedman5f1adf82008-05-21 05:33:10 +000034 if (OwnsDecls) {
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);
Ted Kremenek1a726d72008-06-06 17:21:42 +000040
41 // FIXME: This is a horrible hack. Because there is no clear ownership
42 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
43 // reference, we need to destroy ObjCPropertyDecls here. This will
44 // eventually be fixed when the ownership of ObjCPropertyDecls gets
45 // cleaned up.
46 if (ObjCInterfaceDecl* IDecl = dyn_cast<ObjCInterfaceDecl>(*I))
47 for (ObjCInterfaceDecl::classprop_iterator ID=IDecl->classprop_begin(),
48 ED=IDecl->classprop_end(); ID!=ED; ++ID) {
Ted Kremenek1c8a4132008-06-06 19:48:57 +000049 if (!*ID || Killed.count(*ID)) continue;
Ted Kremenek1a726d72008-06-06 17:21:42 +000050 Killed.insert(*ID);
51 (*ID)->Destroy(*Context);
52 }
53
Ted Kremenek1c8a4132008-06-06 19:48:57 +000054 // FIXME: This is a horrible hack. Because there is no clear ownership
55 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
56 // reference, we need to destroy ObjCPropertyDecls here. This will
57 // eventually be fixed when the ownership of ObjCPropertyDecls gets
58 // cleaned up.
59 if (ObjCProtocolDecl* PDecl = dyn_cast<ObjCProtocolDecl>(*I))
Ted Kremenek400d95f2008-06-06 20:11:53 +000060 for (ObjCProtocolDecl::classprop_iterator ID=PDecl->classprop_begin(),
61 ED=PDecl->classprop_end(); ID!=ED; ++ID) {
62 if (!*ID || Killed.count(*ID)) continue;
63 Killed.insert(*ID);
64 (*ID)->Destroy(*Context);
Ted Kremenek1c8a4132008-06-06 19:48:57 +000065 }
Ted Kremenek400d95f2008-06-06 20:11:53 +000066
67 // FIXME: There is no clear ownership policy now for ObjCInterfaceDecls
68 // referenced by ObjCClassDecls. Some of them can be forward decls that
Ted Kremenek05ac3ef2008-06-06 21:05:33 +000069 // are never later defined (and forward decls can be referenced by
70 // multiple ObjCClassDecls) or the ObjCInterfaceDecl later
71 // becomes a real definition.
Ted Kremenek400d95f2008-06-06 20:11:53 +000072 // Ideally we should have separate objects for forward declarations and
73 // definitions, obviating this problem. Because of this situation,
74 // referenced ObjCInterfaceDecls are destroyed here.
75 if (ObjCClassDecl* CDecl = dyn_cast<ObjCClassDecl>(*I))
76 for (ObjCClassDecl::iterator ID=CDecl->begin(),
77 ED=CDecl->end(); ID!=ED; ++ID) {
78 if (!*ID || Killed.count(*ID)) continue;
79 Killed.insert(*ID);
80 (*ID)->Destroy(*Context);
81 }
Ted Kremenek05ac3ef2008-06-06 21:05:33 +000082
83 // FIXME: There is no clear ownership policy now for ObjCProtocolDecls
84 // referenced by ObjCForwardProtocolDecl. Some of them can be forward
85 // decls that are never later defined (and forward decls can be
86 // referenced by multiple ObjCClassDecls) or the ObjCProtocolDecl
87 // later becomes a real definition.
88 // Ideally we should have separate objects for forward declarations and
89 // definitions, obviating this problem. Because of this situation,
90 // referenced ObjCProtocolDecls are destroyed here.
91 if (ObjCForwardProtocolDecl* FDec = dyn_cast<ObjCForwardProtocolDecl>(*I))
92 for (ObjCForwardProtocolDecl::iterator ID=FDec->begin(),
93 ED=FDec->end(); ID!=ED; ++ID) {
94 if (!*ID || Killed.count(*ID)) continue;
95 Killed.insert(*ID);
96 (*ID)->Destroy(*Context);
97 }
98
Ted Kremenek400d95f2008-06-06 20:11:53 +000099
Eli Friedman1108e7d2008-05-21 05:01:55 +0000100 (*I)->Destroy(*Context);
101 }
Eli Friedman5f1adf82008-05-21 05:33:10 +0000102 }
Eli Friedman1108e7d2008-05-21 05:01:55 +0000103
Eli Friedman5f1adf82008-05-21 05:33:10 +0000104 if (OwnsMetaData && Context) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000105 // The ASTContext object has the sole references to the IdentifierTable
106 // Selectors, and the Target information. Go and delete them, since
107 // the TranslationUnit effectively owns them.
Eli Friedman5f1adf82008-05-21 05:33:10 +0000108
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000109 delete &(Context->Idents);
110 delete &(Context->Selectors);
111 delete &(Context->Target);
112 delete Context;
113 }
Sam Bishop71de20e2008-04-03 05:35:20 +0000114}
Ted Kremenek63ea8632007-12-19 19:27:38 +0000115
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000116bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
117 const llvm::sys::Path& Filename) {
118
119 return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
120}
121
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000122bool clang::EmitASTBitcodeBuffer(const TranslationUnit* TU,
123 std::vector<unsigned char>& Buffer) {
124
125 return TU ? EmitASTBitcodeBuffer(*TU, Buffer) : false;
126}
127
128bool clang::EmitASTBitcodeStream(const TranslationUnit* TU,
129 std::ostream& Stream) {
130
131 return TU ? EmitASTBitcodeStream(*TU, Stream) : false;
132}
133
134bool clang::EmitASTBitcodeBuffer(const TranslationUnit& TU,
135 std::vector<unsigned char>& Buffer) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000136 // Create bitstream.
137 llvm::BitstreamWriter Stream(Buffer);
138
139 // Emit the preamble.
140 Stream.Emit((unsigned)'B', 8);
141 Stream.Emit((unsigned)'C', 8);
142 Stream.Emit(0xC, 4);
143 Stream.Emit(0xF, 4);
144 Stream.Emit(0xE, 4);
145 Stream.Emit(0x0, 4);
146
147 {
148 // Create serializer. Placing it in its own scope assures any necessary
149 // finalization of bits to the buffer in the serializer's dstor.
150 llvm::Serializer Sezr(Stream);
151
152 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +0000153 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000154 }
155
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000156 return true;
157}
158
159bool clang::EmitASTBitcodeStream(const TranslationUnit& TU,
160 std::ostream& Stream) {
161
162 // Reserve 256K for bitstream buffer.
163 std::vector<unsigned char> Buffer;
164 Buffer.reserve(256*1024);
165
166 EmitASTBitcodeBuffer(TU,Buffer);
167
168 // Write the bits to disk.
169 Stream.write((char*)&Buffer.front(), Buffer.size());
170 return true;
171}
172
173bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
174 const llvm::sys::Path& Filename) {
175
176 // Reserve 256K for bitstream buffer.
177 std::vector<unsigned char> Buffer;
178 Buffer.reserve(256*1024);
179
180 EmitASTBitcodeBuffer(TU,Buffer);
181
Ted Kremenek2f743592007-12-05 21:36:08 +0000182 // Write the bits to disk.
183 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
184 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
185 fclose(fp);
186 return true;
187 }
188
189 return false;
190}
191
192void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
193
194 // ===---------------------------------------------------===/
195 // Serialize the top-level decls.
196 // ===---------------------------------------------------===/
197
198 Sezr.EnterBlock(DeclsBlock);
199
200 // Only serialize the head of a decl chain. The ASTConsumer interfaces
201 // provides us with each top-level decl, including those nested in
202 // a decl chain, so we may be passed decls that are already serialized.
203 for (const_iterator I=begin(), E=end(); I!=E; ++I)
204 if (!Sezr.isRegistered(*I))
205 Sezr.EmitOwnedPtr(*I);
206
207 Sezr.ExitBlock();
208
209 // ===---------------------------------------------------===/
210 // Serialize the "Translation Unit" metadata.
211 // ===---------------------------------------------------===/
212
213 // Emit ASTContext.
214 Sezr.EnterBlock(ASTContextBlock);
215 Sezr.EmitOwnedPtr(Context);
216 Sezr.ExitBlock();
217
218 Sezr.EnterBlock(BasicMetadataBlock);
219
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000220 // Block for SourceManager and Target. Allows easy skipping
Ted Kremenek2f743592007-12-05 21:36:08 +0000221 // around to the block for the Selectors during deserialization.
222 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000223
Ted Kremenek2f743592007-12-05 21:36:08 +0000224 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000225 Sezr.Emit(Context->getSourceManager());
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000226
Ted Kremenek2f743592007-12-05 21:36:08 +0000227 // Emit the Target.
228 Sezr.EmitPtr(&Context->Target);
229 Sezr.EmitCStr(Context->Target.getTargetTriple());
230
231 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
232
233 // Emit the Selectors.
234 Sezr.Emit(Context->Selectors);
235
236 // Emit the Identifier Table.
237 Sezr.Emit(Context->Idents);
238
239 Sezr.ExitBlock(); // exit "ASTContextBlock"
240}
241
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000242TranslationUnit*
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000243clang::ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer, FileManager& FMgr) {
244
Ted Kremenek2f743592007-12-05 21:36:08 +0000245 // Check if the file is of the proper length.
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000246 if (MBuffer.getBufferSize() & 0x3) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000247 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
248 return NULL;
249 }
250
251 // Create the bitstream reader.
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000252 unsigned char *BufPtr = (unsigned char *) MBuffer.getBufferStart();
253 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer.getBufferSize());
Ted Kremenek2f743592007-12-05 21:36:08 +0000254
255 if (Stream.Read(8) != 'B' ||
256 Stream.Read(8) != 'C' ||
257 Stream.Read(4) != 0xC ||
258 Stream.Read(4) != 0xF ||
259 Stream.Read(4) != 0xE ||
260 Stream.Read(4) != 0x0) {
261 // FIXME: Provide diagnostic.
262 return NULL;
263 }
264
265 // Create the deserializer.
266 llvm::Deserializer Dezr(Stream);
267
Ted Kremenekdca29272007-12-18 21:44:50 +0000268 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000269}
270
Ted Kremenek0ce902b2008-07-10 22:10:48 +0000271TranslationUnit*
272clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
273
274 // Create the memory buffer that contains the contents of the file.
275 llvm::OwningPtr<llvm::MemoryBuffer>
276 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
277
278 if (!MBuffer) {
279 // FIXME: Provide diagnostic.
280 return NULL;
281 }
282
283 return ReadASTBitcodeBuffer(*MBuffer, FMgr);
284}
285
Ted Kremenek2f743592007-12-05 21:36:08 +0000286TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
287 FileManager& FMgr) {
288
289 // Create the translation unit object.
290 TranslationUnit* TU = new TranslationUnit();
291
292 // ===---------------------------------------------------===/
293 // Deserialize the "Translation Unit" metadata.
294 // ===---------------------------------------------------===/
295
296 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
297 // (which will appear earlier) and record its location.
298
299 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
300 assert (FoundBlock);
301
302 llvm::Deserializer::Location ASTContextBlockLoc =
303 Dezr.getCurrentBlockLocation();
304
305 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
306 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000307
Ted Kremenek2f743592007-12-05 21:36:08 +0000308 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000309 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000310
Ted Kremenekbbced582007-12-12 18:05:32 +0000311 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000312 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
313 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000314 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000315 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000316 }
317
318 // For Selectors, we must read the identifier table first because the
319 // SelectorTable depends on the identifiers being already deserialized.
320 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
321 Dezr.SkipBlock();
322
323 // Read the identifier table.
324 IdentifierTable::CreateAndRegister(Dezr);
325
326 // Now jump back and read the selectors.
327 Dezr.JumpTo(SelectorBlkLoc);
328 SelectorTable::CreateAndRegister(Dezr);
329
330 // Now jump back to ASTContextBlock and read the ASTContext.
331 Dezr.JumpTo(ASTContextBlockLoc);
332 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
333
334 // "Rewind" the stream. Find the block with the serialized top-level decls.
335 Dezr.Rewind();
336 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
337 assert (FoundBlock);
338 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
339
340 while (!Dezr.FinishedBlock(DeclBlockLoc))
Sam Bishope2563ca2008-04-07 21:55:54 +0000341 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context));
Ted Kremenek2f743592007-12-05 21:36:08 +0000342
343 return TU;
344}
345