1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the debug info Metadata classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/IR/DebugInfoMetadata.h" 15 #include "LLVMContextImpl.h" 16 #include "MetadataImpl.h" 17 18 using namespace llvm; 19 20 MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line, 21 unsigned Column, ArrayRef<Metadata *> MDs) 22 : MDNode(C, MDLocationKind, Storage, MDs) { 23 assert((MDs.size() == 1 || MDs.size() == 2) && 24 "Expected a scope and optional inlined-at"); 25 26 // Set line and column. 27 assert(Column < (1u << 16) && "Expected 16-bit column"); 28 29 SubclassData32 = Line; 30 SubclassData16 = Column; 31 } 32 33 static void adjustColumn(unsigned &Column) { 34 // Set to unknown on overflow. We only have 16 bits to play with here. 35 if (Column >= (1u << 16)) 36 Column = 0; 37 } 38 39 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line, 40 unsigned Column, Metadata *Scope, 41 Metadata *InlinedAt, StorageType Storage, 42 bool ShouldCreate) { 43 // Fixup column. 44 adjustColumn(Column); 45 46 if (Storage == Uniqued) { 47 if (auto *N = 48 getUniqued(Context.pImpl->MDLocations, 49 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt))) 50 return N; 51 if (!ShouldCreate) 52 return nullptr; 53 } else { 54 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 55 } 56 57 SmallVector<Metadata *, 2> Ops; 58 Ops.push_back(Scope); 59 if (InlinedAt) 60 Ops.push_back(InlinedAt); 61 return storeImpl(new (Ops.size()) 62 MDLocation(Context, Storage, Line, Column, Ops), 63 Storage, Context.pImpl->MDLocations); 64 } 65 66 static StringRef getString(const MDString *S) { 67 if (S) 68 return S->getString(); 69 return StringRef(); 70 } 71 72 #ifndef NDEBUG 73 static bool isCanonical(const MDString *S) { 74 return !S || !S->getString().empty(); 75 } 76 #endif 77 78 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag, 79 MDString *Header, 80 ArrayRef<Metadata *> DwarfOps, 81 StorageType Storage, 82 bool ShouldCreate) { 83 unsigned Hash = 0; 84 if (Storage == Uniqued) { 85 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps); 86 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key)) 87 return N; 88 if (!ShouldCreate) 89 return nullptr; 90 Hash = Key.getHash(); 91 } else { 92 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 93 } 94 95 // Use a nullptr for empty headers. 96 assert(isCanonical(Header) && "Expected canonical MDString"); 97 Metadata *PreOps[] = {Header}; 98 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode( 99 Context, Storage, Hash, Tag, PreOps, DwarfOps), 100 Storage, Context.pImpl->GenericDebugNodes); 101 } 102 103 void GenericDebugNode::recalculateHash() { 104 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this)); 105 } 106 107 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ 108 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS 109 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ 110 do { \ 111 if (Storage == Uniqued) { \ 112 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ 113 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ 114 return N; \ 115 if (!ShouldCreate) \ 116 return nullptr; \ 117 } else { \ 118 assert(ShouldCreate && \ 119 "Expected non-uniqued nodes to always be created"); \ 120 } \ 121 } while (false) 122 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \ 123 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \ 124 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ 125 Storage, Context.pImpl->CLASS##s) 126 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \ 127 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \ 128 Storage, Context.pImpl->CLASS##s) 129 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \ 130 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \ 131 CLASS(Context, Storage, OPS), \ 132 Storage, Context.pImpl->CLASS##s) 133 134 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo, 135 StorageType Storage, bool ShouldCreate) { 136 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo)); 137 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo)); 138 } 139 140 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value, 141 MDString *Name, StorageType Storage, 142 bool ShouldCreate) { 143 assert(isCanonical(Name) && "Expected canonical MDString"); 144 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name))); 145 Metadata *Ops[] = {Name}; 146 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops); 147 } 148 149 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag, 150 MDString *Name, unsigned SizeInBits, 151 unsigned AlignInBits, unsigned Encoding, 152 StorageType Storage, bool ShouldCreate) { 153 assert(isCanonical(Name) && "Expected canonical MDString"); 154 DEFINE_GETIMPL_LOOKUP( 155 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding)); 156 Metadata *Ops[] = {nullptr, nullptr, Name}; 157 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding), 158 Ops); 159 } 160 161 MDDerivedType *MDDerivedType::getImpl( 162 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 163 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits, 164 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags, 165 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) { 166 assert(isCanonical(Name) && "Expected canonical MDString"); 167 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope, 168 BaseType, SizeInBits, AlignInBits, 169 OffsetInBits, Flags, ExtraData)); 170 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData}; 171 DEFINE_GETIMPL_STORE( 172 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags), 173 Ops); 174 } 175 176 MDCompositeType *MDCompositeType::getImpl( 177 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 178 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits, 179 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags, 180 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, 181 Metadata *TemplateParams, MDString *Identifier, StorageType Storage, 182 bool ShouldCreate) { 183 assert(isCanonical(Name) && "Expected canonical MDString"); 184 DEFINE_GETIMPL_LOOKUP(MDCompositeType, 185 (Tag, getString(Name), File, Line, Scope, BaseType, 186 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, 187 RuntimeLang, VTableHolder, TemplateParams, 188 getString(Identifier))); 189 Metadata *Ops[] = {File, Scope, Name, BaseType, 190 Elements, VTableHolder, TemplateParams, Identifier}; 191 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits, 192 AlignInBits, OffsetInBits, Flags), 193 Ops); 194 } 195 196 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context, 197 unsigned Flags, Metadata *TypeArray, 198 StorageType Storage, 199 bool ShouldCreate) { 200 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray)); 201 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr, 202 TypeArray, nullptr, nullptr}; 203 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops); 204 } 205 206 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename, 207 MDString *Directory, StorageType Storage, 208 bool ShouldCreate) { 209 assert(isCanonical(Filename) && "Expected canonical MDString"); 210 assert(isCanonical(Directory) && "Expected canonical MDString"); 211 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory))); 212 Metadata *NodeOps[] = {Filename, Directory}; 213 Metadata *Ops[] = {MDTuple::get(Context, NodeOps)}; 214 return storeImpl(new (ArrayRef<Metadata *>(Ops).size()) 215 MDFile(Context, Storage, Ops), 216 Storage, Context.pImpl->MDFiles); 217 } 218 219 MDCompileUnit *MDCompileUnit::getImpl( 220 LLVMContext &Context, unsigned SourceLanguage, Metadata *File, 221 MDString *Producer, bool IsOptimized, MDString *Flags, 222 unsigned RuntimeVersion, MDString *SplitDebugFilename, 223 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, 224 Metadata *Subprograms, Metadata *GlobalVariables, 225 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) { 226 assert(isCanonical(Producer) && "Expected canonical MDString"); 227 assert(isCanonical(Flags) && "Expected canonical MDString"); 228 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString"); 229 DEFINE_GETIMPL_LOOKUP( 230 MDCompileUnit, 231 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags), 232 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes, 233 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities)); 234 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes, 235 RetainedTypes, Subprograms, GlobalVariables, 236 ImportedEntities}; 237 DEFINE_GETIMPL_STORE( 238 MDCompileUnit, 239 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops); 240 } 241 242 MDSubprogram *MDSubprogram::getImpl( 243 LLVMContext &Context, Metadata *Scope, MDString *Name, 244 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 245 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine, 246 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex, 247 unsigned Flags, bool IsOptimized, Metadata *Function, 248 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables, 249 StorageType Storage, bool ShouldCreate) { 250 assert(isCanonical(Name) && "Expected canonical MDString"); 251 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 252 DEFINE_GETIMPL_LOOKUP(MDSubprogram, 253 (Scope, getString(Name), getString(LinkageName), File, 254 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine, 255 ContainingType, Virtuality, VirtualIndex, Flags, 256 IsOptimized, Function, TemplateParams, Declaration, 257 Variables)); 258 Metadata *Ops[] = {File, Scope, Name, Name, 259 LinkageName, Type, ContainingType, Function, 260 TemplateParams, Declaration, Variables}; 261 DEFINE_GETIMPL_STORE(MDSubprogram, 262 (Line, ScopeLine, Virtuality, VirtualIndex, Flags, 263 IsLocalToUnit, IsDefinition, IsOptimized), 264 Ops); 265 } 266 267 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, 268 Metadata *File, unsigned Line, 269 unsigned Column, StorageType Storage, 270 bool ShouldCreate) { 271 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column)); 272 Metadata *Ops[] = {File, Scope}; 273 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops); 274 } 275 276 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context, 277 Metadata *Scope, Metadata *File, 278 unsigned Discriminator, 279 StorageType Storage, 280 bool ShouldCreate) { 281 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator)); 282 Metadata *Ops[] = {File, Scope}; 283 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops); 284 } 285 286 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope, 287 Metadata *File, MDString *Name, unsigned Line, 288 StorageType Storage, bool ShouldCreate) { 289 assert(isCanonical(Name) && "Expected canonical MDString"); 290 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line)); 291 Metadata *Ops[] = {File, Scope, Name}; 292 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops); 293 } 294 295 MDTemplateTypeParameter * 296 MDTemplateTypeParameter::getImpl(LLVMContext &Context, Metadata *Scope, 297 MDString *Name, Metadata *Type, 298 StorageType Storage, bool ShouldCreate) { 299 assert(isCanonical(Name) && "Expected canonical MDString"); 300 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, 301 (Scope, getString(Name), Type)); 302 Metadata *Ops[] = {Scope, Name, Type}; 303 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops); 304 } 305 306 MDTemplateValueParameter *MDTemplateValueParameter::getImpl( 307 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name, 308 Metadata *Type, Metadata *Value, StorageType Storage, bool ShouldCreate) { 309 assert(isCanonical(Name) && "Expected canonical MDString"); 310 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter, 311 (Tag, Scope, getString(Name), Type, Value)); 312 Metadata *Ops[] = {Scope, Name, Type, Value}; 313 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops); 314 } 315 316 MDGlobalVariable * 317 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 318 MDString *LinkageName, Metadata *File, unsigned Line, 319 Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 320 Metadata *Variable, 321 Metadata *StaticDataMemberDeclaration, 322 StorageType Storage, bool ShouldCreate) { 323 assert(isCanonical(Name) && "Expected canonical MDString"); 324 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 325 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable, 326 (Scope, getString(Name), getString(LinkageName), File, 327 Line, Type, IsLocalToUnit, IsDefinition, Variable, 328 StaticDataMemberDeclaration)); 329 Metadata *Ops[] = {Scope, Name, File, Type, 330 Name, LinkageName, Variable, StaticDataMemberDeclaration}; 331 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition), 332 Ops); 333 } 334 335 MDLocalVariable *MDLocalVariable::getImpl( 336 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name, 337 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags, 338 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) { 339 assert(isCanonical(Name) && "Expected canonical MDString"); 340 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File, 341 Line, Type, Arg, Flags, InlinedAt)); 342 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt}; 343 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops); 344 } 345 346 MDExpression *MDExpression::getImpl(LLVMContext &Context, 347 ArrayRef<uint64_t> Elements, 348 StorageType Storage, bool ShouldCreate) { 349 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements)); 350 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements)); 351 } 352 353 MDObjCProperty *MDObjCProperty::getImpl( 354 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, 355 MDString *GetterName, MDString *SetterName, unsigned Attributes, 356 Metadata *Type, StorageType Storage, bool ShouldCreate) { 357 assert(isCanonical(Name) && "Expected canonical MDString"); 358 assert(isCanonical(GetterName) && "Expected canonical MDString"); 359 assert(isCanonical(SetterName) && "Expected canonical MDString"); 360 DEFINE_GETIMPL_LOOKUP(MDObjCProperty, 361 (getString(Name), File, Line, getString(GetterName), 362 getString(SetterName), Attributes, Type)); 363 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; 364 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops); 365 } 366 367 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, 368 Metadata *Scope, Metadata *Entity, 369 unsigned Line, MDString *Name, 370 StorageType Storage, 371 bool ShouldCreate) { 372 assert(isCanonical(Name) && "Expected canonical MDString"); 373 DEFINE_GETIMPL_LOOKUP(MDImportedEntity, 374 (Tag, Scope, Entity, Line, getString(Name))); 375 Metadata *Ops[] = {Scope, Entity, Name}; 376 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops); 377 } 378