~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/Analysis/DebugInfo.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
2
 
//
3
 
//                     The LLVM Compiler Infrastructure
4
 
//
5
 
// This file is distributed under the University of Illinois Open Source
6
 
// License. See LICENSE.TXT for details.
7
 
//
8
 
//===----------------------------------------------------------------------===//
9
 
//
10
 
// This file defines a bunch of datatypes that are useful for creating and
11
 
// walking debug info in LLVM IR form. They essentially provide wrappers around
12
 
// the information in the global variables that's needed when constructing the
13
 
// DWARF information.
14
 
//
15
 
//===----------------------------------------------------------------------===//
16
 
 
17
 
#ifndef LLVM_ANALYSIS_DEBUGINFO_H
18
 
#define LLVM_ANALYSIS_DEBUGINFO_H
19
 
 
20
 
#include "llvm/ADT/SmallVector.h"
21
 
#include "llvm/ADT/SmallPtrSet.h"
22
 
#include "llvm/ADT/StringRef.h"
23
 
#include "llvm/Support/Dwarf.h"
24
 
 
25
 
namespace llvm {
26
 
  class BasicBlock;
27
 
  class Constant;
28
 
  class Function;
29
 
  class GlobalVariable;
30
 
  class Module;
31
 
  class Type;
32
 
  class Value;
33
 
  class DbgDeclareInst;
34
 
  class Instruction;
35
 
  class MDNode;
36
 
  class LLVMContext;
37
 
  class raw_ostream;
38
 
 
39
 
  class DIFile;
40
 
  class DISubprogram;
41
 
  class DILexicalBlock;
42
 
  class DIVariable;
43
 
  class DIType;
44
 
 
45
 
  /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
46
 
  /// This should not be stored in a container, because underly MDNode may
47
 
  /// change in certain situations.
48
 
  class DIDescriptor {
49
 
  protected:
50
 
    const MDNode *DbgNode;
51
 
 
52
 
    StringRef getStringField(unsigned Elt) const;
53
 
    unsigned getUnsignedField(unsigned Elt) const {
54
 
      return (unsigned)getUInt64Field(Elt);
55
 
    }
56
 
    uint64_t getUInt64Field(unsigned Elt) const;
57
 
    DIDescriptor getDescriptorField(unsigned Elt) const;
58
 
 
59
 
    template <typename DescTy>
60
 
    DescTy getFieldAs(unsigned Elt) const {
61
 
      return DescTy(getDescriptorField(Elt));
62
 
    }
63
 
 
64
 
    GlobalVariable *getGlobalVariableField(unsigned Elt) const;
65
 
    Constant *getConstantField(unsigned Elt) const;
66
 
    Function *getFunctionField(unsigned Elt) const;
67
 
 
68
 
  public:
69
 
    explicit DIDescriptor() : DbgNode(0) {}
70
 
    explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
71
 
    explicit DIDescriptor(const DIFile F);
72
 
    explicit DIDescriptor(const DISubprogram F);
73
 
    explicit DIDescriptor(const DILexicalBlock F);
74
 
    explicit DIDescriptor(const DIVariable F);
75
 
    explicit DIDescriptor(const DIType F);
76
 
 
77
 
    bool Verify() const { return DbgNode != 0; }
78
 
 
79
 
    operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
80
 
    MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
81
 
 
82
 
    unsigned getVersion() const {
83
 
      return getUnsignedField(0) & LLVMDebugVersionMask;
84
 
    }
85
 
 
86
 
    unsigned getTag() const {
87
 
      return getUnsignedField(0) & ~LLVMDebugVersionMask;
88
 
    }
89
 
 
90
 
    /// print - print descriptor.
91
 
    void print(raw_ostream &OS) const;
92
 
 
93
 
    /// dump - print descriptor to dbgs() with a newline.
94
 
    void dump() const;
95
 
 
96
 
    bool isDerivedType() const;
97
 
    bool isCompositeType() const;
98
 
    bool isBasicType() const;
99
 
    bool isVariable() const;
100
 
    bool isSubprogram() const;
101
 
    bool isGlobalVariable() const;
102
 
    bool isScope() const;
103
 
    bool isFile() const;
104
 
    bool isCompileUnit() const;
105
 
    bool isNameSpace() const;
106
 
    bool isLexicalBlock() const;
107
 
    bool isSubrange() const;
108
 
    bool isEnumerator() const;
109
 
    bool isType() const;
110
 
    bool isGlobal() const;
111
 
  };
112
 
 
113
 
  /// DISubrange - This is used to represent ranges, for array bounds.
114
 
  class DISubrange : public DIDescriptor {
115
 
  public:
116
 
    explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
117
 
 
118
 
    int64_t getLo() const { return (int64_t)getUInt64Field(1); }
119
 
    int64_t getHi() const { return (int64_t)getUInt64Field(2); }
120
 
  };
121
 
 
122
 
  /// DIArray - This descriptor holds an array of descriptors.
123
 
  class DIArray : public DIDescriptor {
124
 
  public:
125
 
    explicit DIArray(const MDNode *N = 0)
126
 
      : DIDescriptor(N) {}
127
 
 
128
 
    unsigned getNumElements() const;
129
 
    DIDescriptor getElement(unsigned Idx) const {
130
 
      return getDescriptorField(Idx);
131
 
    }
132
 
  };
133
 
 
134
 
  /// DIScope - A base class for various scopes.
135
 
  class DIScope : public DIDescriptor {
136
 
  public:
137
 
    explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
138
 
    virtual ~DIScope() {}
139
 
 
140
 
    StringRef getFilename() const;
141
 
    StringRef getDirectory() const;
142
 
  };
143
 
 
144
 
  /// DICompileUnit - A wrapper for a compile unit.
145
 
  class DICompileUnit : public DIScope {
146
 
  public:
147
 
    explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
148
 
 
149
 
    unsigned getLanguage() const   { return getUnsignedField(2); }
150
 
    StringRef getFilename() const  { return getStringField(3);   }
151
 
    StringRef getDirectory() const { return getStringField(4);   }
152
 
    StringRef getProducer() const  { return getStringField(5);   }
153
 
 
154
 
    /// isMain - Each input file is encoded as a separate compile unit in LLVM
155
 
    /// debugging information output. However, many target specific tool chains
156
 
    /// prefer to encode only one compile unit in an object file. In this
157
 
    /// situation, the LLVM code generator will include  debugging information
158
 
    /// entities in the compile unit that is marked as main compile unit. The
159
 
    /// code generator accepts maximum one main compile unit per module. If a
160
 
    /// module does not contain any main compile unit then the code generator
161
 
    /// will emit multiple compile units in the output object file.
162
 
 
163
 
    bool isMain() const                { return getUnsignedField(6); }
164
 
    bool isOptimized() const           { return getUnsignedField(7); }
165
 
    StringRef getFlags() const       { return getStringField(8);   }
166
 
    unsigned getRunTimeVersion() const { return getUnsignedField(9); }
167
 
 
168
 
    /// Verify - Verify that a compile unit is well formed.
169
 
    bool Verify() const;
170
 
 
171
 
    /// print - print compile unit.
172
 
    void print(raw_ostream &OS) const;
173
 
 
174
 
    /// dump - print compile unit to dbgs() with a newline.
175
 
    void dump() const;
176
 
  };
177
 
 
178
 
  /// DIFile - This is a wrapper for a file.
179
 
  class DIFile : public DIScope {
180
 
  public:
181
 
    explicit DIFile(const MDNode *N = 0) : DIScope(N) {
182
 
      if (DbgNode && !isFile())
183
 
        DbgNode = 0;
184
 
    }
185
 
    StringRef getFilename() const  { return getStringField(1);   }
186
 
    StringRef getDirectory() const { return getStringField(2);   }
187
 
    DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
188
 
  };
189
 
 
190
 
  /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
191
 
  /// FIXME: it seems strange that this doesn't have either a reference to the
192
 
  /// type/precision or a file/line pair for location info.
193
 
  class DIEnumerator : public DIDescriptor {
194
 
  public:
195
 
    explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
196
 
 
197
 
    StringRef getName() const        { return getStringField(1); }
198
 
    uint64_t getEnumValue() const      { return getUInt64Field(2); }
199
 
  };
200
 
 
201
 
  /// DIType - This is a wrapper for a type.
202
 
  /// FIXME: Types should be factored much better so that CV qualifiers and
203
 
  /// others do not require a huge and empty descriptor full of zeros.
204
 
  class DIType : public DIScope {
205
 
  public:
206
 
    enum {
207
 
      FlagPrivate          = 1 << 0,
208
 
      FlagProtected        = 1 << 1,
209
 
      FlagFwdDecl          = 1 << 2,
210
 
      FlagAppleBlock       = 1 << 3,
211
 
      FlagBlockByrefStruct = 1 << 4,
212
 
      FlagVirtual          = 1 << 5,
213
 
      FlagArtificial       = 1 << 6  // To identify artificial arguments in
214
 
                                     // a subroutine type. e.g. "this" in c++.
215
 
    };
216
 
 
217
 
  protected:
218
 
    // This ctor is used when the Tag has already been validated by a derived
219
 
    // ctor.
220
 
    DIType(const MDNode *N, bool, bool) : DIScope(N) {}
221
 
 
222
 
  public:
223
 
 
224
 
    /// Verify - Verify that a type descriptor is well formed.
225
 
    bool Verify() const;
226
 
  public:
227
 
    explicit DIType(const MDNode *N);
228
 
    explicit DIType() {}
229
 
    virtual ~DIType() {}
230
 
 
231
 
    DIScope getContext() const          { return getFieldAs<DIScope>(1); }
232
 
    StringRef getName() const           { return getStringField(2);     }
233
 
    DICompileUnit getCompileUnit() const{ 
234
 
      if (getVersion() == llvm::LLVMDebugVersion7)
235
 
        return getFieldAs<DICompileUnit>(3);
236
 
 
237
 
      DIFile F = getFieldAs<DIFile>(3);
238
 
      return F.getCompileUnit();
239
 
    }
240
 
    unsigned getLineNumber() const      { return getUnsignedField(4); }
241
 
    uint64_t getSizeInBits() const      { return getUInt64Field(5); }
242
 
    uint64_t getAlignInBits() const     { return getUInt64Field(6); }
243
 
    // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
244
 
    // carry this is just plain insane.
245
 
    uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
246
 
    unsigned getFlags() const           { return getUnsignedField(8); }
247
 
    bool isPrivate() const {
248
 
      return (getFlags() & FlagPrivate) != 0;
249
 
    }
250
 
    bool isProtected() const {
251
 
      return (getFlags() & FlagProtected) != 0;
252
 
    }
253
 
    bool isForwardDecl() const {
254
 
      return (getFlags() & FlagFwdDecl) != 0;
255
 
    }
256
 
    // isAppleBlock - Return true if this is the Apple Blocks extension.
257
 
    bool isAppleBlockExtension() const {
258
 
      return (getFlags() & FlagAppleBlock) != 0;
259
 
    }
260
 
    bool isBlockByrefStruct() const {
261
 
      return (getFlags() & FlagBlockByrefStruct) != 0;
262
 
    }
263
 
    bool isVirtual() const {
264
 
      return (getFlags() & FlagVirtual) != 0;
265
 
    }
266
 
    bool isArtificial() const {
267
 
      return (getFlags() & FlagArtificial) != 0;
268
 
    }
269
 
    bool isValid() const {
270
 
      return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
271
 
    }
272
 
    StringRef getFilename() const    { return getCompileUnit().getFilename();}
273
 
    StringRef getDirectory() const   { return getCompileUnit().getDirectory();}
274
 
 
275
 
    /// replaceAllUsesWith - Replace all uses of debug info referenced by
276
 
    /// this descriptor.
277
 
    void replaceAllUsesWith(DIDescriptor &D);
278
 
 
279
 
    /// print - print type.
280
 
    void print(raw_ostream &OS) const;
281
 
 
282
 
    /// dump - print type to dbgs() with a newline.
283
 
    void dump() const;
284
 
  };
285
 
 
286
 
  /// DIBasicType - A basic type, like 'int' or 'float'.
287
 
  class DIBasicType : public DIType {
288
 
  public:
289
 
    explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
290
 
 
291
 
    unsigned getEncoding() const { return getUnsignedField(9); }
292
 
 
293
 
    /// Verify - Verify that a basic type descriptor is well formed.
294
 
    bool Verify() const;
295
 
 
296
 
    /// print - print basic type.
297
 
    void print(raw_ostream &OS) const;
298
 
 
299
 
    /// dump - print basic type to dbgs() with a newline.
300
 
    void dump() const;
301
 
  };
302
 
 
303
 
  /// DIDerivedType - A simple derived type, like a const qualified type,
304
 
  /// a typedef, a pointer or reference, etc.
305
 
  class DIDerivedType : public DIType {
306
 
  protected:
307
 
    explicit DIDerivedType(const MDNode *N, bool, bool)
308
 
      : DIType(N, true, true) {}
309
 
  public:
310
 
    explicit DIDerivedType(const MDNode *N = 0)
311
 
      : DIType(N, true, true) {}
312
 
 
313
 
    DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
314
 
 
315
 
    /// getOriginalTypeSize - If this type is derived from a base type then
316
 
    /// return base type size.
317
 
    uint64_t getOriginalTypeSize() const;
318
 
 
319
 
    /// Verify - Verify that a derived type descriptor is well formed.
320
 
    bool Verify() const;
321
 
 
322
 
    /// print - print derived type.
323
 
    void print(raw_ostream &OS) const;
324
 
 
325
 
    /// dump - print derived type to dbgs() with a newline.
326
 
    void dump() const;
327
 
  };
328
 
 
329
 
  /// DICompositeType - This descriptor holds a type that can refer to multiple
330
 
  /// other types, like a function or struct.
331
 
  /// FIXME: Why is this a DIDerivedType??
332
 
  class DICompositeType : public DIDerivedType {
333
 
  public:
334
 
    explicit DICompositeType(const MDNode *N = 0)
335
 
      : DIDerivedType(N, true, true) {
336
 
      if (N && !isCompositeType())
337
 
        DbgNode = 0;
338
 
    }
339
 
 
340
 
    DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
341
 
    unsigned getRunTimeLang() const { return getUnsignedField(11); }
342
 
    DICompositeType getContainingType() const {
343
 
      return getFieldAs<DICompositeType>(12);
344
 
    }
345
 
 
346
 
    /// Verify - Verify that a composite type descriptor is well formed.
347
 
    bool Verify() const;
348
 
 
349
 
    /// print - print composite type.
350
 
    void print(raw_ostream &OS) const;
351
 
 
352
 
    /// dump - print composite type to dbgs() with a newline.
353
 
    void dump() const;
354
 
  };
355
 
 
356
 
  /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
357
 
  class DISubprogram : public DIScope {
358
 
  public:
359
 
    explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
360
 
 
361
 
    DIScope getContext() const          { return getFieldAs<DIScope>(2); }
362
 
    StringRef getName() const         { return getStringField(3); }
363
 
    StringRef getDisplayName() const  { return getStringField(4); }
364
 
    StringRef getLinkageName() const  { return getStringField(5); }
365
 
    DICompileUnit getCompileUnit() const{ 
366
 
      if (getVersion() == llvm::LLVMDebugVersion7)
367
 
        return getFieldAs<DICompileUnit>(6);
368
 
 
369
 
      DIFile F = getFieldAs<DIFile>(6); 
370
 
      return F.getCompileUnit();
371
 
    }
372
 
    unsigned getLineNumber() const      { return getUnsignedField(7); }
373
 
    DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
374
 
 
375
 
    /// getReturnTypeName - Subprogram return types are encoded either as
376
 
    /// DIType or as DICompositeType.
377
 
    StringRef getReturnTypeName() const {
378
 
      DICompositeType DCT(getFieldAs<DICompositeType>(8));
379
 
      if (DCT.Verify()) {
380
 
        DIArray A = DCT.getTypeArray();
381
 
        DIType T(A.getElement(0));
382
 
        return T.getName();
383
 
      }
384
 
      DIType T(getFieldAs<DIType>(8));
385
 
      return T.getName();
386
 
    }
387
 
 
388
 
    /// isLocalToUnit - Return true if this subprogram is local to the current
389
 
    /// compile unit, like 'static' in C.
390
 
    unsigned isLocalToUnit() const     { return getUnsignedField(9); }
391
 
    unsigned isDefinition() const      { return getUnsignedField(10); }
392
 
 
393
 
    unsigned getVirtuality() const { return getUnsignedField(11); }
394
 
    unsigned getVirtualIndex() const { return getUnsignedField(12); }
395
 
 
396
 
    DICompositeType getContainingType() const {
397
 
      return getFieldAs<DICompositeType>(13);
398
 
    }
399
 
    unsigned isArtificial() const    { return getUnsignedField(14); }
400
 
    unsigned isOptimized() const;
401
 
 
402
 
    StringRef getFilename() const    { 
403
 
      if (getVersion() == llvm::LLVMDebugVersion7)
404
 
        return getCompileUnit().getFilename();
405
 
 
406
 
      DIFile F = getFieldAs<DIFile>(6); 
407
 
      return F.getFilename();
408
 
    }
409
 
 
410
 
    StringRef getDirectory() const   { 
411
 
      if (getVersion() == llvm::LLVMDebugVersion7)
412
 
        return getCompileUnit().getFilename();
413
 
 
414
 
      DIFile F = getFieldAs<DIFile>(6); 
415
 
      return F.getDirectory();
416
 
    }
417
 
 
418
 
    /// Verify - Verify that a subprogram descriptor is well formed.
419
 
    bool Verify() const;
420
 
 
421
 
    /// print - print subprogram.
422
 
    void print(raw_ostream &OS) const;
423
 
 
424
 
    /// dump - print subprogram to dbgs() with a newline.
425
 
    void dump() const;
426
 
 
427
 
    /// describes - Return true if this subprogram provides debugging
428
 
    /// information for the function F.
429
 
    bool describes(const Function *F);
430
 
 
431
 
    Function *getFunction() const { return getFunctionField(16); }
432
 
  };
433
 
 
434
 
  /// DIGlobalVariable - This is a wrapper for a global variable.
435
 
  class DIGlobalVariable : public DIDescriptor {
436
 
  public:
437
 
    explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
438
 
 
439
 
    DIScope getContext() const          { return getFieldAs<DIScope>(2); }
440
 
    StringRef getName() const         { return getStringField(3); }
441
 
    StringRef getDisplayName() const  { return getStringField(4); }
442
 
    StringRef getLinkageName() const  { return getStringField(5); }
443
 
    DICompileUnit getCompileUnit() const{ 
444
 
      if (getVersion() == llvm::LLVMDebugVersion7)
445
 
        return getFieldAs<DICompileUnit>(6);
446
 
 
447
 
      DIFile F = getFieldAs<DIFile>(6); 
448
 
      return F.getCompileUnit();
449
 
    }
450
 
 
451
 
    unsigned getLineNumber() const      { return getUnsignedField(7); }
452
 
    DIType getType() const              { return getFieldAs<DIType>(8); }
453
 
    unsigned isLocalToUnit() const      { return getUnsignedField(9); }
454
 
    unsigned isDefinition() const       { return getUnsignedField(10); }
455
 
 
456
 
    GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
457
 
    Constant *getConstant() const   { return getConstantField(11); }
458
 
 
459
 
    /// Verify - Verify that a global variable descriptor is well formed.
460
 
    bool Verify() const;
461
 
 
462
 
    /// print - print global variable.
463
 
    void print(raw_ostream &OS) const;
464
 
 
465
 
    /// dump - print global variable to dbgs() with a newline.
466
 
    void dump() const;
467
 
  };
468
 
 
469
 
  /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
470
 
  /// global etc).
471
 
  class DIVariable : public DIDescriptor {
472
 
  public:
473
 
    explicit DIVariable(const MDNode *N = 0)
474
 
      : DIDescriptor(N) {}
475
 
 
476
 
    DIScope getContext() const          { return getFieldAs<DIScope>(1); }
477
 
    StringRef getName() const           { return getStringField(2);     }
478
 
    DICompileUnit getCompileUnit() const{ 
479
 
      if (getVersion() == llvm::LLVMDebugVersion7)
480
 
        return getFieldAs<DICompileUnit>(3);
481
 
 
482
 
      DIFile F = getFieldAs<DIFile>(3); 
483
 
      return F.getCompileUnit();
484
 
    }
485
 
    unsigned getLineNumber() const      { return getUnsignedField(4); }
486
 
    DIType getType() const              { return getFieldAs<DIType>(5); }
487
 
 
488
 
 
489
 
    /// Verify - Verify that a variable descriptor is well formed.
490
 
    bool Verify() const;
491
 
 
492
 
    /// HasComplexAddr - Return true if the variable has a complex address.
493
 
    bool hasComplexAddress() const {
494
 
      return getNumAddrElements() > 0;
495
 
    }
496
 
 
497
 
    unsigned getNumAddrElements() const;
498
 
    
499
 
    uint64_t getAddrElement(unsigned Idx) const {
500
 
      return getUInt64Field(Idx+6);
501
 
    }
502
 
 
503
 
    /// isBlockByrefVariable - Return true if the variable was declared as
504
 
    /// a "__block" variable (Apple Blocks).
505
 
    bool isBlockByrefVariable() const {
506
 
      return getType().isBlockByrefStruct();
507
 
    }
508
 
 
509
 
    /// isInlinedFnArgument - Return trule if this variable provides debugging
510
 
    /// information for an inlined function arguments.
511
 
    bool isInlinedFnArgument(const Function *CurFn);
512
 
 
513
 
    /// print - print variable.
514
 
    void print(raw_ostream &OS) const;
515
 
 
516
 
    /// dump - print variable to dbgs() with a newline.
517
 
    void dump() const;
518
 
  };
519
 
 
520
 
  /// DILexicalBlock - This is a wrapper for a lexical block.
521
 
  class DILexicalBlock : public DIScope {
522
 
  public:
523
 
    explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
524
 
    DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
525
 
    unsigned getLineNumber() const   { return getUnsignedField(2);         }
526
 
    unsigned getColumnNumber() const { return getUnsignedField(3);         }
527
 
    StringRef getDirectory() const {
528
 
      DIFile F = getFieldAs<DIFile>(4);
529
 
      StringRef dir = F.getDirectory();
530
 
      return !dir.empty() ? dir : getContext().getDirectory();
531
 
    }
532
 
    StringRef getFilename() const {
533
 
      DIFile F = getFieldAs<DIFile>(4);
534
 
      StringRef filename = F.getFilename();
535
 
      return !filename.empty() ? filename : getContext().getFilename();
536
 
    }
537
 
  };
538
 
 
539
 
  /// DINameSpace - A wrapper for a C++ style name space.
540
 
  class DINameSpace : public DIScope { 
541
 
  public:
542
 
    explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
543
 
    DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
544
 
    StringRef getName() const      { return getStringField(2);           }
545
 
    StringRef getDirectory() const { return getContext().getDirectory(); }
546
 
    StringRef getFilename() const  { return getContext().getFilename();  }
547
 
    DICompileUnit getCompileUnit() const{ 
548
 
      if (getVersion() == llvm::LLVMDebugVersion7)
549
 
        return getFieldAs<DICompileUnit>(3);
550
 
 
551
 
      DIFile F = getFieldAs<DIFile>(3); 
552
 
      return F.getCompileUnit();
553
 
    }
554
 
    unsigned getLineNumber() const { return getUnsignedField(4);         }
555
 
    bool Verify() const;
556
 
  };
557
 
 
558
 
  /// DILocation - This object holds location information. This object
559
 
  /// is not associated with any DWARF tag.
560
 
  class DILocation : public DIDescriptor {
561
 
  public:
562
 
    explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
563
 
 
564
 
    unsigned getLineNumber() const     { return getUnsignedField(0); }
565
 
    unsigned getColumnNumber() const   { return getUnsignedField(1); }
566
 
    DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
567
 
    DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
568
 
    StringRef getFilename() const    { return getScope().getFilename(); }
569
 
    StringRef getDirectory() const   { return getScope().getDirectory(); }
570
 
    bool Verify() const;
571
 
  };
572
 
 
573
 
  /// DIFactory - This object assists with the construction of the various
574
 
  /// descriptors.
575
 
  class DIFactory {
576
 
    Module &M;
577
 
    LLVMContext& VMContext;
578
 
 
579
 
    Function *DeclareFn;     // llvm.dbg.declare
580
 
    Function *ValueFn;       // llvm.dbg.value
581
 
 
582
 
    DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
583
 
    void operator=(const DIFactory&); // DO NOT IMPLEMENT
584
 
  public:
585
 
    enum ComplexAddrKind { OpPlus=1, OpDeref };
586
 
 
587
 
    explicit DIFactory(Module &m);
588
 
 
589
 
    /// GetOrCreateArray - Create an descriptor for an array of descriptors.
590
 
    /// This implicitly uniques the arrays created.
591
 
    DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
592
 
 
593
 
    /// GetOrCreateSubrange - Create a descriptor for a value range.  This
594
 
    /// implicitly uniques the values returned.
595
 
    DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
596
 
 
597
 
    /// CreateCompileUnit - Create a new descriptor for the specified compile
598
 
    /// unit.
599
 
    DICompileUnit CreateCompileUnit(unsigned LangID,
600
 
                                    StringRef Filename,
601
 
                                    StringRef Directory,
602
 
                                    StringRef Producer,
603
 
                                    bool isMain = false,
604
 
                                    bool isOptimized = false,
605
 
                                    StringRef Flags = "",
606
 
                                    unsigned RunTimeVer = 0);
607
 
 
608
 
    /// CreateFile -  Create a new descriptor for the specified file.
609
 
    DIFile CreateFile(StringRef Filename, StringRef Directory,
610
 
                      DICompileUnit CU);
611
 
 
612
 
    /// CreateEnumerator - Create a single enumerator value.
613
 
    DIEnumerator CreateEnumerator(StringRef Name, uint64_t Val);
614
 
 
615
 
    /// CreateBasicType - Create a basic type like int, float, etc.
616
 
    DIBasicType CreateBasicType(DIDescriptor Context, StringRef Name,
617
 
                                DIFile F, unsigned LineNumber,
618
 
                                uint64_t SizeInBits, uint64_t AlignInBits,
619
 
                                uint64_t OffsetInBits, unsigned Flags,
620
 
                                unsigned Encoding);
621
 
 
622
 
    /// CreateBasicType - Create a basic type like int, float, etc.
623
 
    DIBasicType CreateBasicTypeEx(DIDescriptor Context, StringRef Name,
624
 
                                DIFile F, unsigned LineNumber,
625
 
                                Constant *SizeInBits, Constant *AlignInBits,
626
 
                                Constant *OffsetInBits, unsigned Flags,
627
 
                                unsigned Encoding);
628
 
 
629
 
    /// CreateDerivedType - Create a derived type like const qualified type,
630
 
    /// pointer, typedef, etc.
631
 
    DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
632
 
                                    StringRef Name,
633
 
                                    DIFile F,
634
 
                                    unsigned LineNumber,
635
 
                                    uint64_t SizeInBits, uint64_t AlignInBits,
636
 
                                    uint64_t OffsetInBits, unsigned Flags,
637
 
                                    DIType DerivedFrom);
638
 
 
639
 
    /// CreateDerivedType - Create a derived type like const qualified type,
640
 
    /// pointer, typedef, etc.
641
 
    DIDerivedType CreateDerivedTypeEx(unsigned Tag, DIDescriptor Context,
642
 
                                      StringRef Name,
643
 
                                      DIFile F,
644
 
                                      unsigned LineNumber,
645
 
                                      Constant *SizeInBits, 
646
 
                                      Constant *AlignInBits,
647
 
                                      Constant *OffsetInBits, unsigned Flags,
648
 
                                      DIType DerivedFrom);
649
 
 
650
 
    /// CreateCompositeType - Create a composite type like array, struct, etc.
651
 
    DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
652
 
                                        StringRef Name,
653
 
                                        DIFile F,
654
 
                                        unsigned LineNumber,
655
 
                                        uint64_t SizeInBits,
656
 
                                        uint64_t AlignInBits,
657
 
                                        uint64_t OffsetInBits, unsigned Flags,
658
 
                                        DIType DerivedFrom,
659
 
                                        DIArray Elements,
660
 
                                        unsigned RunTimeLang = 0,
661
 
                                        MDNode *ContainingType = 0);
662
 
 
663
 
    /// CreateTemporaryType - Create a temporary forward-declared type.
664
 
    DIType CreateTemporaryType();
665
 
 
666
 
    /// CreateArtificialType - Create a new DIType with "artificial" flag set.
667
 
    DIType CreateArtificialType(DIType Ty);
668
 
 
669
 
    /// CreateCompositeType - Create a composite type like array, struct, etc.
670
 
    DICompositeType CreateCompositeTypeEx(unsigned Tag, DIDescriptor Context,
671
 
                                          StringRef Name,
672
 
                                          DIFile F,
673
 
                                          unsigned LineNumber,
674
 
                                          Constant *SizeInBits,
675
 
                                          Constant *AlignInBits,
676
 
                                          Constant *OffsetInBits, 
677
 
                                          unsigned Flags,
678
 
                                          DIType DerivedFrom,
679
 
                                          DIArray Elements,
680
 
                                          unsigned RunTimeLang = 0,
681
 
                                          MDNode *ContainingType = 0);
682
 
 
683
 
    /// CreateSubprogram - Create a new descriptor for the specified subprogram.
684
 
    /// See comments in DISubprogram for descriptions of these fields.
685
 
    DISubprogram CreateSubprogram(DIDescriptor Context, StringRef Name,
686
 
                                  StringRef DisplayName,
687
 
                                  StringRef LinkageName,
688
 
                                  DIFile F, unsigned LineNo,
689
 
                                  DIType Ty, bool isLocalToUnit,
690
 
                                  bool isDefinition,
691
 
                                  unsigned VK = 0,
692
 
                                  unsigned VIndex = 0,
693
 
                                  DIType = DIType(),
694
 
                                  bool isArtificial = 0,
695
 
                                  bool isOptimized = false,
696
 
                                  Function *Fn = 0);
697
 
 
698
 
    /// CreateSubprogramDefinition - Create new subprogram descriptor for the
699
 
    /// given declaration. 
700
 
    DISubprogram CreateSubprogramDefinition(DISubprogram &SPDeclaration);
701
 
 
702
 
    /// CreateGlobalVariable - Create a new descriptor for the specified global.
703
 
    DIGlobalVariable
704
 
    CreateGlobalVariable(DIDescriptor Context, StringRef Name,
705
 
                         StringRef DisplayName,
706
 
                         StringRef LinkageName,
707
 
                         DIFile F,
708
 
                         unsigned LineNo, DIType Ty, bool isLocalToUnit,
709
 
                         bool isDefinition, llvm::GlobalVariable *GV);
710
 
 
711
 
    /// CreateGlobalVariable - Create a new descriptor for the specified constant.
712
 
    DIGlobalVariable
713
 
    CreateGlobalVariable(DIDescriptor Context, StringRef Name,
714
 
                         StringRef DisplayName,
715
 
                         StringRef LinkageName,
716
 
                         DIFile F,
717
 
                         unsigned LineNo, DIType Ty, bool isLocalToUnit,
718
 
                         bool isDefinition, llvm::Constant *C);
719
 
 
720
 
    /// CreateVariable - Create a new descriptor for the specified variable.
721
 
    DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
722
 
                              StringRef Name,
723
 
                              DIFile F, unsigned LineNo,
724
 
                              DIType Ty, bool AlwaysPreserve = false);
725
 
 
726
 
    /// CreateComplexVariable - Create a new descriptor for the specified
727
 
    /// variable which has a complex address expression for its address.
728
 
    DIVariable CreateComplexVariable(unsigned Tag, DIDescriptor Context,
729
 
                                     const std::string &Name,
730
 
                                     DIFile F, unsigned LineNo,
731
 
                                     DIType Ty,
732
 
                                     SmallVector<Value *, 9> &addr);
733
 
 
734
 
    /// CreateLexicalBlock - This creates a descriptor for a lexical block
735
 
    /// with the specified parent context.
736
 
    DILexicalBlock CreateLexicalBlock(DIDescriptor Context, DIFile F,
737
 
                                      unsigned Line = 0, unsigned Col = 0);
738
 
 
739
 
    /// CreateNameSpace - This creates new descriptor for a namespace
740
 
    /// with the specified parent context.
741
 
    DINameSpace CreateNameSpace(DIDescriptor Context, StringRef Name,
742
 
                                DIFile F, unsigned LineNo);
743
 
 
744
 
    /// CreateLocation - Creates a debug info location.
745
 
    DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
746
 
                              DIScope S, DILocation OrigLoc);
747
 
 
748
 
    /// CreateLocation - Creates a debug info location.
749
 
    DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
750
 
                              DIScope S, MDNode *OrigLoc = 0);
751
 
 
752
 
    /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
753
 
    Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
754
 
                               BasicBlock *InsertAtEnd);
755
 
 
756
 
    /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
757
 
    Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
758
 
                               Instruction *InsertBefore);
759
 
 
760
 
    /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
761
 
    Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
762
 
                                         DIVariable D, BasicBlock *InsertAtEnd);
763
 
 
764
 
    /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
765
 
    Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
766
 
                                       DIVariable D, Instruction *InsertBefore);
767
 
  private:
768
 
    Constant *GetTagConstant(unsigned TAG);
769
 
  };
770
 
 
771
 
  bool getLocationInfo(const Value *V, std::string &DisplayName,
772
 
                       std::string &Type, unsigned &LineNo, std::string &File,
773
 
                       std::string &Dir);
774
 
 
775
 
  /// getDISubprogram - Find subprogram that is enclosing this scope.
776
 
  DISubprogram getDISubprogram(const MDNode *Scope);
777
 
 
778
 
  /// getDICompositeType - Find underlying composite type.
779
 
  DICompositeType getDICompositeType(DIType T);
780
 
 
781
 
  class DebugInfoFinder {
782
 
  public:
783
 
    /// processModule - Process entire module and collect debug info
784
 
    /// anchors.
785
 
    void processModule(Module &M);
786
 
 
787
 
  private:
788
 
    /// processType - Process DIType.
789
 
    void processType(DIType DT);
790
 
 
791
 
    /// processLexicalBlock - Process DILexicalBlock.
792
 
    void processLexicalBlock(DILexicalBlock LB);
793
 
 
794
 
    /// processSubprogram - Process DISubprogram.
795
 
    void processSubprogram(DISubprogram SP);
796
 
 
797
 
    /// processDeclare - Process DbgDeclareInst.
798
 
    void processDeclare(DbgDeclareInst *DDI);
799
 
 
800
 
    /// processLocation - Process DILocation.
801
 
    void processLocation(DILocation Loc);
802
 
 
803
 
    /// addCompileUnit - Add compile unit into CUs.
804
 
    bool addCompileUnit(DICompileUnit CU);
805
 
 
806
 
    /// addGlobalVariable - Add global variable into GVs.
807
 
    bool addGlobalVariable(DIGlobalVariable DIG);
808
 
 
809
 
    // addSubprogram - Add subprgoram into SPs.
810
 
    bool addSubprogram(DISubprogram SP);
811
 
 
812
 
    /// addType - Add type into Tys.
813
 
    bool addType(DIType DT);
814
 
 
815
 
  public:
816
 
    typedef SmallVector<MDNode *, 8>::const_iterator iterator;
817
 
    iterator compile_unit_begin()    const { return CUs.begin(); }
818
 
    iterator compile_unit_end()      const { return CUs.end(); }
819
 
    iterator subprogram_begin()      const { return SPs.begin(); }
820
 
    iterator subprogram_end()        const { return SPs.end(); }
821
 
    iterator global_variable_begin() const { return GVs.begin(); }
822
 
    iterator global_variable_end()   const { return GVs.end(); }
823
 
    iterator type_begin()            const { return TYs.begin(); }
824
 
    iterator type_end()              const { return TYs.end(); }
825
 
 
826
 
    unsigned compile_unit_count()    const { return CUs.size(); }
827
 
    unsigned global_variable_count() const { return GVs.size(); }
828
 
    unsigned subprogram_count()      const { return SPs.size(); }
829
 
    unsigned type_count()            const { return TYs.size(); }
830
 
 
831
 
  private:
832
 
    SmallVector<MDNode *, 8> CUs;  // Compile Units
833
 
    SmallVector<MDNode *, 8> SPs;  // Subprograms
834
 
    SmallVector<MDNode *, 8> GVs;  // Global Variables;
835
 
    SmallVector<MDNode *, 8> TYs;  // Types
836
 
    SmallPtrSet<MDNode *, 64> NodesSeen;
837
 
  };
838
 
} // end namespace llvm
839
 
 
840
 
#endif