Include global types, that are referenced through local variables, in debug_pubtypes...
[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 "llvm/Analysis/DebugInfo.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/OwningPtr.h"
22
23 namespace llvm {
24
25 class DwarfDebug;
26 class MachineLocation;
27 class MachineOperand;
28 class ConstantInt;
29 class DbgVariable;
30
31 //===----------------------------------------------------------------------===//
32 /// CompileUnit - This dwarf writer support class manages information associate
33 /// with a source file.
34 class CompileUnit {
35   /// ID - File identifier for source.
36   ///
37   unsigned ID;
38
39   /// Die - Compile unit debug information entry.
40   ///
41   const OwningPtr<DIE> CUDie;
42
43   /// Asm - Target of Dwarf emission.
44   AsmPrinter *Asm;
45
46   DwarfDebug *DD;
47
48   /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
49   DIE *IndexTyDie;
50
51   /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
52   /// variables to debug information entries.
53   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
54
55   /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
56   /// descriptors to debug information entries using a DIEEntry proxy.
57   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
58
59   /// Globals - A map of globally visible named entities for this unit.
60   ///
61   StringMap<DIE*> Globals;
62
63   /// GlobalTypes - A map of globally visible types for this unit.
64   ///
65   StringMap<DIE*> GlobalTypes;
66
67   /// DIEBlocks - A list of all the DIEBlocks in use.
68   std::vector<DIEBlock *> DIEBlocks;
69
70 public:
71   CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW);
72   ~CompileUnit();
73
74   // Accessors.
75   unsigned getID()                  const { return ID; }
76   DIE* getCUDie()                   const { return CUDie.get(); }
77   const StringMap<DIE*> &getGlobals()     const { return Globals; }
78   const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
79
80   /// hasContent - Return true if this compile unit has something to write out.
81   ///
82   bool hasContent() const { return !CUDie->getChildren().empty(); }
83
84   /// addGlobal - Add a new global entity to the compile unit.
85   ///
86   void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
87
88   /// addGlobalType - Add a new global type to the compile unit.
89   ///
90   void addGlobalType(StringRef Name, DIE *Die) {
91     GlobalTypes[Name] = Die;
92   }
93
94   /// getDIE - Returns the debug information entry map slot for the
95   /// specified debug variable.
96   DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
97
98   DIEBlock *getDIEBlock() { 
99     return new (DIEValueAllocator) DIEBlock();
100   }
101
102   /// insertDIE - Insert DIE into the map.
103   void insertDIE(const MDNode *N, DIE *D) {
104     MDNodeToDieMap.insert(std::make_pair(N, D));
105   }
106
107   /// getDIEEntry - Returns the debug information entry for the specified
108   /// debug variable.
109   DIEEntry *getDIEEntry(const MDNode *N) {
110     DenseMap<const MDNode *, DIEEntry *>::iterator I =
111       MDNodeToDIEEntryMap.find(N);
112     if (I == MDNodeToDIEEntryMap.end())
113       return NULL;
114     return I->second;
115   }
116
117   /// insertDIEEntry - Insert debug information entry into the map.
118   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
119     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
120   }
121
122   /// addDie - Adds or interns the DIE to the compile unit.
123   ///
124   void addDie(DIE *Buffer) {
125     this->CUDie->addChild(Buffer);
126   }
127
128   // getIndexTyDie - Get an anonymous type for index type.
129   DIE *getIndexTyDie() {
130     return IndexTyDie;
131   }
132
133   // setIndexTyDie - Set D as anonymous type for index which can be reused
134   // later.
135   void setIndexTyDie(DIE *D) {
136     IndexTyDie = D;
137   }
138 public:
139
140   /// addUInt - Add an unsigned integer attribute data and value.
141   ///
142   void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
143
144   /// addSInt - Add an signed integer attribute data and value.
145   ///
146   void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
147
148   /// addString - Add a string attribute data and value.
149   ///
150   void addString(DIE *Die, unsigned Attribute, unsigned Form,
151                  const StringRef Str);
152
153   /// addLabel - Add a Dwarf label attribute data and value.
154   ///
155   void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
156                 const MCSymbol *Label);
157
158   /// addDelta - Add a label delta attribute data and value.
159   ///
160   void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
161                 const MCSymbol *Hi, const MCSymbol *Lo);
162
163   /// addDIEEntry - Add a DIE attribute data and value.
164   ///
165   void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
166   
167   /// addBlock - Add block data.
168   ///
169   void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
170
171   /// addSourceLine - Add location information to specified debug information
172   /// entry.
173   void addSourceLine(DIE *Die, DIVariable V);
174   void addSourceLine(DIE *Die, DIGlobalVariable G);
175   void addSourceLine(DIE *Die, DISubprogram SP);
176   void addSourceLine(DIE *Die, DIType Ty);
177   void addSourceLine(DIE *Die, DINameSpace NS);
178
179   /// addAddress - Add an address attribute to a die based on the location
180   /// provided.
181   void addAddress(DIE *Die, unsigned Attribute,
182                   const MachineLocation &Location);
183
184   /// addConstantValue - Add constant value entry in variable DIE.
185   bool addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
186   bool addConstantValue(DIE *Die, ConstantInt *CI, bool Unsigned);
187
188   /// addConstantFPValue - Add constant value entry in variable DIE.
189   bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
190
191   /// addTemplateParams - Add template parameters in buffer.
192   void addTemplateParams(DIE &Buffer, DIArray TParams);
193
194   /// addRegisterOp - Add register operand.
195   void addRegisterOp(DIE *TheDie, unsigned Reg);
196
197   /// addRegisterOffset - Add register offset.
198   void addRegisterOffset(DIE *TheDie, unsigned Reg, int64_t Offset);
199
200   /// addComplexAddress - Start with the address based on the location provided,
201   /// and generate the DWARF information necessary to find the actual variable
202   /// (navigating the extra location information encoded in the type) based on
203   /// the starting location.  Add the DWARF information to the die.
204   ///
205   void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
206                          const MachineLocation &Location);
207
208   // FIXME: Should be reformulated in terms of addComplexAddress.
209   /// addBlockByrefAddress - Start with the address based on the location
210   /// provided, and generate the DWARF information necessary to find the
211   /// actual Block variable (navigating the Block struct) based on the
212   /// starting location.  Add the DWARF information to the die.  Obsolete,
213   /// please use addComplexAddress instead.
214   ///
215   void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
216                             const MachineLocation &Location);
217
218   /// addVariableAddress - Add DW_AT_location attribute for a 
219   /// DbgVariable based on provided MachineLocation.
220   void addVariableAddress(DbgVariable *&DV, DIE *Die, MachineLocation Location);
221
222   /// addToContextOwner - Add Die into the list of its context owner's children.
223   void addToContextOwner(DIE *Die, DIDescriptor Context);
224
225   /// addType - Add a new type attribute to the specified entity.
226   void addType(DIE *Entity, DIType Ty);
227
228   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
229   DIE *getOrCreateNameSpace(DINameSpace NS);
230
231   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
232   /// given DIType.
233   DIE *getOrCreateTypeDIE(DIType Ty);
234
235   /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE 
236   /// for the given DITemplateTypeParameter.
237   DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
238
239   /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE 
240   /// for the given DITemplateValueParameter.
241   DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
242
243   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
244   /// information entry.
245   DIEEntry *createDIEEntry(DIE *Entry);
246
247   void addPubTypes(DISubprogram SP);
248
249   /// constructTypeDIE - Construct basic type die from DIBasicType.
250   void constructTypeDIE(DIE &Buffer,
251                         DIBasicType BTy);
252
253   /// constructTypeDIE - Construct derived type die from DIDerivedType.
254   void constructTypeDIE(DIE &Buffer,
255                         DIDerivedType DTy);
256
257   /// constructTypeDIE - Construct type DIE from DICompositeType.
258   void constructTypeDIE(DIE &Buffer,
259                         DICompositeType CTy);
260
261   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
262   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
263
264   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
265   void constructArrayTypeDIE(DIE &Buffer, 
266                              DICompositeType *CTy);
267
268   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
269   DIE *constructEnumTypeDIE(DIEnumerator ETy);
270
271   /// createMemberDIE - Create new member DIE.
272   DIE *createMemberDIE(DIDerivedType DT);
273
274 private:
275
276   // DIEValueAllocator - All DIEValues are allocated through this allocator.
277   BumpPtrAllocator DIEValueAllocator;
278   DIEInteger *DIEIntegerOne;
279 };
280
281 } // end llvm namespace
282 #endif