AsmPrinter: Make DIEValue::Ty private, NFC
[oota-llvm.git] / include / llvm / CodeGen / DIE.h
1 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- 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 // Data structures for DWARF info entries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
20 #include "llvm/Support/Dwarf.h"
21 #include <vector>
22
23 namespace llvm {
24 class AsmPrinter;
25 class MCExpr;
26 class MCSymbol;
27 class raw_ostream;
28 class DwarfTypeUnit;
29
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
32 /// Dwarf abbreviation.
33 class DIEAbbrevData {
34   /// Attribute - Dwarf attribute code.
35   ///
36   dwarf::Attribute Attribute;
37
38   /// Form - Dwarf form code.
39   ///
40   dwarf::Form Form;
41
42 public:
43   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
44
45   // Accessors.
46   dwarf::Attribute getAttribute() const { return Attribute; }
47   dwarf::Form getForm() const { return Form; }
48
49   /// Profile - Used to gather unique data for the abbreviation folding set.
50   ///
51   void Profile(FoldingSetNodeID &ID) const;
52 };
53
54 //===--------------------------------------------------------------------===//
55 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
56 /// information object.
57 class DIEAbbrev : public FoldingSetNode {
58   /// Unique number for node.
59   ///
60   unsigned Number;
61
62   /// Tag - Dwarf tag code.
63   ///
64   dwarf::Tag Tag;
65
66   /// Children - Whether or not this node has children.
67   ///
68   // This cheats a bit in all of the uses since the values in the standard
69   // are 0 and 1 for no children and children respectively.
70   bool Children;
71
72   /// Data - Raw data bytes for abbreviation.
73   ///
74   SmallVector<DIEAbbrevData, 12> Data;
75
76 public:
77   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
78
79   // Accessors.
80   dwarf::Tag getTag() const { return Tag; }
81   unsigned getNumber() const { return Number; }
82   bool hasChildren() const { return Children; }
83   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
84   void setChildrenFlag(bool hasChild) { Children = hasChild; }
85   void setNumber(unsigned N) { Number = N; }
86
87   /// AddAttribute - Adds another set of attribute information to the
88   /// abbreviation.
89   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
90     Data.push_back(DIEAbbrevData(Attribute, Form));
91   }
92
93   /// Profile - Used to gather unique data for the abbreviation folding set.
94   ///
95   void Profile(FoldingSetNodeID &ID) const;
96
97   /// Emit - Print the abbreviation using the specified asm printer.
98   ///
99   void Emit(const AsmPrinter *AP) const;
100
101 #ifndef NDEBUG
102   void print(raw_ostream &O);
103   void dump();
104 #endif
105 };
106
107 //===--------------------------------------------------------------------===//
108 /// DIE - A structured debug information entry.  Has an abbreviation which
109 /// describes its organization.
110 class DIEValue;
111
112 class DIE {
113 protected:
114   /// Offset - Offset in debug info section.
115   ///
116   unsigned Offset;
117
118   /// Size - Size of instance + children.
119   ///
120   unsigned Size;
121
122   /// Abbrev - Buffer for constructing abbreviation.
123   ///
124   DIEAbbrev Abbrev;
125
126   /// Children DIEs.
127   ///
128   // This can't be a vector<DIE> because pointer validity is requirent for the
129   // Parent pointer and DIEEntry.
130   // It can't be a list<DIE> because some clients need pointer validity before
131   // the object has been added to any child list
132   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
133   // be more convoluted than beneficial.
134   std::vector<std::unique_ptr<DIE>> Children;
135
136   DIE *Parent;
137
138   /// Attribute values.
139   ///
140   SmallVector<DIEValue *, 12> Values;
141
142 protected:
143   DIE()
144       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
145         Parent(nullptr) {}
146
147 public:
148   explicit DIE(dwarf::Tag Tag)
149       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
150         Parent(nullptr) {}
151
152   // Accessors.
153   DIEAbbrev &getAbbrev() { return Abbrev; }
154   const DIEAbbrev &getAbbrev() const { return Abbrev; }
155   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
156   dwarf::Tag getTag() const { return Abbrev.getTag(); }
157   unsigned getOffset() const { return Offset; }
158   unsigned getSize() const { return Size; }
159   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
160     return Children;
161   }
162   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
163   DIE *getParent() const { return Parent; }
164   /// Climb up the parent chain to get the compile or type unit DIE this DIE
165   /// belongs to.
166   const DIE *getUnit() const;
167   /// Similar to getUnit, returns null when DIE is not added to an
168   /// owner yet.
169   const DIE *getUnitOrNull() const;
170   void setOffset(unsigned O) { Offset = O; }
171   void setSize(unsigned S) { Size = S; }
172
173   /// addValue - Add a value and attributes to a DIE.
174   ///
175   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
176     Abbrev.AddAttribute(Attribute, Form);
177     Values.push_back(Value);
178   }
179
180   /// addChild - Add a child to the DIE.
181   ///
182   void addChild(std::unique_ptr<DIE> Child) {
183     assert(!Child->getParent());
184     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
185     Child->Parent = this;
186     Children.push_back(std::move(Child));
187   }
188
189   /// findAttribute - Find a value in the DIE with the attribute given,
190   /// returns NULL if no such attribute exists.
191   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
192
193 #ifndef NDEBUG
194   void print(raw_ostream &O, unsigned IndentCount = 0) const;
195   void dump();
196 #endif
197 };
198
199 //===--------------------------------------------------------------------===//
200 /// DIEValue - A debug information entry value. Some of these roughly correlate
201 /// to DWARF attribute classes.
202 ///
203 class DIEValue {
204 public:
205   enum Type {
206     isInteger,
207     isString,
208     isExpr,
209     isLabel,
210     isDelta,
211     isEntry,
212     isTypeSignature,
213     isBlock,
214     isLoc,
215     isLocList,
216   };
217
218 private:
219   /// Ty - Type of data stored in the value.
220   ///
221   Type Ty;
222
223 protected:
224   explicit DIEValue(Type T) : Ty(T) {}
225   ~DIEValue() {}
226
227 public:
228   // Accessors
229   Type getType() const { return Ty; }
230
231   /// EmitValue - Emit value via the Dwarf writer.
232   ///
233   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
234
235   /// SizeOf - Return the size of a value in bytes.
236   ///
237   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
238
239 #ifndef NDEBUG
240   void print(raw_ostream &O) const;
241   void dump() const;
242 #endif
243 };
244
245 //===--------------------------------------------------------------------===//
246 /// DIEInteger - An integer value DIE.
247 ///
248 class DIEInteger : public DIEValue {
249   friend DIEValue;
250
251   uint64_t Integer;
252
253 public:
254   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
255
256   /// BestForm - Choose the best form for integer.
257   ///
258   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
259     if (IsSigned) {
260       const int64_t SignedInt = Int;
261       if ((char)Int == SignedInt)
262         return dwarf::DW_FORM_data1;
263       if ((short)Int == SignedInt)
264         return dwarf::DW_FORM_data2;
265       if ((int)Int == SignedInt)
266         return dwarf::DW_FORM_data4;
267     } else {
268       if ((unsigned char)Int == Int)
269         return dwarf::DW_FORM_data1;
270       if ((unsigned short)Int == Int)
271         return dwarf::DW_FORM_data2;
272       if ((unsigned int)Int == Int)
273         return dwarf::DW_FORM_data4;
274     }
275     return dwarf::DW_FORM_data8;
276   }
277
278   uint64_t getValue() const { return Integer; }
279   void setValue(uint64_t Val) { Integer = Val; }
280
281   // Implement isa/cast/dyncast.
282   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
283
284 private:
285   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
286   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
287
288 #ifndef NDEBUG
289   void printImpl(raw_ostream &O) const;
290 #endif
291 };
292
293 //===--------------------------------------------------------------------===//
294 /// DIEExpr - An expression DIE.
295 //
296 class DIEExpr : public DIEValue {
297   friend class DIEValue;
298
299   const MCExpr *Expr;
300
301 public:
302   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
303
304   /// getValue - Get MCExpr.
305   ///
306   const MCExpr *getValue() const { return Expr; }
307
308   // Implement isa/cast/dyncast.
309   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
310
311 private:
312   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
313   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
314
315 #ifndef NDEBUG
316   void printImpl(raw_ostream &O) const;
317 #endif
318 };
319
320 //===--------------------------------------------------------------------===//
321 /// DIELabel - A label DIE.
322 //
323 class DIELabel : public DIEValue {
324   friend class DIEValue;
325
326   const MCSymbol *Label;
327
328 public:
329   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
330
331   /// getValue - Get MCSymbol.
332   ///
333   const MCSymbol *getValue() const { return Label; }
334
335   // Implement isa/cast/dyncast.
336   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
337
338 private:
339   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
340   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
341
342 #ifndef NDEBUG
343   void printImpl(raw_ostream &O) const;
344 #endif
345 };
346
347 //===--------------------------------------------------------------------===//
348 /// DIEDelta - A simple label difference DIE.
349 ///
350 class DIEDelta : public DIEValue {
351   friend class DIEValue;
352
353   const MCSymbol *LabelHi;
354   const MCSymbol *LabelLo;
355
356 public:
357   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
358       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
359
360   // Implement isa/cast/dyncast.
361   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
362
363 private:
364   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
365   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
366
367 #ifndef NDEBUG
368   void printImpl(raw_ostream &O) const;
369 #endif
370 };
371
372 //===--------------------------------------------------------------------===//
373 /// DIEString - A container for string values.
374 ///
375 class DIEString : public DIEValue {
376   friend class DIEValue;
377
378   DwarfStringPoolEntryRef S;
379
380 public:
381   DIEString(DwarfStringPoolEntryRef S) : DIEValue(isString), S(S) {}
382
383   /// getString - Grab the string out of the object.
384   StringRef getString() const { return S.getString(); }
385
386   // Implement isa/cast/dyncast.
387   static bool classof(const DIEValue *D) { return D->getType() == isString; }
388
389 private:
390   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
391   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
392
393 #ifndef NDEBUG
394   void printImpl(raw_ostream &O) const;
395 #endif
396 };
397
398 //===--------------------------------------------------------------------===//
399 /// DIEEntry - A pointer to another debug information entry.  An instance of
400 /// this class can also be used as a proxy for a debug information entry not
401 /// yet defined (ie. types.)
402 class DIEEntry : public DIEValue {
403   friend class DIEValue;
404
405   DIE &Entry;
406
407 public:
408   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
409   }
410
411   DIE &getEntry() const { return Entry; }
412
413   /// Returns size of a ref_addr entry.
414   static unsigned getRefAddrSize(const AsmPrinter *AP);
415
416   // Implement isa/cast/dyncast.
417   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
418
419 private:
420   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
421   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
422     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
423                                            : sizeof(int32_t);
424   }
425
426 #ifndef NDEBUG
427   void printImpl(raw_ostream &O) const;
428 #endif
429 };
430
431 //===--------------------------------------------------------------------===//
432 /// \brief A signature reference to a type unit.
433 class DIETypeSignature : public DIEValue {
434   friend class DIEValue;
435
436   const DwarfTypeUnit &Unit;
437
438 public:
439   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
440       : DIEValue(isTypeSignature), Unit(Unit) {}
441
442   // \brief Implement isa/cast/dyncast.
443   static bool classof(const DIEValue *E) {
444     return E->getType() == isTypeSignature;
445   }
446
447 private:
448   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
449   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
450     assert(Form == dwarf::DW_FORM_ref_sig8);
451     return 8;
452   }
453
454 #ifndef NDEBUG
455   void printImpl(raw_ostream &O) const;
456 #endif
457 };
458
459 //===--------------------------------------------------------------------===//
460 /// DIELoc - Represents an expression location.
461 //
462 class DIELoc : public DIEValue, public DIE {
463   friend class DIEValue;
464
465   mutable unsigned Size; // Size in bytes excluding size header.
466 public:
467   DIELoc() : DIEValue(isLoc), Size(0) {}
468
469   /// ComputeSize - Calculate the size of the location expression.
470   ///
471   unsigned ComputeSize(const AsmPrinter *AP) const;
472
473   /// BestForm - Choose the best form for data.
474   ///
475   dwarf::Form BestForm(unsigned DwarfVersion) const {
476     if (DwarfVersion > 3)
477       return dwarf::DW_FORM_exprloc;
478     // Pre-DWARF4 location expressions were blocks and not exprloc.
479     if ((unsigned char)Size == Size)
480       return dwarf::DW_FORM_block1;
481     if ((unsigned short)Size == Size)
482       return dwarf::DW_FORM_block2;
483     if ((unsigned int)Size == Size)
484       return dwarf::DW_FORM_block4;
485     return dwarf::DW_FORM_block;
486   }
487
488   // Implement isa/cast/dyncast.
489   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
490
491 private:
492   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
493   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
494
495 #ifndef NDEBUG
496   void printImpl(raw_ostream &O) const;
497 #endif
498 };
499
500 //===--------------------------------------------------------------------===//
501 /// DIEBlock - Represents a block of values.
502 //
503 class DIEBlock : public DIEValue, public DIE {
504   friend class DIEValue;
505
506   mutable unsigned Size; // Size in bytes excluding size header.
507 public:
508   DIEBlock() : DIEValue(isBlock), Size(0) {}
509
510   /// ComputeSize - Calculate the size of the location expression.
511   ///
512   unsigned ComputeSize(const AsmPrinter *AP) const;
513
514   /// BestForm - Choose the best form for data.
515   ///
516   dwarf::Form BestForm() const {
517     if ((unsigned char)Size == Size)
518       return dwarf::DW_FORM_block1;
519     if ((unsigned short)Size == Size)
520       return dwarf::DW_FORM_block2;
521     if ((unsigned int)Size == Size)
522       return dwarf::DW_FORM_block4;
523     return dwarf::DW_FORM_block;
524   }
525
526   // Implement isa/cast/dyncast.
527   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
528
529 private:
530   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
531   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
532
533 #ifndef NDEBUG
534   void printImpl(raw_ostream &O) const;
535 #endif
536 };
537
538 //===--------------------------------------------------------------------===//
539 /// DIELocList - Represents a pointer to a location list in the debug_loc
540 /// section.
541 //
542 class DIELocList : public DIEValue {
543   friend class DIEValue;
544
545   // Index into the .debug_loc vector.
546   size_t Index;
547
548 public:
549   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
550
551   /// getValue - Grab the current index out.
552   size_t getValue() const { return Index; }
553
554   // Implement isa/cast/dyncast.
555   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
556
557 private:
558   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
559   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
560
561 #ifndef NDEBUG
562   void printImpl(raw_ostream &O) const;
563 #endif
564 };
565
566 } // end llvm namespace
567
568 #endif