blob: f8416f18fa1d7aa888659c6e68f2364a03f760da [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);
40 (*I)->Destroy(*Context);
41 }
Eli Friedman5f1adf82008-05-21 05:33:10 +000042 }
Eli Friedman1108e7d2008-05-21 05:01:55 +000043
Eli Friedman5f1adf82008-05-21 05:33:10 +000044 if (OwnsMetaData && Context) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000045 // The ASTContext object has the sole references to the IdentifierTable
46 // Selectors, and the Target information. Go and delete them, since
47 // the TranslationUnit effectively owns them.
Eli Friedman5f1adf82008-05-21 05:33:10 +000048
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000049 delete &(Context->Idents);
50 delete &(Context->Selectors);
51 delete &(Context->Target);
52 delete Context;
53 }
Sam Bishop71de20e2008-04-03 05:35:20 +000054}
Ted Kremenek63ea8632007-12-19 19:27:38 +000055
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000056bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
57 const llvm::sys::Path& Filename) {
58
59 return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
60}
61
Ted Kremenekdca29272007-12-18 21:44:50 +000062bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
63 const llvm::sys::Path& Filename) {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000064
Ted Kremenek2f743592007-12-05 21:36:08 +000065 // Reserve 256K for bitstream buffer.
66 std::vector<unsigned char> Buffer;
67 Buffer.reserve(256*1024);
68
69 // Create bitstream.
70 llvm::BitstreamWriter Stream(Buffer);
71
72 // Emit the preamble.
73 Stream.Emit((unsigned)'B', 8);
74 Stream.Emit((unsigned)'C', 8);
75 Stream.Emit(0xC, 4);
76 Stream.Emit(0xF, 4);
77 Stream.Emit(0xE, 4);
78 Stream.Emit(0x0, 4);
79
80 {
81 // Create serializer. Placing it in its own scope assures any necessary
82 // finalization of bits to the buffer in the serializer's dstor.
83 llvm::Serializer Sezr(Stream);
84
85 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +000086 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +000087 }
88
89 // Write the bits to disk.
90 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
91 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
92 fclose(fp);
93 return true;
94 }
95
96 return false;
97}
98
99void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
100
101 // ===---------------------------------------------------===/
102 // Serialize the top-level decls.
103 // ===---------------------------------------------------===/
104
105 Sezr.EnterBlock(DeclsBlock);
106
107 // Only serialize the head of a decl chain. The ASTConsumer interfaces
108 // provides us with each top-level decl, including those nested in
109 // a decl chain, so we may be passed decls that are already serialized.
110 for (const_iterator I=begin(), E=end(); I!=E; ++I)
111 if (!Sezr.isRegistered(*I))
112 Sezr.EmitOwnedPtr(*I);
113
114 Sezr.ExitBlock();
115
116 // ===---------------------------------------------------===/
117 // Serialize the "Translation Unit" metadata.
118 // ===---------------------------------------------------===/
119
120 // Emit ASTContext.
121 Sezr.EnterBlock(ASTContextBlock);
122 Sezr.EmitOwnedPtr(Context);
123 Sezr.ExitBlock();
124
125 Sezr.EnterBlock(BasicMetadataBlock);
126
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000127 // Block for SourceManager and Target. Allows easy skipping
Ted Kremenek2f743592007-12-05 21:36:08 +0000128 // around to the block for the Selectors during deserialization.
129 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000130
Ted Kremenek2f743592007-12-05 21:36:08 +0000131 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000132 Sezr.Emit(Context->getSourceManager());
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000133
Ted Kremenek2f743592007-12-05 21:36:08 +0000134 // 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 Kremeneke7d07d12008-06-04 15:55:15 +0000211
Ted Kremenekbbced582007-12-12 18:05:32 +0000212 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000213 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
214 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000215 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000216 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000217 }
218
219 // For Selectors, we must read the identifier table first because the
220 // SelectorTable depends on the identifiers being already deserialized.
221 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
222 Dezr.SkipBlock();
223
224 // Read the identifier table.
225 IdentifierTable::CreateAndRegister(Dezr);
226
227 // Now jump back and read the selectors.
228 Dezr.JumpTo(SelectorBlkLoc);
229 SelectorTable::CreateAndRegister(Dezr);
230
231 // Now jump back to ASTContextBlock and read the ASTContext.
232 Dezr.JumpTo(ASTContextBlockLoc);
233 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
234
235 // "Rewind" the stream. Find the block with the serialized top-level decls.
236 Dezr.Rewind();
237 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
238 assert (FoundBlock);
239 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
240
241 while (!Dezr.FinishedBlock(DeclBlockLoc))
Sam Bishope2563ca2008-04-07 21:55:54 +0000242 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context));
Ted Kremenek2f743592007-12-05 21:36:08 +0000243
244 return TU;
245}
246