blob: e98b1182251ed22cd8a2ff7de4f91d2c6075fde1 [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
Devang Patel161b2f42011-04-12 23:21:44 +0000193/// addDelta - Add a label delta attribute data and value.
194///
195void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
196 const MCSymbol *Hi, const MCSymbol *Lo) {
197 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
198 Die->addValue(Attribute, Form, Value);
199}
200
201/// addDIEEntry - Add a DIE attribute data and value.
202///
203void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
204 DIE *Entry) {
205 Die->addValue(Attribute, Form, createDIEEntry(Entry));
206}
207
Devang Patel161b2f42011-04-12 23:21:44 +0000208/// addBlock - Add block data.
209///
210void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
211 DIEBlock *Block) {
212 Block->ComputeSize(Asm);
213 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
214 Die->addValue(Attribute, Block->BestForm(), Block);
215}
216
217/// addSourceLine - Add location information to specified debug information
218/// entry.
219void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
220 // Verify variable.
221 if (!V.Verify())
222 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000223
Devang Patel161b2f42011-04-12 23:21:44 +0000224 unsigned Line = V.getLineNumber();
225 if (Line == 0)
226 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000227 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000228 V.getContext().getDirectory());
229 assert(FileID && "Invalid file id");
230 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
231 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
232}
233
234/// addSourceLine - Add location information to specified debug information
235/// entry.
236void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
237 // Verify global variable.
238 if (!G.Verify())
239 return;
240
241 unsigned Line = G.getLineNumber();
242 if (Line == 0)
243 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000244 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000245 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, DISubprogram SP) {
253 // Verify subprogram.
254 if (!SP.Verify())
255 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000256
Devang Patel161b2f42011-04-12 23:21:44 +0000257 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000258 unsigned Line = SP.getLineNumber();
259 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000260 return;
261
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000262 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000263 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000264 assert(FileID && "Invalid file id");
265 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
266 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
267}
268
269/// addSourceLine - Add location information to specified debug information
270/// entry.
271void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
272 // Verify type.
273 if (!Ty.Verify())
274 return;
275
276 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000277 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000278 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000279 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000280 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000281 assert(FileID && "Invalid file id");
282 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
283 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
284}
285
286/// addSourceLine - Add location information to specified debug information
287/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000288void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
289 // Verify type.
290 if (!Ty.Verify())
291 return;
292
293 unsigned Line = Ty.getLineNumber();
294 if (Line == 0)
295 return;
296 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000297 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000298 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000299 assert(FileID && "Invalid file id");
300 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
301 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
302}
303
304/// addSourceLine - Add location information to specified debug information
305/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000306void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
307 // Verify namespace.
308 if (!NS.Verify())
309 return;
310
311 unsigned Line = NS.getLineNumber();
312 if (Line == 0)
313 return;
314 StringRef FN = NS.getFilename();
315
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000316 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000317 assert(FileID && "Invalid file id");
318 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
319 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
320}
321
Eric Christopher8b4310b2012-11-21 00:34:38 +0000322/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000323/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000324void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000325 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000326 if (DV->variableHasComplexAddress())
327 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
328 else if (DV->isBlockByrefVariable())
329 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
330 else
331 addAddress(Die, dwarf::DW_AT_location, Location);
332}
333
Devang Patel116da2f2011-04-26 19:06:18 +0000334/// addRegisterOp - Add register operand.
335void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
336 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
337 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
338 if (DWReg < 32)
339 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
340 else {
341 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
342 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
343 }
344}
345
346/// addRegisterOffset - Add register offset.
347void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
348 int64_t Offset) {
349 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
350 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
351 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
352 if (Reg == TRI->getFrameRegister(*Asm->MF))
353 // If variable offset is based in frame register then use fbreg.
354 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
355 else if (DWReg < 32)
356 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
357 else {
358 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
359 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
360 }
361 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
362}
363
364/// addAddress - Add an address attribute to a die based on the location
365/// provided.
366void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
367 const MachineLocation &Location) {
368 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
369
370 if (Location.isReg())
371 addRegisterOp(Block, Location.getReg());
372 else
373 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
374
375 // Now attach the location information to the DIE.
376 addBlock(Die, Attribute, 0, Block);
377}
378
Devang Patel161b2f42011-04-12 23:21:44 +0000379/// addComplexAddress - Start with the address based on the location provided,
380/// and generate the DWARF information necessary to find the actual variable
381/// given the extra address information encoded in the DIVariable, starting from
382/// the starting location. Add the DWARF information to the die.
383///
384void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
385 unsigned Attribute,
386 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000387 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000388 unsigned N = DV->getNumAddrElements();
389 unsigned i = 0;
390 if (Location.isReg()) {
391 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
392 // If first address element is OpPlus then emit
393 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
394 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
395 i = 2;
396 } else
397 addRegisterOp(Block, Location.getReg());
398 }
Devang Patel116da2f2011-04-26 19:06:18 +0000399 else
400 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000401
Devang Patelc26f5442011-04-28 02:22:40 +0000402 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000403 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000404 if (Element == DIBuilder::OpPlus) {
405 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
406 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
407 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000408 if (!Location.isReg())
409 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000410 } else llvm_unreachable("unknown DIBuilder Opcode");
411 }
412
413 // Now attach the location information to the DIE.
414 addBlock(Die, Attribute, 0, Block);
415}
416
417/* Byref variables, in Blocks, are declared by the programmer as "SomeType
418 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
419 gives the variable VarName either the struct, or a pointer to the struct, as
420 its type. This is necessary for various behind-the-scenes things the
421 compiler needs to do with by-reference variables in Blocks.
422
423 However, as far as the original *programmer* is concerned, the variable
424 should still have type 'SomeType', as originally declared.
425
426 The function getBlockByrefType dives into the __Block_byref_x_VarName
427 struct to find the original type of the variable, which is then assigned to
428 the variable's Debug Information Entry as its real type. So far, so good.
429 However now the debugger will expect the variable VarName to have the type
430 SomeType. So we need the location attribute for the variable to be an
431 expression that explains to the debugger how to navigate through the
432 pointers and struct to find the actual variable of type SomeType.
433
434 The following function does just that. We start by getting
435 the "normal" location for the variable. This will be the location
436 of either the struct __Block_byref_x_VarName or the pointer to the
437 struct __Block_byref_x_VarName.
438
439 The struct will look something like:
440
441 struct __Block_byref_x_VarName {
442 ... <various fields>
443 struct __Block_byref_x_VarName *forwarding;
444 ... <various other fields>
445 SomeType VarName;
446 ... <maybe more fields>
447 };
448
449 If we are given the struct directly (as our starting point) we
450 need to tell the debugger to:
451
452 1). Add the offset of the forwarding field.
453
454 2). Follow that pointer to get the real __Block_byref_x_VarName
455 struct to use (the real one may have been copied onto the heap).
456
457 3). Add the offset for the field VarName, to find the actual variable.
458
459 If we started with a pointer to the struct, then we need to
460 dereference that pointer first, before the other steps.
461 Translating this into DWARF ops, we will need to append the following
462 to the current location description for the variable:
463
464 DW_OP_deref -- optional, if we start with a pointer
465 DW_OP_plus_uconst <forward_fld_offset>
466 DW_OP_deref
467 DW_OP_plus_uconst <varName_fld_offset>
468
469 That is what this function does. */
470
471/// addBlockByrefAddress - Start with the address based on the location
472/// provided, and generate the DWARF information necessary to find the
473/// actual Block variable (navigating the Block struct) based on the
474/// starting location. Add the DWARF information to the die. For
475/// more information, read large comment just above here.
476///
477void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
478 unsigned Attribute,
479 const MachineLocation &Location) {
480 DIType Ty = DV->getType();
481 DIType TmpTy = Ty;
482 unsigned Tag = Ty.getTag();
483 bool isPointer = false;
484
485 StringRef varName = DV->getName();
486
487 if (Tag == dwarf::DW_TAG_pointer_type) {
488 DIDerivedType DTy = DIDerivedType(Ty);
489 TmpTy = DTy.getTypeDerivedFrom();
490 isPointer = true;
491 }
492
493 DICompositeType blockStruct = DICompositeType(TmpTy);
494
495 // Find the __forwarding field and the variable field in the __Block_byref
496 // struct.
497 DIArray Fields = blockStruct.getTypeArray();
498 DIDescriptor varField = DIDescriptor();
499 DIDescriptor forwardingField = DIDescriptor();
500
501 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
502 DIDescriptor Element = Fields.getElement(i);
503 DIDerivedType DT = DIDerivedType(Element);
504 StringRef fieldName = DT.getName();
505 if (fieldName == "__forwarding")
506 forwardingField = Element;
507 else if (fieldName == varName)
508 varField = Element;
509 }
510
511 // Get the offsets for the forwarding field and the variable field.
512 unsigned forwardingFieldOffset =
513 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
514 unsigned varFieldOffset =
515 DIDerivedType(varField).getOffsetInBits() >> 3;
516
517 // Decode the original location, and use that as the start of the byref
518 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000519 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
520
Eric Christophercaba2632012-07-04 02:02:18 +0000521 if (Location.isReg())
522 addRegisterOp(Block, Location.getReg());
523 else
524 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000525
526 // If we started with a pointer to the __Block_byref... struct, then
527 // the first thing we need to do is dereference the pointer (DW_OP_deref).
528 if (isPointer)
529 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
530
531 // Next add the offset for the '__forwarding' field:
532 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
533 // adding the offset if it's 0.
534 if (forwardingFieldOffset > 0) {
535 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
536 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
537 }
538
539 // Now dereference the __forwarding field to get to the real __Block_byref
540 // struct: DW_OP_deref.
541 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
542
543 // Now that we've got the real __Block_byref... struct, add the offset
544 // for the variable's field to get to the location of the actual variable:
545 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
546 if (varFieldOffset > 0) {
547 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
548 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
549 }
550
551 // Now attach the location information to the DIE.
552 addBlock(Die, Attribute, 0, Block);
553}
554
Devang Patel4ec14b02011-07-20 21:57:04 +0000555/// isTypeSigned - Return true if the type is signed.
556static bool isTypeSigned(DIType Ty, int *SizeInBits) {
557 if (Ty.isDerivedType())
558 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
559 if (Ty.isBasicType())
560 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
561 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
562 *SizeInBits = Ty.getSizeInBits();
563 return true;
564 }
565 return false;
566}
567
Devang Patel161b2f42011-04-12 23:21:44 +0000568/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000569bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
570 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000571 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000572 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000573 int SizeInBits = -1;
574 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
575 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
576 switch (SizeInBits) {
577 case 8: Form = dwarf::DW_FORM_data1; break;
578 case 16: Form = dwarf::DW_FORM_data2; break;
579 case 32: Form = dwarf::DW_FORM_data4; break;
580 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000581 default: break;
582 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000583 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000584 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000585
Devang Patel161b2f42011-04-12 23:21:44 +0000586 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
587 return true;
588}
589
590/// addConstantFPValue - Add constant value entry in variable DIE.
591bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000592 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000593 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
594 APFloat FPImm = MO.getFPImm()->getValueAPF();
595
596 // Get the raw data form of the floating point.
597 const APInt FltVal = FPImm.bitcastToAPInt();
598 const char *FltPtr = (const char*)FltVal.getRawData();
599
600 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000601 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000602 int Incr = (LittleEndian ? 1 : -1);
603 int Start = (LittleEndian ? 0 : NumBytes - 1);
604 int Stop = (LittleEndian ? NumBytes : -1);
605
606 // Output the constant to DWARF one byte at a time.
607 for (; Start != Stop; Start += Incr)
608 addUInt(Block, 0, dwarf::DW_FORM_data1,
609 (unsigned char)0xFF & FltPtr[Start]);
610
611 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
612 return true;
613}
614
615/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000616bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000617 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000618 unsigned CIBitWidth = CI->getBitWidth();
619 if (CIBitWidth <= 64) {
620 unsigned form = 0;
621 switch (CIBitWidth) {
622 case 8: form = dwarf::DW_FORM_data1; break;
623 case 16: form = dwarf::DW_FORM_data2; break;
624 case 32: form = dwarf::DW_FORM_data4; break;
625 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000626 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000627 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
628 }
Devang Patel161b2f42011-04-12 23:21:44 +0000629 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000630 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000631 else
Devang Pateld6a81362011-05-28 00:39:18 +0000632 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000633 return true;
634 }
635
636 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
637
638 // Get the raw data form of the large APInt.
639 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000640 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000641
642 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000643 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000644
645 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000646 for (int i = 0; i < NumBytes; i++) {
647 uint8_t c;
648 if (LittleEndian)
649 c = Ptr64[i / 8] >> (8 * (i & 7));
650 else
651 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
652 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
653 }
Devang Patel161b2f42011-04-12 23:21:44 +0000654
655 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
656 return true;
657}
658
659/// addTemplateParams - Add template parameters in buffer.
660void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
661 // Add template parameters.
662 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
663 DIDescriptor Element = TParams.getElement(i);
664 if (Element.isTemplateTypeParameter())
665 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
666 DITemplateTypeParameter(Element)));
667 else if (Element.isTemplateValueParameter())
668 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
669 DITemplateValueParameter(Element)));
670 }
Devang Patel161b2f42011-04-12 23:21:44 +0000671}
Nick Lewycky746cb672011-10-26 22:55:33 +0000672
Eric Christopher6b6061f2013-01-16 01:22:23 +0000673/// getOrCreateContextDIE - Get context owner's DIE.
674DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
675 if (Context.isType())
676 return getOrCreateTypeDIE(DIType(Context));
677 else if (Context.isNameSpace())
678 return getOrCreateNameSpace(DINameSpace(Context));
679 else if (Context.isSubprogram())
680 return getOrCreateSubprogramDIE(DISubprogram(Context));
681 else
682 return getDIE(Context);
683}
684
Devang Patel161b2f42011-04-12 23:21:44 +0000685/// addToContextOwner - Add Die into the list of its context owner's children.
686void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000687 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000688 ContextDIE->addChild(Die);
689 else
690 addDie(Die);
691}
692
693/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
694/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000695DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
696 DIType Ty(TyNode);
697 if (!Ty.Verify())
698 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000699 DIE *TyDIE = getDIE(Ty);
700 if (TyDIE)
701 return TyDIE;
702
703 // Create new type.
704 TyDIE = new DIE(dwarf::DW_TAG_base_type);
705 insertDIE(Ty, TyDIE);
706 if (Ty.isBasicType())
707 constructTypeDIE(*TyDIE, DIBasicType(Ty));
708 else if (Ty.isCompositeType())
709 constructTypeDIE(*TyDIE, DICompositeType(Ty));
710 else {
711 assert(Ty.isDerivedType() && "Unknown kind of DIType");
712 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
713 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000714 // If this is a named finished type then include it in the list of types
715 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000716 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
717 bool IsImplementation = 0;
718 if (Ty.isCompositeType()) {
719 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000720 // A runtime language of 0 actually means C/C++ and that any
721 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000722 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000723 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000724 }
Eric Christophere0167892012-01-06 23:03:37 +0000725 unsigned Flags = IsImplementation ?
726 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
727 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000728 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000729
Devang Patel161b2f42011-04-12 23:21:44 +0000730 addToContextOwner(TyDIE, Ty.getContext());
731 return TyDIE;
732}
733
734/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000735void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000736 if (!Ty.Verify())
737 return;
738
739 // Check for pre-existence.
740 DIEEntry *Entry = getDIEEntry(Ty);
741 // If it exists then use the existing value.
742 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000743 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000744 return;
745 }
746
747 // Construct type.
748 DIE *Buffer = getOrCreateTypeDIE(Ty);
749
750 // Set up proxy.
751 Entry = createDIEEntry(Buffer);
752 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000753 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000754
755 // If this is a complete composite type then include it in the
756 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000757 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000758}
759
760/// addGlobalType - Add a new global type to the compile unit.
761///
Devang Patelc20bdf12011-06-01 00:23:24 +0000762void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000763 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000764 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
765 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000766 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000767 if (DIEEntry *Entry = getDIEEntry(Ty))
768 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000769}
770
Devang Patel31c5d052011-05-06 16:57:54 +0000771/// addPubTypes - Add type for pubtypes section.
772void CompileUnit::addPubTypes(DISubprogram SP) {
773 DICompositeType SPTy = SP.getType();
774 unsigned SPTag = SPTy.getTag();
775 if (SPTag != dwarf::DW_TAG_subroutine_type)
776 return;
777
778 DIArray Args = SPTy.getTypeArray();
779 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
780 DIType ATy(Args.getElement(i));
781 if (!ATy.Verify())
782 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000783 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000784 }
785}
786
Devang Patel161b2f42011-04-12 23:21:44 +0000787/// constructTypeDIE - Construct basic type die from DIBasicType.
788void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
789 // Get core information.
790 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000791 // Add name if not anonymous or intermediate type.
792 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000793 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000794
795 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
796 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
797 // Unspecified types has only name, nothing else.
798 return;
799 }
800
801 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000802 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000803 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000804
Devang Patel161b2f42011-04-12 23:21:44 +0000805 uint64_t Size = BTy.getSizeInBits() >> 3;
806 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
807}
808
809/// constructTypeDIE - Construct derived type die from DIDerivedType.
810void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
811 // Get core information.
812 StringRef Name = DTy.getName();
813 uint64_t Size = DTy.getSizeInBits() >> 3;
814 unsigned Tag = DTy.getTag();
815
816 // FIXME - Workaround for templates.
817 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
818
819 Buffer.setTag(Tag);
820
821 // Map to main type, void will not have a type.
822 DIType FromTy = DTy.getTypeDerivedFrom();
823 addType(&Buffer, FromTy);
824
825 // Add name if not anonymous or intermediate type.
826 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000827 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000828
829 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000830 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000831 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
832
David Blaikie62fdfb52013-01-07 05:51:15 +0000833 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
834 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
835 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000836 // Add source line info if available and TyDesc is not a forward declaration.
837 if (!DTy.isForwardDecl())
838 addSourceLine(&Buffer, DTy);
839}
840
841/// constructTypeDIE - Construct type DIE from DICompositeType.
842void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
843 // Get core information.
844 StringRef Name = CTy.getName();
845
846 uint64_t Size = CTy.getSizeInBits() >> 3;
847 unsigned Tag = CTy.getTag();
848 Buffer.setTag(Tag);
849
850 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000851 case dwarf::DW_TAG_array_type:
852 constructArrayTypeDIE(Buffer, &CTy);
853 break;
854 case dwarf::DW_TAG_enumeration_type: {
855 DIArray Elements = CTy.getTypeArray();
856
857 // Add enumerators to enumeration type.
858 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
859 DIE *ElemDie = NULL;
860 DIDescriptor Enum(Elements.getElement(i));
861 if (Enum.isEnumerator()) {
862 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
863 Buffer.addChild(ElemDie);
864 }
865 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000866 DIType DTy = CTy.getTypeDerivedFrom();
867 if (DTy.Verify()) {
868 addType(&Buffer, DTy);
869 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
870 }
Devang Patel161b2f42011-04-12 23:21:44 +0000871 }
872 break;
873 case dwarf::DW_TAG_subroutine_type: {
874 // Add return type.
875 DIArray Elements = CTy.getTypeArray();
876 DIDescriptor RTy = Elements.getElement(0);
877 addType(&Buffer, DIType(RTy));
878
879 bool isPrototyped = true;
880 // Add arguments.
881 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
882 DIDescriptor Ty = Elements.getElement(i);
883 if (Ty.isUnspecifiedParameter()) {
884 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
885 Buffer.addChild(Arg);
886 isPrototyped = false;
887 } else {
888 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
889 addType(Arg, DIType(Ty));
890 Buffer.addChild(Arg);
891 }
892 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000893 // Add prototype flag if we're dealing with a C language and the
894 // function has been prototyped.
895 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000896 (Language == dwarf::DW_LANG_C89 ||
897 Language == dwarf::DW_LANG_C99 ||
898 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000899 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000900 }
901 break;
902 case dwarf::DW_TAG_structure_type:
903 case dwarf::DW_TAG_union_type:
904 case dwarf::DW_TAG_class_type: {
905 // Add elements to structure type.
906 DIArray Elements = CTy.getTypeArray();
907
908 // A forward struct declared type may not have elements available.
909 unsigned N = Elements.getNumElements();
910 if (N == 0)
911 break;
912
913 // Add elements to structure type.
914 for (unsigned i = 0; i < N; ++i) {
915 DIDescriptor Element = Elements.getElement(i);
916 DIE *ElemDie = NULL;
917 if (Element.isSubprogram()) {
918 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000919 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000920 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000921 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000922 dwarf::DW_ACCESS_protected);
923 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000924 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000925 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000926 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000927 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000928 dwarf::DW_ACCESS_public);
929 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000930 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000931 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000932 DIDerivedType DDTy(Element);
933 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
934 ElemDie = new DIE(dwarf::DW_TAG_friend);
935 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000936 } else if (DDTy.isStaticMember())
937 ElemDie = createStaticMemberDIE(DDTy);
938 else
939 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000940 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000941 DIObjCProperty Property(Element);
942 ElemDie = new DIE(Property.getTag());
943 StringRef PropertyName = Property.getObjCPropertyName();
944 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000945 addType(ElemDie, Property.getType());
946 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000947 StringRef GetterName = Property.getObjCPropertyGetterName();
948 if (!GetterName.empty())
949 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
950 StringRef SetterName = Property.getObjCPropertySetterName();
951 if (!SetterName.empty())
952 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
953 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000954 if (Property.isReadOnlyObjCProperty())
955 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
956 if (Property.isReadWriteObjCProperty())
957 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
958 if (Property.isAssignObjCProperty())
959 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
960 if (Property.isRetainObjCProperty())
961 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
962 if (Property.isCopyObjCProperty())
963 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
964 if (Property.isNonAtomicObjCProperty())
965 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
966 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000967 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000968 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000969
Devang Patel30d409c2012-02-07 23:33:58 +0000970 DIEEntry *Entry = getDIEEntry(Element);
971 if (!Entry) {
972 Entry = createDIEEntry(ElemDie);
973 insertDIEEntry(Element, Entry);
974 }
Devang Patel9e11eb12012-02-04 01:30:32 +0000975 } else
Devang Patel161b2f42011-04-12 23:21:44 +0000976 continue;
977 Buffer.addChild(ElemDie);
978 }
979
980 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000981 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +0000982
Devang Patel161b2f42011-04-12 23:21:44 +0000983 DICompositeType ContainingType = CTy.getContainingType();
984 if (DIDescriptor(ContainingType).isCompositeType())
985 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
986 getOrCreateTypeDIE(DIType(ContainingType)));
987 else {
988 DIDescriptor Context = CTy.getContext();
989 addToContextOwner(&Buffer, Context);
990 }
991
Devang Patel201e6cd2011-05-12 21:29:42 +0000992 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000993 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +0000994
Eric Christopher1a8e8862011-12-16 23:42:42 +0000995 // Add template parameters to a class, structure or union types.
996 // FIXME: The support isn't in the metadata for this yet.
997 if (Tag == dwarf::DW_TAG_class_type ||
998 Tag == dwarf::DW_TAG_structure_type ||
999 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001000 addTemplateParams(Buffer, CTy.getTemplateParams());
1001
1002 break;
1003 }
1004 default:
1005 break;
1006 }
1007
1008 // Add name if not anonymous or intermediate type.
1009 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001010 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001011
Eric Christopher4a5d8392012-05-22 18:45:18 +00001012 if (Tag == dwarf::DW_TAG_enumeration_type ||
1013 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 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001017 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001018 if (Size)
1019 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001020 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001021 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001022 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1023
1024 // If we're a forward decl, say so.
1025 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001026 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001027
1028 // Add source line info if available.
1029 if (!CTy.isForwardDecl())
1030 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001031
1032 // No harm in adding the runtime language to the declaration.
1033 unsigned RLang = CTy.getRunTimeLang();
1034 if (RLang)
1035 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1036 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001037 }
1038}
1039
Eric Christopher8b4310b2012-11-21 00:34:38 +00001040/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001041/// for the given DITemplateTypeParameter.
1042DIE *
1043CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1044 DIE *ParamDIE = getDIE(TP);
1045 if (ParamDIE)
1046 return ParamDIE;
1047
1048 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1049 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001050 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001051 return ParamDIE;
1052}
1053
Eric Christopher8b4310b2012-11-21 00:34:38 +00001054/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001055/// for the given DITemplateValueParameter.
1056DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001057CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001058 DIE *ParamDIE = getDIE(TPV);
1059 if (ParamDIE)
1060 return ParamDIE;
1061
1062 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1063 addType(ParamDIE, TPV.getType());
1064 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001065 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001066 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001067 TPV.getValue());
1068 return ParamDIE;
1069}
1070
Devang Patel31c5d052011-05-06 16:57:54 +00001071/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1072DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1073 DIE *NDie = getDIE(NS);
1074 if (NDie)
1075 return NDie;
1076 NDie = new DIE(dwarf::DW_TAG_namespace);
1077 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001078 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001079 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001080 addAccelNamespace(NS.getName(), NDie);
1081 } else
1082 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001083 addSourceLine(NDie, NS);
1084 addToContextOwner(NDie, NS.getContext());
1085 return NDie;
1086}
1087
Devang Pateldbc64af2011-08-15 17:24:54 +00001088/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1089/// printer to not emit usual symbol prefix before the symbol name is used then
1090/// return linkage name after skipping this special LLVM prefix.
1091static StringRef getRealLinkageName(StringRef LinkageName) {
1092 char One = '\1';
1093 if (LinkageName.startswith(StringRef(&One, 1)))
1094 return LinkageName.substr(1);
1095 return LinkageName;
1096}
1097
1098/// getOrCreateSubprogramDIE - Create new DIE using SP.
1099DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1100 DIE *SPDie = getDIE(SP);
1101 if (SPDie)
1102 return SPDie;
1103
Peter Collingbourne27302f02012-05-27 18:36:44 +00001104 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1105
1106 // DW_TAG_inlined_subroutine may refer to this DIE.
1107 insertDIE(SP, SPDie);
1108
Rafael Espindola01b55b42011-11-10 22:34:29 +00001109 DISubprogram SPDecl = SP.getFunctionDeclaration();
1110 DIE *DeclDie = NULL;
1111 if (SPDecl.isSubprogram()) {
1112 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1113 }
1114
Devang Pateldbc64af2011-08-15 17:24:54 +00001115 // Add to context owner.
1116 addToContextOwner(SPDie, SP.getContext());
1117
1118 // Add function template parameters.
1119 addTemplateParams(*SPDie, SP.getTemplateParams());
1120
Eric Christophere9722e12012-04-16 23:54:23 +00001121 // Unfortunately this code needs to stay here instead of below the
1122 // AT_specification code in order to work around a bug in older
1123 // gdbs that requires the linkage name to resolve multiple template
1124 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001125 // TODO: Remove this set of code when we get rid of the old gdb
1126 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001127 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001128 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001129 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1130 getRealLinkageName(LinkageName));
1131
Devang Pateldbc64af2011-08-15 17:24:54 +00001132 // If this DIE is going to refer declaration info using AT_specification
1133 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001134 if (DeclDie) {
1135 // Refer function declaration directly.
1136 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1137 DeclDie);
1138
Devang Pateldbc64af2011-08-15 17:24:54 +00001139 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001140 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001141
Eric Christophercbbd5b12012-08-23 22:52:55 +00001142 // Add the linkage name if we have one.
1143 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1144 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1145 getRealLinkageName(LinkageName));
1146
Devang Pateldbc64af2011-08-15 17:24:54 +00001147 // Constructors and operators for anonymous aggregates do not have names.
1148 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001149 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001150
1151 addSourceLine(SPDie, SP);
1152
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001153 // Add the prototype if we have a prototype and we have a C like
1154 // language.
1155 if (SP.isPrototyped() &&
1156 (Language == dwarf::DW_LANG_C89 ||
1157 Language == dwarf::DW_LANG_C99 ||
1158 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001159 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001160
1161 // Add Return Type.
1162 DICompositeType SPTy = SP.getType();
1163 DIArray Args = SPTy.getTypeArray();
1164 unsigned SPTag = SPTy.getTag();
1165
1166 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1167 addType(SPDie, SPTy);
1168 else
1169 addType(SPDie, DIType(Args.getElement(0)));
1170
1171 unsigned VK = SP.getVirtuality();
1172 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001173 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001174 DIEBlock *Block = getDIEBlock();
1175 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1176 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1177 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1178 ContainingTypeMap.insert(std::make_pair(SPDie,
1179 SP.getContainingType()));
1180 }
1181
1182 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001183 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001184
Devang Pateldbc64af2011-08-15 17:24:54 +00001185 // Add arguments. Do not add arguments for subprogram definition. They will
1186 // be handled while processing variables.
1187 DICompositeType SPTy = SP.getType();
1188 DIArray Args = SPTy.getTypeArray();
1189 unsigned SPTag = SPTy.getTag();
1190
1191 if (SPTag == dwarf::DW_TAG_subroutine_type)
1192 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1193 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001194 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001195 addType(Arg, ATy);
1196 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001197 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001198 SPDie->addChild(Arg);
1199 }
1200 }
1201
1202 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001203 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001204
1205 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001206 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001207
1208 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001209 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001210
1211 if (unsigned isa = Asm->getISAEncoding()) {
1212 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1213 }
1214
1215 return SPDie;
1216}
1217
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001218// Return const expression if value is a GEP to access merged global
1219// constant. e.g.
1220// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1221static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1222 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1223 if (!CE || CE->getNumOperands() != 3 ||
1224 CE->getOpcode() != Instruction::GetElementPtr)
1225 return NULL;
1226
1227 // First operand points to a global struct.
1228 Value *Ptr = CE->getOperand(0);
1229 if (!isa<GlobalValue>(Ptr) ||
1230 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1231 return NULL;
1232
1233 // Second operand is zero.
1234 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1235 if (!CI || !CI->isZero())
1236 return NULL;
1237
1238 // Third operand is offset.
1239 if (!isa<ConstantInt>(CE->getOperand(2)))
1240 return NULL;
1241
1242 return CE;
1243}
1244
1245/// createGlobalVariableDIE - create global variable DIE.
1246void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001247 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001248 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001249 return;
1250
Devang Patel49e2f032011-08-18 22:21:50 +00001251 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001252 if (!GV.Verify())
1253 return;
1254
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001255 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001256 DIType GTy = GV.getType();
1257
1258 // If this is a static data member definition, some attributes belong
1259 // to the declaration DIE.
1260 DIE *VariableDIE = NULL;
1261 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1262 if (SDMDecl.Verify()) {
1263 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1264 // We need the declaration DIE that is in the static member's class.
1265 // But that class might not exist in the DWARF yet.
1266 // Creating the class will create the static member decl DIE.
1267 getOrCreateContextDIE(SDMDecl.getContext());
1268 VariableDIE = getDIE(SDMDecl);
1269 assert(VariableDIE && "Static member decl has no context?");
1270 }
1271
1272 // If this is not a static data member definition, create the variable
1273 // DIE and add the initial set of attributes to it.
1274 if (!VariableDIE) {
1275 VariableDIE = new DIE(GV.getTag());
1276 // Add to map.
1277 insertDIE(N, VariableDIE);
1278
1279 // Add name and type.
1280 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1281 addType(VariableDIE, GTy);
1282
1283 // Add scoping info.
1284 if (!GV.isLocalToUnit())
1285 addFlag(VariableDIE, dwarf::DW_AT_external);
1286
1287 // Add line number info.
1288 addSourceLine(VariableDIE, GV);
1289 // Add to context owner.
1290 addToContextOwner(VariableDIE, GVContext);
1291 }
1292
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001293 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001294 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001295 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001296 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001297 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001298 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001299 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1300 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1301 addLabel(Block, 0, dwarf::DW_FORM_udata,
1302 Asm->Mang->getSymbol(GV.getGlobal()));
1303 // Do not create specification DIE if context is either compile unit
1304 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001305 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001306 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1307 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001308 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001309 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1310 dwarf::DW_FORM_ref4, VariableDIE);
1311 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001312 // A static member's declaration is already flagged as such.
1313 if (!SDMDecl.Verify())
1314 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001315 addDie(VariableSpecDIE);
1316 } else {
1317 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001318 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001319 // Add linkage name.
1320 StringRef LinkageName = GV.getLinkageName();
1321 if (!LinkageName.empty() && isGlobalVariable)
1322 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1323 getRealLinkageName(LinkageName));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001324 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001325 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1326 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1327 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001328 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001329 // GV is a merged global.
1330 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1331 Value *Ptr = CE->getOperand(0);
1332 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1333 addLabel(Block, 0, dwarf::DW_FORM_udata,
1334 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1335 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1336 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001337 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001338 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001339 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1340 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1341 }
1342
Eric Christopherd117fbb2011-11-11 01:55:22 +00001343 if (addToAccelTable) {
1344 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1345 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001346
Eric Christopherd117fbb2011-11-11 01:55:22 +00001347 // If the linkage name is different than the name, go ahead and output
1348 // that as well into the name table.
1349 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1350 addAccelName(GV.getLinkageName(), AddrDIE);
1351 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001352
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001353 return;
1354}
1355
Devang Patel161b2f42011-04-12 23:21:44 +00001356/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001357void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1358 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001359 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1360 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001361
Bill Wendling222c2fd2012-12-06 07:38:10 +00001362 // The LowerBound value defines the lower bounds which is typically zero for
1363 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1364 // Count == -1 then the array is unbounded and we do not emit
1365 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1366 // Count == 0, then the array has zero elements in which case we do not emit
1367 // an upper bound.
1368 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001369 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001370 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001371
Bill Wendling6afe4782012-12-06 07:55:19 +00001372 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001373 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1374
1375 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001376 // FIXME: An unbounded array should reference the expression that defines
1377 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001378 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001379
Devang Patel161b2f42011-04-12 23:21:44 +00001380 Buffer.addChild(DW_Subrange);
1381}
1382
1383/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1384void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1385 DICompositeType *CTy) {
1386 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001387 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001388 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001389
1390 // Emit derived type.
1391 addType(&Buffer, CTy->getTypeDerivedFrom());
1392 DIArray Elements = CTy->getTypeArray();
1393
1394 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001395 // FIXME: This type should be passed down from the front end
1396 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001397 DIE *IdxTy = getIndexTyDie();
1398 if (!IdxTy) {
1399 // Construct an anonymous type for index type.
1400 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001401 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001402 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1403 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1404 dwarf::DW_ATE_signed);
1405 addDie(IdxTy);
1406 setIndexTyDie(IdxTy);
1407 }
1408
1409 // Add subranges to array type.
1410 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1411 DIDescriptor Element = Elements.getElement(i);
1412 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1413 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1414 }
1415}
1416
1417/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1418DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1419 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1420 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001421 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001422 int64_t Value = ETy.getEnumValue();
1423 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1424 return Enumerator;
1425}
1426
Devang Pateldbc64af2011-08-15 17:24:54 +00001427/// constructContainingTypeDIEs - Construct DIEs for types that contain
1428/// vtables.
1429void CompileUnit::constructContainingTypeDIEs() {
1430 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1431 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1432 DIE *SPDie = CI->first;
1433 const MDNode *N = CI->second;
1434 if (!N) continue;
1435 DIE *NDie = getDIE(N);
1436 if (!NDie) continue;
1437 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1438 }
1439}
1440
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001441/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1442DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1443 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001444
1445 // Translate tag to proper Dwarf tag.
1446 unsigned Tag = DV->getTag();
1447
1448 // Define variable debug information entry.
1449 DIE *VariableDie = new DIE(Tag);
1450 DbgVariable *AbsVar = DV->getAbstractVariable();
1451 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1452 if (AbsDIE)
1453 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1454 dwarf::DW_FORM_ref4, AbsDIE);
1455 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001456 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001457 addSourceLine(VariableDie, DV->getVariable());
1458 addType(VariableDie, DV->getType());
1459 }
1460
1461 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001462 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001463
1464 if (isScopeAbstract) {
1465 DV->setDIE(VariableDie);
1466 return VariableDie;
1467 }
1468
1469 // Add variable address.
1470
1471 unsigned Offset = DV->getDotDebugLocOffset();
1472 if (Offset != ~0U) {
1473 addLabel(VariableDie, dwarf::DW_AT_location,
1474 dwarf::DW_FORM_data4,
1475 Asm->GetTempSymbol("debug_loc", Offset));
1476 DV->setDIE(VariableDie);
1477 return VariableDie;
1478 }
1479
Eric Christopher8cf5e742011-10-03 15:49:20 +00001480 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001481 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1482 bool updated = false;
1483 if (DVInsn->getNumOperands() == 3) {
1484 if (DVInsn->getOperand(0).isReg()) {
1485 const MachineOperand RegOp = DVInsn->getOperand(0);
1486 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1487 if (DVInsn->getOperand(1).isImm() &&
1488 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1489 unsigned FrameReg = 0;
1490 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001491 int Offset =
1492 TFI->getFrameIndexReference(*Asm->MF,
1493 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001494 FrameReg);
1495 MachineLocation Location(FrameReg, Offset);
1496 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001497
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001498 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001499 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001500 MachineLocation(RegOp.getReg()));
1501 updated = true;
1502 }
1503 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001504 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001505 addConstantValue(VariableDie, DVInsn->getOperand(0),
1506 DV->getType());
1507 else if (DVInsn->getOperand(0).isFPImm())
1508 updated =
1509 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1510 else if (DVInsn->getOperand(0).isCImm())
1511 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001512 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001513 DVInsn->getOperand(0).getCImm(),
1514 DV->getType().isUnsignedDIType());
1515 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001516 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001517 Asm->getDebugValueLocation(DVInsn));
1518 updated = true;
1519 }
1520 if (!updated) {
1521 // If variableDie is not updated then DBG_VALUE instruction does not
1522 // have valid variable info.
1523 delete VariableDie;
1524 return NULL;
1525 }
1526 DV->setDIE(VariableDie);
1527 return VariableDie;
1528 } else {
1529 // .. else use frame index.
1530 int FI = DV->getFrameIndex();
1531 if (FI != ~0) {
1532 unsigned FrameReg = 0;
1533 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001534 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001535 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1536 MachineLocation Location(FrameReg, Offset);
1537 addVariableAddress(DV, VariableDie, Location);
1538 }
1539 }
1540
1541 DV->setDIE(VariableDie);
1542 return VariableDie;
1543}
1544
Devang Patel161b2f42011-04-12 23:21:44 +00001545/// createMemberDIE - Create new member DIE.
1546DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1547 DIE *MemberDie = new DIE(DT.getTag());
1548 StringRef Name = DT.getName();
1549 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001550 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001551
1552 addType(MemberDie, DT.getTypeDerivedFrom());
1553
1554 addSourceLine(MemberDie, DT);
1555
1556 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1557 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1558
1559 uint64_t Size = DT.getSizeInBits();
1560 uint64_t FieldSize = DT.getOriginalTypeSize();
1561
1562 if (Size != FieldSize) {
1563 // Handle bitfield.
1564 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1565 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1566
1567 uint64_t Offset = DT.getOffsetInBits();
1568 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1569 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1570 uint64_t FieldOffset = (HiMark - FieldSize);
1571 Offset -= FieldOffset;
1572
1573 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001574 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001575 Offset = FieldSize - (Offset + Size);
1576 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1577
1578 // Here WD_AT_data_member_location points to the anonymous
1579 // field that includes this bit field.
1580 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1581
1582 } else
1583 // This is not a bitfield.
1584 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1585
1586 if (DT.getTag() == dwarf::DW_TAG_inheritance
1587 && DT.isVirtual()) {
1588
1589 // For C++, virtual base classes are not at fixed offset. Use following
1590 // expression to extract appropriate offset from vtable.
1591 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1592
1593 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1594 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1595 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1596 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1597 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1598 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1599 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1600 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1601
1602 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1603 VBaseLocationDie);
1604 } else
1605 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1606
1607 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001608 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001609 dwarf::DW_ACCESS_protected);
1610 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001611 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001612 dwarf::DW_ACCESS_private);
1613 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001614 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001615 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001616 dwarf::DW_ACCESS_public);
1617 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001618 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001619 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001620
1621 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001622 if (MDNode *PNode = DT.getObjCProperty())
1623 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001624 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001625 PropertyDie);
1626
David Blaikie01bc2b32012-12-13 22:43:07 +00001627 if (DT.isArtificial())
1628 addFlag(MemberDie, dwarf::DW_AT_artificial);
1629
Devang Patel9e11eb12012-02-04 01:30:32 +00001630 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001631 StringRef PropertyName = DT.getObjCPropertyName();
1632 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001633 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001634 StringRef GetterName = DT.getObjCPropertyGetterName();
1635 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001636 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001637 StringRef SetterName = DT.getObjCPropertySetterName();
1638 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001639 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001640 unsigned PropertyAttributes = 0;
1641 if (DT.isReadOnlyObjCProperty())
1642 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1643 if (DT.isReadWriteObjCProperty())
1644 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1645 if (DT.isAssignObjCProperty())
1646 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1647 if (DT.isRetainObjCProperty())
1648 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1649 if (DT.isCopyObjCProperty())
1650 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1651 if (DT.isNonAtomicObjCProperty())
1652 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1653 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001654 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001655 PropertyAttributes);
1656 }
Devang Patel161b2f42011-04-12 23:21:44 +00001657 return MemberDie;
1658}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001659
1660/// createStaticMemberDIE - Create new DIE for C++ static member.
1661DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1662 if (!DT.Verify())
1663 return NULL;
1664
1665 DIE *StaticMemberDIE = new DIE(DT.getTag());
1666 DIType Ty = DT.getTypeDerivedFrom();
1667
1668 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1669 addType(StaticMemberDIE, Ty);
1670 addSourceLine(StaticMemberDIE, DT);
1671 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1672 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1673
1674 // FIXME: We could omit private if the parent is a class_type, and
1675 // public if the parent is something else.
1676 if (DT.isProtected())
1677 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1678 dwarf::DW_ACCESS_protected);
1679 else if (DT.isPrivate())
1680 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1681 dwarf::DW_ACCESS_private);
1682 else
1683 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1684 dwarf::DW_ACCESS_public);
1685
1686 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1687 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
1688
1689 insertDIE(DT, StaticMemberDIE);
1690 return StaticMemberDIE;
1691}