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