Ted Kremenek | 27cc3c2 | 2007-12-13 17:54:02 +0000 | [diff] [blame] | 1 | //===--- TranslationUnit.cpp - Abstraction for Translation Units ----------===// |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 7 | // |
| 8 | // FIXME: This should eventually be moved out of the driver, or replaced |
| 9 | // with its eventual successor. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Ted Kremenek | 3a0b28b | 2007-12-18 21:36:21 +0000 | [diff] [blame] | 13 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 14 | |
| 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 Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/OwningPtr.h" |
| 25 | #include "llvm/ADT/DenseSet.h" |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Ted Kremenek | ab74937 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 29 | enum { BasicMetadataBlock = 1, |
| 30 | ASTContextBlock = 2, |
| 31 | DeclsBlock = 3 }; |
| 32 | |
Sam Bishop | fc8584c | 2008-04-03 14:20:28 +0000 | [diff] [blame] | 33 | TranslationUnit::~TranslationUnit() { |
Eli Friedman | 01a8b6c | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 34 | if (OwnsMetaData && Context) { |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 35 | // The ASTContext object has the sole references to the IdentifierTable |
| 36 | // Selectors, and the Target information. Go and delete them, since |
| 37 | // the TranslationUnit effectively owns them. |
Chris Lattner | 9c18fde | 2009-03-28 01:44:40 +0000 | [diff] [blame^] | 38 | delete &Context->Idents; |
| 39 | delete &Context->Selectors; |
| 40 | delete &Context->Target; |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 41 | delete Context; |
| 42 | } |
Sam Bishop | 4949251 | 2008-04-03 05:35:20 +0000 | [diff] [blame] | 43 | } |
Ted Kremenek | ab74937 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 44 | |
Zhongxing Xu | 605ad51 | 2008-12-21 04:46:06 +0000 | [diff] [blame] | 45 | bool clang::EmitASTBitcodeFile(const TranslationUnit* TU, |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 46 | const llvm::sys::Path& Filename) { |
| 47 | |
| 48 | return TU ? EmitASTBitcodeFile(*TU, Filename) : false; |
| 49 | } |
| 50 | |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 51 | bool clang::EmitASTBitcodeBuffer(const TranslationUnit* TU, |
| 52 | std::vector<unsigned char>& Buffer) { |
| 53 | |
| 54 | return TU ? EmitASTBitcodeBuffer(*TU, Buffer) : false; |
| 55 | } |
| 56 | |
| 57 | bool clang::EmitASTBitcodeStream(const TranslationUnit* TU, |
| 58 | std::ostream& Stream) { |
| 59 | |
| 60 | return TU ? EmitASTBitcodeStream(*TU, Stream) : false; |
| 61 | } |
| 62 | |
| 63 | bool clang::EmitASTBitcodeBuffer(const TranslationUnit& TU, |
| 64 | std::vector<unsigned char>& Buffer) { |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 65 | // Create bitstream. |
| 66 | llvm::BitstreamWriter Stream(Buffer); |
| 67 | |
| 68 | // Emit the preamble. |
| 69 | Stream.Emit((unsigned)'B', 8); |
| 70 | Stream.Emit((unsigned)'C', 8); |
| 71 | Stream.Emit(0xC, 4); |
| 72 | Stream.Emit(0xF, 4); |
| 73 | Stream.Emit(0xE, 4); |
| 74 | Stream.Emit(0x0, 4); |
| 75 | |
| 76 | { |
| 77 | // Create serializer. Placing it in its own scope assures any necessary |
| 78 | // finalization of bits to the buffer in the serializer's dstor. |
| 79 | llvm::Serializer Sezr(Stream); |
| 80 | |
| 81 | // Emit the translation unit. |
Ted Kremenek | eccf070 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 82 | TU.Emit(Sezr); |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool clang::EmitASTBitcodeStream(const TranslationUnit& TU, |
| 89 | std::ostream& Stream) { |
| 90 | |
| 91 | // Reserve 256K for bitstream buffer. |
| 92 | std::vector<unsigned char> Buffer; |
| 93 | Buffer.reserve(256*1024); |
| 94 | |
| 95 | EmitASTBitcodeBuffer(TU,Buffer); |
| 96 | |
| 97 | // Write the bits to disk. |
| 98 | Stream.write((char*)&Buffer.front(), Buffer.size()); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool clang::EmitASTBitcodeFile(const TranslationUnit& TU, |
| 103 | const llvm::sys::Path& Filename) { |
| 104 | |
| 105 | // Reserve 256K for bitstream buffer. |
| 106 | std::vector<unsigned char> Buffer; |
| 107 | Buffer.reserve(256*1024); |
| 108 | |
| 109 | EmitASTBitcodeBuffer(TU,Buffer); |
| 110 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 111 | // Write the bits to disk. |
| 112 | if (FILE* fp = fopen(Filename.c_str(),"wb")) { |
| 113 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 114 | fclose(fp); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | void TranslationUnit::Emit(llvm::Serializer& Sezr) const { |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 122 | // ===---------------------------------------------------===/ |
| 123 | // Serialize the "Translation Unit" metadata. |
| 124 | // ===---------------------------------------------------===/ |
| 125 | |
| 126 | // Emit ASTContext. |
| 127 | Sezr.EnterBlock(ASTContextBlock); |
| 128 | Sezr.EmitOwnedPtr(Context); |
Zhongxing Xu | 80da19a | 2008-12-21 13:00:52 +0000 | [diff] [blame] | 129 | Sezr.ExitBlock(); // exit "ASTContextBlock" |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 130 | |
| 131 | Sezr.EnterBlock(BasicMetadataBlock); |
| 132 | |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 133 | // Block for SourceManager and Target. Allows easy skipping |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 134 | // around to the block for the Selectors during deserialization. |
| 135 | Sezr.EnterBlock(); |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 137 | // Emit the SourceManager. |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 138 | Sezr.Emit(Context->getSourceManager()); |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 140 | // Emit the Target. |
| 141 | Sezr.EmitPtr(&Context->Target); |
| 142 | Sezr.EmitCStr(Context->Target.getTargetTriple()); |
| 143 | |
Zhongxing Xu | 80da19a | 2008-12-21 13:00:52 +0000 | [diff] [blame] | 144 | Sezr.ExitBlock(); // exit "SourceManager and Target Block" |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 145 | |
| 146 | // Emit the Selectors. |
| 147 | Sezr.Emit(Context->Selectors); |
| 148 | |
| 149 | // Emit the Identifier Table. |
| 150 | Sezr.Emit(Context->Idents); |
| 151 | |
Zhongxing Xu | 80da19a | 2008-12-21 13:00:52 +0000 | [diff] [blame] | 152 | Sezr.ExitBlock(); // exit "BasicMetadataBlock" |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 155 | TranslationUnit* |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 156 | clang::ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer, FileManager& FMgr) { |
| 157 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 158 | // Check if the file is of the proper length. |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 159 | if (MBuffer.getBufferSize() & 0x3) { |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 160 | // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes." |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | // Create the bitstream reader. |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 165 | unsigned char *BufPtr = (unsigned char *) MBuffer.getBufferStart(); |
| 166 | llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer.getBufferSize()); |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 167 | |
| 168 | if (Stream.Read(8) != 'B' || |
| 169 | Stream.Read(8) != 'C' || |
| 170 | Stream.Read(4) != 0xC || |
| 171 | Stream.Read(4) != 0xF || |
| 172 | Stream.Read(4) != 0xE || |
| 173 | Stream.Read(4) != 0x0) { |
| 174 | // FIXME: Provide diagnostic. |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | // Create the deserializer. |
| 179 | llvm::Deserializer Dezr(Stream); |
| 180 | |
Ted Kremenek | eccf070 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 181 | return TranslationUnit::Create(Dezr,FMgr); |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ted Kremenek | 438a8f0 | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 184 | TranslationUnit* |
| 185 | clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) { |
| 186 | |
| 187 | // Create the memory buffer that contains the contents of the file. |
| 188 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 189 | MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); |
| 190 | |
| 191 | if (!MBuffer) { |
| 192 | // FIXME: Provide diagnostic. |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | return ReadASTBitcodeBuffer(*MBuffer, FMgr); |
| 197 | } |
| 198 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 199 | TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr, |
| 200 | FileManager& FMgr) { |
| 201 | |
| 202 | // Create the translation unit object. |
| 203 | TranslationUnit* TU = new TranslationUnit(); |
| 204 | |
| 205 | // ===---------------------------------------------------===/ |
| 206 | // Deserialize the "Translation Unit" metadata. |
| 207 | // ===---------------------------------------------------===/ |
| 208 | |
| 209 | // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock |
| 210 | // (which will appear earlier) and record its location. |
| 211 | |
| 212 | bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock); |
| 213 | assert (FoundBlock); |
| 214 | |
| 215 | llvm::Deserializer::Location ASTContextBlockLoc = |
| 216 | Dezr.getCurrentBlockLocation(); |
| 217 | |
| 218 | FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock); |
| 219 | assert (FoundBlock); |
Ted Kremenek | ab74937 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 221 | // Read the SourceManager. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 222 | SourceManager::CreateAndRegister(Dezr,FMgr); |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 38591a2 | 2007-12-12 18:05:32 +0000 | [diff] [blame] | 224 | { // Read the TargetInfo. |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 225 | llvm::SerializedPtrID PtrID = Dezr.ReadPtrID(); |
| 226 | char* triple = Dezr.ReadCStr(NULL,0,true); |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 227 | Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple))); |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 228 | delete [] triple; |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // For Selectors, we must read the identifier table first because the |
| 232 | // SelectorTable depends on the identifiers being already deserialized. |
Chris Lattner | b6eccdc | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 233 | llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation(); |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 234 | Dezr.SkipBlock(); |
| 235 | |
| 236 | // Read the identifier table. |
| 237 | IdentifierTable::CreateAndRegister(Dezr); |
| 238 | |
| 239 | // Now jump back and read the selectors. |
| 240 | Dezr.JumpTo(SelectorBlkLoc); |
| 241 | SelectorTable::CreateAndRegister(Dezr); |
| 242 | |
| 243 | // Now jump back to ASTContextBlock and read the ASTContext. |
| 244 | Dezr.JumpTo(ASTContextBlockLoc); |
| 245 | TU->Context = Dezr.ReadOwnedPtr<ASTContext>(); |
| 246 | |
Ted Kremenek | 6f046da | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 247 | return TU; |
| 248 | } |
| 249 | |