DebugInfo: Move DIFlag-related API from DIDescriptor to DebugNode
[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 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/IR/Function.h"
19
20 using namespace llvm;
21
22 MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
23                        unsigned Column, ArrayRef<Metadata *> MDs)
24     : MDNode(C, MDLocationKind, Storage, MDs) {
25   assert((MDs.size() == 1 || MDs.size() == 2) &&
26          "Expected a scope and optional inlined-at");
27
28   // Set line and column.
29   assert(Column < (1u << 16) && "Expected 16-bit column");
30
31   SubclassData32 = Line;
32   SubclassData16 = Column;
33 }
34
35 static void adjustColumn(unsigned &Column) {
36   // Set to unknown on overflow.  We only have 16 bits to play with here.
37   if (Column >= (1u << 16))
38     Column = 0;
39 }
40
41 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
42                                 unsigned Column, Metadata *Scope,
43                                 Metadata *InlinedAt, StorageType Storage,
44                                 bool ShouldCreate) {
45   // Fixup column.
46   adjustColumn(Column);
47
48   assert(Scope && "Expected scope");
49   if (Storage == Uniqued) {
50     if (auto *N =
51             getUniqued(Context.pImpl->MDLocations,
52                        MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
53       return N;
54     if (!ShouldCreate)
55       return nullptr;
56   } else {
57     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58   }
59
60   SmallVector<Metadata *, 2> Ops;
61   Ops.push_back(Scope);
62   if (InlinedAt)
63     Ops.push_back(InlinedAt);
64   return storeImpl(new (Ops.size())
65                        MDLocation(Context, Storage, Line, Column, Ops),
66                    Storage, Context.pImpl->MDLocations);
67 }
68
69 unsigned DebugNode::getFlag(StringRef Flag) {
70   return StringSwitch<unsigned>(Flag)
71 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72 #include "llvm/IR/DebugInfoFlags.def"
73       .Default(0);
74 }
75
76 const char *DebugNode::getFlagString(unsigned Flag) {
77   switch (Flag) {
78   default:
79     return "";
80 #define HANDLE_DI_FLAG(ID, NAME)                                               \
81   case Flag##NAME:                                                             \
82     return "DIFlag" #NAME;
83 #include "llvm/IR/DebugInfoFlags.def"
84   }
85 }
86
87 unsigned DebugNode::splitFlags(unsigned Flags,
88                                SmallVectorImpl<unsigned> &SplitFlags) {
89   // Accessibility flags need to be specially handled, since they're packed
90   // together.
91   if (unsigned A = Flags & FlagAccessibility) {
92     if (A == FlagPrivate)
93       SplitFlags.push_back(FlagPrivate);
94     else if (A == FlagProtected)
95       SplitFlags.push_back(FlagProtected);
96     else
97       SplitFlags.push_back(FlagPublic);
98     Flags &= ~A;
99   }
100
101 #define HANDLE_DI_FLAG(ID, NAME)                                               \
102   if (unsigned Bit = Flags & ID) {                                             \
103     SplitFlags.push_back(Bit);                                                 \
104     Flags &= ~Bit;                                                             \
105   }
106 #include "llvm/IR/DebugInfoFlags.def"
107
108   return Flags;
109 }
110
111
112 static StringRef getString(const MDString *S) {
113   if (S)
114     return S->getString();
115   return StringRef();
116 }
117
118 #ifndef NDEBUG
119 static bool isCanonical(const MDString *S) {
120   return !S || !S->getString().empty();
121 }
122 #endif
123
124 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
125                                             MDString *Header,
126                                             ArrayRef<Metadata *> DwarfOps,
127                                             StorageType Storage,
128                                             bool ShouldCreate) {
129   unsigned Hash = 0;
130   if (Storage == Uniqued) {
131     GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
132     if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
133       return N;
134     if (!ShouldCreate)
135       return nullptr;
136     Hash = Key.getHash();
137   } else {
138     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
139   }
140
141   // Use a nullptr for empty headers.
142   assert(isCanonical(Header) && "Expected canonical MDString");
143   Metadata *PreOps[] = {Header};
144   return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
145                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
146                    Storage, Context.pImpl->GenericDebugNodes);
147 }
148
149 void GenericDebugNode::recalculateHash() {
150   setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
151 }
152
153 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
154 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
155 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
156   do {                                                                         \
157     if (Storage == Uniqued) {                                                  \
158       if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
159                                CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
160         return N;                                                              \
161       if (!ShouldCreate)                                                       \
162         return nullptr;                                                        \
163     } else {                                                                   \
164       assert(ShouldCreate &&                                                   \
165              "Expected non-uniqued nodes to always be created");               \
166     }                                                                          \
167   } while (false)
168 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
169   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
170                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
171                    Storage, Context.pImpl->CLASS##s)
172 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
173   return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)),        \
174                    Storage, Context.pImpl->CLASS##s)
175 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
176   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
177                        CLASS(Context, Storage, OPS),                           \
178                    Storage, Context.pImpl->CLASS##s)
179
180 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
181                                 StorageType Storage, bool ShouldCreate) {
182   DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
183   DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
184 }
185
186 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
187                                     MDString *Name, StorageType Storage,
188                                     bool ShouldCreate) {
189   assert(isCanonical(Name) && "Expected canonical MDString");
190   DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
191   Metadata *Ops[] = {Name};
192   DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
193 }
194
195 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
196                                   MDString *Name, uint64_t SizeInBits,
197                                   uint64_t AlignInBits, unsigned Encoding,
198                                   StorageType Storage, bool ShouldCreate) {
199   assert(isCanonical(Name) && "Expected canonical MDString");
200   DEFINE_GETIMPL_LOOKUP(
201       MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
202   Metadata *Ops[] = {nullptr, nullptr, Name};
203   DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
204                        Ops);
205 }
206
207 MDDerivedType *MDDerivedType::getImpl(
208     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
209     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
210     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
211     Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
212   assert(isCanonical(Name) && "Expected canonical MDString");
213   DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
214                                         BaseType, SizeInBits, AlignInBits,
215                                         OffsetInBits, Flags, ExtraData));
216   Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
217   DEFINE_GETIMPL_STORE(
218       MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
219       Ops);
220 }
221
222 MDCompositeType *MDCompositeType::getImpl(
223     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
224     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
225     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
226     Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
227     Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
228     bool ShouldCreate) {
229   assert(isCanonical(Name) && "Expected canonical MDString");
230   DEFINE_GETIMPL_LOOKUP(MDCompositeType,
231                         (Tag, getString(Name), File, Line, Scope, BaseType,
232                          SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
233                          RuntimeLang, VTableHolder, TemplateParams,
234                          getString(Identifier)));
235   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
236                      Elements, VTableHolder, TemplateParams, Identifier};
237   DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
238                                          AlignInBits, OffsetInBits, Flags),
239                        Ops);
240 }
241
242 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
243                                             unsigned Flags, Metadata *TypeArray,
244                                             StorageType Storage,
245                                             bool ShouldCreate) {
246   DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
247   Metadata *Ops[] = {nullptr,   nullptr, nullptr, nullptr,
248                      TypeArray, nullptr, nullptr, nullptr};
249   DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
250 }
251
252 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
253                         MDString *Directory, StorageType Storage,
254                         bool ShouldCreate) {
255   assert(isCanonical(Filename) && "Expected canonical MDString");
256   assert(isCanonical(Directory) && "Expected canonical MDString");
257   DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
258   Metadata *Ops[] = {Filename, Directory};
259   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
260 }
261
262 MDCompileUnit *MDCompileUnit::getImpl(
263     LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
264     MDString *Producer, bool IsOptimized, MDString *Flags,
265     unsigned RuntimeVersion, MDString *SplitDebugFilename,
266     unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
267     Metadata *Subprograms, Metadata *GlobalVariables,
268     Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
269   assert(isCanonical(Producer) && "Expected canonical MDString");
270   assert(isCanonical(Flags) && "Expected canonical MDString");
271   assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
272   DEFINE_GETIMPL_LOOKUP(
273       MDCompileUnit,
274       (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
275        RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
276        RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
277   Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
278                      RetainedTypes, Subprograms, GlobalVariables,
279                      ImportedEntities};
280   DEFINE_GETIMPL_STORE(
281       MDCompileUnit,
282       (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
283 }
284
285 MDSubprogram *MDLocalScope::getSubprogram() const {
286   if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
287     return Block->getScope()->getSubprogram();
288   return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
289 }
290
291 MDSubprogram *MDSubprogram::getImpl(
292     LLVMContext &Context, Metadata *Scope, MDString *Name,
293     MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
294     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
295     Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
296     unsigned Flags, bool IsOptimized, Metadata *Function,
297     Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
298     StorageType Storage, bool ShouldCreate) {
299   assert(isCanonical(Name) && "Expected canonical MDString");
300   assert(isCanonical(LinkageName) && "Expected canonical MDString");
301   DEFINE_GETIMPL_LOOKUP(MDSubprogram,
302                         (Scope, getString(Name), getString(LinkageName), File,
303                          Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
304                          ContainingType, Virtuality, VirtualIndex, Flags,
305                          IsOptimized, Function, TemplateParams, Declaration,
306                          Variables));
307   Metadata *Ops[] = {File,           Scope,       Name,           Name,
308                      LinkageName,    Type,        ContainingType, Function,
309                      TemplateParams, Declaration, Variables};
310   DEFINE_GETIMPL_STORE(MDSubprogram,
311                        (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
312                         IsLocalToUnit, IsDefinition, IsOptimized),
313                        Ops);
314 }
315
316 void MDSubprogram::replaceFunction(Function *F) {
317   replaceFunction(F ? ConstantAsMetadata::get(F)
318                     : static_cast<ConstantAsMetadata *>(nullptr));
319 }
320
321 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
322                                         Metadata *File, unsigned Line,
323                                         unsigned Column, StorageType Storage,
324                                         bool ShouldCreate) {
325   assert(Scope && "Expected scope");
326   DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
327   Metadata *Ops[] = {File, Scope};
328   DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
329 }
330
331 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
332                                                 Metadata *Scope, Metadata *File,
333                                                 unsigned Discriminator,
334                                                 StorageType Storage,
335                                                 bool ShouldCreate) {
336   assert(Scope && "Expected scope");
337   DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
338   Metadata *Ops[] = {File, Scope};
339   DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
340 }
341
342 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
343                                   Metadata *File, MDString *Name, unsigned Line,
344                                   StorageType Storage, bool ShouldCreate) {
345   assert(isCanonical(Name) && "Expected canonical MDString");
346   DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
347   Metadata *Ops[] = {File, Scope, Name};
348   DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
349 }
350
351 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
352                                                           MDString *Name,
353                                                           Metadata *Type,
354                                                           StorageType Storage,
355                                                           bool ShouldCreate) {
356   assert(isCanonical(Name) && "Expected canonical MDString");
357   DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
358   Metadata *Ops[] = {Name, Type};
359   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
360 }
361
362 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
363     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
364     Metadata *Value, StorageType Storage, bool ShouldCreate) {
365   assert(isCanonical(Name) && "Expected canonical MDString");
366   DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
367                         (Tag, getString(Name), Type, Value));
368   Metadata *Ops[] = {Name, Type, Value};
369   DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
370 }
371
372 MDGlobalVariable *
373 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
374                           MDString *LinkageName, Metadata *File, unsigned Line,
375                           Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
376                           Metadata *Variable,
377                           Metadata *StaticDataMemberDeclaration,
378                           StorageType Storage, bool ShouldCreate) {
379   assert(isCanonical(Name) && "Expected canonical MDString");
380   assert(isCanonical(LinkageName) && "Expected canonical MDString");
381   DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
382                         (Scope, getString(Name), getString(LinkageName), File,
383                          Line, Type, IsLocalToUnit, IsDefinition, Variable,
384                          StaticDataMemberDeclaration));
385   Metadata *Ops[] = {Scope, Name,        File,     Type,
386                      Name,  LinkageName, Variable, StaticDataMemberDeclaration};
387   DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
388                        Ops);
389 }
390
391 MDLocalVariable *MDLocalVariable::getImpl(
392     LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
393     Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
394     Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
395   // Truncate Arg to 8 bits.
396   //
397   // FIXME: This is gross (and should be changed to an assert or removed), but
398   // it matches historical behaviour for now.
399   Arg &= (1u << 8) - 1;
400
401   assert(Scope && "Expected scope");
402   assert(isCanonical(Name) && "Expected canonical MDString");
403   DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
404                                           Line, Type, Arg, Flags, InlinedAt));
405   Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
406   DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
407 }
408
409 MDExpression *MDExpression::getImpl(LLVMContext &Context,
410                                     ArrayRef<uint64_t> Elements,
411                                     StorageType Storage, bool ShouldCreate) {
412   DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
413   DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
414 }
415
416 unsigned MDExpression::ExprOperand::getSize() const {
417   switch (getOp()) {
418   case dwarf::DW_OP_bit_piece:
419     return 3;
420   case dwarf::DW_OP_plus:
421     return 2;
422   default:
423     return 1;
424   }
425 }
426
427 bool MDExpression::isValid() const {
428   for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
429     // Check that there's space for the operand.
430     if (I->get() + I->getSize() > E->get())
431       return false;
432
433     // Check that the operand is valid.
434     switch (I->getOp()) {
435     default:
436       return false;
437     case dwarf::DW_OP_bit_piece:
438       // Piece expressions must be at the end.
439       return I->get() + I->getSize() == E->get();
440     case dwarf::DW_OP_plus:
441     case dwarf::DW_OP_deref:
442       break;
443     }
444   }
445   return true;
446 }
447
448 MDObjCProperty *MDObjCProperty::getImpl(
449     LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
450     MDString *GetterName, MDString *SetterName, unsigned Attributes,
451     Metadata *Type, StorageType Storage, bool ShouldCreate) {
452   assert(isCanonical(Name) && "Expected canonical MDString");
453   assert(isCanonical(GetterName) && "Expected canonical MDString");
454   assert(isCanonical(SetterName) && "Expected canonical MDString");
455   DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
456                         (getString(Name), File, Line, getString(GetterName),
457                          getString(SetterName), Attributes, Type));
458   Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
459   DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
460 }
461
462 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
463                                             Metadata *Scope, Metadata *Entity,
464                                             unsigned Line, MDString *Name,
465                                             StorageType Storage,
466                                             bool ShouldCreate) {
467   assert(isCanonical(Name) && "Expected canonical MDString");
468   DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
469                         (Tag, Scope, Entity, Line, getString(Name)));
470   Metadata *Ops[] = {Scope, Entity, Name};
471   DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);
472 }