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