blob: fdc37a8443730b66531fe43e6016432325964a04 [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
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();
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000102
Ted Kremenek2f743592007-12-05 21:36:08 +0000103 // 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*
Ted Kremenekdca29272007-12-18 21:44:50 +0000125clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
Ted Kremenek2f743592007-12-05 21:36:08 +0000126
127 // Create the memory buffer that contains the contents of the file.
Ted Kremenekee533642007-12-20 19:47:16 +0000128 llvm::OwningPtr<llvm::MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000129 MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
Ted Kremenek2f743592007-12-05 21:36:08 +0000130
131 if (!MBuffer) {
132 // FIXME: Provide diagnostic.
133 return NULL;
134 }
135
136 // Check if the file is of the proper length.
137 if (MBuffer->getBufferSize() & 0x3) {
138 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
139 return NULL;
140 }
141
142 // Create the bitstream reader.
143 unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
144 llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
145
146 if (Stream.Read(8) != 'B' ||
147 Stream.Read(8) != 'C' ||
148 Stream.Read(4) != 0xC ||
149 Stream.Read(4) != 0xF ||
150 Stream.Read(4) != 0xE ||
151 Stream.Read(4) != 0x0) {
152 // FIXME: Provide diagnostic.
153 return NULL;
154 }
155
156 // Create the deserializer.
157 llvm::Deserializer Dezr(Stream);
158
Ted Kremenekdca29272007-12-18 21:44:50 +0000159 return TranslationUnit::Create(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000160}
161
162TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
163 FileManager& FMgr) {
164
165 // Create the translation unit object.
166 TranslationUnit* TU = new TranslationUnit();
167
168 // ===---------------------------------------------------===/
169 // Deserialize the "Translation Unit" metadata.
170 // ===---------------------------------------------------===/
171
172 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
173 // (which will appear earlier) and record its location.
174
175 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
176 assert (FoundBlock);
177
178 llvm::Deserializer::Location ASTContextBlockLoc =
179 Dezr.getCurrentBlockLocation();
180
181 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
182 assert (FoundBlock);
Ted Kremenek63ea8632007-12-19 19:27:38 +0000183
Ted Kremenek2f743592007-12-05 21:36:08 +0000184 // Read the SourceManager.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000185 SourceManager::CreateAndRegister(Dezr,FMgr);
Ted Kremenek2f743592007-12-05 21:36:08 +0000186
187 // Read the LangOptions.
188 TU->LangOpts.Read(Dezr);
189
Ted Kremenekbbced582007-12-12 18:05:32 +0000190 { // Read the TargetInfo.
Ted Kremenek2f743592007-12-05 21:36:08 +0000191 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
192 char* triple = Dezr.ReadCStr(NULL,0,true);
Chris Lattner42e67372008-03-05 01:18:20 +0000193 Dezr.RegisterPtr(PtrID,TargetInfo::CreateTargetInfo(std::string(triple)));
Ted Kremenek2f743592007-12-05 21:36:08 +0000194 delete [] triple;
Ted Kremenek2f743592007-12-05 21:36:08 +0000195 }
196
197 // For Selectors, we must read the identifier table first because the
198 // SelectorTable depends on the identifiers being already deserialized.
199 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
200 Dezr.SkipBlock();
201
202 // Read the identifier table.
203 IdentifierTable::CreateAndRegister(Dezr);
204
205 // Now jump back and read the selectors.
206 Dezr.JumpTo(SelectorBlkLoc);
207 SelectorTable::CreateAndRegister(Dezr);
208
209 // Now jump back to ASTContextBlock and read the ASTContext.
210 Dezr.JumpTo(ASTContextBlockLoc);
211 TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
212
213 // "Rewind" the stream. Find the block with the serialized top-level decls.
214 Dezr.Rewind();
215 FoundBlock = Dezr.SkipToBlock(DeclsBlock);
216 assert (FoundBlock);
217 llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation();
218
219 while (!Dezr.FinishedBlock(DeclBlockLoc))
220 TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>());
221
222 return TU;
223}
224