1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
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 defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_DEBUGINFO_H
18 #define LLVM_DEBUGINFO_H
20 #include "llvm/Support/Casting.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Dwarf.h"
28 namespace llvm {
29 class BasicBlock;
30 class Constant;
31 class Function;
32 class GlobalVariable;
33 class Module;
34 class Type;
35 class Value;
36 class DbgDeclareInst;
37 class DbgValueInst;
38 class Instruction;
39 class MDNode;
40 class MDString;
41 class NamedMDNode;
42 class LLVMContext;
43 class raw_ostream;
45 class DIFile;
46 class DISubprogram;
47 class DILexicalBlock;
48 class DILexicalBlockFile;
49 class DIVariable;
50 class DIType;
51 class DIScope;
52 class DIObjCProperty;
54 /// Maps from type identifier to the actual MDNode.
55 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
57 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
58 /// This should not be stored in a container, because the underlying MDNode
59 /// may change in certain situations.
60 class DIDescriptor {
61 // Befriends DIRef so DIRef can befriend the protected member
62 // function: getFieldAs<DIRef>.
63 template <typename T> friend class DIRef;
65 public:
66 enum {
67 FlagPrivate = 1 << 0,
68 FlagProtected = 1 << 1,
69 FlagFwdDecl = 1 << 2,
70 FlagAppleBlock = 1 << 3,
71 FlagBlockByrefStruct = 1 << 4,
72 FlagVirtual = 1 << 5,
73 FlagArtificial = 1 << 6,
74 FlagExplicit = 1 << 7,
75 FlagPrototyped = 1 << 8,
76 FlagObjcClassComplete = 1 << 9,
77 FlagObjectPointer = 1 << 10,
78 FlagVector = 1 << 11,
79 FlagStaticMember = 1 << 12,
80 FlagIndirectVariable = 1 << 13
81 };
83 protected:
84 const MDNode *DbgNode;
86 StringRef getStringField(unsigned Elt) const;
87 unsigned getUnsignedField(unsigned Elt) const {
88 return (unsigned)getUInt64Field(Elt);
89 }
90 uint64_t getUInt64Field(unsigned Elt) const;
91 int64_t getInt64Field(unsigned Elt) const;
92 DIDescriptor getDescriptorField(unsigned Elt) const;
94 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
95 return DescTy(getDescriptorField(Elt));
96 }
98 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
99 Constant *getConstantField(unsigned Elt) const;
100 Function *getFunctionField(unsigned Elt) const;
101 void replaceFunctionField(unsigned Elt, Function *F);
103 public:
104 explicit DIDescriptor(const MDNode *N = 0) : DbgNode(N) {}
106 bool Verify() const;
108 operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
109 MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
111 // An explicit operator bool so that we can do testing of DI values
112 // easily.
113 // FIXME: This operator bool isn't actually protecting anything at the
114 // moment due to the conversion operator above making DIDescriptor nodes
115 // implicitly convertable to bool.
116 LLVM_EXPLICIT operator bool() const { return DbgNode != 0; }
118 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
119 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
121 uint16_t getTag() const {
122 return getUnsignedField(0) & ~LLVMDebugVersionMask;
123 }
125 bool isDerivedType() const;
126 bool isCompositeType() const;
127 bool isBasicType() const;
128 bool isVariable() const;
129 bool isSubprogram() const;
130 bool isGlobalVariable() const;
131 bool isScope() const;
132 bool isFile() const;
133 bool isCompileUnit() const;
134 bool isNameSpace() const;
135 bool isLexicalBlockFile() const;
136 bool isLexicalBlock() const;
137 bool isSubrange() const;
138 bool isEnumerator() const;
139 bool isType() const;
140 bool isUnspecifiedParameter() const;
141 bool isTemplateTypeParameter() const;
142 bool isTemplateValueParameter() const;
143 bool isObjCProperty() const;
144 bool isImportedEntity() const;
146 /// print - print descriptor.
147 void print(raw_ostream &OS) const;
149 /// dump - print descriptor to dbgs() with a newline.
150 void dump() const;
151 };
153 /// DISubrange - This is used to represent ranges, for array bounds.
154 class DISubrange : public DIDescriptor {
155 friend class DIDescriptor;
156 void printInternal(raw_ostream &OS) const;
158 public:
159 explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
161 int64_t getLo() const { return getInt64Field(1); }
162 int64_t getCount() const { return getInt64Field(2); }
163 bool Verify() const;
164 };
166 /// DIArray - This descriptor holds an array of descriptors.
167 class DIArray : public DIDescriptor {
168 public:
169 explicit DIArray(const MDNode *N = 0) : DIDescriptor(N) {}
171 unsigned getNumElements() const;
172 DIDescriptor getElement(unsigned Idx) const {
173 return getDescriptorField(Idx);
174 }
175 };
177 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
178 /// FIXME: it seems strange that this doesn't have either a reference to the
179 /// type/precision or a file/line pair for location info.
180 class DIEnumerator : public DIDescriptor {
181 friend class DIDescriptor;
182 void printInternal(raw_ostream &OS) const;
184 public:
185 explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
187 StringRef getName() const { return getStringField(1); }
188 int64_t getEnumValue() const { return getInt64Field(2); }
189 bool Verify() const;
190 };
192 template <typename T> class DIRef;
193 typedef DIRef<DIScope> DIScopeRef;
194 typedef DIRef<DIType> DITypeRef;
196 /// DIScope - A base class for various scopes.
197 class DIScope : public DIDescriptor {
198 protected:
199 friend class DIDescriptor;
200 void printInternal(raw_ostream &OS) const;
202 public:
203 explicit DIScope(const MDNode *N = 0) : DIDescriptor(N) {}
205 /// Gets the parent scope for this scope node or returns a
206 /// default constructed scope.
207 DIScopeRef getContext() const;
208 /// If the scope node has a name, return that, else return an empty string.
209 StringRef getName() const;
210 StringRef getFilename() const;
211 StringRef getDirectory() const;
213 /// Generate a reference to this DIScope. Uses the type identifier instead
214 /// of the actual MDNode if possible, to help type uniquing.
215 DIScopeRef getRef() const;
216 };
218 /// Represents reference to a DIDescriptor, abstracts over direct and
219 /// identifier-based metadata references.
220 template <typename T> class DIRef {
221 template <typename DescTy>
222 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
223 friend DIScopeRef DIScope::getContext() const;
224 friend DIScopeRef DIScope::getRef() const;
226 /// Val can be either a MDNode or a MDString, in the latter,
227 /// MDString specifies the type identifier.
228 const Value *Val;
229 explicit DIRef(const Value *V);
231 public:
232 T resolve(const DITypeIdentifierMap &Map) const;
233 StringRef getName() const {
234 if (!Val)
235 return StringRef();
237 if (const MDNode *MD = dyn_cast<MDNode>(Val))
238 return T(MD).getName();
240 const MDString *MS = cast<MDString>(Val);
241 return MS->getString();
242 }
243 operator Value *() const { return const_cast<Value *>(Val); }
244 };
246 template <typename T>
247 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
248 if (!Val)
249 return T();
251 if (const MDNode *MD = dyn_cast<MDNode>(Val))
252 return T(MD);
254 const MDString *MS = cast<MDString>(Val);
255 // Find the corresponding MDNode.
256 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
257 assert(Iter != Map.end() && "Identifier not in the type map?");
258 assert(DIDescriptor(Iter->second).isType() &&
259 "MDNode in DITypeIdentifierMap should be a DIType.");
260 return T(Iter->second);
261 }
263 /// Specialize getFieldAs to handle fields that are references to DIScopes.
264 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
265 /// Specialize DIRef constructor for DIScopeRef.
266 template <> DIRef<DIScope>::DIRef(const Value *V);
268 /// Specialize getFieldAs to handle fields that are references to DITypes.
269 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
270 /// Specialize DIRef constructor for DITypeRef.
271 template <> DIRef<DIType>::DIRef(const Value *V);
273 /// DIType - This is a wrapper for a type.
274 /// FIXME: Types should be factored much better so that CV qualifiers and
275 /// others do not require a huge and empty descriptor full of zeros.
276 class DIType : public DIScope {
277 protected:
278 friend class DIDescriptor;
279 void printInternal(raw_ostream &OS) const;
281 public:
282 DIType(const MDNode *N = 0) : DIScope(N) {}
284 /// Verify - Verify that a type descriptor is well formed.
285 bool Verify() const;
287 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
288 StringRef getName() const { return getStringField(3); }
289 unsigned getLineNumber() const { return getUnsignedField(4); }
290 uint64_t getSizeInBits() const { return getUInt64Field(5); }
291 uint64_t getAlignInBits() const { return getUInt64Field(6); }
292 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
293 // carry this is just plain insane.
294 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
295 unsigned getFlags() const { return getUnsignedField(8); }
296 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
297 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
298 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
299 // isAppleBlock - Return true if this is the Apple Blocks extension.
300 bool isAppleBlockExtension() const {
301 return (getFlags() & FlagAppleBlock) != 0;
302 }
303 bool isBlockByrefStruct() const {
304 return (getFlags() & FlagBlockByrefStruct) != 0;
305 }
306 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
307 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
308 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
309 bool isObjcClassComplete() const {
310 return (getFlags() & FlagObjcClassComplete) != 0;
311 }
312 bool isVector() const { return (getFlags() & FlagVector) != 0; }
313 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
314 bool isValid() const { return DbgNode && isType(); }
316 /// replaceAllUsesWith - Replace all uses of debug info referenced by
317 /// this descriptor.
318 void replaceAllUsesWith(DIDescriptor &D);
319 void replaceAllUsesWith(MDNode *D);
320 };
322 /// DIBasicType - A basic type, like 'int' or 'float'.
323 class DIBasicType : public DIType {
324 public:
325 explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
327 unsigned getEncoding() const { return getUnsignedField(9); }
329 /// Verify - Verify that a basic type descriptor is well formed.
330 bool Verify() const;
331 };
333 /// DIDerivedType - A simple derived type, like a const qualified type,
334 /// a typedef, a pointer or reference, et cetera. Or, a data member of
335 /// a class/struct/union.
336 class DIDerivedType : public DIType {
337 friend class DIDescriptor;
338 void printInternal(raw_ostream &OS) const;
340 public:
341 explicit DIDerivedType(const MDNode *N = 0) : DIType(N) {}
343 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
345 /// getObjCProperty - Return property node, if this ivar is
346 /// associated with one.
347 MDNode *getObjCProperty() const;
349 DITypeRef getClassType() const {
350 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
351 return getFieldAs<DITypeRef>(10);
352 }
354 Constant *getConstant() const {
355 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
356 return getConstantField(10);
357 }
359 /// Verify - Verify that a derived type descriptor is well formed.
360 bool Verify() const;
361 };
363 /// DICompositeType - This descriptor holds a type that can refer to multiple
364 /// other types, like a function or struct.
365 /// DICompositeType is derived from DIDerivedType because some
366 /// composite types (such as enums) can be derived from basic types
367 // FIXME: Make this derive from DIType directly & just store the
368 // base type in a single DIType field.
369 class DICompositeType : public DIDerivedType {
370 friend class DIDescriptor;
371 void printInternal(raw_ostream &OS) const;
373 public:
374 explicit DICompositeType(const MDNode *N = 0) : DIDerivedType(N) {}
376 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
377 void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
378 void addMember(DIDescriptor D);
379 unsigned getRunTimeLang() const { return getUnsignedField(11); }
380 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
381 void setContainingType(DICompositeType ContainingType);
382 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
383 MDString *getIdentifier() const;
385 /// Verify - Verify that a composite type descriptor is well formed.
386 bool Verify() const;
387 };
389 /// DIFile - This is a wrapper for a file.
390 class DIFile : public DIScope {
391 friend class DIDescriptor;
393 public:
394 explicit DIFile(const MDNode *N = 0) : DIScope(N) {}
395 MDNode *getFileNode() const;
396 bool Verify() const;
397 };
399 /// DICompileUnit - A wrapper for a compile unit.
400 class DICompileUnit : public DIScope {
401 friend class DIDescriptor;
402 void printInternal(raw_ostream &OS) const;
404 public:
405 explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
407 unsigned getLanguage() const { return getUnsignedField(2); }
408 StringRef getProducer() const { return getStringField(3); }
410 bool isOptimized() const { return getUnsignedField(4) != 0; }
411 StringRef getFlags() const { return getStringField(5); }
412 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
414 DIArray getEnumTypes() const;
415 DIArray getRetainedTypes() const;
416 DIArray getSubprograms() const;
417 DIArray getGlobalVariables() const;
418 DIArray getImportedEntities() const;
420 StringRef getSplitDebugFilename() const { return getStringField(12); }
422 /// Verify - Verify that a compile unit is well formed.
423 bool Verify() const;
424 };
426 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
427 class DISubprogram : public DIScope {
428 friend class DIDescriptor;
429 void printInternal(raw_ostream &OS) const;
431 public:
432 explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
434 DIScope getContext() const { return getFieldAs<DIScope>(2); }
435 StringRef getName() const { return getStringField(3); }
436 StringRef getDisplayName() const { return getStringField(4); }
437 StringRef getLinkageName() const { return getStringField(5); }
438 unsigned getLineNumber() const { return getUnsignedField(6); }
439 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
441 /// isLocalToUnit - Return true if this subprogram is local to the current
442 /// compile unit, like 'static' in C.
443 unsigned isLocalToUnit() const { return getUnsignedField(8); }
444 unsigned isDefinition() const { return getUnsignedField(9); }
446 unsigned getVirtuality() const { return getUnsignedField(10); }
447 unsigned getVirtualIndex() const { return getUnsignedField(11); }
449 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
451 unsigned getFlags() const { return getUnsignedField(13); }
453 unsigned isArtificial() const {
454 return (getUnsignedField(13) & FlagArtificial) != 0;
455 }
456 /// isPrivate - Return true if this subprogram has "private"
457 /// access specifier.
458 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
459 /// isProtected - Return true if this subprogram has "protected"
460 /// access specifier.
461 bool isProtected() const {
462 return (getUnsignedField(13) & FlagProtected) != 0;
463 }
464 /// isExplicit - Return true if this subprogram is marked as explicit.
465 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
466 /// isPrototyped - Return true if this subprogram is prototyped.
467 bool isPrototyped() const {
468 return (getUnsignedField(13) & FlagPrototyped) != 0;
469 }
471 unsigned isOptimized() const;
473 /// Verify - Verify that a subprogram descriptor is well formed.
474 bool Verify() const;
476 /// describes - Return true if this subprogram provides debugging
477 /// information for the function F.
478 bool describes(const Function *F);
480 Function *getFunction() const { return getFunctionField(15); }
481 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
482 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
483 DISubprogram getFunctionDeclaration() const {
484 return getFieldAs<DISubprogram>(17);
485 }
486 MDNode *getVariablesNodes() const;
487 DIArray getVariables() const;
489 /// getScopeLineNumber - Get the beginning of the scope of the
490 /// function, not necessarily where the name of the program
491 /// starts.
492 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
493 };
495 /// DILexicalBlock - This is a wrapper for a lexical block.
496 class DILexicalBlock : public DIScope {
497 public:
498 explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
499 DIScope getContext() const { return getFieldAs<DIScope>(2); }
500 unsigned getLineNumber() const { return getUnsignedField(3); }
501 unsigned getColumnNumber() const { return getUnsignedField(4); }
502 bool Verify() const;
503 };
505 /// DILexicalBlockFile - This is a wrapper for a lexical block with
506 /// a filename change.
507 class DILexicalBlockFile : public DIScope {
508 public:
509 explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
510 DIScope getContext() const {
511 if (getScope().isSubprogram())
512 return getScope();
513 return getScope().getContext();
514 }
515 unsigned getLineNumber() const { return getScope().getLineNumber(); }
516 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
517 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
518 bool Verify() const;
519 };
521 /// DINameSpace - A wrapper for a C++ style name space.
522 class DINameSpace : public DIScope {
523 friend class DIDescriptor;
524 void printInternal(raw_ostream &OS) const;
526 public:
527 explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
528 DIScope getContext() const { return getFieldAs<DIScope>(2); }
529 StringRef getName() const { return getStringField(3); }
530 unsigned getLineNumber() const { return getUnsignedField(4); }
531 bool Verify() const;
532 };
534 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
535 class DITemplateTypeParameter : public DIDescriptor {
536 public:
537 explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
539 DIScope getContext() const { return getFieldAs<DIScope>(1); }
540 StringRef getName() const { return getStringField(2); }
541 DIType getType() const { return getFieldAs<DIType>(3); }
542 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
543 StringRef getDirectory() const {
544 return getFieldAs<DIFile>(4).getDirectory();
545 }
546 unsigned getLineNumber() const { return getUnsignedField(5); }
547 unsigned getColumnNumber() const { return getUnsignedField(6); }
548 bool Verify() const;
549 };
551 /// DITemplateValueParameter - This is a wrapper for template value parameter.
552 class DITemplateValueParameter : public DIDescriptor {
553 public:
554 explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
556 DIScope getContext() const { return getFieldAs<DIScope>(1); }
557 StringRef getName() const { return getStringField(2); }
558 DIType getType() const { return getFieldAs<DIType>(3); }
559 Value *getValue() const;
560 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
561 StringRef getDirectory() const {
562 return getFieldAs<DIFile>(5).getDirectory();
563 }
564 unsigned getLineNumber() const { return getUnsignedField(6); }
565 unsigned getColumnNumber() const { return getUnsignedField(7); }
566 bool Verify() const;
567 };
569 /// DIGlobalVariable - This is a wrapper for a global variable.
570 class DIGlobalVariable : public DIDescriptor {
571 friend class DIDescriptor;
572 void printInternal(raw_ostream &OS) const;
574 public:
575 explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
577 DIScope getContext() const { return getFieldAs<DIScope>(2); }
578 StringRef getName() const { return getStringField(3); }
579 StringRef getDisplayName() const { return getStringField(4); }
580 StringRef getLinkageName() const { return getStringField(5); }
581 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
582 StringRef getDirectory() const {
583 return getFieldAs<DIFile>(6).getDirectory();
584 }
586 unsigned getLineNumber() const { return getUnsignedField(7); }
587 DIType getType() const { return getFieldAs<DIType>(8); }
588 unsigned isLocalToUnit() const { return getUnsignedField(9); }
589 unsigned isDefinition() const { return getUnsignedField(10); }
591 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
592 Constant *getConstant() const { return getConstantField(11); }
593 DIDerivedType getStaticDataMemberDeclaration() const {
594 return getFieldAs<DIDerivedType>(12);
595 }
597 /// Verify - Verify that a global variable descriptor is well formed.
598 bool Verify() const;
599 };
601 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
602 /// global etc).
603 class DIVariable : public DIDescriptor {
604 friend class DIDescriptor;
605 void printInternal(raw_ostream &OS) const;
607 public:
608 explicit DIVariable(const MDNode *N = 0) : DIDescriptor(N) {}
610 DIScope getContext() const { return getFieldAs<DIScope>(1); }
611 StringRef getName() const { return getStringField(2); }
612 DIFile getFile() const { return getFieldAs<DIFile>(3); }
613 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
614 unsigned getArgNumber() const {
615 unsigned L = getUnsignedField(4);
616 return L >> 24;
617 }
618 DIType getType() const { return getFieldAs<DIType>(5); }
620 /// isArtificial - Return true if this variable is marked as "artificial".
621 bool isArtificial() const {
622 return (getUnsignedField(6) & FlagArtificial) != 0;
623 }
625 bool isObjectPointer() const {
626 return (getUnsignedField(6) & FlagObjectPointer) != 0;
627 }
629 /// \brief Return true if this variable is represented as a pointer.
630 bool isIndirect() const {
631 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
632 }
634 /// getInlinedAt - If this variable is inlined then return inline location.
635 MDNode *getInlinedAt() const;
637 /// Verify - Verify that a variable descriptor is well formed.
638 bool Verify() const;
640 /// HasComplexAddr - Return true if the variable has a complex address.
641 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
643 unsigned getNumAddrElements() const;
645 uint64_t getAddrElement(unsigned Idx) const {
646 return getUInt64Field(Idx + 8);
647 }
649 /// isBlockByrefVariable - Return true if the variable was declared as
650 /// a "__block" variable (Apple Blocks).
651 bool isBlockByrefVariable() const { return getType().isBlockByrefStruct(); }
653 /// isInlinedFnArgument - Return true if this variable provides debugging
654 /// information for an inlined function arguments.
655 bool isInlinedFnArgument(const Function *CurFn);
657 void printExtendedName(raw_ostream &OS) const;
658 };
660 /// DILocation - This object holds location information. This object
661 /// is not associated with any DWARF tag.
662 class DILocation : public DIDescriptor {
663 public:
664 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
666 unsigned getLineNumber() const { return getUnsignedField(0); }
667 unsigned getColumnNumber() const { return getUnsignedField(1); }
668 DIScope getScope() const { return getFieldAs<DIScope>(2); }
669 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
670 StringRef getFilename() const { return getScope().getFilename(); }
671 StringRef getDirectory() const { return getScope().getDirectory(); }
672 bool Verify() const;
673 };
675 class DIObjCProperty : public DIDescriptor {
676 friend class DIDescriptor;
677 void printInternal(raw_ostream &OS) const;
679 public:
680 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
682 StringRef getObjCPropertyName() const { return getStringField(1); }
683 DIFile getFile() const { return getFieldAs<DIFile>(2); }
684 unsigned getLineNumber() const { return getUnsignedField(3); }
686 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
687 StringRef getObjCPropertySetterName() const { return getStringField(5); }
688 bool isReadOnlyObjCProperty() const {
689 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
690 }
691 bool isReadWriteObjCProperty() const {
692 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
693 }
694 bool isAssignObjCProperty() const {
695 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
696 }
697 bool isRetainObjCProperty() const {
698 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
699 }
700 bool isCopyObjCProperty() const {
701 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
702 }
703 bool isNonAtomicObjCProperty() const {
704 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
705 }
707 DIType getType() const { return getFieldAs<DIType>(7); }
709 /// Verify - Verify that a derived type descriptor is well formed.
710 bool Verify() const;
711 };
713 /// \brief An imported module (C++ using directive or similar).
714 class DIImportedEntity : public DIDescriptor {
715 friend class DIDescriptor;
716 void printInternal(raw_ostream &OS) const;
718 public:
719 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
720 DIScope getContext() const { return getFieldAs<DIScope>(1); }
721 DIDescriptor getEntity() const { return getFieldAs<DIDescriptor>(2); }
722 unsigned getLineNumber() const { return getUnsignedField(3); }
723 StringRef getName() const { return getStringField(4); }
724 bool Verify() const;
725 };
727 /// getDISubprogram - Find subprogram that is enclosing this scope.
728 DISubprogram getDISubprogram(const MDNode *Scope);
730 /// getDICompositeType - Find underlying composite type.
731 DICompositeType getDICompositeType(DIType T);
733 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
734 /// to hold function specific information.
735 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
737 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
738 /// suitable to hold function specific information.
739 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
741 /// createInlinedVariable - Create a new inlined variable based on current
742 /// variable.
743 /// @param DV Current Variable.
744 /// @param InlinedScope Location at current variable is inlined.
745 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
746 LLVMContext &VMContext);
748 /// cleanseInlinedVariable - Remove inlined scope from the variable.
749 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
751 /// Construct DITypeIdentifierMap by going through retained types of each CU.
752 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
754 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
755 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
756 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
757 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
758 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
759 /// used by the CUs.
760 class DebugInfoFinder {
761 public:
762 /// processModule - Process entire module and collect debug info
763 /// anchors.
764 void processModule(const Module &M);
766 /// processDeclare - Process DbgDeclareInst.
767 void processDeclare(const DbgDeclareInst *DDI);
768 /// Process DbgValueInst.
769 void processValue(const DbgValueInst *DVI);
770 /// processLocation - Process DILocation.
771 void processLocation(DILocation Loc);
773 /// Clear all lists.
774 void reset();
776 private:
777 /// processType - Process DIType.
778 void processType(DIType DT);
780 /// processLexicalBlock - Process DILexicalBlock.
781 void processLexicalBlock(DILexicalBlock LB);
783 /// processSubprogram - Process DISubprogram.
784 void processSubprogram(DISubprogram SP);
786 void processScope(DIScope Scope);
788 /// addCompileUnit - Add compile unit into CUs.
789 bool addCompileUnit(DICompileUnit CU);
791 /// addGlobalVariable - Add global variable into GVs.
792 bool addGlobalVariable(DIGlobalVariable DIG);
794 // addSubprogram - Add subprogram into SPs.
795 bool addSubprogram(DISubprogram SP);
797 /// addType - Add type into Tys.
798 bool addType(DIType DT);
800 bool addScope(DIScope Scope);
802 public:
803 typedef SmallVectorImpl<MDNode *>::const_iterator iterator;
804 iterator compile_unit_begin() const { return CUs.begin(); }
805 iterator compile_unit_end() const { return CUs.end(); }
806 iterator subprogram_begin() const { return SPs.begin(); }
807 iterator subprogram_end() const { return SPs.end(); }
808 iterator global_variable_begin() const { return GVs.begin(); }
809 iterator global_variable_end() const { return GVs.end(); }
810 iterator type_begin() const { return TYs.begin(); }
811 iterator type_end() const { return TYs.end(); }
812 iterator scope_begin() const { return Scopes.begin(); }
813 iterator scope_end() const { return Scopes.end(); }
815 unsigned compile_unit_count() const { return CUs.size(); }
816 unsigned global_variable_count() const { return GVs.size(); }
817 unsigned subprogram_count() const { return SPs.size(); }
818 unsigned type_count() const { return TYs.size(); }
819 unsigned scope_count() const { return Scopes.size(); }
821 private:
822 SmallVector<MDNode *, 8> CUs; // Compile Units
823 SmallVector<MDNode *, 8> SPs; // Subprograms
824 SmallVector<MDNode *, 8> GVs; // Global Variables;
825 SmallVector<MDNode *, 8> TYs; // Types
826 SmallVector<MDNode *, 8> Scopes; // Scopes
827 SmallPtrSet<MDNode *, 64> NodesSeen;
828 DITypeIdentifierMap TypeIdentifierMap;
829 };
830 } // end namespace llvm
832 #endif