blob: 8211db1b74821482e3a9a85743b6ace8685d9d21 [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 Kremenekee533642007-12-20 19:47:16 +000023#include "llvm/ADT/OwningPtr.h"
Ted Kremenek2f743592007-12-05 21:36:08 +000024
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
Sam Bishop71de20e2008-04-03 05:35:20 +000033TranslationUnit::~TranslationUnit()
34{
35 for (iterator I=begin(), E=end(); I!=E; ++I)
36 delete *I;
37}
Ted Kremenek63ea8632007-12-19 19:27:38 +000038
Ted Kremenekdca29272007-12-18 21:44:50 +000039bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
40 const llvm::sys::Path& Filename) {
Ted Kremenek2f743592007-12-05 21:36:08 +000041
42 // Reserve 256K for bitstream buffer.
43 std::vector<unsigned char> Buffer;
44 Buffer.reserve(256*1024);
45
46 // Create bitstream.
47 llvm::BitstreamWriter Stream(Buffer);
48
49 // Emit the preamble.
50 Stream.Emit((unsigned)'B', 8);
51 Stream.Emit((unsigned)'C', 8);
52 Stream.Emit(0xC, 4);
53 Stream.Emit(0xF, 4);
54 Stream.Emit(0xE, 4);
55 Stream.Emit(0x0, 4);
56
57 {
58 // Create serializer. Placing it in its own scope assures any necessary
59 // finalization of bits to the buffer in the serializer's dstor.
60 llvm::Serializer Sezr(Stream);
61
62 // Emit the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +000063 TU.Emit(Sezr);
Ted Kremenek2f743592007-12-05 21:36:08 +000064 }
65
66 // Write the bits to disk.
67 if (FILE* fp = fopen(Filename.c_str(),"wb")) {
68 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
69 fclose(fp);
70 return true;
71 }
72
73 return false;
74}
75
76void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
77
78 // ===---------------------------------------------------===/
79 // Serialize the top-level decls.
80 // ===---------------------------------------------------===/
81
82 Sezr.EnterBlock(DeclsBlock);
83
84 // Only serialize the head of a decl chain. The ASTConsumer interfaces
85 // provides us with each top-level decl, including those nested in
86 // a decl chain, so we may be passed decls that are already serialized.
87 for (const_iterator I=begin(), E=end(); I!=E; ++I)
88 if (!Sezr.isRegistered(*I))
89 Sezr.EmitOwnedPtr(*I);
90
91 Sezr.ExitBlock();
92
93 // ===---------------------------------------------------===/
94 // Serialize the "Translation Unit" metadata.
95 // ===---------------------------------------------------===/
96
97 // Emit ASTContext.
98 Sezr.EnterBlock(ASTContextBlock);
99 Sezr.EmitOwnedPtr(Context);
100 Sezr.ExitBlock();
101
102 Sezr.EnterBlock(BasicMetadataBlock);
103
104 // Block for SourceManager, LangOptions, and Target. Allows easy skipping
105 // around to the block for the Selectors during deserialization.
106 Sezr.EnterBlock();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000107
Ted Kremenek2f743592007-12-05 21:36:08 +0000108 // Emit the SourceManager.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000109 Sezr.Emit(Context->getSourceManager());
Ted Kremenek2f743592007-12-05 21:36:08 +0000110
111 // Emit the LangOptions.
112 Sezr.Emit(LangOpts);
113
114 // Emit the Target.
115 Sezr.EmitPtr(&Context->Target);
116 Sezr.EmitCStr(Context->Target.getTargetTriple());
117
118 Sezr.ExitBlock(); // exit "BasicMetadataBlock"
119
120 // Emit the Selectors.
121 Sezr.Emit(Context->Selectors);
122
123 // Emit the Identifier Table.
124 Sezr.Emit(Context->Idents);
125
126 Sezr.ExitBlock(); // exit "ASTContextBlock"
127}
128
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000129TranslationUnit*
Ted Kremenekdca29272007-12-18 21:44:50 +0000130clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000131
132 // Create the memory buffer that contains the contents of the file.
Ted Kremenekee533642007-12-20 19:47:16 +0000133 llvm::OwningPtr<llvm::MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000134 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
Ted Kremenek2f743592007-12-05 21:36:08 +0000135
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
Ted Kremenek2f743592007-12-05 21:36:08 +0000189 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000190 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000191
192 // Read the LangOptions.
193 TU->LangOpts.Read(Dezr);
194
Ted Kremenekbbced582007-12-12 18:05:32 +0000195 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000196 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
197 char* triple = Dezr.ReadCStr(NULL,0,true);
Chris Lattner42e67372008-03-05 01:18:20 +0000198 Dezr.RegisterPtr(PtrID,TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000199 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000200 }
201
202 // For Selectors, we must read the identifier table first because the
203 // SelectorTable depends on the identifiers being already deserialized.
204 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
205 Dezr.SkipBlock();
206
207 // Read the identifier table.
208 IdentifierTable::CreateAndRegister(Dezr);
209
210 // Now jump back and read the selectors.
211 Dezr.JumpTo(SelectorBlkLoc);
212 SelectorTable::CreateAndRegister(Dezr);
213
214 // Now jump back to ASTContextBlock and read the ASTContext.
215 Dezr.JumpTo(ASTContextBlockLoc);
216 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
217
218 // "Rewind" the stream. Find the block with the serialized top-level decls.
219 Dezr.Rewind();
220 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
221 assert (FoundBlock);
222 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
223
224 while (!Dezr.FinishedBlock(DeclBlockLoc))
225 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>());
226
227 return TU;
228}
229