blob: 255e0833c513d1a19c4f130b397c09d0807a20fd [file] [log] [blame]
Devang Patel161b2f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
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//
Eric Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031
32using namespace llvm;
33
34/// CompileUnit - Compile unit constructor.
Eli Benderskyd4a05e02012-12-03 18:45:45 +000035CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, AsmPrinter *A,
Eric Christopher2e5d8702012-12-20 21:58:36 +000036 DwarfDebug *DW, DwarfUnits *DWU)
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
38 IndexTyDie(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000039 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
40}
41
42/// ~CompileUnit - Destructor for compile unit.
43CompileUnit::~CompileUnit() {
44 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
45 DIEBlocks[j]->~DIEBlock();
46}
47
48/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
49/// information entry.
50DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
51 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
52 return Value;
53}
54
Bill Wendling6afe4782012-12-06 07:55:19 +000055/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000056/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000057int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000058 switch (Language) {
59 default:
60 break;
61
62 case dwarf::DW_LANG_C89:
63 case dwarf::DW_LANG_C99:
64 case dwarf::DW_LANG_C:
65 case dwarf::DW_LANG_C_plus_plus:
66 case dwarf::DW_LANG_ObjC:
67 case dwarf::DW_LANG_ObjC_plus_plus:
68 return 0;
69
70 case dwarf::DW_LANG_Fortran77:
71 case dwarf::DW_LANG_Fortran90:
72 case dwarf::DW_LANG_Fortran95:
73 return 1;
74
75 // The languages below have valid values only if the DWARF version >= 4.
76 case dwarf::DW_LANG_Java:
77 case dwarf::DW_LANG_Python:
78 case dwarf::DW_LANG_UPC:
79 case dwarf::DW_LANG_D:
80 if (dwarf::DWARF_VERSION >= 4)
81 return 0;
82 break;
83
84 case dwarf::DW_LANG_Ada83:
85 case dwarf::DW_LANG_Ada95:
86 case dwarf::DW_LANG_Cobol74:
87 case dwarf::DW_LANG_Cobol85:
88 case dwarf::DW_LANG_Modula2:
89 case dwarf::DW_LANG_Pascal83:
90 case dwarf::DW_LANG_PLI:
91 if (dwarf::DWARF_VERSION >= 4)
92 return 1;
93 break;
94 }
95
96 return -1;
97}
98
Eric Christopher873cf0a2012-08-24 01:14:27 +000099/// addFlag - Add a flag that is true.
100void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
101 if (!DD->useDarwinGDBCompat())
102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
103 DIEIntegerOne);
104 else
105 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
106}
107
Devang Patel161b2f42011-04-12 23:21:44 +0000108/// addUInt - Add an unsigned integer attribute data and value.
109///
110void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
111 unsigned Form, uint64_t Integer) {
112 if (!Form) Form = DIEInteger::BestForm(false, Integer);
113 DIEValue *Value = Integer == 1 ?
114 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
115 Die->addValue(Attribute, Form, Value);
116}
117
118/// addSInt - Add an signed integer attribute data and value.
119///
120void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
121 unsigned Form, int64_t Integer) {
122 if (!Form) Form = DIEInteger::BestForm(true, Integer);
123 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
124 Die->addValue(Attribute, Form, Value);
125}
126
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000127/// addString - Add a string attribute data and value. We always emit a
128/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000129/// more predictable sizes. In the case of split dwarf we emit an index
130/// into another table which gets us the static offset into the string
131/// table.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000132void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000133 if (!DD->useSplitDwarf()) {
134 MCSymbol *Symb = DU->getStringPoolEntry(String);
135 DIEValue *Value;
136 if (Asm->needsRelocationsForDwarfStringPool())
137 Value = new (DIEValueAllocator) DIELabel(Symb);
138 else {
139 MCSymbol *StringPool = DU->getStringPoolSym();
140 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
141 }
142 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
143 } else {
144 unsigned idx = DU->getStringPoolIndex(String);
145 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
146 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
147 }
148}
149
150/// addLocalString - Add a string attribute data and value. This is guaranteed
151/// to be in the local string pool instead of indirected.
152void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
153 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000154 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000155 DIEValue *Value;
156 if (Asm->needsRelocationsForDwarfStringPool())
157 Value = new (DIEValueAllocator) DIELabel(Symb);
158 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000159 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000160 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000161 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000162 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000163}
164
165/// addLabel - Add a Dwarf label attribute data and value.
166///
167void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
168 const MCSymbol *Label) {
169 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
170 Die->addValue(Attribute, Form, Value);
171}
172
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000173/// addLabelAddress - Add a dwarf label attribute data and value using
174/// DW_FORM_addr or DW_FORM_GNU_addr_index.
175///
176void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
177 MCSymbol *Label) {
178 if (!DD->useSplitDwarf()) {
179 if (Label != NULL) {
180 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
181 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
182 } else {
183 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
184 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
185 }
186 } else {
187 unsigned idx = DU->getAddrPoolIndex(Label);
188 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
189 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
190 }
191}
192
Eric Christopher0969ddf2013-01-18 22:11:33 +0000193/// addOpAddress - Add a dwarf op address data and value using the
194/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
195///
196void CompileUnit::addOpAddress(DIE *Die, MCSymbol *Sym) {
197
198 if (!DD->useSplitDwarf()) {
199 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
200 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
201 } else {
202 unsigned idx = DU->getAddrPoolIndex(Sym);
203 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
204 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
205 Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
206 }
207}
208
Devang Patel161b2f42011-04-12 23:21:44 +0000209/// addDelta - Add a label delta attribute data and value.
210///
211void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
212 const MCSymbol *Hi, const MCSymbol *Lo) {
213 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
214 Die->addValue(Attribute, Form, Value);
215}
216
217/// addDIEEntry - Add a DIE attribute data and value.
218///
219void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
220 DIE *Entry) {
221 Die->addValue(Attribute, Form, createDIEEntry(Entry));
222}
223
Devang Patel161b2f42011-04-12 23:21:44 +0000224/// addBlock - Add block data.
225///
226void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
227 DIEBlock *Block) {
228 Block->ComputeSize(Asm);
229 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
230 Die->addValue(Attribute, Block->BestForm(), Block);
231}
232
233/// addSourceLine - Add location information to specified debug information
234/// entry.
235void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
236 // Verify variable.
237 if (!V.Verify())
238 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000239
Devang Patel161b2f42011-04-12 23:21:44 +0000240 unsigned Line = V.getLineNumber();
241 if (Line == 0)
242 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000243 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000244 V.getContext().getDirectory());
245 assert(FileID && "Invalid file id");
246 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
247 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
248}
249
250/// addSourceLine - Add location information to specified debug information
251/// entry.
252void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
253 // Verify global variable.
254 if (!G.Verify())
255 return;
256
257 unsigned Line = G.getLineNumber();
258 if (Line == 0)
259 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000260 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000261 assert(FileID && "Invalid file id");
262 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
263 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
264}
265
266/// addSourceLine - Add location information to specified debug information
267/// entry.
268void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
269 // Verify subprogram.
270 if (!SP.Verify())
271 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000272
Devang Patel161b2f42011-04-12 23:21:44 +0000273 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000274 unsigned Line = SP.getLineNumber();
275 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000276 return;
277
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000278 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000279 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000280 assert(FileID && "Invalid file id");
281 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
282 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
283}
284
285/// addSourceLine - Add location information to specified debug information
286/// entry.
287void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
288 // Verify type.
289 if (!Ty.Verify())
290 return;
291
292 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000293 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000294 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000295 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000296 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000297 assert(FileID && "Invalid file id");
298 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
299 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
300}
301
302/// addSourceLine - Add location information to specified debug information
303/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000304void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
305 // Verify type.
306 if (!Ty.Verify())
307 return;
308
309 unsigned Line = Ty.getLineNumber();
310 if (Line == 0)
311 return;
312 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000313 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000314 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000315 assert(FileID && "Invalid file id");
316 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
317 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
318}
319
320/// addSourceLine - Add location information to specified debug information
321/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000322void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
323 // Verify namespace.
324 if (!NS.Verify())
325 return;
326
327 unsigned Line = NS.getLineNumber();
328 if (Line == 0)
329 return;
330 StringRef FN = NS.getFilename();
331
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000332 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000333 assert(FileID && "Invalid file id");
334 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
335 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
336}
337
Eric Christopher8b4310b2012-11-21 00:34:38 +0000338/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000339/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000340void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000341 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000342 if (DV->variableHasComplexAddress())
343 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
344 else if (DV->isBlockByrefVariable())
345 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
346 else
347 addAddress(Die, dwarf::DW_AT_location, Location);
348}
349
Devang Patel116da2f2011-04-26 19:06:18 +0000350/// addRegisterOp - Add register operand.
351void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
352 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
353 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
354 if (DWReg < 32)
355 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
356 else {
357 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
358 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
359 }
360}
361
362/// addRegisterOffset - Add register offset.
363void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
364 int64_t Offset) {
365 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
366 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
367 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
368 if (Reg == TRI->getFrameRegister(*Asm->MF))
369 // If variable offset is based in frame register then use fbreg.
370 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
371 else if (DWReg < 32)
372 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
373 else {
374 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
375 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
376 }
377 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
378}
379
380/// addAddress - Add an address attribute to a die based on the location
381/// provided.
382void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
383 const MachineLocation &Location) {
384 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
385
386 if (Location.isReg())
387 addRegisterOp(Block, Location.getReg());
388 else
389 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
390
391 // Now attach the location information to the DIE.
392 addBlock(Die, Attribute, 0, Block);
393}
394
Devang Patel161b2f42011-04-12 23:21:44 +0000395/// addComplexAddress - Start with the address based on the location provided,
396/// and generate the DWARF information necessary to find the actual variable
397/// given the extra address information encoded in the DIVariable, starting from
398/// the starting location. Add the DWARF information to the die.
399///
400void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
401 unsigned Attribute,
402 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000403 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000404 unsigned N = DV->getNumAddrElements();
405 unsigned i = 0;
406 if (Location.isReg()) {
407 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
408 // If first address element is OpPlus then emit
409 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
410 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
411 i = 2;
412 } else
413 addRegisterOp(Block, Location.getReg());
414 }
Devang Patel116da2f2011-04-26 19:06:18 +0000415 else
416 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000417
Devang Patelc26f5442011-04-28 02:22:40 +0000418 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000419 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000420 if (Element == DIBuilder::OpPlus) {
421 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
422 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
423 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000424 if (!Location.isReg())
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000426 } else llvm_unreachable("unknown DIBuilder Opcode");
427 }
428
429 // Now attach the location information to the DIE.
430 addBlock(Die, Attribute, 0, Block);
431}
432
433/* Byref variables, in Blocks, are declared by the programmer as "SomeType
434 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
435 gives the variable VarName either the struct, or a pointer to the struct, as
436 its type. This is necessary for various behind-the-scenes things the
437 compiler needs to do with by-reference variables in Blocks.
438
439 However, as far as the original *programmer* is concerned, the variable
440 should still have type 'SomeType', as originally declared.
441
442 The function getBlockByrefType dives into the __Block_byref_x_VarName
443 struct to find the original type of the variable, which is then assigned to
444 the variable's Debug Information Entry as its real type. So far, so good.
445 However now the debugger will expect the variable VarName to have the type
446 SomeType. So we need the location attribute for the variable to be an
447 expression that explains to the debugger how to navigate through the
448 pointers and struct to find the actual variable of type SomeType.
449
450 The following function does just that. We start by getting
451 the "normal" location for the variable. This will be the location
452 of either the struct __Block_byref_x_VarName or the pointer to the
453 struct __Block_byref_x_VarName.
454
455 The struct will look something like:
456
457 struct __Block_byref_x_VarName {
458 ... <various fields>
459 struct __Block_byref_x_VarName *forwarding;
460 ... <various other fields>
461 SomeType VarName;
462 ... <maybe more fields>
463 };
464
465 If we are given the struct directly (as our starting point) we
466 need to tell the debugger to:
467
468 1). Add the offset of the forwarding field.
469
470 2). Follow that pointer to get the real __Block_byref_x_VarName
471 struct to use (the real one may have been copied onto the heap).
472
473 3). Add the offset for the field VarName, to find the actual variable.
474
475 If we started with a pointer to the struct, then we need to
476 dereference that pointer first, before the other steps.
477 Translating this into DWARF ops, we will need to append the following
478 to the current location description for the variable:
479
480 DW_OP_deref -- optional, if we start with a pointer
481 DW_OP_plus_uconst <forward_fld_offset>
482 DW_OP_deref
483 DW_OP_plus_uconst <varName_fld_offset>
484
485 That is what this function does. */
486
487/// addBlockByrefAddress - Start with the address based on the location
488/// provided, and generate the DWARF information necessary to find the
489/// actual Block variable (navigating the Block struct) based on the
490/// starting location. Add the DWARF information to the die. For
491/// more information, read large comment just above here.
492///
493void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
494 unsigned Attribute,
495 const MachineLocation &Location) {
496 DIType Ty = DV->getType();
497 DIType TmpTy = Ty;
498 unsigned Tag = Ty.getTag();
499 bool isPointer = false;
500
501 StringRef varName = DV->getName();
502
503 if (Tag == dwarf::DW_TAG_pointer_type) {
504 DIDerivedType DTy = DIDerivedType(Ty);
505 TmpTy = DTy.getTypeDerivedFrom();
506 isPointer = true;
507 }
508
509 DICompositeType blockStruct = DICompositeType(TmpTy);
510
511 // Find the __forwarding field and the variable field in the __Block_byref
512 // struct.
513 DIArray Fields = blockStruct.getTypeArray();
514 DIDescriptor varField = DIDescriptor();
515 DIDescriptor forwardingField = DIDescriptor();
516
517 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
518 DIDescriptor Element = Fields.getElement(i);
519 DIDerivedType DT = DIDerivedType(Element);
520 StringRef fieldName = DT.getName();
521 if (fieldName == "__forwarding")
522 forwardingField = Element;
523 else if (fieldName == varName)
524 varField = Element;
525 }
526
527 // Get the offsets for the forwarding field and the variable field.
528 unsigned forwardingFieldOffset =
529 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
530 unsigned varFieldOffset =
531 DIDerivedType(varField).getOffsetInBits() >> 3;
532
533 // Decode the original location, and use that as the start of the byref
534 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000535 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
536
Eric Christophercaba2632012-07-04 02:02:18 +0000537 if (Location.isReg())
538 addRegisterOp(Block, Location.getReg());
539 else
540 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000541
542 // If we started with a pointer to the __Block_byref... struct, then
543 // the first thing we need to do is dereference the pointer (DW_OP_deref).
544 if (isPointer)
545 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
546
547 // Next add the offset for the '__forwarding' field:
548 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
549 // adding the offset if it's 0.
550 if (forwardingFieldOffset > 0) {
551 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
552 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
553 }
554
555 // Now dereference the __forwarding field to get to the real __Block_byref
556 // struct: DW_OP_deref.
557 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
558
559 // Now that we've got the real __Block_byref... struct, add the offset
560 // for the variable's field to get to the location of the actual variable:
561 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
562 if (varFieldOffset > 0) {
563 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
564 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
565 }
566
567 // Now attach the location information to the DIE.
568 addBlock(Die, Attribute, 0, Block);
569}
570
Devang Patel4ec14b02011-07-20 21:57:04 +0000571/// isTypeSigned - Return true if the type is signed.
572static bool isTypeSigned(DIType Ty, int *SizeInBits) {
573 if (Ty.isDerivedType())
574 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
575 if (Ty.isBasicType())
576 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
577 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
578 *SizeInBits = Ty.getSizeInBits();
579 return true;
580 }
581 return false;
582}
583
Devang Patel161b2f42011-04-12 23:21:44 +0000584/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000585bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
586 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000587 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000588 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000589 int SizeInBits = -1;
590 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
591 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
592 switch (SizeInBits) {
593 case 8: Form = dwarf::DW_FORM_data1; break;
594 case 16: Form = dwarf::DW_FORM_data2; break;
595 case 32: Form = dwarf::DW_FORM_data4; break;
596 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000597 default: break;
598 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000599 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000600 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000601
Devang Patel161b2f42011-04-12 23:21:44 +0000602 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
603 return true;
604}
605
606/// addConstantFPValue - Add constant value entry in variable DIE.
607bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000608 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000609 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
610 APFloat FPImm = MO.getFPImm()->getValueAPF();
611
612 // Get the raw data form of the floating point.
613 const APInt FltVal = FPImm.bitcastToAPInt();
614 const char *FltPtr = (const char*)FltVal.getRawData();
615
616 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000617 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000618 int Incr = (LittleEndian ? 1 : -1);
619 int Start = (LittleEndian ? 0 : NumBytes - 1);
620 int Stop = (LittleEndian ? NumBytes : -1);
621
622 // Output the constant to DWARF one byte at a time.
623 for (; Start != Stop; Start += Incr)
624 addUInt(Block, 0, dwarf::DW_FORM_data1,
625 (unsigned char)0xFF & FltPtr[Start]);
626
627 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
628 return true;
629}
630
631/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000632bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000633 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000634 unsigned CIBitWidth = CI->getBitWidth();
635 if (CIBitWidth <= 64) {
636 unsigned form = 0;
637 switch (CIBitWidth) {
638 case 8: form = dwarf::DW_FORM_data1; break;
639 case 16: form = dwarf::DW_FORM_data2; break;
640 case 32: form = dwarf::DW_FORM_data4; break;
641 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000642 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000643 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
644 }
Devang Patel161b2f42011-04-12 23:21:44 +0000645 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000646 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000647 else
Devang Pateld6a81362011-05-28 00:39:18 +0000648 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000649 return true;
650 }
651
652 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
653
654 // Get the raw data form of the large APInt.
655 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000656 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000657
658 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000659 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000660
661 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000662 for (int i = 0; i < NumBytes; i++) {
663 uint8_t c;
664 if (LittleEndian)
665 c = Ptr64[i / 8] >> (8 * (i & 7));
666 else
667 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
668 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
669 }
Devang Patel161b2f42011-04-12 23:21:44 +0000670
671 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
672 return true;
673}
674
675/// addTemplateParams - Add template parameters in buffer.
676void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
677 // Add template parameters.
678 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
679 DIDescriptor Element = TParams.getElement(i);
680 if (Element.isTemplateTypeParameter())
681 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
682 DITemplateTypeParameter(Element)));
683 else if (Element.isTemplateValueParameter())
684 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
685 DITemplateValueParameter(Element)));
686 }
Devang Patel161b2f42011-04-12 23:21:44 +0000687}
Nick Lewycky746cb672011-10-26 22:55:33 +0000688
Eric Christopher6b6061f2013-01-16 01:22:23 +0000689/// getOrCreateContextDIE - Get context owner's DIE.
690DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
691 if (Context.isType())
692 return getOrCreateTypeDIE(DIType(Context));
693 else if (Context.isNameSpace())
694 return getOrCreateNameSpace(DINameSpace(Context));
695 else if (Context.isSubprogram())
696 return getOrCreateSubprogramDIE(DISubprogram(Context));
697 else
698 return getDIE(Context);
699}
700
Devang Patel161b2f42011-04-12 23:21:44 +0000701/// addToContextOwner - Add Die into the list of its context owner's children.
702void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000703 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000704 ContextDIE->addChild(Die);
705 else
706 addDie(Die);
707}
708
709/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
710/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000711DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
712 DIType Ty(TyNode);
713 if (!Ty.Verify())
714 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000715 DIE *TyDIE = getDIE(Ty);
716 if (TyDIE)
717 return TyDIE;
718
719 // Create new type.
720 TyDIE = new DIE(dwarf::DW_TAG_base_type);
721 insertDIE(Ty, TyDIE);
722 if (Ty.isBasicType())
723 constructTypeDIE(*TyDIE, DIBasicType(Ty));
724 else if (Ty.isCompositeType())
725 constructTypeDIE(*TyDIE, DICompositeType(Ty));
726 else {
727 assert(Ty.isDerivedType() && "Unknown kind of DIType");
728 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
729 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000730 // If this is a named finished type then include it in the list of types
731 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000732 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
733 bool IsImplementation = 0;
734 if (Ty.isCompositeType()) {
735 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000736 // A runtime language of 0 actually means C/C++ and that any
737 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000738 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000739 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000740 }
Eric Christophere0167892012-01-06 23:03:37 +0000741 unsigned Flags = IsImplementation ?
742 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
743 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000744 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000745
Devang Patel161b2f42011-04-12 23:21:44 +0000746 addToContextOwner(TyDIE, Ty.getContext());
747 return TyDIE;
748}
749
750/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000751void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000752 if (!Ty.Verify())
753 return;
754
755 // Check for pre-existence.
756 DIEEntry *Entry = getDIEEntry(Ty);
757 // If it exists then use the existing value.
758 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000759 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000760 return;
761 }
762
763 // Construct type.
764 DIE *Buffer = getOrCreateTypeDIE(Ty);
765
766 // Set up proxy.
767 Entry = createDIEEntry(Buffer);
768 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000769 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000770
771 // If this is a complete composite type then include it in the
772 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000773 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000774}
775
776/// addGlobalType - Add a new global type to the compile unit.
777///
Devang Patelc20bdf12011-06-01 00:23:24 +0000778void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000779 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000780 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
781 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000782 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000783 if (DIEEntry *Entry = getDIEEntry(Ty))
784 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000785}
786
Devang Patel31c5d052011-05-06 16:57:54 +0000787/// addPubTypes - Add type for pubtypes section.
788void CompileUnit::addPubTypes(DISubprogram SP) {
789 DICompositeType SPTy = SP.getType();
790 unsigned SPTag = SPTy.getTag();
791 if (SPTag != dwarf::DW_TAG_subroutine_type)
792 return;
793
794 DIArray Args = SPTy.getTypeArray();
795 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
796 DIType ATy(Args.getElement(i));
797 if (!ATy.Verify())
798 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000799 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000800 }
801}
802
Devang Patel161b2f42011-04-12 23:21:44 +0000803/// constructTypeDIE - Construct basic type die from DIBasicType.
804void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
805 // Get core information.
806 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000807 // Add name if not anonymous or intermediate type.
808 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000809 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000810
811 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
812 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
813 // Unspecified types has only name, nothing else.
814 return;
815 }
816
817 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000818 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000819 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000820
Devang Patel161b2f42011-04-12 23:21:44 +0000821 uint64_t Size = BTy.getSizeInBits() >> 3;
822 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
823}
824
825/// constructTypeDIE - Construct derived type die from DIDerivedType.
826void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
827 // Get core information.
828 StringRef Name = DTy.getName();
829 uint64_t Size = DTy.getSizeInBits() >> 3;
830 unsigned Tag = DTy.getTag();
831
832 // FIXME - Workaround for templates.
833 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
834
835 Buffer.setTag(Tag);
836
837 // Map to main type, void will not have a type.
838 DIType FromTy = DTy.getTypeDerivedFrom();
839 addType(&Buffer, FromTy);
840
841 // Add name if not anonymous or intermediate type.
842 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000843 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000844
845 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000846 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000847 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
848
David Blaikie62fdfb52013-01-07 05:51:15 +0000849 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
850 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
851 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000852 // Add source line info if available and TyDesc is not a forward declaration.
853 if (!DTy.isForwardDecl())
854 addSourceLine(&Buffer, DTy);
855}
856
857/// constructTypeDIE - Construct type DIE from DICompositeType.
858void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
859 // Get core information.
860 StringRef Name = CTy.getName();
861
862 uint64_t Size = CTy.getSizeInBits() >> 3;
863 unsigned Tag = CTy.getTag();
864 Buffer.setTag(Tag);
865
866 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000867 case dwarf::DW_TAG_array_type:
868 constructArrayTypeDIE(Buffer, &CTy);
869 break;
870 case dwarf::DW_TAG_enumeration_type: {
871 DIArray Elements = CTy.getTypeArray();
872
873 // Add enumerators to enumeration type.
874 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
875 DIE *ElemDie = NULL;
876 DIDescriptor Enum(Elements.getElement(i));
877 if (Enum.isEnumerator()) {
878 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
879 Buffer.addChild(ElemDie);
880 }
881 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000882 DIType DTy = CTy.getTypeDerivedFrom();
883 if (DTy.Verify()) {
884 addType(&Buffer, DTy);
885 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
886 }
Devang Patel161b2f42011-04-12 23:21:44 +0000887 }
888 break;
889 case dwarf::DW_TAG_subroutine_type: {
890 // Add return type.
891 DIArray Elements = CTy.getTypeArray();
892 DIDescriptor RTy = Elements.getElement(0);
893 addType(&Buffer, DIType(RTy));
894
895 bool isPrototyped = true;
896 // Add arguments.
897 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
898 DIDescriptor Ty = Elements.getElement(i);
899 if (Ty.isUnspecifiedParameter()) {
900 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
901 Buffer.addChild(Arg);
902 isPrototyped = false;
903 } else {
904 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
905 addType(Arg, DIType(Ty));
906 Buffer.addChild(Arg);
907 }
908 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000909 // Add prototype flag if we're dealing with a C language and the
910 // function has been prototyped.
911 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000912 (Language == dwarf::DW_LANG_C89 ||
913 Language == dwarf::DW_LANG_C99 ||
914 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000915 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000916 }
917 break;
918 case dwarf::DW_TAG_structure_type:
919 case dwarf::DW_TAG_union_type:
920 case dwarf::DW_TAG_class_type: {
921 // Add elements to structure type.
922 DIArray Elements = CTy.getTypeArray();
923
924 // A forward struct declared type may not have elements available.
925 unsigned N = Elements.getNumElements();
926 if (N == 0)
927 break;
928
929 // Add elements to structure type.
930 for (unsigned i = 0; i < N; ++i) {
931 DIDescriptor Element = Elements.getElement(i);
932 DIE *ElemDie = NULL;
933 if (Element.isSubprogram()) {
934 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000935 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000936 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000937 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000938 dwarf::DW_ACCESS_protected);
939 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000940 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000941 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000942 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000943 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000944 dwarf::DW_ACCESS_public);
945 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000946 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000947 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000948 DIDerivedType DDTy(Element);
949 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
950 ElemDie = new DIE(dwarf::DW_TAG_friend);
951 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000952 } else if (DDTy.isStaticMember())
953 ElemDie = createStaticMemberDIE(DDTy);
954 else
955 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000956 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000957 DIObjCProperty Property(Element);
958 ElemDie = new DIE(Property.getTag());
959 StringRef PropertyName = Property.getObjCPropertyName();
960 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000961 addType(ElemDie, Property.getType());
962 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000963 StringRef GetterName = Property.getObjCPropertyGetterName();
964 if (!GetterName.empty())
965 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
966 StringRef SetterName = Property.getObjCPropertySetterName();
967 if (!SetterName.empty())
968 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
969 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000970 if (Property.isReadOnlyObjCProperty())
971 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
972 if (Property.isReadWriteObjCProperty())
973 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
974 if (Property.isAssignObjCProperty())
975 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
976 if (Property.isRetainObjCProperty())
977 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
978 if (Property.isCopyObjCProperty())
979 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
980 if (Property.isNonAtomicObjCProperty())
981 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
982 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000983 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000984 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000985
Devang Patel30d409c2012-02-07 23:33:58 +0000986 DIEEntry *Entry = getDIEEntry(Element);
987 if (!Entry) {
988 Entry = createDIEEntry(ElemDie);
989 insertDIEEntry(Element, Entry);
990 }
Devang Patel9e11eb12012-02-04 01:30:32 +0000991 } else
Devang Patel161b2f42011-04-12 23:21:44 +0000992 continue;
993 Buffer.addChild(ElemDie);
994 }
995
996 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000997 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +0000998
Devang Patel161b2f42011-04-12 23:21:44 +0000999 DICompositeType ContainingType = CTy.getContainingType();
1000 if (DIDescriptor(ContainingType).isCompositeType())
1001 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1002 getOrCreateTypeDIE(DIType(ContainingType)));
1003 else {
1004 DIDescriptor Context = CTy.getContext();
1005 addToContextOwner(&Buffer, Context);
1006 }
1007
Devang Patel201e6cd2011-05-12 21:29:42 +00001008 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001009 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001010
Eric Christopher1a8e8862011-12-16 23:42:42 +00001011 // Add template parameters to a class, structure or union types.
1012 // FIXME: The support isn't in the metadata for this yet.
1013 if (Tag == dwarf::DW_TAG_class_type ||
1014 Tag == dwarf::DW_TAG_structure_type ||
1015 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001016 addTemplateParams(Buffer, CTy.getTemplateParams());
1017
1018 break;
1019 }
1020 default:
1021 break;
1022 }
1023
1024 // Add name if not anonymous or intermediate type.
1025 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001026 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001027
Eric Christopher4a5d8392012-05-22 18:45:18 +00001028 if (Tag == dwarf::DW_TAG_enumeration_type ||
1029 Tag == dwarf::DW_TAG_class_type ||
1030 Tag == dwarf::DW_TAG_structure_type ||
1031 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001032 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001033 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001034 if (Size)
1035 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001036 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001037 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001038 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1039
1040 // If we're a forward decl, say so.
1041 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001042 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001043
1044 // Add source line info if available.
1045 if (!CTy.isForwardDecl())
1046 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001047
1048 // No harm in adding the runtime language to the declaration.
1049 unsigned RLang = CTy.getRunTimeLang();
1050 if (RLang)
1051 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1052 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001053 }
1054}
1055
Eric Christopher8b4310b2012-11-21 00:34:38 +00001056/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001057/// for the given DITemplateTypeParameter.
1058DIE *
1059CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1060 DIE *ParamDIE = getDIE(TP);
1061 if (ParamDIE)
1062 return ParamDIE;
1063
1064 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1065 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001066 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001067 return ParamDIE;
1068}
1069
Eric Christopher8b4310b2012-11-21 00:34:38 +00001070/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001071/// for the given DITemplateValueParameter.
1072DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001073CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001074 DIE *ParamDIE = getDIE(TPV);
1075 if (ParamDIE)
1076 return ParamDIE;
1077
1078 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1079 addType(ParamDIE, TPV.getType());
1080 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001081 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001082 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001083 TPV.getValue());
1084 return ParamDIE;
1085}
1086
Devang Patel31c5d052011-05-06 16:57:54 +00001087/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1088DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1089 DIE *NDie = getDIE(NS);
1090 if (NDie)
1091 return NDie;
1092 NDie = new DIE(dwarf::DW_TAG_namespace);
1093 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001094 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001095 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001096 addAccelNamespace(NS.getName(), NDie);
1097 } else
1098 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001099 addSourceLine(NDie, NS);
1100 addToContextOwner(NDie, NS.getContext());
1101 return NDie;
1102}
1103
Devang Pateldbc64af2011-08-15 17:24:54 +00001104/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1105/// printer to not emit usual symbol prefix before the symbol name is used then
1106/// return linkage name after skipping this special LLVM prefix.
1107static StringRef getRealLinkageName(StringRef LinkageName) {
1108 char One = '\1';
1109 if (LinkageName.startswith(StringRef(&One, 1)))
1110 return LinkageName.substr(1);
1111 return LinkageName;
1112}
1113
1114/// getOrCreateSubprogramDIE - Create new DIE using SP.
1115DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1116 DIE *SPDie = getDIE(SP);
1117 if (SPDie)
1118 return SPDie;
1119
Peter Collingbourne27302f02012-05-27 18:36:44 +00001120 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1121
1122 // DW_TAG_inlined_subroutine may refer to this DIE.
1123 insertDIE(SP, SPDie);
1124
Rafael Espindola01b55b42011-11-10 22:34:29 +00001125 DISubprogram SPDecl = SP.getFunctionDeclaration();
1126 DIE *DeclDie = NULL;
1127 if (SPDecl.isSubprogram()) {
1128 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1129 }
1130
Devang Pateldbc64af2011-08-15 17:24:54 +00001131 // Add to context owner.
1132 addToContextOwner(SPDie, SP.getContext());
1133
1134 // Add function template parameters.
1135 addTemplateParams(*SPDie, SP.getTemplateParams());
1136
Eric Christophere9722e12012-04-16 23:54:23 +00001137 // Unfortunately this code needs to stay here instead of below the
1138 // AT_specification code in order to work around a bug in older
1139 // gdbs that requires the linkage name to resolve multiple template
1140 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001141 // TODO: Remove this set of code when we get rid of the old gdb
1142 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001143 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001144 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001145 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1146 getRealLinkageName(LinkageName));
1147
Devang Pateldbc64af2011-08-15 17:24:54 +00001148 // If this DIE is going to refer declaration info using AT_specification
1149 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001150 if (DeclDie) {
1151 // Refer function declaration directly.
1152 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1153 DeclDie);
1154
Devang Pateldbc64af2011-08-15 17:24:54 +00001155 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001156 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001157
Eric Christophercbbd5b12012-08-23 22:52:55 +00001158 // Add the linkage name if we have one.
1159 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1160 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1161 getRealLinkageName(LinkageName));
1162
Devang Pateldbc64af2011-08-15 17:24:54 +00001163 // Constructors and operators for anonymous aggregates do not have names.
1164 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001165 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001166
1167 addSourceLine(SPDie, SP);
1168
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001169 // Add the prototype if we have a prototype and we have a C like
1170 // language.
1171 if (SP.isPrototyped() &&
1172 (Language == dwarf::DW_LANG_C89 ||
1173 Language == dwarf::DW_LANG_C99 ||
1174 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001175 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001176
1177 // Add Return Type.
1178 DICompositeType SPTy = SP.getType();
1179 DIArray Args = SPTy.getTypeArray();
1180 unsigned SPTag = SPTy.getTag();
1181
1182 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1183 addType(SPDie, SPTy);
1184 else
1185 addType(SPDie, DIType(Args.getElement(0)));
1186
1187 unsigned VK = SP.getVirtuality();
1188 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001189 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001190 DIEBlock *Block = getDIEBlock();
1191 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1192 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1193 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1194 ContainingTypeMap.insert(std::make_pair(SPDie,
1195 SP.getContainingType()));
1196 }
1197
1198 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001199 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001200
Devang Pateldbc64af2011-08-15 17:24:54 +00001201 // Add arguments. Do not add arguments for subprogram definition. They will
1202 // be handled while processing variables.
1203 DICompositeType SPTy = SP.getType();
1204 DIArray Args = SPTy.getTypeArray();
1205 unsigned SPTag = SPTy.getTag();
1206
1207 if (SPTag == dwarf::DW_TAG_subroutine_type)
1208 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1209 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001210 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001211 addType(Arg, ATy);
1212 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001213 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001214 SPDie->addChild(Arg);
1215 }
1216 }
1217
1218 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001219 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001220
1221 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001222 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001223
1224 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001225 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001226
1227 if (unsigned isa = Asm->getISAEncoding()) {
1228 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1229 }
1230
1231 return SPDie;
1232}
1233
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001234// Return const expression if value is a GEP to access merged global
1235// constant. e.g.
1236// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1237static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1238 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1239 if (!CE || CE->getNumOperands() != 3 ||
1240 CE->getOpcode() != Instruction::GetElementPtr)
1241 return NULL;
1242
1243 // First operand points to a global struct.
1244 Value *Ptr = CE->getOperand(0);
1245 if (!isa<GlobalValue>(Ptr) ||
1246 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1247 return NULL;
1248
1249 // Second operand is zero.
1250 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1251 if (!CI || !CI->isZero())
1252 return NULL;
1253
1254 // Third operand is offset.
1255 if (!isa<ConstantInt>(CE->getOperand(2)))
1256 return NULL;
1257
1258 return CE;
1259}
1260
1261/// createGlobalVariableDIE - create global variable DIE.
1262void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001263 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001264 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001265 return;
1266
Devang Patel49e2f032011-08-18 22:21:50 +00001267 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001268 if (!GV.Verify())
1269 return;
1270
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001271 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001272 DIType GTy = GV.getType();
1273
1274 // If this is a static data member definition, some attributes belong
1275 // to the declaration DIE.
1276 DIE *VariableDIE = NULL;
1277 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1278 if (SDMDecl.Verify()) {
1279 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1280 // We need the declaration DIE that is in the static member's class.
1281 // But that class might not exist in the DWARF yet.
1282 // Creating the class will create the static member decl DIE.
1283 getOrCreateContextDIE(SDMDecl.getContext());
1284 VariableDIE = getDIE(SDMDecl);
1285 assert(VariableDIE && "Static member decl has no context?");
1286 }
1287
1288 // If this is not a static data member definition, create the variable
1289 // DIE and add the initial set of attributes to it.
1290 if (!VariableDIE) {
1291 VariableDIE = new DIE(GV.getTag());
1292 // Add to map.
1293 insertDIE(N, VariableDIE);
1294
1295 // Add name and type.
1296 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1297 addType(VariableDIE, GTy);
1298
1299 // Add scoping info.
1300 if (!GV.isLocalToUnit())
1301 addFlag(VariableDIE, dwarf::DW_AT_external);
1302
1303 // Add line number info.
1304 addSourceLine(VariableDIE, GV);
1305 // Add to context owner.
1306 addToContextOwner(VariableDIE, GVContext);
1307 }
1308
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001309 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001310 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001311 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001312 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001313 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001314 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001315 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopher0969ddf2013-01-18 22:11:33 +00001316 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001317 // Do not create specification DIE if context is either compile unit
1318 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001319 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001320 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1321 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001322 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001323 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1324 dwarf::DW_FORM_ref4, VariableDIE);
1325 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001326 // A static member's declaration is already flagged as such.
1327 if (!SDMDecl.Verify())
1328 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001329 addDie(VariableSpecDIE);
1330 } else {
1331 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001332 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001333 // Add linkage name.
1334 StringRef LinkageName = GV.getLinkageName();
1335 if (!LinkageName.empty() && isGlobalVariable)
1336 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1337 getRealLinkageName(LinkageName));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001338 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001339 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1340 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1341 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001342 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001343 // GV is a merged global.
1344 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1345 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001346 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001347 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1348 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001349 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001350 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001351 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1352 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1353 }
1354
Eric Christopherd117fbb2011-11-11 01:55:22 +00001355 if (addToAccelTable) {
1356 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1357 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001358
Eric Christopherd117fbb2011-11-11 01:55:22 +00001359 // If the linkage name is different than the name, go ahead and output
1360 // that as well into the name table.
1361 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1362 addAccelName(GV.getLinkageName(), AddrDIE);
1363 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001364
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001365 return;
1366}
1367
Devang Patel161b2f42011-04-12 23:21:44 +00001368/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001369void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1370 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001371 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1372 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001373
Bill Wendling222c2fd2012-12-06 07:38:10 +00001374 // The LowerBound value defines the lower bounds which is typically zero for
1375 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1376 // Count == -1 then the array is unbounded and we do not emit
1377 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1378 // Count == 0, then the array has zero elements in which case we do not emit
1379 // an upper bound.
1380 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001381 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001382 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001383
Bill Wendling6afe4782012-12-06 07:55:19 +00001384 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001385 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1386
1387 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001388 // FIXME: An unbounded array should reference the expression that defines
1389 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001390 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001391
Devang Patel161b2f42011-04-12 23:21:44 +00001392 Buffer.addChild(DW_Subrange);
1393}
1394
1395/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1396void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1397 DICompositeType *CTy) {
1398 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001399 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001400 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001401
1402 // Emit derived type.
1403 addType(&Buffer, CTy->getTypeDerivedFrom());
1404 DIArray Elements = CTy->getTypeArray();
1405
1406 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001407 // FIXME: This type should be passed down from the front end
1408 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001409 DIE *IdxTy = getIndexTyDie();
1410 if (!IdxTy) {
1411 // Construct an anonymous type for index type.
1412 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001413 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001414 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1415 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1416 dwarf::DW_ATE_signed);
1417 addDie(IdxTy);
1418 setIndexTyDie(IdxTy);
1419 }
1420
1421 // Add subranges to array type.
1422 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1423 DIDescriptor Element = Elements.getElement(i);
1424 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1425 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1426 }
1427}
1428
1429/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1430DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1431 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1432 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001433 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001434 int64_t Value = ETy.getEnumValue();
1435 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1436 return Enumerator;
1437}
1438
Devang Pateldbc64af2011-08-15 17:24:54 +00001439/// constructContainingTypeDIEs - Construct DIEs for types that contain
1440/// vtables.
1441void CompileUnit::constructContainingTypeDIEs() {
1442 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1443 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1444 DIE *SPDie = CI->first;
1445 const MDNode *N = CI->second;
1446 if (!N) continue;
1447 DIE *NDie = getDIE(N);
1448 if (!NDie) continue;
1449 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1450 }
1451}
1452
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001453/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1454DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1455 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001456
1457 // Translate tag to proper Dwarf tag.
1458 unsigned Tag = DV->getTag();
1459
1460 // Define variable debug information entry.
1461 DIE *VariableDie = new DIE(Tag);
1462 DbgVariable *AbsVar = DV->getAbstractVariable();
1463 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1464 if (AbsDIE)
1465 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1466 dwarf::DW_FORM_ref4, AbsDIE);
1467 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001468 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001469 addSourceLine(VariableDie, DV->getVariable());
1470 addType(VariableDie, DV->getType());
1471 }
1472
1473 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001474 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001475
1476 if (isScopeAbstract) {
1477 DV->setDIE(VariableDie);
1478 return VariableDie;
1479 }
1480
1481 // Add variable address.
1482
1483 unsigned Offset = DV->getDotDebugLocOffset();
1484 if (Offset != ~0U) {
1485 addLabel(VariableDie, dwarf::DW_AT_location,
1486 dwarf::DW_FORM_data4,
1487 Asm->GetTempSymbol("debug_loc", Offset));
1488 DV->setDIE(VariableDie);
1489 return VariableDie;
1490 }
1491
Eric Christopher8cf5e742011-10-03 15:49:20 +00001492 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001493 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1494 bool updated = false;
1495 if (DVInsn->getNumOperands() == 3) {
1496 if (DVInsn->getOperand(0).isReg()) {
1497 const MachineOperand RegOp = DVInsn->getOperand(0);
1498 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1499 if (DVInsn->getOperand(1).isImm() &&
1500 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1501 unsigned FrameReg = 0;
1502 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001503 int Offset =
1504 TFI->getFrameIndexReference(*Asm->MF,
1505 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001506 FrameReg);
1507 MachineLocation Location(FrameReg, Offset);
1508 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001509
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001510 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001511 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001512 MachineLocation(RegOp.getReg()));
1513 updated = true;
1514 }
1515 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001516 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001517 addConstantValue(VariableDie, DVInsn->getOperand(0),
1518 DV->getType());
1519 else if (DVInsn->getOperand(0).isFPImm())
1520 updated =
1521 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1522 else if (DVInsn->getOperand(0).isCImm())
1523 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001524 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001525 DVInsn->getOperand(0).getCImm(),
1526 DV->getType().isUnsignedDIType());
1527 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001528 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001529 Asm->getDebugValueLocation(DVInsn));
1530 updated = true;
1531 }
1532 if (!updated) {
1533 // If variableDie is not updated then DBG_VALUE instruction does not
1534 // have valid variable info.
1535 delete VariableDie;
1536 return NULL;
1537 }
1538 DV->setDIE(VariableDie);
1539 return VariableDie;
1540 } else {
1541 // .. else use frame index.
1542 int FI = DV->getFrameIndex();
1543 if (FI != ~0) {
1544 unsigned FrameReg = 0;
1545 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001546 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001547 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1548 MachineLocation Location(FrameReg, Offset);
1549 addVariableAddress(DV, VariableDie, Location);
1550 }
1551 }
1552
1553 DV->setDIE(VariableDie);
1554 return VariableDie;
1555}
1556
Devang Patel161b2f42011-04-12 23:21:44 +00001557/// createMemberDIE - Create new member DIE.
1558DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1559 DIE *MemberDie = new DIE(DT.getTag());
1560 StringRef Name = DT.getName();
1561 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001562 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001563
1564 addType(MemberDie, DT.getTypeDerivedFrom());
1565
1566 addSourceLine(MemberDie, DT);
1567
1568 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1569 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1570
1571 uint64_t Size = DT.getSizeInBits();
1572 uint64_t FieldSize = DT.getOriginalTypeSize();
1573
1574 if (Size != FieldSize) {
1575 // Handle bitfield.
1576 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1577 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1578
1579 uint64_t Offset = DT.getOffsetInBits();
1580 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1581 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1582 uint64_t FieldOffset = (HiMark - FieldSize);
1583 Offset -= FieldOffset;
1584
1585 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001586 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001587 Offset = FieldSize - (Offset + Size);
1588 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1589
1590 // Here WD_AT_data_member_location points to the anonymous
1591 // field that includes this bit field.
1592 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1593
1594 } else
1595 // This is not a bitfield.
1596 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1597
1598 if (DT.getTag() == dwarf::DW_TAG_inheritance
1599 && DT.isVirtual()) {
1600
1601 // For C++, virtual base classes are not at fixed offset. Use following
1602 // expression to extract appropriate offset from vtable.
1603 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1604
1605 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1606 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1607 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1608 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1609 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1610 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1611 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1612 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1613
1614 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1615 VBaseLocationDie);
1616 } else
1617 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1618
1619 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001620 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001621 dwarf::DW_ACCESS_protected);
1622 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001623 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001624 dwarf::DW_ACCESS_private);
1625 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001626 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001627 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001628 dwarf::DW_ACCESS_public);
1629 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001630 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001631 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001632
1633 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001634 if (MDNode *PNode = DT.getObjCProperty())
1635 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001636 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001637 PropertyDie);
1638
David Blaikie01bc2b32012-12-13 22:43:07 +00001639 if (DT.isArtificial())
1640 addFlag(MemberDie, dwarf::DW_AT_artificial);
1641
Devang Patel9e11eb12012-02-04 01:30:32 +00001642 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001643 StringRef PropertyName = DT.getObjCPropertyName();
1644 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001645 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001646 StringRef GetterName = DT.getObjCPropertyGetterName();
1647 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001648 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001649 StringRef SetterName = DT.getObjCPropertySetterName();
1650 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001651 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001652 unsigned PropertyAttributes = 0;
1653 if (DT.isReadOnlyObjCProperty())
1654 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1655 if (DT.isReadWriteObjCProperty())
1656 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1657 if (DT.isAssignObjCProperty())
1658 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1659 if (DT.isRetainObjCProperty())
1660 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1661 if (DT.isCopyObjCProperty())
1662 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1663 if (DT.isNonAtomicObjCProperty())
1664 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1665 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001666 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001667 PropertyAttributes);
1668 }
Devang Patel161b2f42011-04-12 23:21:44 +00001669 return MemberDie;
1670}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001671
1672/// createStaticMemberDIE - Create new DIE for C++ static member.
1673DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1674 if (!DT.Verify())
1675 return NULL;
1676
1677 DIE *StaticMemberDIE = new DIE(DT.getTag());
1678 DIType Ty = DT.getTypeDerivedFrom();
1679
1680 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1681 addType(StaticMemberDIE, Ty);
1682 addSourceLine(StaticMemberDIE, DT);
1683 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1684 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1685
1686 // FIXME: We could omit private if the parent is a class_type, and
1687 // public if the parent is something else.
1688 if (DT.isProtected())
1689 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1690 dwarf::DW_ACCESS_protected);
1691 else if (DT.isPrivate())
1692 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1693 dwarf::DW_ACCESS_private);
1694 else
1695 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1696 dwarf::DW_ACCESS_public);
1697
1698 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1699 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
1700
1701 insertDIE(DT, StaticMemberDIE);
1702 return StaticMemberDIE;
1703}