Add DebugInfoBuilder. Patch by Talin!
[oota-llvm.git] / include / llvm / Support / DebugInfoBuilder.h
1 //===-- llvm/Support/DebugInfoBuilder.h - -----------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the DebugInfoBuilder class, which is
11 // a helper class used to construct source level debugging information.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_DEBUGINFOBUILDER_H
16 #define LLVM_SUPPORT_DEBUGINFOBUILDER_H
17
18 #include <llvm/Module.h>
19 #include <string>
20
21 namespace llvm {
22     
23 class Type;
24 class IntegerType;
25 class FloatType;
26 class StructType;
27 class PointerType;
28 class Module;
29 class GlobalVariable;
30 class Constant;
31
32 namespace sys {
33     class Path;
34 }
35
36 /// Helper class used to construct source-level debugging information.
37 ///
38 /// The helper contains a notion of "current context", which is a
39 /// DWARF descriptor object representing the scope (module, class,
40 /// function, etc.) that currently encloses the definitions being
41 /// emitted.
42 ///
43 /// Initially, you should call setModule() to specify the target
44 /// module. Descriptors which are generated will be inserted into
45 /// this module. This also generates the initial set of anchor
46 /// descriptors, if they have not already been created for the module.
47 ///
48 /// Next, you should call createCompileUnitDescriptor() to create
49 /// the descriptor for the current compilation unit. This method
50 /// sets the current context to the newly created descriptor.
51 ///
52 /// Once that has been done, you can then create descriptors for
53 /// global definitions (functions, variables, etc.). You can use
54 /// setContext() to modify the current context. setContext() returns
55 /// a reference to the previous context, allowing easy restoration
56 /// of the previous context.
57 class DebugInfoBuilder {
58 private:
59     Module * module;
60     PointerType * anyPtrType;    // Pointer to empty struct
61     StructType * anchorType;
62     GlobalVariable * compileUnit;
63     GlobalVariable * context;
64     GlobalVariable * compileUnitAnchor;
65     GlobalVariable * globalVariableAnchor;
66     GlobalVariable * subprogramAnchor;
67     GlobalVariable * compileUnitDescriptor;
68
69     // Create an anchor with the specified tag.
70     GlobalVariable * createAnchor(unsigned anchorTag, const char * anchorName);
71
72     // Calculate alignement for primitive types.
73     unsigned getBasicAlignment(unsigned sizeInBits);
74
75     // Calculate the size of the specified LLVM type.
76     Constant * getSize(const Type * type);
77
78     // Calculate the alignment of the specified LLVM type.
79     Constant * getAlignment(const Type * type);
80
81 public:
82     /// Constructor
83     DebugInfoBuilder();
84
85     /// Return the type defined by llvm.dbg.anchor.type
86     StructType * getAnchorType() const { return anchorType; }
87     
88     /// Set the reference to the module where we will insert debugging
89     /// information. Also defines the debug info types for the module and
90     /// creates the initial anchors. Also changes the current context to the
91     // global context for that module.
92     void setModule(Module * m);
93     
94     /// Emit a compile unit descriptor. This should be done once for each
95     /// module before any other debug descriptors are created. This also
96     /// changes the current context to the global context for the compile unit.
97     GlobalVariable * createCompileUnitDescriptor(
98         unsigned langId,
99         const sys::Path & srcPath,
100         const std::string & producer);
101
102     /// Set a new context, returning the previous context. The context is the
103     /// debug descriptor representing the current scope (module, function,
104     /// class, etc.)
105     GlobalVariable * setContext(GlobalVariable * ctx) {
106         GlobalVariable * prev = context;
107         context = ctx;
108         return prev;
109     }
110     
111     /// Emit a subprogram descriptor in the current context.
112     GlobalVariable * createSubProgramDescriptor(
113         const std::string & name,       // Name of the subprogram
114         const std::string & qualName,   // Fully-qualified name
115         unsigned line,                  // Line number
116         GlobalVariable * typeDesc,      // Type descriptor
117         bool isInternalScoped,          // True if internal to module.
118         bool isDefined);                // True if defined in this module.
119
120     /// Create a type descriptor for a primitive type.
121     GlobalVariable * createBasicTypeDescriptor(
122         std::string & name,
123         unsigned line,
124         unsigned sizeInBits,
125         unsigned alignmentInBits,
126         unsigned offsetInBits,
127         unsigned typeEncoding);
128
129     /// Create a type descriptor for an integer type
130     GlobalVariable * createIntegerTypeDescriptor(
131         std::string & name, const IntegerType * type, bool isSigned);
132
133     /// Create a type descriptor for an character type
134     GlobalVariable * createCharacterTypeDescriptor(
135         std::string & name, const IntegerType * type, bool isSigned);
136
137     /// Create a type descriptor for an floating-point type
138     GlobalVariable * createFloatTypeDescriptor(std::string & name,
139         const Type * type);
140
141     /// Create a type descriptor for a pointer type.
142     GlobalVariable * createPointerTypeDescriptor(
143         std::string & name,             // Name of the type
144         GlobalVariable * referenceType, // Descriptor for what is pointed to
145         const PointerType * type,       // LLVM type of the pointer
146         unsigned line);                 // Line number of definition (0 if none)
147 };
148
149 }
150
151 #endif