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