blob: e78502f267d67bca6bc49401a081483c1c74fe7b [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) {
49 if (Killed.count(*ID)) continue;
50 Killed.insert(*ID);
51 (*ID)->Destroy(*Context);
52 }
53
Eli Friedman1108e7d2008-05-21 05:01:55 +000054 (*I)->Destroy(*Context);
55 }
Eli Friedman5f1adf82008-05-21 05:33:10 +000056 }
Eli Friedman1108e7d2008-05-21 05:01:55 +000057
Eli Friedman5f1adf82008-05-21 05:33:10 +000058 if (OwnsMetaData && Context) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000059 // The ASTContext object has the sole references to the IdentifierTable
60 // Selectors, and the Target information. Go and delete them, since
61 // the TranslationUnit effectively owns them.
Eli Friedman5f1adf82008-05-21 05:33:10 +000062
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000063 delete &(Context->Idents);
64 delete &(Context->Selectors);
65 delete &(Context->Target);
66 delete Context;
67 }
Sam Bishop71de20e2008-04-03 05:35:20 +000068}
Ted Kremenek63ea8632007-12-19 19:27:38 +000069
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000070bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
71 const llvm::sys::Path& Filename) {
72
73 return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
74}
75
Ted Kremenekdca29272007-12-18 21:44:50 +000076bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
77 const llvm::sys::Path& Filename) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000078
Ted Kremenek2f743592007-12-05 21:36:08 +000079 // Reserve 256K for bitstream buffer.
80 std::vector<unsigned char> Buffer;
81 Buffer.reserve(256*1024);
82
83 // Create bitstream.
84 llvm::BitstreamWriter Stream(Buffer);
85
86 // Emit the preamble.
87 Stream.Emit((unsigned)'B', 8);
88 Stream.Emit((unsigned)'C', 8);
89 Stream.Emit(0xC, 4);
90 Stream.Emit(0xF, 4);
91 Stream.Emit(0xE, 4);
92 Stream.Emit(0x0, 4);
93
94 {
95 // Create serializer. Placing it in its own scope assures any necessary
96 // finalization of bits to the buffer in the serializer's dstor.
97 llvm::Serializer Sezr(Stream);
98
99 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +0000100 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000101 }
102
103 // Write the bits to disk.
104 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
105 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
106 fclose(fp);
107 return true;
108 }
109
110 return false;
111}
112
113void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
114
115 // ===---------------------------------------------------===/
116 // Serialize the top-level decls.
117 // ===---------------------------------------------------===/
118
119 Sezr.EnterBlock(DeclsBlock);
120
121 // Only serialize the head of a decl chain. The ASTConsumer interfaces
122 // provides us with each top-level decl, including those nested in
123 // a decl chain, so we may be passed decls that are already serialized.
124 for (const_iterator I=begin(), E=end(); I!=E; ++I)
125 if (!Sezr.isRegistered(*I))
126 Sezr.EmitOwnedPtr(*I);
127
128 Sezr.ExitBlock();
129
130 // ===---------------------------------------------------===/
131 // Serialize the "Translation Unit" metadata.
132 // ===---------------------------------------------------===/
133
134 // Emit ASTContext.
135 Sezr.EnterBlock(ASTContextBlock);
136 Sezr.EmitOwnedPtr(Context);
137 Sezr.ExitBlock();
138
139 Sezr.EnterBlock(BasicMetadataBlock);
140
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000141 // Block for SourceManager and Target. Allows easy skipping
Ted Kremenek2f743592007-12-05 21:36:08 +0000142 // around to the block for the Selectors during deserialization.
143 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000144
Ted Kremenek2f743592007-12-05 21:36:08 +0000145 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000146 Sezr.Emit(Context->getSourceManager());
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000147
Ted Kremenek2f743592007-12-05 21:36:08 +0000148 // Emit the Target.
149 Sezr.EmitPtr(&Context->Target);
150 Sezr.EmitCStr(Context->Target.getTargetTriple());
151
152 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
153
154 // Emit the Selectors.
155 Sezr.Emit(Context->Selectors);
156
157 // Emit the Identifier Table.
158 Sezr.Emit(Context->Idents);
159
160 Sezr.ExitBlock(); // exit "ASTContextBlock"
161}
162
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000163TranslationUnit*
Ted Kremenekdca29272007-12-18 21:44:50 +0000164clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000165
166 // Create the memory buffer that contains the contents of the file.
Ted Kremenekee533642007-12-20 19:47:16 +0000167 llvm::OwningPtr<llvm::MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000168 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
Ted Kremenek2f743592007-12-05 21:36:08 +0000169
170 if (!MBuffer) {
171 // FIXME: Provide diagnostic.
172 return NULL;
173 }
174
175 // Check if the file is of the proper length.
176 if (MBuffer->getBufferSize() & 0x3) {
177 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
178 return NULL;
179 }
180
181 // Create the bitstream reader.
182 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
183 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
184
185 if (Stream.Read(8) != 'B' ||
186 Stream.Read(8) != 'C' ||
187 Stream.Read(4) != 0xC ||
188 Stream.Read(4) != 0xF ||
189 Stream.Read(4) != 0xE ||
190 Stream.Read(4) != 0x0) {
191 // FIXME: Provide diagnostic.
192 return NULL;
193 }
194
195 // Create the deserializer.
196 llvm::Deserializer Dezr(Stream);
197
Ted Kremenekdca29272007-12-18 21:44:50 +0000198 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000199}
200
201TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
202 FileManager& FMgr) {
203
204 // Create the translation unit object.
205 TranslationUnit* TU = new TranslationUnit();
206
207 // ===---------------------------------------------------===/
208 // Deserialize the "Translation Unit" metadata.
209 // ===---------------------------------------------------===/
210
211 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
212 // (which will appear earlier) and record its location.
213
214 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
215 assert (FoundBlock);
216
217 llvm::Deserializer::Location ASTContextBlockLoc =
218 Dezr.getCurrentBlockLocation();
219
220 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
221 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000222
Ted Kremenek2f743592007-12-05 21:36:08 +0000223 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000224 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000225
Ted Kremenekbbced582007-12-12 18:05:32 +0000226 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000227 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
228 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000229 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000230 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000231 }
232
233 // For Selectors, we must read the identifier table first because the
234 // SelectorTable depends on the identifiers being already deserialized.
235 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
236 Dezr.SkipBlock();
237
238 // Read the identifier table.
239 IdentifierTable::CreateAndRegister(Dezr);
240
241 // Now jump back and read the selectors.
242 Dezr.JumpTo(SelectorBlkLoc);
243 SelectorTable::CreateAndRegister(Dezr);
244
245 // Now jump back to ASTContextBlock and read the ASTContext.
246 Dezr.JumpTo(ASTContextBlockLoc);
247 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
248
249 // "Rewind" the stream. Find the block with the serialized top-level decls.
250 Dezr.Rewind();
251 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
252 assert (FoundBlock);
253 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
254
255 while (!Dezr.FinishedBlock(DeclBlockLoc))
Sam Bishope2563ca2008-04-07 21:55:54 +0000256 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context));
Ted Kremenek2f743592007-12-05 21:36:08 +0000257
258 return TU;
259}
260