Fix one comment and update another. Slightly reformat.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCompileUnit.h
1 //===-- llvm/CodeGen/DwarfCompileUnit.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 CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16
17 #include "DIE.h"
18 #include "DwarfDebug.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/DebugInfo.h"
23 #include "llvm/MC/MCExpr.h"
24
25 namespace llvm {
26
27 class MachineLocation;
28 class MachineOperand;
29 class ConstantInt;
30 class ConstantFP;
31 class DbgVariable;
32
33 //===----------------------------------------------------------------------===//
34 /// CompileUnit - This dwarf writer support class manages information associated
35 /// with a source file.
36 class CompileUnit {
37   /// UniqueID - a numeric ID unique among all CUs in the module
38   ///
39   unsigned UniqueID;
40
41   /// Node - MDNode for the compile unit.
42   const MDNode *Node;
43
44   /// Die - Compile unit debug information entry.
45   ///
46   const OwningPtr<DIE> CUDie;
47
48   /// Asm - Target of Dwarf emission.
49   AsmPrinter *Asm;
50
51   // Holders for some common dwarf information.
52   DwarfDebug *DD;
53   DwarfUnits *DU;
54
55   /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
56   DIE *IndexTyDie;
57
58   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
59   /// variables to debug information entries.
60   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
61
62   /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
63   /// descriptors to debug information entries using a DIEEntry proxy.
64   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
65
66   /// GlobalNames - A map of globally visible named entities for this unit.
67   ///
68   StringMap<DIE *> GlobalNames;
69
70   /// GlobalTypes - A map of globally visible types for this unit.
71   ///
72   StringMap<DIE *> GlobalTypes;
73
74   /// AccelNames - A map of names for the name accelerator table.
75   ///
76   StringMap<std::vector<DIE *> > AccelNames;
77   StringMap<std::vector<DIE *> > AccelObjC;
78   StringMap<std::vector<DIE *> > AccelNamespace;
79   StringMap<std::vector<std::pair<DIE *, unsigned> > > AccelTypes;
80
81   /// DIEBlocks - A list of all the DIEBlocks in use.
82   std::vector<DIEBlock *> DIEBlocks;
83
84   /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
85   /// need DW_AT_containing_type attribute. This attribute points to a DIE that
86   /// corresponds to the MDNode mapped with the subprogram DIE.
87   DenseMap<DIE *, const MDNode *> ContainingTypeMap;
88
89 public:
90   CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
91               DwarfDebug *DW, DwarfUnits *DWU);
92   ~CompileUnit();
93
94   // Accessors.
95   unsigned getUniqueID() const { return UniqueID; }
96   uint16_t getLanguage() const { return DICompileUnit(Node).getLanguage(); }
97   const MDNode *getNode() const { return Node; }
98   DIE *getCUDie() const { return CUDie.get(); }
99   const StringMap<DIE *> &getGlobalNames() const { return GlobalNames; }
100   const StringMap<DIE *> &getGlobalTypes() const { return GlobalTypes; }
101
102   const StringMap<std::vector<DIE *> > &getAccelNames() const {
103     return AccelNames;
104   }
105   const StringMap<std::vector<DIE *> > &getAccelObjC() const {
106     return AccelObjC;
107   }
108   const StringMap<std::vector<DIE *> > &getAccelNamespace() const {
109     return AccelNamespace;
110   }
111   const StringMap<std::vector<std::pair<DIE *, unsigned> > > &
112   getAccelTypes() const {
113     return AccelTypes;
114   }
115
116   /// hasContent - Return true if this compile unit has something to write out.
117   ///
118   bool hasContent() const { return !CUDie->getChildren().empty(); }
119
120   /// addGlobalName - Add a new global entity to the compile unit.
121   ///
122   void addGlobalName(StringRef Name, DIE *Die);
123
124   /// addGlobalType - Add a new global type to the compile unit.
125   ///
126   void addGlobalType(DIType Ty);
127
128   /// addPubTypes - Add a set of types from the subprogram to the global types.
129   void addPubTypes(DISubprogram SP);
130
131   /// addAccelName - Add a new name to the name accelerator table.
132   void addAccelName(StringRef Name, DIE *Die);
133
134   /// addAccelObjC - Add a new name to the ObjC accelerator table.
135   void addAccelObjC(StringRef Name, DIE *Die);
136
137   /// addAccelNamespace - Add a new name to the namespace accelerator table.
138   void addAccelNamespace(StringRef Name, DIE *Die);
139
140   /// addAccelType - Add a new type to the type accelerator table.
141   void addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die);
142
143   /// getDIE - Returns the debug information entry map slot for the
144   /// specified debug variable.
145   DIE *getDIE(const MDNode *N) const { return MDNodeToDieMap.lookup(N); }
146
147   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
148
149   /// insertDIE - Insert DIE into the map.
150   void insertDIE(const MDNode *N, DIE *D) {
151     MDNodeToDieMap.insert(std::make_pair(N, D));
152   }
153
154   /// addDie - Adds or interns the DIE to the compile unit.
155   ///
156   void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
157
158   /// addFlag - Add a flag that is true to the DIE.
159   void addFlag(DIE *Die, uint16_t Attribute);
160
161   /// addUInt - Add an unsigned integer attribute data and value.
162   ///
163   void addUInt(DIE *Die, uint16_t Attribute, uint16_t Form, uint64_t Integer);
164
165   /// addSInt - Add an signed integer attribute data and value.
166   ///
167   void addSInt(DIE *Die, uint16_t Attribute, uint16_t Form, int64_t Integer);
168
169   /// addString - Add a string attribute data and value.
170   ///
171   void addString(DIE *Die, uint16_t Attribute, const StringRef Str);
172
173   /// addLocalString - Add a string attribute data and value.
174   ///
175   void addLocalString(DIE *Die, uint16_t Attribute, const StringRef Str);
176
177   /// addExpr - Add a Dwarf expression attribute data and value.
178   ///
179   void addExpr(DIE *Die, uint16_t Attribute, uint16_t Form, const MCExpr *Expr);
180
181   /// addLabel - Add a Dwarf label attribute data and value.
182   ///
183   void addLabel(DIE *Die, uint16_t Attribute, uint16_t Form,
184                 const MCSymbol *Label);
185
186   /// addLabelAddress - Add a dwarf label attribute data and value using
187   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
188   ///
189   void addLabelAddress(DIE *Die, uint16_t Attribute, MCSymbol *Label);
190
191   /// addOpAddress - Add a dwarf op address data and value using the
192   /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
193   ///
194   void addOpAddress(DIE *Die, const MCSymbol *Label);
195   void addOpAddress(DIE *Die, const MCSymbolRefExpr *Label);
196
197   /// addDelta - Add a label delta attribute data and value.
198   ///
199   void addDelta(DIE *Die, uint16_t Attribute, uint16_t Form, const MCSymbol *Hi,
200                 const MCSymbol *Lo);
201
202   /// addDIEEntry - Add a DIE attribute data and value.
203   ///
204   void addDIEEntry(DIE *Die, uint16_t Attribute, uint16_t Form, DIE *Entry);
205
206   /// addBlock - Add block data.
207   ///
208   void addBlock(DIE *Die, uint16_t Attribute, uint16_t Form, DIEBlock *Block);
209
210   /// addSourceLine - Add location information to specified debug information
211   /// entry.
212   void addSourceLine(DIE *Die, DIVariable V);
213   void addSourceLine(DIE *Die, DIGlobalVariable G);
214   void addSourceLine(DIE *Die, DISubprogram SP);
215   void addSourceLine(DIE *Die, DIType Ty);
216   void addSourceLine(DIE *Die, DINameSpace NS);
217   void addSourceLine(DIE *Die, DIObjCProperty Ty);
218
219   /// addAddress - Add an address attribute to a die based on the location
220   /// provided.
221   void addAddress(DIE *Die, uint16_t Attribute, const MachineLocation &Location,
222                   bool Indirect = false);
223
224   /// addConstantValue - Add constant value entry in variable DIE.
225   void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
226   void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
227   void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
228
229   /// addConstantFPValue - Add constant value entry in variable DIE.
230   void addConstantFPValue(DIE *Die, const MachineOperand &MO);
231   void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
232
233   /// addTemplateParams - Add template parameters in buffer.
234   void addTemplateParams(DIE &Buffer, DIArray TParams);
235
236   /// addRegisterOp - Add register operand.
237   void addRegisterOp(DIE *TheDie, unsigned Reg);
238
239   /// addRegisterOffset - Add register offset.
240   void addRegisterOffset(DIE *TheDie, unsigned Reg, int64_t Offset);
241
242   /// addComplexAddress - Start with the address based on the location provided,
243   /// and generate the DWARF information necessary to find the actual variable
244   /// (navigating the extra location information encoded in the type) based on
245   /// the starting location.  Add the DWARF information to the die.
246   ///
247   void addComplexAddress(const DbgVariable &DV, DIE *Die, uint16_t Attribute,
248                          const MachineLocation &Location);
249
250   // FIXME: Should be reformulated in terms of addComplexAddress.
251   /// addBlockByrefAddress - Start with the address based on the location
252   /// provided, and generate the DWARF information necessary to find the
253   /// actual Block variable (navigating the Block struct) based on the
254   /// starting location.  Add the DWARF information to the die.  Obsolete,
255   /// please use addComplexAddress instead.
256   ///
257   void addBlockByrefAddress(const DbgVariable &DV, DIE *Die, uint16_t Attribute,
258                             const MachineLocation &Location);
259
260   /// addVariableAddress - Add DW_AT_location attribute for a
261   /// DbgVariable based on provided MachineLocation.
262   void addVariableAddress(const DbgVariable &DV, DIE *Die,
263                           MachineLocation Location);
264
265   /// addToContextOwner - Add Die into the list of its context owner's children.
266   void addToContextOwner(DIE *Die, DIScope Context);
267
268   /// addType - Add a new type attribute to the specified entity. This takes
269   /// and attribute parameter because DW_AT_friend attributes are also
270   /// type references.
271   void addType(DIE *Entity, DIType Ty, uint16_t Attribute = dwarf::DW_AT_type);
272
273   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
274   DIE *getOrCreateNameSpace(DINameSpace NS);
275
276   /// getOrCreateSubprogramDIE - Create new DIE using SP.
277   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
278
279   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
280   /// given DIType.
281   DIE *getOrCreateTypeDIE(const MDNode *N);
282
283   /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
284   /// for the given DITemplateTypeParameter.
285   DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
286
287   /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create
288   /// new DIE for the given DITemplateValueParameter.
289   DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
290
291   /// getOrCreateContextDIE - Get context owner's DIE.
292   DIE *getOrCreateContextDIE(DIScope Context);
293
294   /// createGlobalVariableDIE - create global variable DIE.
295   void createGlobalVariableDIE(const MDNode *N);
296
297   /// constructContainingTypeDIEs - Construct DIEs for types that contain
298   /// vtables.
299   void constructContainingTypeDIEs();
300
301   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
302   DIE *constructVariableDIE(DbgVariable *DV, bool isScopeAbstract);
303
304 private:
305   /// constructTypeDIE - Construct basic type die from DIBasicType.
306   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
307
308   /// constructTypeDIE - Construct derived type die from DIDerivedType.
309   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
310
311   /// constructTypeDIE - Construct type DIE from DICompositeType.
312   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
313
314   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
315   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
316
317   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
318   void constructArrayTypeDIE(DIE &Buffer, DICompositeType *CTy);
319
320   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
321   DIE *constructEnumTypeDIE(DIEnumerator ETy);
322
323   /// createMemberDIE - Create new member DIE.
324   DIE *createMemberDIE(DIDerivedType DT);
325
326   /// createStaticMemberDIE - Create new static data member DIE.
327   DIE *createStaticMemberDIE(DIDerivedType DT);
328
329   /// getLowerBoundDefault - Return the default lower bound for an array. If the
330   /// DWARF version doesn't handle the language, return -1.
331   int64_t getDefaultLowerBound() const;
332
333   /// getDIEEntry - Returns the debug information entry for the specified
334   /// debug variable.
335   DIEEntry *getDIEEntry(const MDNode *N) const {
336     return MDNodeToDIEEntryMap.lookup(N);
337   }
338
339   /// insertDIEEntry - Insert debug information entry into the map.
340   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
341     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
342   }
343
344   // getIndexTyDie - Get an anonymous type for index type.
345   DIE *getIndexTyDie() { return IndexTyDie; }
346
347   // setIndexTyDie - Set D as anonymous type for index which can be reused
348   // later.
349   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
350
351   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
352   /// information entry.
353   DIEEntry *createDIEEntry(DIE *Entry);
354
355   /// resolve - Look in the DwarfDebug map for the MDNode that
356   /// corresponds to the reference.
357   template <typename T> T resolve(DIRef<T> Ref) const {
358     return DD->resolve(Ref);
359   }
360
361 private:
362
363   // DIEValueAllocator - All DIEValues are allocated through this allocator.
364   BumpPtrAllocator DIEValueAllocator;
365   DIEInteger *DIEIntegerOne;
366 };
367
368 } // end llvm namespace
369 #endif