68f6bf843d813fcd931880e6f1f3b07a32c41dfb
[oota-llvm.git] / lib / IR / DebugInfoMetadata.cpp
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
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));
134 }
135
136 MDEnumerator *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
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),
154                        Ops);
155 }
156
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),
169       Ops);
170 }
171
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,
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
192 MDSubroutineType *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
202 MDFile *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
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(
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
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,
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
263 MDLexicalBlock *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
272 MDLexicalBlockFile *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
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);
289 }
290
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);
301 }
302
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);
313 }
314
315 MDGlobalVariable *
316 MDGlobalVariable::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
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);
343 }
344
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));
350 }
351
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);
364 }
365
366 MDImportedEntity *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 }