DebugInfo: Remove DIArray and DITypeArray typedefs
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfUnit.h
1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- 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 contains support for writing dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
16
17 #include "DwarfDebug.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/DIE.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/MC/MCDwarf.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSection.h"
28
29 namespace llvm {
30
31 class MachineLocation;
32 class MachineOperand;
33 class ConstantInt;
34 class ConstantFP;
35 class DbgVariable;
36 class DwarfCompileUnit;
37
38 // Data structure to hold a range for range lists.
39 class RangeSpan {
40 public:
41   RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
42   const MCSymbol *getStart() const { return Start; }
43   const MCSymbol *getEnd() const { return End; }
44   void setEnd(const MCSymbol *E) { End = E; }
45
46 private:
47   const MCSymbol *Start, *End;
48 };
49
50 class RangeSpanList {
51 private:
52   // Index for locating within the debug_range section this particular span.
53   MCSymbol *RangeSym;
54   // List of ranges.
55   SmallVector<RangeSpan, 2> Ranges;
56
57 public:
58   RangeSpanList(MCSymbol *Sym, SmallVector<RangeSpan, 2> Ranges)
59       : RangeSym(Sym), Ranges(std::move(Ranges)) {}
60   MCSymbol *getSym() const { return RangeSym; }
61   const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
62   void addRange(RangeSpan Range) { Ranges.push_back(Range); }
63 };
64
65 //===----------------------------------------------------------------------===//
66 /// This dwarf writer support class manages information associated with a
67 /// source file.
68 class DwarfUnit {
69 protected:
70   /// A numeric ID unique among all CUs in the module
71   unsigned UniqueID;
72
73   /// MDNode for the compile unit.
74   const MDCompileUnit *CUNode;
75
76   /// Unit debug information entry.
77   DIE UnitDie;
78
79   /// Offset of the UnitDie from beginning of debug info section.
80   unsigned DebugInfoOffset;
81
82   /// Target of Dwarf emission.
83   AsmPrinter *Asm;
84
85   // Holders for some common dwarf information.
86   DwarfDebug *DD;
87   DwarfFile *DU;
88
89   /// An anonymous type for index type.  Owned by UnitDie.
90   DIE *IndexTyDie;
91
92   /// Tracks the mapping of unit level debug information variables to debug
93   /// information entries.
94   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
95
96   /// Tracks the mapping of unit level debug information descriptors to debug
97   /// information entries using a DIEEntry proxy.
98   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
99
100   /// A list of all the DIEBlocks in use.
101   std::vector<DIEBlock *> DIEBlocks;
102
103   /// A list of all the DIELocs in use.
104   std::vector<DIELoc *> DIELocs;
105
106   /// This map is used to keep track of subprogram DIEs that need
107   /// DW_AT_containing_type attribute. This attribute points to a DIE that
108   /// corresponds to the MDNode mapped with the subprogram DIE.
109   DenseMap<DIE *, const DebugNode *> ContainingTypeMap;
110
111   // All DIEValues are allocated through this allocator.
112   BumpPtrAllocator DIEValueAllocator;
113
114   // A preallocated DIEValue because 1 is used frequently.
115   DIEInteger *DIEIntegerOne;
116
117   /// The section this unit will be emitted in.
118   const MCSection *Section;
119
120   DwarfUnit(unsigned UID, dwarf::Tag, const MDCompileUnit *CU, AsmPrinter *A,
121             DwarfDebug *DW, DwarfFile *DWU);
122
123   /// \brief Add a string attribute data and value.
124   ///
125   /// This is guaranteed to be in the local string pool instead of indirected.
126   void addLocalString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
127
128   void addIndexedString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
129
130   bool applySubprogramDefinitionAttributes(const MDSubprogram *SP, DIE &SPDie);
131
132 public:
133   virtual ~DwarfUnit();
134
135   void initSection(const MCSection *Section);
136
137   const MCSection *getSection() const {
138     assert(Section);
139     return Section;
140   }
141
142   // Accessors.
143   AsmPrinter* getAsmPrinter() const { return Asm; }
144   unsigned getUniqueID() const { return UniqueID; }
145   uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
146   const MDCompileUnit *getCUNode() const { return CUNode; }
147   DIE &getUnitDie() { return UnitDie; }
148
149   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
150   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
151
152   /// \brief Return true if this compile unit has something to write out.
153   bool hasContent() const { return !UnitDie.getChildren().empty(); }
154
155   /// \brief Get string containing language specific context for a global name.
156   ///
157   /// Walks the metadata parent chain in a language specific manner (using the
158   /// compile unit language) and returns it as a string. This is done at the
159   /// metadata level because DIEs may not currently have been added to the
160   /// parent context and walking the DIEs looking for names is more expensive
161   /// than walking the metadata.
162   std::string getParentContextString(const MDScope *Context) const;
163
164   /// Add a new global name to the compile unit.
165   virtual void addGlobalName(StringRef Name, DIE &Die, const MDScope *Context) {
166   }
167
168   /// Add a new global type to the compile unit.
169   virtual void addGlobalType(const MDType *Ty, const DIE &Die,
170                              const MDScope *Context) {}
171
172   /// \brief Add a new name to the namespace accelerator table.
173   void addAccelNamespace(StringRef Name, const DIE &Die);
174
175   /// \brief Returns the DIE map slot for the specified debug variable.
176   ///
177   /// We delegate the request to DwarfDebug when the MDNode can be part of the
178   /// type system, since DIEs for the type system can be shared across CUs and
179   /// the mappings are kept in DwarfDebug.
180   DIE *getDIE(const DebugNode *D) const;
181
182   /// \brief Returns a fresh newly allocated DIELoc.
183   DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
184
185   /// \brief Insert DIE into the map.
186   ///
187   /// We delegate the request to DwarfDebug when the MDNode can be part of the
188   /// type system, since DIEs for the type system can be shared across CUs and
189   /// the mappings are kept in DwarfDebug.
190   void insertDIE(const DebugNode *Desc, DIE *D);
191
192   /// \brief Add a flag that is true to the DIE.
193   void addFlag(DIE &Die, dwarf::Attribute Attribute);
194
195   /// \brief Add an unsigned integer attribute data and value.
196   void addUInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
197                uint64_t Integer);
198
199   void addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer);
200
201   /// \brief Add an signed integer attribute data and value.
202   void addSInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
203                int64_t Integer);
204
205   void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
206
207   /// \brief Add a string attribute data and value.
208   ///
209   /// We always emit a reference to the string pool instead of immediate
210   /// strings so that DIEs have more predictable sizes. In the case of split
211   /// dwarf we emit an index into another table which gets us the static offset
212   /// into the string table.
213   void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
214
215   /// \brief Add a Dwarf label attribute data and value.
216   void addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
217                 const MCSymbol *Label);
218
219   void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
220
221   /// \brief Add an offset into a section attribute data and value.
222   void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
223
224   /// \brief Add a dwarf op address data and value using the form given and an
225   /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
226   void addOpAddress(DIELoc &Die, const MCSymbol *Label);
227
228   /// \brief Add a label delta attribute data and value.
229   void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
230                      const MCSymbol *Lo);
231
232   /// \brief Add a DIE attribute data and value.
233   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
234
235   /// \brief Add a DIE attribute data and value.
236   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry *Entry);
237
238   void addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type);
239
240   /// \brief Add block data.
241   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
242
243   /// \brief Add block data.
244   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
245
246   /// \brief Add location information to specified debug information entry.
247   void addSourceLine(DIE &Die, unsigned Line, StringRef File,
248                      StringRef Directory);
249   void addSourceLine(DIE &Die, const MDLocalVariable *V);
250   void addSourceLine(DIE &Die, const MDGlobalVariable *G);
251   void addSourceLine(DIE &Die, const MDSubprogram *SP);
252   void addSourceLine(DIE &Die, const MDType *Ty);
253   void addSourceLine(DIE &Die, const MDNamespace *NS);
254   void addSourceLine(DIE &Die, const MDObjCProperty *Ty);
255
256   /// \brief Add constant value entry in variable DIE.
257   void addConstantValue(DIE &Die, const MachineOperand &MO, const MDType *Ty);
258   void addConstantValue(DIE &Die, const ConstantInt *CI, const MDType *Ty);
259   void addConstantValue(DIE &Die, const APInt &Val, const MDType *Ty);
260   void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
261   void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
262
263   /// \brief Add constant value entry in variable DIE.
264   void addConstantFPValue(DIE &Die, const MachineOperand &MO);
265   void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
266
267   /// \brief Add a linkage name, if it isn't empty.
268   void addLinkageName(DIE &Die, StringRef LinkageName);
269
270   /// \brief Add template parameters in buffer.
271   void addTemplateParams(DIE &Buffer, DebugNodeArray TParams);
272
273   /// \brief Add register operand.
274   /// \returns false if the register does not exist, e.g., because it was never
275   /// materialized.
276   bool addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
277                           unsigned SizeInBits = 0, unsigned OffsetInBits = 0);
278
279   /// \brief Add register offset.
280   /// \returns false if the register does not exist, e.g., because it was never
281   /// materialized.
282   bool addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
283
284   // FIXME: Should be reformulated in terms of addComplexAddress.
285   /// Start with the address based on the location provided, and generate the
286   /// DWARF information necessary to find the actual Block variable (navigating
287   /// the Block struct) based on the starting location.  Add the DWARF
288   /// information to the die.  Obsolete, please use addComplexAddress instead.
289   void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
290                             dwarf::Attribute Attribute,
291                             const MachineLocation &Location);
292
293   /// \brief Add a new type attribute to the specified entity.
294   ///
295   /// This takes and attribute parameter because DW_AT_friend attributes are
296   /// also type references.
297   void addType(DIE &Entity, const MDType *Ty,
298                dwarf::Attribute Attribute = dwarf::DW_AT_type);
299
300   DIE *getOrCreateNameSpace(const MDNamespace *NS);
301   DIE *getOrCreateSubprogramDIE(const MDSubprogram *SP, bool Minimal = false);
302
303   void applySubprogramAttributes(const MDSubprogram *SP, DIE &SPDie,
304                                  bool Minimal = false);
305
306   /// \brief Find existing DIE or create new DIE for the given type.
307   DIE *getOrCreateTypeDIE(const MDNode *N);
308
309   /// \brief Get context owner's DIE.
310   DIE *createTypeDIE(const MDCompositeType *Ty);
311
312   /// \brief Get context owner's DIE.
313   DIE *getOrCreateContextDIE(const MDScope *Context);
314
315   /// \brief Construct DIEs for types that contain vtables.
316   void constructContainingTypeDIEs();
317
318   /// \brief Construct function argument DIEs.
319   void constructSubprogramArguments(DIE &Buffer, MDTypeRefArray Args);
320
321   /// Create a DIE with the given Tag, add the DIE to its parent, and
322   /// call insertDIE if MD is not null.
323   DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DebugNode *N = nullptr);
324
325   /// Compute the size of a header for this unit, not including the initial
326   /// length field.
327   virtual unsigned getHeaderSize() const {
328     return sizeof(int16_t) + // DWARF version number
329            sizeof(int32_t) + // Offset Into Abbrev. Section
330            sizeof(int8_t);   // Pointer Size (in bytes)
331   }
332
333   /// Emit the header for this unit, not including the initial length field.
334   virtual void emitHeader(bool UseOffsets);
335
336   virtual DwarfCompileUnit &getCU() = 0;
337
338   void constructTypeDIE(DIE &Buffer, const MDCompositeType *CTy);
339
340 protected:
341   /// \brief Create new static data member DIE.
342   DIE *getOrCreateStaticMemberDIE(const MDDerivedType *DT);
343
344   /// Look up the source ID with the given directory and source file names. If
345   /// none currently exists, create a new ID and insert it in the line table.
346   virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
347
348   /// \brief Look in the DwarfDebug map for the MDNode that corresponds to the
349   /// reference.
350   template <typename T> T *resolve(TypedDebugNodeRef<T> Ref) const {
351     return DD->resolve(Ref);
352   }
353
354 private:
355   void constructTypeDIE(DIE &Buffer, const MDBasicType *BTy);
356   void constructTypeDIE(DIE &Buffer, const MDDerivedType *DTy);
357   void constructTypeDIE(DIE &Buffer, const MDSubroutineType *DTy);
358   void constructSubrangeDIE(DIE &Buffer, const MDSubrange *SR, DIE *IndexTy);
359   void constructArrayTypeDIE(DIE &Buffer, const MDCompositeType *CTy);
360   void constructEnumTypeDIE(DIE &Buffer, const MDCompositeType *CTy);
361   void constructMemberDIE(DIE &Buffer, const MDDerivedType *DT);
362   void constructTemplateTypeParameterDIE(DIE &Buffer,
363                                          const MDTemplateTypeParameter *TP);
364   void constructTemplateValueParameterDIE(DIE &Buffer,
365                                           const MDTemplateValueParameter *TVP);
366
367   /// \brief Return the default lower bound for an array.
368   ///
369   /// If the DWARF version doesn't handle the language, return -1.
370   int64_t getDefaultLowerBound() const;
371
372   /// \brief Returns the DIE entry for the specified debug variable.
373   DIEEntry *getDIEEntry(const MDNode *N) const {
374     return MDNodeToDIEEntryMap.lookup(N);
375   }
376
377   /// \brief Insert debug information entry into the map.
378   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
379     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
380   }
381
382   /// \brief Get an anonymous type for index type.
383   DIE *getIndexTyDie();
384
385   /// \brief Set D as anonymous type for index which can be reused later.
386   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
387
388   /// \brief Creates a new DIEEntry to be a proxy for a debug information
389   /// entry.
390   DIEEntry *createDIEEntry(DIE &Entry);
391
392   /// If this is a named finished type then include it in the list of types for
393   /// the accelerator tables.
394   void updateAcceleratorTables(const MDScope *Context, const MDType *Ty,
395                                const DIE &TyDIE);
396
397   virtual bool isDwoUnit() const = 0;
398 };
399
400 class DwarfTypeUnit : public DwarfUnit {
401   uint64_t TypeSignature;
402   const DIE *Ty;
403   DwarfCompileUnit &CU;
404   MCDwarfDwoLineTable *SplitLineTable;
405
406   unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
407   bool isDwoUnit() const override;
408
409 public:
410   DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
411                 DwarfDebug *DW, DwarfFile *DWU,
412                 MCDwarfDwoLineTable *SplitLineTable = nullptr);
413
414   void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
415   uint64_t getTypeSignature() const { return TypeSignature; }
416   void setType(const DIE *Ty) { this->Ty = Ty; }
417
418   /// Emit the header for this unit, not including the initial length field.
419   void emitHeader(bool UseOffsets) override;
420   unsigned getHeaderSize() const override {
421     return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
422            sizeof(uint32_t);                               // Type DIE Offset
423   }
424   DwarfCompileUnit &getCU() override { return CU; }
425 };
426 } // end llvm namespace
427 #endif