blob: 51f6b57da1f5ba29e796fbeda12ef9676c0dd00b [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//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
23#include "llvm/ADT/scoped_ptr.h"
24
25#include <stdio.h>
26
27namespace {
28 enum { BasicMetadataBlock = 1,
29 ASTContextBlock = 2,
30 DeclsBlock = 3 };
31}
32
33using namespace clang;
34
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000035bool TranslationUnit::EmitBitcodeFile(const llvm::sys::Path& Filename) const {
Ted Kremenek2f743592007-12-05 21:36:08 +000036
37 // Reserve 256K for bitstream buffer.
38 std::vector<unsigned char> Buffer;
39 Buffer.reserve(256*1024);
40
41 // Create bitstream.
42 llvm::BitstreamWriter Stream(Buffer);
43
44 // Emit the preamble.
45 Stream.Emit((unsigned)'B', 8);
46 Stream.Emit((unsigned)'C', 8);
47 Stream.Emit(0xC, 4);
48 Stream.Emit(0xF, 4);
49 Stream.Emit(0xE, 4);
50 Stream.Emit(0x0, 4);
51
52 {
53 // Create serializer. Placing it in its own scope assures any necessary
54 // finalization of bits to the buffer in the serializer's dstor.
55 llvm::Serializer Sezr(Stream);
56
57 // Emit the translation unit.
58 Emit(Sezr);
59 }
60
61 // Write the bits to disk.
62 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
63 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
64 fclose(fp);
65 return true;
66 }
67
68 return false;
69}
70
71void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
72
73 // ===---------------------------------------------------===/
74 // Serialize the top-level decls.
75 // ===---------------------------------------------------===/
76
77 Sezr.EnterBlock(DeclsBlock);
78
79 // Only serialize the head of a decl chain. The ASTConsumer interfaces
80 // provides us with each top-level decl, including those nested in
81 // a decl chain, so we may be passed decls that are already serialized.
82 for (const_iterator I=begin(), E=end(); I!=E; ++I)
83 if (!Sezr.isRegistered(*I))
84 Sezr.EmitOwnedPtr(*I);
85
86 Sezr.ExitBlock();
87
88 // ===---------------------------------------------------===/
89 // Serialize the "Translation Unit" metadata.
90 // ===---------------------------------------------------===/
91
92 // Emit ASTContext.
93 Sezr.EnterBlock(ASTContextBlock);
94 Sezr.EmitOwnedPtr(Context);
95 Sezr.ExitBlock();
96
97 Sezr.EnterBlock(BasicMetadataBlock);
98
99 // Block for SourceManager, LangOptions, and Target. Allows easy skipping
100 // around to the block for the Selectors during deserialization.
101 Sezr.EnterBlock();
102
103 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000104 Sezr.Emit(Context->getSourceManager());
Ted Kremenek2f743592007-12-05 21:36:08 +0000105
106 // Emit the LangOptions.
107 Sezr.Emit(LangOpts);
108
109 // Emit the Target.
110 Sezr.EmitPtr(&Context->Target);
111 Sezr.EmitCStr(Context->Target.getTargetTriple());
112
113 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
114
115 // Emit the Selectors.
116 Sezr.Emit(Context->Selectors);
117
118 // Emit the Identifier Table.
119 Sezr.Emit(Context->Idents);
120
121 Sezr.ExitBlock(); // exit "ASTContextBlock"
122}
123
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000124TranslationUnit*
125TranslationUnit::ReadBitcodeFile(const llvm::sys::Path& Filename,
126 FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000127
128 // Create the memory buffer that contains the contents of the file.
129 llvm::scoped_ptr<llvm::MemoryBuffer>
130 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str(),
131 strlen(Filename.c_str())));
132
133 if (!MBuffer) {
134 // FIXME: Provide diagnostic.
135 return NULL;
136 }
137
138 // Check if the file is of the proper length.
139 if (MBuffer->getBufferSize() & 0x3) {
140 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
141 return NULL;
142 }
143
144 // Create the bitstream reader.
145 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
146 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
147
148 if (Stream.Read(8) != 'B' ||
149 Stream.Read(8) != 'C' ||
150 Stream.Read(4) != 0xC ||
151 Stream.Read(4) != 0xF ||
152 Stream.Read(4) != 0xE ||
153 Stream.Read(4) != 0x0) {
154 // FIXME: Provide diagnostic.
155 return NULL;
156 }
157
158 // Create the deserializer.
159 llvm::Deserializer Dezr(Stream);
160
161 return Create(Dezr,FMgr);
162}
163
164TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
165 FileManager& FMgr) {
166
167 // Create the translation unit object.
168 TranslationUnit* TU = new TranslationUnit();
169
170 // ===---------------------------------------------------===/
171 // Deserialize the "Translation Unit" metadata.
172 // ===---------------------------------------------------===/
173
174 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
175 // (which will appear earlier) and record its location.
176
177 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
178 assert (FoundBlock);
179
180 llvm::Deserializer::Location ASTContextBlockLoc =
181 Dezr.getCurrentBlockLocation();
182
183 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
184 assert (FoundBlock);
185
186 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000187 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000188
189 // Read the LangOptions.
190 TU->LangOpts.Read(Dezr);
191
Ted Kremenekbbced582007-12-12 18:05:32 +0000192 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000193 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
194 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekbbced582007-12-12 18:05:32 +0000195 std::string Triple(triple);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000196 Dezr.RegisterPtr(PtrID,TargetInfo::CreateTargetInfo(&Triple,
Ted Kremenekbbced582007-12-12 18:05:32 +0000197 &Triple+1));
Ted Kremenek2f743592007-12-05 21:36:08 +0000198 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000199 }
200
201 // For Selectors, we must read the identifier table first because the
202 // SelectorTable depends on the identifiers being already deserialized.
203 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
204 Dezr.SkipBlock();
205
206 // Read the identifier table.
207 IdentifierTable::CreateAndRegister(Dezr);
208
209 // Now jump back and read the selectors.
210 Dezr.JumpTo(SelectorBlkLoc);
211 SelectorTable::CreateAndRegister(Dezr);
212
213 // Now jump back to ASTContextBlock and read the ASTContext.
214 Dezr.JumpTo(ASTContextBlockLoc);
215 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
216
217 // "Rewind" the stream. Find the block with the serialized top-level decls.
218 Dezr.Rewind();
219 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
220 assert (FoundBlock);
221 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
222
223 while (!Dezr.FinishedBlock(DeclBlockLoc))
224 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>());
225
226 return TU;
227}
228