blob: 68f6bf843d813fcd931880e6f1f3b07a32c41dfb [file] [log] [blame]
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00001//===- 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
18using namespace llvm;
19
20MDLocation::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.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000027 assert(Column < (1u << 16) && "Expected 16-bit column");
28
29 SubclassData32 = Line;
30 SubclassData16 = Column;
31}
32
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000033static 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
39MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
40 unsigned Column, Metadata *Scope,
41 Metadata *InlinedAt, StorageType Storage,
42 bool ShouldCreate) {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +000043 // Fixup column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000044 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
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000066static StringRef getString(const MDString *S) {
67 if (S)
68 return S->getString();
69 return StringRef();
70}
71
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000072#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000073static bool isCanonical(const MDString *S) {
74 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000075}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000076#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000077
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000078GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000079 MDString *Header,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000080 ArrayRef<Metadata *> DwarfOps,
81 StorageType Storage,
82 bool ShouldCreate) {
83 unsigned Hash = 0;
84 if (Storage == Uniqued) {
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000085 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000086 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.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000096 assert(isCanonical(Header) && "Expected canonical MDString");
97 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000098 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
99 Context, Storage, Hash, Tag, PreOps, DwarfOps),
100 Storage, Context.pImpl->GenericDebugNodes);
101}
102
103void GenericDebugNode::recalculateHash() {
104 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
105}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000106
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
130MDSubrange *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));
134}
135
136MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
137 MDString *Name, StorageType Storage,
138 bool ShouldCreate) {
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);
143}
144
145MDBasicType *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),
154 Ops);
155}
156
157MDDerivedType *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),
169 Ops);
170}
171
172MDCompositeType *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,
178 bool ShouldCreate) {
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),
189 Ops);
190}
191
192MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
193 unsigned Flags, Metadata *TypeArray,
194 StorageType Storage,
195 bool ShouldCreate) {
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);
200}
201
202MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
203 MDString *Directory, StorageType Storage,
204 bool ShouldCreate) {
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);
213}
214
215MDCompileUnit *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(
226 MDCompileUnit,
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,
232 ImportedEntities};
233 DEFINE_GETIMPL_STORE(
234 MDCompileUnit,
235 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
236}
237
238MDSubprogram *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,
253 Variables));
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),
260 Ops);
261}
262
263MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
264 Metadata *File, unsigned Line,
265 unsigned Column, StorageType Storage,
266 bool ShouldCreate) {
267 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
268 Metadata *Ops[] = {File, Scope};
269 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
270}
271
272MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
273 Metadata *Scope, Metadata *File,
274 unsigned Discriminator,
275 StorageType Storage,
276 bool ShouldCreate) {
277 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
278 Metadata *Ops[] = {File, Scope};
279 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
280}
281
282MDNamespace *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);
289}
290
291MDTemplateTypeParameter *
292MDTemplateTypeParameter::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);
301}
302
303MDTemplateValueParameter *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);
313}
314
315MDGlobalVariable *
316MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
317 MDString *LinkageName, Metadata *File, unsigned Line,
318 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
319 Metadata *Variable,
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),
331 Ops);
332}
333
334MDLocalVariable *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);
343}
344
345MDExpression *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));
350}
351
352MDObjCProperty *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);
364}
365
366MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
367 Metadata *Scope, Metadata *Entity,
368 unsigned Line, MDString *Name,
369 StorageType Storage,
370 bool ShouldCreate) {
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);
376}