blob: a6836257890a3c6d27247043e92c10d3affecffc [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))
60 for (ObjCInterfaceDecl::classprop_iterator PD=PDecl->classprop_begin(),
61 ED=PDecl->classprop_end(); PD!=ED; ++PD) {
62 if (!*PD || Killed.count(*PD)) continue;
63 Killed.insert(*PD);
64 (*PD)->Destroy(*Context);
65 }
66
Eli Friedman1108e7d2008-05-21 05:01:55 +000067 (*I)->Destroy(*Context);
68 }
Eli Friedman5f1adf82008-05-21 05:33:10 +000069 }
Eli Friedman1108e7d2008-05-21 05:01:55 +000070
Eli Friedman5f1adf82008-05-21 05:33:10 +000071 if (OwnsMetaData && Context) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000072 // The ASTContext object has the sole references to the IdentifierTable
73 // Selectors, and the Target information. Go and delete them, since
74 // the TranslationUnit effectively owns them.
Eli Friedman5f1adf82008-05-21 05:33:10 +000075
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000076 delete &(Context->Idents);
77 delete &(Context->Selectors);
78 delete &(Context->Target);
79 delete Context;
80 }
Sam Bishop71de20e2008-04-03 05:35:20 +000081}
Ted Kremenek63ea8632007-12-19 19:27:38 +000082
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000083bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
84 const llvm::sys::Path& Filename) {
85
86 return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
87}
88
Ted Kremenekdca29272007-12-18 21:44:50 +000089bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
90 const llvm::sys::Path& Filename) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000091
Ted Kremenek2f743592007-12-05 21:36:08 +000092 // Reserve 256K for bitstream buffer.
93 std::vector<unsigned char> Buffer;
94 Buffer.reserve(256*1024);
95
96 // Create bitstream.
97 llvm::BitstreamWriter Stream(Buffer);
98
99 // Emit the preamble.
100 Stream.Emit((unsigned)'B', 8);
101 Stream.Emit((unsigned)'C', 8);
102 Stream.Emit(0xC, 4);
103 Stream.Emit(0xF, 4);
104 Stream.Emit(0xE, 4);
105 Stream.Emit(0x0, 4);
106
107 {
108 // Create serializer. Placing it in its own scope assures any necessary
109 // finalization of bits to the buffer in the serializer's dstor.
110 llvm::Serializer Sezr(Stream);
111
112 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +0000113 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000114 }
115
116 // Write the bits to disk.
117 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
118 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
119 fclose(fp);
120 return true;
121 }
122
123 return false;
124}
125
126void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
127
128 // ===---------------------------------------------------===/
129 // Serialize the top-level decls.
130 // ===---------------------------------------------------===/
131
132 Sezr.EnterBlock(DeclsBlock);
133
134 // Only serialize the head of a decl chain. The ASTConsumer interfaces
135 // provides us with each top-level decl, including those nested in
136 // a decl chain, so we may be passed decls that are already serialized.
137 for (const_iterator I=begin(), E=end(); I!=E; ++I)
138 if (!Sezr.isRegistered(*I))
139 Sezr.EmitOwnedPtr(*I);
140
141 Sezr.ExitBlock();
142
143 // ===---------------------------------------------------===/
144 // Serialize the "Translation Unit" metadata.
145 // ===---------------------------------------------------===/
146
147 // Emit ASTContext.
148 Sezr.EnterBlock(ASTContextBlock);
149 Sezr.EmitOwnedPtr(Context);
150 Sezr.ExitBlock();
151
152 Sezr.EnterBlock(BasicMetadataBlock);
153
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000154 // Block for SourceManager and Target. Allows easy skipping
Ted Kremenek2f743592007-12-05 21:36:08 +0000155 // around to the block for the Selectors during deserialization.
156 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000157
Ted Kremenek2f743592007-12-05 21:36:08 +0000158 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000159 Sezr.Emit(Context->getSourceManager());
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000160
Ted Kremenek2f743592007-12-05 21:36:08 +0000161 // Emit the Target.
162 Sezr.EmitPtr(&Context->Target);
163 Sezr.EmitCStr(Context->Target.getTargetTriple());
164
165 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
166
167 // Emit the Selectors.
168 Sezr.Emit(Context->Selectors);
169
170 // Emit the Identifier Table.
171 Sezr.Emit(Context->Idents);
172
173 Sezr.ExitBlock(); // exit "ASTContextBlock"
174}
175
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000176TranslationUnit*
Ted Kremenekdca29272007-12-18 21:44:50 +0000177clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000178
179 // Create the memory buffer that contains the contents of the file.
Ted Kremenekee533642007-12-20 19:47:16 +0000180 llvm::OwningPtr<llvm::MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000181 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
Ted Kremenek2f743592007-12-05 21:36:08 +0000182
183 if (!MBuffer) {
184 // FIXME: Provide diagnostic.
185 return NULL;
186 }
187
188 // Check if the file is of the proper length.
189 if (MBuffer->getBufferSize() & 0x3) {
190 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
191 return NULL;
192 }
193
194 // Create the bitstream reader.
195 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
196 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
197
198 if (Stream.Read(8) != 'B' ||
199 Stream.Read(8) != 'C' ||
200 Stream.Read(4) != 0xC ||
201 Stream.Read(4) != 0xF ||
202 Stream.Read(4) != 0xE ||
203 Stream.Read(4) != 0x0) {
204 // FIXME: Provide diagnostic.
205 return NULL;
206 }
207
208 // Create the deserializer.
209 llvm::Deserializer Dezr(Stream);
210
Ted Kremenekdca29272007-12-18 21:44:50 +0000211 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000212}
213
214TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
215 FileManager& FMgr) {
216
217 // Create the translation unit object.
218 TranslationUnit* TU = new TranslationUnit();
219
220 // ===---------------------------------------------------===/
221 // Deserialize the "Translation Unit" metadata.
222 // ===---------------------------------------------------===/
223
224 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
225 // (which will appear earlier) and record its location.
226
227 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
228 assert (FoundBlock);
229
230 llvm::Deserializer::Location ASTContextBlockLoc =
231 Dezr.getCurrentBlockLocation();
232
233 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
234 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000235
Ted Kremenek2f743592007-12-05 21:36:08 +0000236 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000237 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000238
Ted Kremenekbbced582007-12-12 18:05:32 +0000239 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000240 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
241 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000242 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000243 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000244 }
245
246 // For Selectors, we must read the identifier table first because the
247 // SelectorTable depends on the identifiers being already deserialized.
248 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
249 Dezr.SkipBlock();
250
251 // Read the identifier table.
252 IdentifierTable::CreateAndRegister(Dezr);
253
254 // Now jump back and read the selectors.
255 Dezr.JumpTo(SelectorBlkLoc);
256 SelectorTable::CreateAndRegister(Dezr);
257
258 // Now jump back to ASTContextBlock and read the ASTContext.
259 Dezr.JumpTo(ASTContextBlockLoc);
260 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
261
262 // "Rewind" the stream. Find the block with the serialized top-level decls.
263 Dezr.Rewind();
264 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
265 assert (FoundBlock);
266 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
267
268 while (!Dezr.FinishedBlock(DeclBlockLoc))
Sam Bishope2563ca2008-04-07 21:55:54 +0000269 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context));
Ted Kremenek2f743592007-12-05 21:36:08 +0000270
271 return TU;
272}
273