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 (OwnsDecls) { |
Eli Friedman | 1108e7d | 2008-05-21 05:01:55 +0000 | [diff] [blame] | 35 | llvm::DenseSet<Decl*> Killed; |
| 36 | for (iterator I=begin(), E=end(); I!=E; ++I) { |
| 37 | if (Killed.count(*I)) continue; |
| 38 | |
| 39 | Killed.insert(*I); |
Ted Kremenek | 1a726d7 | 2008-06-06 17:21:42 +0000 | [diff] [blame] | 40 | |
| 41 | // FIXME: This is a horrible hack. Because there is no clear ownership |
| 42 | // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they |
| 43 | // reference, we need to destroy ObjCPropertyDecls here. This will |
| 44 | // eventually be fixed when the ownership of ObjCPropertyDecls gets |
| 45 | // cleaned up. |
| 46 | if (ObjCInterfaceDecl* IDecl = dyn_cast<ObjCInterfaceDecl>(*I)) |
| 47 | for (ObjCInterfaceDecl::classprop_iterator ID=IDecl->classprop_begin(), |
| 48 | ED=IDecl->classprop_end(); ID!=ED; ++ID) { |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame^] | 49 | if (!*ID || Killed.count(*ID)) continue; |
Ted Kremenek | 1a726d7 | 2008-06-06 17:21:42 +0000 | [diff] [blame] | 50 | Killed.insert(*ID); |
| 51 | (*ID)->Destroy(*Context); |
| 52 | } |
| 53 | |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame^] | 54 | // FIXME: This is a horrible hack. Because there is no clear ownership |
| 55 | // role between ObjCProtocolDecls and the ObjCPropertyDecls that they |
| 56 | // reference, we need to destroy ObjCPropertyDecls here. This will |
| 57 | // eventually be fixed when the ownership of ObjCPropertyDecls gets |
| 58 | // cleaned up. |
| 59 | if (ObjCProtocolDecl* PDecl = dyn_cast<ObjCProtocolDecl>(*I)) |
| 60 | for (ObjCInterfaceDecl::classprop_iterator PD=PDecl->classprop_begin(), |
| 61 | ED=PDecl->classprop_end(); PD!=ED; ++PD) { |
| 62 | if (!*PD || Killed.count(*PD)) continue; |
| 63 | Killed.insert(*PD); |
| 64 | (*PD)->Destroy(*Context); |
| 65 | } |
| 66 | |
Eli Friedman | 1108e7d | 2008-05-21 05:01:55 +0000 | [diff] [blame] | 67 | (*I)->Destroy(*Context); |
| 68 | } |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 69 | } |
Eli Friedman | 1108e7d | 2008-05-21 05:01:55 +0000 | [diff] [blame] | 70 | |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 71 | if (OwnsMetaData && Context) { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 72 | // The ASTContext object has the sole references to the IdentifierTable |
| 73 | // Selectors, and the Target information. Go and delete them, since |
| 74 | // the TranslationUnit effectively owns them. |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 76 | delete &(Context->Idents); |
| 77 | delete &(Context->Selectors); |
| 78 | delete &(Context->Target); |
| 79 | delete Context; |
| 80 | } |
Sam Bishop | 71de20e | 2008-04-03 05:35:20 +0000 | [diff] [blame] | 81 | } |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 83 | bool clang::EmitASTBitcodeFile(const TranslationUnit* TU, |
| 84 | const llvm::sys::Path& Filename) { |
| 85 | |
| 86 | return TU ? EmitASTBitcodeFile(*TU, Filename) : false; |
| 87 | } |
| 88 | |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 89 | bool clang::EmitASTBitcodeFile(const TranslationUnit& TU, |
| 90 | const llvm::sys::Path& Filename) { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 92 | // Reserve 256K for bitstream buffer. |
| 93 | std::vector<unsigned char> Buffer; |
| 94 | Buffer.reserve(256*1024); |
| 95 | |
| 96 | // Create bitstream. |
| 97 | llvm::BitstreamWriter Stream(Buffer); |
| 98 | |
| 99 | // Emit the preamble. |
| 100 | Stream.Emit((unsigned)'B', 8); |
| 101 | Stream.Emit((unsigned)'C', 8); |
| 102 | Stream.Emit(0xC, 4); |
| 103 | Stream.Emit(0xF, 4); |
| 104 | Stream.Emit(0xE, 4); |
| 105 | Stream.Emit(0x0, 4); |
| 106 | |
| 107 | { |
| 108 | // Create serializer. Placing it in its own scope assures any necessary |
| 109 | // finalization of bits to the buffer in the serializer's dstor. |
| 110 | llvm::Serializer Sezr(Stream); |
| 111 | |
| 112 | // Emit the translation unit. |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 113 | TU.Emit(Sezr); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Write the bits to disk. |
| 117 | if (FILE* fp = fopen(Filename.c_str(),"wb")) { |
| 118 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 119 | fclose(fp); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | void TranslationUnit::Emit(llvm::Serializer& Sezr) const { |
| 127 | |
| 128 | // ===---------------------------------------------------===/ |
| 129 | // Serialize the top-level decls. |
| 130 | // ===---------------------------------------------------===/ |
| 131 | |
| 132 | Sezr.EnterBlock(DeclsBlock); |
| 133 | |
| 134 | // Only serialize the head of a decl chain. The ASTConsumer interfaces |
| 135 | // provides us with each top-level decl, including those nested in |
| 136 | // a decl chain, so we may be passed decls that are already serialized. |
| 137 | for (const_iterator I=begin(), E=end(); I!=E; ++I) |
| 138 | if (!Sezr.isRegistered(*I)) |
| 139 | Sezr.EmitOwnedPtr(*I); |
| 140 | |
| 141 | Sezr.ExitBlock(); |
| 142 | |
| 143 | // ===---------------------------------------------------===/ |
| 144 | // Serialize the "Translation Unit" metadata. |
| 145 | // ===---------------------------------------------------===/ |
| 146 | |
| 147 | // Emit ASTContext. |
| 148 | Sezr.EnterBlock(ASTContextBlock); |
| 149 | Sezr.EmitOwnedPtr(Context); |
| 150 | Sezr.ExitBlock(); |
| 151 | |
| 152 | Sezr.EnterBlock(BasicMetadataBlock); |
| 153 | |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 154 | // Block for SourceManager and Target. Allows easy skipping |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 155 | // around to the block for the Selectors during deserialization. |
| 156 | Sezr.EnterBlock(); |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 158 | // Emit the SourceManager. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 159 | Sezr.Emit(Context->getSourceManager()); |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 161 | // Emit the Target. |
| 162 | Sezr.EmitPtr(&Context->Target); |
| 163 | Sezr.EmitCStr(Context->Target.getTargetTriple()); |
| 164 | |
| 165 | Sezr.ExitBlock(); // exit "BasicMetadataBlock" |
| 166 | |
| 167 | // Emit the Selectors. |
| 168 | Sezr.Emit(Context->Selectors); |
| 169 | |
| 170 | // Emit the Identifier Table. |
| 171 | Sezr.Emit(Context->Idents); |
| 172 | |
| 173 | Sezr.ExitBlock(); // exit "ASTContextBlock" |
| 174 | } |
| 175 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 176 | TranslationUnit* |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 177 | clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) { |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 178 | |
| 179 | // Create the memory buffer that contains the contents of the file. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 180 | llvm::OwningPtr<llvm::MemoryBuffer> |
Chris Lattner | 35de512 | 2008-04-01 18:04:30 +0000 | [diff] [blame] | 181 | MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 182 | |
| 183 | if (!MBuffer) { |
| 184 | // FIXME: Provide diagnostic. |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | // Check if the file is of the proper length. |
| 189 | if (MBuffer->getBufferSize() & 0x3) { |
| 190 | // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes." |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | // Create the bitstream reader. |
| 195 | unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart(); |
| 196 | llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize()); |
| 197 | |
| 198 | if (Stream.Read(8) != 'B' || |
| 199 | Stream.Read(8) != 'C' || |
| 200 | Stream.Read(4) != 0xC || |
| 201 | Stream.Read(4) != 0xF || |
| 202 | Stream.Read(4) != 0xE || |
| 203 | Stream.Read(4) != 0x0) { |
| 204 | // FIXME: Provide diagnostic. |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | // Create the deserializer. |
| 209 | llvm::Deserializer Dezr(Stream); |
| 210 | |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 211 | return TranslationUnit::Create(Dezr,FMgr); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr, |
| 215 | FileManager& FMgr) { |
| 216 | |
| 217 | // Create the translation unit object. |
| 218 | TranslationUnit* TU = new TranslationUnit(); |
| 219 | |
| 220 | // ===---------------------------------------------------===/ |
| 221 | // Deserialize the "Translation Unit" metadata. |
| 222 | // ===---------------------------------------------------===/ |
| 223 | |
| 224 | // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock |
| 225 | // (which will appear earlier) and record its location. |
| 226 | |
| 227 | bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock); |
| 228 | assert (FoundBlock); |
| 229 | |
| 230 | llvm::Deserializer::Location ASTContextBlockLoc = |
| 231 | Dezr.getCurrentBlockLocation(); |
| 232 | |
| 233 | FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock); |
| 234 | assert (FoundBlock); |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 236 | // Read the SourceManager. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 237 | SourceManager::CreateAndRegister(Dezr,FMgr); |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | bbced58 | 2007-12-12 18:05:32 +0000 | [diff] [blame] | 239 | { // Read the TargetInfo. |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 240 | llvm::SerializedPtrID PtrID = Dezr.ReadPtrID(); |
| 241 | char* triple = Dezr.ReadCStr(NULL,0,true); |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 242 | Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple))); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 243 | delete [] triple; |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // For Selectors, we must read the identifier table first because the |
| 247 | // SelectorTable depends on the identifiers being already deserialized. |
| 248 | llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation(); |
| 249 | Dezr.SkipBlock(); |
| 250 | |
| 251 | // Read the identifier table. |
| 252 | IdentifierTable::CreateAndRegister(Dezr); |
| 253 | |
| 254 | // Now jump back and read the selectors. |
| 255 | Dezr.JumpTo(SelectorBlkLoc); |
| 256 | SelectorTable::CreateAndRegister(Dezr); |
| 257 | |
| 258 | // Now jump back to ASTContextBlock and read the ASTContext. |
| 259 | Dezr.JumpTo(ASTContextBlockLoc); |
| 260 | TU->Context = Dezr.ReadOwnedPtr<ASTContext>(); |
| 261 | |
| 262 | // "Rewind" the stream. Find the block with the serialized top-level decls. |
| 263 | Dezr.Rewind(); |
| 264 | FoundBlock = Dezr.SkipToBlock(DeclsBlock); |
| 265 | assert (FoundBlock); |
| 266 | llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation(); |
| 267 | |
| 268 | while (!Dezr.FinishedBlock(DeclBlockLoc)) |
Sam Bishop | e2563ca | 2008-04-07 21:55:54 +0000 | [diff] [blame] | 269 | TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context)); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 270 | |
| 271 | return TU; |
| 272 | } |
| 273 | |