1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the debug info Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/DebugInfoMetadata.h"
15 #include "LLVMContextImpl.h"
16 #include "MetadataImpl.h"
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");
26 // Set line and column.
27 assert(Column < (1u << 16) && "Expected 16-bit column");
29 SubclassData32 = Line;
30 SubclassData16 = Column;
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))
39 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
40 unsigned Column, Metadata *Scope,
41 Metadata *InlinedAt, StorageType Storage,
46 if (Storage == Uniqued) {
48 getUniqued(Context.pImpl->MDLocations,
49 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
54 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
57 SmallVector<Metadata *, 2> Ops;
60 Ops.push_back(InlinedAt);
61 return storeImpl(new (Ops.size())
62 MDLocation(Context, Storage, Line, Column, Ops),
63 Storage, Context.pImpl->MDLocations);
66 static StringRef getString(const MDString *S) {
68 return S->getString();
73 static bool isCanonical(const MDString *S) {
74 return !S || !S->getString().empty();
78 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
80 ArrayRef<Metadata *> DwarfOps,
84 if (Storage == Uniqued) {
85 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
86 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
92 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
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);
103 void GenericDebugNode::recalculateHash() {
104 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
107 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
108 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
109 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
111 if (Storage == Uniqued) { \
112 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
113 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
118 assert(ShouldCreate && \
119 "Expected non-uniqued nodes to always be created"); \
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)
130 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
131 StorageType Storage, bool ShouldCreate) {
132 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
133 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
136 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
137 MDString *Name, StorageType Storage,
139 assert(isCanonical(Name) && "Expected canonical MDString");
140 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
141 Metadata *Ops[] = {Name};
142 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
145 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
146 MDString *Name, unsigned SizeInBits,
147 unsigned AlignInBits, unsigned Encoding,
148 StorageType Storage, bool ShouldCreate) {
149 assert(isCanonical(Name) && "Expected canonical MDString");
150 DEFINE_GETIMPL_LOOKUP(
151 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
152 Metadata *Ops[] = {nullptr, nullptr, Name};
153 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
157 MDDerivedType *MDDerivedType::getImpl(
158 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
159 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
160 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
161 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
162 assert(isCanonical(Name) && "Expected canonical MDString");
163 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
164 BaseType, SizeInBits, AlignInBits,
165 OffsetInBits, Flags, ExtraData));
166 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
167 DEFINE_GETIMPL_STORE(
168 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
172 MDCompositeType *MDCompositeType::getImpl(
173 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
174 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
175 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
176 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
177 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
179 assert(isCanonical(Name) && "Expected canonical MDString");
180 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
181 (Tag, getString(Name), File, Line, Scope, BaseType,
182 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
183 RuntimeLang, VTableHolder, TemplateParams,
184 getString(Identifier)));
185 Metadata *Ops[] = {File, Scope, Name, BaseType,
186 Elements, VTableHolder, TemplateParams, Identifier};
187 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
188 AlignInBits, OffsetInBits, Flags),
192 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
193 unsigned Flags, Metadata *TypeArray,
196 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
197 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
198 TypeArray, nullptr, nullptr};
199 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
202 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
203 MDString *Directory, StorageType Storage,
205 assert(isCanonical(Filename) && "Expected canonical MDString");
206 assert(isCanonical(Directory) && "Expected canonical MDString");
207 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
208 Metadata *NodeOps[] = {Filename, Directory};
209 Metadata *Ops[] = {MDTuple::get(Context, NodeOps)};
210 return storeImpl(new (ArrayRef<Metadata *>(Ops).size())
211 MDFile(Context, Storage, Ops),
212 Storage, Context.pImpl->MDFiles);
215 MDCompileUnit *MDCompileUnit::getImpl(
216 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
217 MDString *Producer, bool IsOptimized, MDString *Flags,
218 unsigned RuntimeVersion, MDString *SplitDebugFilename,
219 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
220 Metadata *Subprograms, Metadata *GlobalVariables,
221 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
222 assert(isCanonical(Producer) && "Expected canonical MDString");
223 assert(isCanonical(Flags) && "Expected canonical MDString");
224 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
225 DEFINE_GETIMPL_LOOKUP(
227 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
228 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
229 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
230 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
231 RetainedTypes, Subprograms, GlobalVariables,
233 DEFINE_GETIMPL_STORE(
235 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
238 MDSubprogram *MDSubprogram::getImpl(
239 LLVMContext &Context, Metadata *Scope, MDString *Name,
240 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
241 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
242 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
243 unsigned Flags, bool IsOptimized, Metadata *Function,
244 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
245 StorageType Storage, bool ShouldCreate) {
246 assert(isCanonical(Name) && "Expected canonical MDString");
247 assert(isCanonical(LinkageName) && "Expected canonical MDString");
248 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
249 (Scope, getString(Name), getString(LinkageName), File,
250 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
251 ContainingType, Virtuality, VirtualIndex, Flags,
252 IsOptimized, Function, TemplateParams, Declaration,
254 Metadata *Ops[] = {File, Scope, Name, Name,
255 LinkageName, Type, ContainingType, Function,
256 TemplateParams, Declaration, Variables};
257 DEFINE_GETIMPL_STORE(MDSubprogram,
258 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
259 IsLocalToUnit, IsDefinition, IsOptimized),
263 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
264 Metadata *File, unsigned Line,
265 unsigned Column, StorageType Storage,
267 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
268 Metadata *Ops[] = {File, Scope};
269 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
272 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
273 Metadata *Scope, Metadata *File,
274 unsigned Discriminator,
277 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
278 Metadata *Ops[] = {File, Scope};
279 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
282 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
283 Metadata *File, MDString *Name, unsigned Line,
284 StorageType Storage, bool ShouldCreate) {
285 assert(isCanonical(Name) && "Expected canonical MDString");
286 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
287 Metadata *Ops[] = {File, Scope, Name};
288 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
291 MDTemplateTypeParameter *
292 MDTemplateTypeParameter::getImpl(LLVMContext &Context, Metadata *Scope,
293 MDString *Name, Metadata *Type, Metadata *File,
294 unsigned Line, unsigned Column,
295 StorageType Storage, bool ShouldCreate) {
296 assert(isCanonical(Name) && "Expected canonical MDString");
297 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter,
298 (Scope, getString(Name), Type, File, Line, Column));
299 Metadata *Ops[] = {File, Scope, Name, Type};
300 DEFINE_GETIMPL_STORE(MDTemplateTypeParameter, (Line, Column), Ops);
303 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
304 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
305 Metadata *Type, Metadata *Value, Metadata *File, unsigned Line,
306 unsigned Column, StorageType Storage, bool ShouldCreate) {
307 assert(isCanonical(Name) && "Expected canonical MDString");
308 DEFINE_GETIMPL_LOOKUP(
309 MDTemplateValueParameter,
310 (Tag, Scope, getString(Name), Type, Value, File, Line, Column));
311 Metadata *Ops[] = {File, Scope, Name, Type, Value};
312 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag, Line, Column), Ops);
316 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
317 MDString *LinkageName, Metadata *File, unsigned Line,
318 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
320 Metadata *StaticDataMemberDeclaration,
321 StorageType Storage, bool ShouldCreate) {
322 assert(isCanonical(Name) && "Expected canonical MDString");
323 assert(isCanonical(LinkageName) && "Expected canonical MDString");
324 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
325 (Scope, getString(Name), getString(LinkageName), File,
326 Line, Type, IsLocalToUnit, IsDefinition, Variable,
327 StaticDataMemberDeclaration));
328 Metadata *Ops[] = {Scope, Name, File, Type,
329 Name, LinkageName, Variable, StaticDataMemberDeclaration};
330 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
334 MDLocalVariable *MDLocalVariable::getImpl(
335 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
336 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
337 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
338 assert(isCanonical(Name) && "Expected canonical MDString");
339 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
340 Line, Type, Arg, Flags, InlinedAt));
341 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
342 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
345 MDExpression *MDExpression::getImpl(LLVMContext &Context,
346 ArrayRef<uint64_t> Elements,
347 StorageType Storage, bool ShouldCreate) {
348 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
349 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
352 MDObjCProperty *MDObjCProperty::getImpl(
353 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
354 MDString *GetterName, MDString *SetterName, unsigned Attributes,
355 Metadata *Type, StorageType Storage, bool ShouldCreate) {
356 assert(isCanonical(Name) && "Expected canonical MDString");
357 assert(isCanonical(GetterName) && "Expected canonical MDString");
358 assert(isCanonical(SetterName) && "Expected canonical MDString");
359 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
360 (getString(Name), File, Line, getString(GetterName),
361 getString(SetterName), Attributes, Type));
362 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
363 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
366 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
367 Metadata *Scope, Metadata *Entity,
368 unsigned Line, MDString *Name,
371 assert(isCanonical(Name) && "Expected canonical MDString");
372 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
373 (Tag, Scope, Entity, Line, getString(Name)));
374 Metadata *Ops[] = {Scope, Entity, Name};
375 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);