blob: 41d9c79f89a32e5294192b7e236e6a33f15d9809 [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
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
33
Ted Kremenekdca29272007-12-18 21:44:50 +000034bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
35 const llvm::sys::Path& Filename) {
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.
Ted Kremenekdca29272007-12-18 21:44:50 +000058 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +000059 }
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
Ted Kremenek63ea8632007-12-19 19:27:38 +0000103 // Emit the name of the source file.
104 Sezr.EmitCStr(SourceFile.c_str());
105 Sezr.FlushRecord();
106
Ted Kremenek2f743592007-12-05 21:36:08 +0000107 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000108 Sezr.Emit(Context->getSourceManager());
Ted Kremenek2f743592007-12-05 21:36:08 +0000109
110 // Emit the LangOptions.
111 Sezr.Emit(LangOpts);
112
113 // Emit the Target.
114 Sezr.EmitPtr(&Context->Target);
115 Sezr.EmitCStr(Context->Target.getTargetTriple());
116
117 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
118
119 // Emit the Selectors.
120 Sezr.Emit(Context->Selectors);
121
122 // Emit the Identifier Table.
123 Sezr.Emit(Context->Idents);
124
125 Sezr.ExitBlock(); // exit "ASTContextBlock"
126}
127
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000128TranslationUnit*
Ted Kremenekdca29272007-12-18 21:44:50 +0000129clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000130
131 // Create the memory buffer that contains the contents of the file.
132 llvm::scoped_ptr<llvm::MemoryBuffer>
133 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str(),
134 strlen(Filename.c_str())));
135
136 if (!MBuffer) {
137 // FIXME: Provide diagnostic.
138 return NULL;
139 }
140
141 // Check if the file is of the proper length.
142 if (MBuffer->getBufferSize() & 0x3) {
143 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
144 return NULL;
145 }
146
147 // Create the bitstream reader.
148 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
149 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
150
151 if (Stream.Read(8) != 'B' ||
152 Stream.Read(8) != 'C' ||
153 Stream.Read(4) != 0xC ||
154 Stream.Read(4) != 0xF ||
155 Stream.Read(4) != 0xE ||
156 Stream.Read(4) != 0x0) {
157 // FIXME: Provide diagnostic.
158 return NULL;
159 }
160
161 // Create the deserializer.
162 llvm::Deserializer Dezr(Stream);
163
Ted Kremenekdca29272007-12-18 21:44:50 +0000164 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000165}
166
167TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
168 FileManager& FMgr) {
169
170 // Create the translation unit object.
171 TranslationUnit* TU = new TranslationUnit();
172
173 // ===---------------------------------------------------===/
174 // Deserialize the "Translation Unit" metadata.
175 // ===---------------------------------------------------===/
176
177 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
178 // (which will appear earlier) and record its location.
179
180 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
181 assert (FoundBlock);
182
183 llvm::Deserializer::Location ASTContextBlockLoc =
184 Dezr.getCurrentBlockLocation();
185
186 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
187 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000188
189 { // Read the SourceFile.
190 char* SName = Dezr.ReadCStr(NULL,0,true);
191 TU->SourceFile = SName;
192 delete [] SName;
193 }
Ted Kremenek2f743592007-12-05 21:36:08 +0000194
195 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000196 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000197
198 // Read the LangOptions.
199 TU->LangOpts.Read(Dezr);
200
Ted Kremenekbbced582007-12-12 18:05:32 +0000201 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000202 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
203 char* triple = Dezr.ReadCStr(NULL,0,true);
Ted Kremenekbbced582007-12-12 18:05:32 +0000204 std::string Triple(triple);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000205 Dezr.RegisterPtr(PtrID,TargetInfo::CreateTargetInfo(&Triple,
Ted Kremenekbbced582007-12-12 18:05:32 +0000206 &Triple+1));
Ted Kremenek2f743592007-12-05 21:36:08 +0000207 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000208 }
209
210 // For Selectors, we must read the identifier table first because the
211 // SelectorTable depends on the identifiers being already deserialized.
212 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
213 Dezr.SkipBlock();
214
215 // Read the identifier table.
216 IdentifierTable::CreateAndRegister(Dezr);
217
218 // Now jump back and read the selectors.
219 Dezr.JumpTo(SelectorBlkLoc);
220 SelectorTable::CreateAndRegister(Dezr);
221
222 // Now jump back to ASTContextBlock and read the ASTContext.
223 Dezr.JumpTo(ASTContextBlockLoc);
224 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
225
226 // "Rewind" the stream. Find the block with the serialized top-level decls.
227 Dezr.Rewind();
228 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
229 assert (FoundBlock);
230 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
231
232 while (!Dezr.FinishedBlock(DeclBlockLoc))
233 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>());
234
235 return TU;
236}
237