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)) |
Ted Kremenek | 400d95f | 2008-06-06 20:11:53 +0000 | [diff] [blame] | 60 | for (ObjCProtocolDecl::classprop_iterator ID=PDecl->classprop_begin(), |
| 61 | ED=PDecl->classprop_end(); ID!=ED; ++ID) { |
| 62 | if (!*ID || Killed.count(*ID)) continue; |
| 63 | Killed.insert(*ID); |
| 64 | (*ID)->Destroy(*Context); |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame] | 65 | } |
Ted Kremenek | 400d95f | 2008-06-06 20:11:53 +0000 | [diff] [blame] | 66 | |
| 67 | // FIXME: There is no clear ownership policy now for ObjCInterfaceDecls |
| 68 | // referenced by ObjCClassDecls. Some of them can be forward decls that |
Ted Kremenek | 05ac3ef | 2008-06-06 21:05:33 +0000 | [diff] [blame] | 69 | // are never later defined (and forward decls can be referenced by |
| 70 | // multiple ObjCClassDecls) or the ObjCInterfaceDecl later |
| 71 | // becomes a real definition. |
Ted Kremenek | 400d95f | 2008-06-06 20:11:53 +0000 | [diff] [blame] | 72 | // Ideally we should have separate objects for forward declarations and |
| 73 | // definitions, obviating this problem. Because of this situation, |
| 74 | // referenced ObjCInterfaceDecls are destroyed here. |
| 75 | if (ObjCClassDecl* CDecl = dyn_cast<ObjCClassDecl>(*I)) |
| 76 | for (ObjCClassDecl::iterator ID=CDecl->begin(), |
| 77 | ED=CDecl->end(); ID!=ED; ++ID) { |
| 78 | if (!*ID || Killed.count(*ID)) continue; |
| 79 | Killed.insert(*ID); |
| 80 | (*ID)->Destroy(*Context); |
| 81 | } |
Ted Kremenek | 05ac3ef | 2008-06-06 21:05:33 +0000 | [diff] [blame] | 82 | |
| 83 | // FIXME: There is no clear ownership policy now for ObjCProtocolDecls |
| 84 | // referenced by ObjCForwardProtocolDecl. Some of them can be forward |
| 85 | // decls that are never later defined (and forward decls can be |
| 86 | // referenced by multiple ObjCClassDecls) or the ObjCProtocolDecl |
| 87 | // later becomes a real definition. |
| 88 | // Ideally we should have separate objects for forward declarations and |
| 89 | // definitions, obviating this problem. Because of this situation, |
| 90 | // referenced ObjCProtocolDecls are destroyed here. |
| 91 | if (ObjCForwardProtocolDecl* FDec = dyn_cast<ObjCForwardProtocolDecl>(*I)) |
| 92 | for (ObjCForwardProtocolDecl::iterator ID=FDec->begin(), |
| 93 | ED=FDec->end(); ID!=ED; ++ID) { |
| 94 | if (!*ID || Killed.count(*ID)) continue; |
| 95 | Killed.insert(*ID); |
| 96 | (*ID)->Destroy(*Context); |
| 97 | } |
| 98 | |
Ted Kremenek | 400d95f | 2008-06-06 20:11:53 +0000 | [diff] [blame] | 99 | |
Eli Friedman | 1108e7d | 2008-05-21 05:01:55 +0000 | [diff] [blame] | 100 | (*I)->Destroy(*Context); |
| 101 | } |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 102 | } |
Eli Friedman | 1108e7d | 2008-05-21 05:01:55 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 104 | if (OwnsMetaData && Context) { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 105 | // The ASTContext object has the sole references to the IdentifierTable |
| 106 | // Selectors, and the Target information. Go and delete them, since |
| 107 | // the TranslationUnit effectively owns them. |
Eli Friedman | 5f1adf8 | 2008-05-21 05:33:10 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 109 | delete &(Context->Idents); |
| 110 | delete &(Context->Selectors); |
| 111 | delete &(Context->Target); |
| 112 | delete Context; |
| 113 | } |
Sam Bishop | 71de20e | 2008-04-03 05:35:20 +0000 | [diff] [blame] | 114 | } |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 116 | bool clang::EmitASTBitcodeFile(const TranslationUnit* TU, |
| 117 | const llvm::sys::Path& Filename) { |
| 118 | |
| 119 | return TU ? EmitASTBitcodeFile(*TU, Filename) : false; |
| 120 | } |
| 121 | |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 122 | bool clang::EmitASTBitcodeBuffer(const TranslationUnit* TU, |
| 123 | std::vector<unsigned char>& Buffer) { |
| 124 | |
| 125 | return TU ? EmitASTBitcodeBuffer(*TU, Buffer) : false; |
| 126 | } |
| 127 | |
| 128 | bool clang::EmitASTBitcodeStream(const TranslationUnit* TU, |
| 129 | std::ostream& Stream) { |
| 130 | |
| 131 | return TU ? EmitASTBitcodeStream(*TU, Stream) : false; |
| 132 | } |
| 133 | |
| 134 | bool clang::EmitASTBitcodeBuffer(const TranslationUnit& TU, |
| 135 | std::vector<unsigned char>& Buffer) { |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 136 | // Create bitstream. |
| 137 | llvm::BitstreamWriter Stream(Buffer); |
| 138 | |
| 139 | // Emit the preamble. |
| 140 | Stream.Emit((unsigned)'B', 8); |
| 141 | Stream.Emit((unsigned)'C', 8); |
| 142 | Stream.Emit(0xC, 4); |
| 143 | Stream.Emit(0xF, 4); |
| 144 | Stream.Emit(0xE, 4); |
| 145 | Stream.Emit(0x0, 4); |
| 146 | |
| 147 | { |
| 148 | // Create serializer. Placing it in its own scope assures any necessary |
| 149 | // finalization of bits to the buffer in the serializer's dstor. |
| 150 | llvm::Serializer Sezr(Stream); |
| 151 | |
| 152 | // Emit the translation unit. |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 153 | TU.Emit(Sezr); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool clang::EmitASTBitcodeStream(const TranslationUnit& TU, |
| 160 | std::ostream& Stream) { |
| 161 | |
| 162 | // Reserve 256K for bitstream buffer. |
| 163 | std::vector<unsigned char> Buffer; |
| 164 | Buffer.reserve(256*1024); |
| 165 | |
| 166 | EmitASTBitcodeBuffer(TU,Buffer); |
| 167 | |
| 168 | // Write the bits to disk. |
| 169 | Stream.write((char*)&Buffer.front(), Buffer.size()); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool clang::EmitASTBitcodeFile(const TranslationUnit& TU, |
| 174 | const llvm::sys::Path& Filename) { |
| 175 | |
| 176 | // Reserve 256K for bitstream buffer. |
| 177 | std::vector<unsigned char> Buffer; |
| 178 | Buffer.reserve(256*1024); |
| 179 | |
| 180 | EmitASTBitcodeBuffer(TU,Buffer); |
| 181 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 182 | // Write the bits to disk. |
| 183 | if (FILE* fp = fopen(Filename.c_str(),"wb")) { |
| 184 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 185 | fclose(fp); |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | void TranslationUnit::Emit(llvm::Serializer& Sezr) const { |
| 193 | |
| 194 | // ===---------------------------------------------------===/ |
| 195 | // Serialize the top-level decls. |
| 196 | // ===---------------------------------------------------===/ |
| 197 | |
| 198 | Sezr.EnterBlock(DeclsBlock); |
| 199 | |
| 200 | // Only serialize the head of a decl chain. The ASTConsumer interfaces |
| 201 | // provides us with each top-level decl, including those nested in |
| 202 | // a decl chain, so we may be passed decls that are already serialized. |
| 203 | for (const_iterator I=begin(), E=end(); I!=E; ++I) |
| 204 | if (!Sezr.isRegistered(*I)) |
| 205 | Sezr.EmitOwnedPtr(*I); |
| 206 | |
| 207 | Sezr.ExitBlock(); |
| 208 | |
| 209 | // ===---------------------------------------------------===/ |
| 210 | // Serialize the "Translation Unit" metadata. |
| 211 | // ===---------------------------------------------------===/ |
| 212 | |
| 213 | // Emit ASTContext. |
| 214 | Sezr.EnterBlock(ASTContextBlock); |
| 215 | Sezr.EmitOwnedPtr(Context); |
| 216 | Sezr.ExitBlock(); |
| 217 | |
| 218 | Sezr.EnterBlock(BasicMetadataBlock); |
| 219 | |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 220 | // Block for SourceManager and Target. Allows easy skipping |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 221 | // around to the block for the Selectors during deserialization. |
| 222 | Sezr.EnterBlock(); |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 224 | // Emit the SourceManager. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 225 | Sezr.Emit(Context->getSourceManager()); |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 227 | // Emit the Target. |
| 228 | Sezr.EmitPtr(&Context->Target); |
| 229 | Sezr.EmitCStr(Context->Target.getTargetTriple()); |
| 230 | |
| 231 | Sezr.ExitBlock(); // exit "BasicMetadataBlock" |
| 232 | |
| 233 | // Emit the Selectors. |
| 234 | Sezr.Emit(Context->Selectors); |
| 235 | |
| 236 | // Emit the Identifier Table. |
| 237 | Sezr.Emit(Context->Idents); |
| 238 | |
| 239 | Sezr.ExitBlock(); // exit "ASTContextBlock" |
| 240 | } |
| 241 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 242 | TranslationUnit* |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 243 | clang::ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer, FileManager& FMgr) { |
| 244 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 245 | // Check if the file is of the proper length. |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 246 | if (MBuffer.getBufferSize() & 0x3) { |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 247 | // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes." |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | // Create the bitstream reader. |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 252 | unsigned char *BufPtr = (unsigned char *) MBuffer.getBufferStart(); |
| 253 | llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer.getBufferSize()); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 254 | |
| 255 | if (Stream.Read(8) != 'B' || |
| 256 | Stream.Read(8) != 'C' || |
| 257 | Stream.Read(4) != 0xC || |
| 258 | Stream.Read(4) != 0xF || |
| 259 | Stream.Read(4) != 0xE || |
| 260 | Stream.Read(4) != 0x0) { |
| 261 | // FIXME: Provide diagnostic. |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | // Create the deserializer. |
| 266 | llvm::Deserializer Dezr(Stream); |
| 267 | |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 268 | return TranslationUnit::Create(Dezr,FMgr); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Ted Kremenek | 0ce902b | 2008-07-10 22:10:48 +0000 | [diff] [blame] | 271 | TranslationUnit* |
| 272 | clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) { |
| 273 | |
| 274 | // Create the memory buffer that contains the contents of the file. |
| 275 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 276 | MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); |
| 277 | |
| 278 | if (!MBuffer) { |
| 279 | // FIXME: Provide diagnostic. |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | return ReadASTBitcodeBuffer(*MBuffer, FMgr); |
| 284 | } |
| 285 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 286 | TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr, |
| 287 | FileManager& FMgr) { |
| 288 | |
| 289 | // Create the translation unit object. |
| 290 | TranslationUnit* TU = new TranslationUnit(); |
| 291 | |
| 292 | // ===---------------------------------------------------===/ |
| 293 | // Deserialize the "Translation Unit" metadata. |
| 294 | // ===---------------------------------------------------===/ |
| 295 | |
| 296 | // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock |
| 297 | // (which will appear earlier) and record its location. |
| 298 | |
| 299 | bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock); |
| 300 | assert (FoundBlock); |
| 301 | |
| 302 | llvm::Deserializer::Location ASTContextBlockLoc = |
| 303 | Dezr.getCurrentBlockLocation(); |
| 304 | |
| 305 | FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock); |
| 306 | assert (FoundBlock); |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 308 | // Read the SourceManager. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 309 | SourceManager::CreateAndRegister(Dezr,FMgr); |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | bbced58 | 2007-12-12 18:05:32 +0000 | [diff] [blame] | 311 | { // Read the TargetInfo. |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 312 | llvm::SerializedPtrID PtrID = Dezr.ReadPtrID(); |
| 313 | char* triple = Dezr.ReadCStr(NULL,0,true); |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 314 | Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple))); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 315 | delete [] triple; |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // For Selectors, we must read the identifier table first because the |
| 319 | // SelectorTable depends on the identifiers being already deserialized. |
| 320 | llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation(); |
| 321 | Dezr.SkipBlock(); |
| 322 | |
| 323 | // Read the identifier table. |
| 324 | IdentifierTable::CreateAndRegister(Dezr); |
| 325 | |
| 326 | // Now jump back and read the selectors. |
| 327 | Dezr.JumpTo(SelectorBlkLoc); |
| 328 | SelectorTable::CreateAndRegister(Dezr); |
| 329 | |
| 330 | // Now jump back to ASTContextBlock and read the ASTContext. |
| 331 | Dezr.JumpTo(ASTContextBlockLoc); |
| 332 | TU->Context = Dezr.ReadOwnedPtr<ASTContext>(); |
| 333 | |
| 334 | // "Rewind" the stream. Find the block with the serialized top-level decls. |
| 335 | Dezr.Rewind(); |
| 336 | FoundBlock = Dezr.SkipToBlock(DeclsBlock); |
| 337 | assert (FoundBlock); |
| 338 | llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation(); |
| 339 | |
| 340 | while (!Dezr.FinishedBlock(DeclBlockLoc)) |
Sam Bishop | e2563ca | 2008-04-07 21:55:54 +0000 | [diff] [blame] | 341 | TU->AddTopLevelDecl(Dezr.ReadOwnedPtr<Decl>(*TU->Context)); |
Ted Kremenek | 2f74359 | 2007-12-05 21:36:08 +0000 | [diff] [blame] | 342 | |
| 343 | return TU; |
| 344 | } |
| 345 | |