comparison ir/irstruct.h @ 1228:79758fd2f48a

Added Doxygen file. Completely seperated type and symbol generation. Should fix a lot of bugs, but is not yet 100% complete.
author Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
date Wed, 15 Apr 2009 20:06:25 +0200
parents ba9d6292572a
children e67c85d6e680
comparison
equal deleted inserted replaced
1215:08f87d8cd101 1228:79758fd2f48a
4 #include "ir/ir.h" 4 #include "ir/ir.h"
5 5
6 #include <vector> 6 #include <vector>
7 #include <map> 7 #include <map>
8 8
9 struct IrInterface; 9 // DMD forward declarations
10 struct StructInitializer;
10 11
11 void addZeros(std::vector<const llvm::Type*>& inits, size_t pos, size_t offset);
12 void addZeros(std::vector<llvm::Constant*>& inits, size_t pos, size_t offset);
13 void addZeros(std::vector<llvm::Value*>& inits, size_t pos, size_t offset);
14
15 //////////////////////////////////////////////////////////////////////////////
16 //////////////////////////////////////////////////////////////////////////////
17 ////////////////////////////////////////////////////////////////////////////// 12 //////////////////////////////////////////////////////////////////////////////
18 13
19 // represents a struct or class 14 // represents a struct or class
20 // it is used during codegen to hold all the vital info we need 15 // it is used during codegen to hold all the vital info we need
21 struct IrStruct : IrBase 16 struct IrStruct : IrBase
22 { 17 {
23 ///////////////////////////////////////////////////////////////////// 18 /// Constructor.
24 /////////////////////////////////////////////////////////////////////
25
26 typedef std::vector<VarDeclaration*> VarDeclVector;
27
28 typedef std::map<ClassDeclaration*, IrInterface*> InterfaceMap;
29 typedef InterfaceMap::iterator InterfaceMapIter;
30
31 typedef std::vector<IrInterface*> InterfaceVector;
32 typedef InterfaceVector::iterator InterfaceVectorIter;
33
34 // vector of LLVM types
35 typedef std::vector<const llvm::Type*> TypeVector;
36
37 /////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////
39
40 // Anon represents an anonymous struct union block inside an aggregate
41 // during LLVM type construction.
42 struct Anon
43 {
44 bool isunion;
45 Anon* parent;
46
47 TypeVector types;
48
49 Anon(bool IsUnion, Anon* par) : isunion(IsUnion), parent(par) {}
50 };
51
52 /////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////
54
55 /// ctor
56 IrStruct(AggregateDeclaration* agg); 19 IrStruct(AggregateDeclaration* agg);
57 20
58 /// dtor 21 //////////////////////////////////////////////////////////////////////////
59 virtual ~IrStruct(); 22 // public fields,
23 // FIXME this is basically stuff I just haven't gotten around to yet.
60 24
61 ///////////////////////////////////////////////////////////////////// 25 /// The D aggregate.
62 /////////////////////////////////////////////////////////////////////
63
64 /// push an anonymous struct/union
65 void pushAnon(bool isunion);
66
67 /// pops an anonymous struct/union
68 void popAnon();
69
70 /// adds field
71 void addVar(VarDeclaration* var);
72
73 /////////////////////////////////////////////////////////////////////
74 /////////////////////////////////////////////////////////////////////
75
76 /// build the aggr type
77 const LLType* build();
78
79 /// put the aggr initializers in a vector
80 void buildDefaultConstInit(std::vector<llvm::Constant*>& inits);
81
82 /// ditto - but also builds the constant struct, for convenience
83 LLConstant* buildDefaultConstInit();
84
85 /////////////////////////////////////////////////////////////////////
86 /////////////////////////////////////////////////////////////////////
87
88 // the D aggregate
89 AggregateDeclaration* aggrdecl; 26 AggregateDeclaration* aggrdecl;
90 27
91 // vector of VarDeclarations in this aggregate 28 /// Aggregate D type.
92 VarDeclVector varDecls;
93
94 // vector of VarDeclarations that contribute to the default initializer
95 VarDeclVector defVars;
96
97 // true if the default initializer has been built
98 bool defaultFound;
99
100 // top element
101 Anon* anon;
102
103 // toplevel types in this aggr
104 TypeVector types;
105
106 // current index
107 // always the same as types.size()
108 size_t index;
109
110 // aggregate D type
111 Type* type; 29 Type* type;
112 30
113 // class vtable type 31 /// true only for: align(1) struct S { ... }
114 llvm::PATypeHolder vtblTy; 32 bool packed;
115 llvm::PATypeHolder vtblInitTy;
116 33
117 // initializer type opaque (type of global matches initializer, not formal type) 34 /// Composite type debug description.
118 llvm::PATypeHolder initOpaque; 35 llvm::DICompositeType diCompositeType;
119 llvm::PATypeHolder classInfoOpaque;
120 36
121 // map/vector of interfaces implemented 37 //////////////////////////////////////////////////////////////////////////
122 InterfaceMap interfaceMap;
123 InterfaceVector interfaceVec;
124 38
125 // interface info array global 39 /// Create the __initZ symbol lazily.
126 LLGlobalVariable* interfaceInfos; 40 LLGlobalVariable* getInitSymbol();
41 /// Builds the __initZ initializer constant lazily.
42 LLConstant* getDefaultInit();
127 43
128 // ... 44 /// Create the __vtblZ symbol lazily.
129 bool defined; 45 LLGlobalVariable* getVtblSymbol();
130 bool constinited; 46 /// Builds the __vtblZ initializer constant lazily.
47 LLConstant* getVtblInit();
131 48
132 // vtbl global and initializer 49 /// Create the __ClassZ symbol lazily.
133 LLGlobalVariable* vtbl; 50 LLGlobalVariable* getClassInfoSymbol();
51 /// Builds the __ClassZ initializer constant lazily.
52 LLConstant* getClassInfoInit();
53
54 /// Create the __interfaceInfos symbol lazily.
55 LLGlobalVariable* getInterfaceArraySymbol();
56
57 /// Creates a StructInitializer constant.
58 LLConstant* createStructInitializer(StructInitializer* si);
59
60 //////////////////////////////////////////////////////////////////////////
61 protected:
62 /// Static default initializer global.
63 llvm::GlobalVariable* init;
64 /// Static default initializer constant.
65 LLConstant* constInit;
66
67 /// Vtbl global.
68 llvm::GlobalVariable* vtbl;
69 /// Vtbl initializer constant.
134 LLConstant* constVtbl; 70 LLConstant* constVtbl;
135 71
136 // static initializers global and constant 72 /// ClassInfo global.
137 LLGlobalVariable* init; 73 llvm::GlobalVariable* classInfo;
138 LLConstant* constInit; 74 /// ClassInfo initializer constant.
75 LLConstant* constClassInfo;
139 76
140 // classinfo global and initializer constant 77 /// Map for mapping ClassDeclaration* to LLVM GlobalVariable.
141 LLGlobalVariable* classInfo; 78 typedef std::map<ClassDeclaration*, llvm::GlobalVariable*> ClassGlobalMap;
142 LLConstant* constClassInfo;
143 bool classInfoDeclared;
144 bool classInfoDefined;
145 // vector of interfaces that should be put in ClassInfo.interfaces
146 InterfaceVector classInfoInterfaces;
147 79
148 // align(1) struct S { ... } 80 /// Map from of interface vtbls implemented by this class.
149 bool packed; 81 ClassGlobalMap interfaceVtblMap;
150 82
151 // composite type debug description 83 /// Interface info array global.
152 llvm::DICompositeType diCompositeType; 84 /// Basically: static object.Interface[num_interfaces]
85 llvm::GlobalVariable* classInterfacesArray;
153 86
154 std::vector<VarDeclaration*> staticVars; 87 //////////////////////////////////////////////////////////////////////////
155 std::vector<FuncDeclaration*> structFuncs; 88
89 /// Create static default initializer for struct.
90 LLConstant* createStructDefaultInitializer();
91
92 /// Create static default initializer for class.
93 LLConstant* createClassDefaultInitializer();
94
95 /// Returns vtbl for interface implementation, creates it if not already built.
96 llvm::GlobalVariable* getInterfaceVtbl(BaseClass* b, bool new_inst);
97
98 /// Add base class data to initializer list.
99 /// Also creates the IrField instance for each data field.
100 void addBaseClassInits(
101 std::vector<llvm::Constant*>& constants,
102 ClassDeclaration* base,
103 size_t& offset,
104 size_t& field_index);
105
106 // FIXME make this a member instead
107 friend LLConstant* DtoDefineClassInfo(ClassDeclaration* cd);
108
109 /// Create the Interface[] interfaces ClassInfo field initializer.
110 LLConstant* getClassInfoInterfaces();
156 }; 111 };
157 112
158 ////////////////////////////////////////////////////////////////////////////// 113 //////////////////////////////////////////////////////////////////////////////
159 //////////////////////////////////////////////////////////////////////////////
160 //////////////////////////////////////////////////////////////////////////////
161
162 // represents interface implemented by a class
163 struct IrInterface : IrBase
164 {
165 BaseClass* base;
166 ClassDeclaration* decl;
167
168 llvm::PATypeHolder vtblInitTy;
169
170 LLConstant* vtblInit;
171 LLGlobalVariable* vtbl;
172 Array vtblDecls; // array of FuncDecls that make up the vtbl
173
174 const LLStructType* infoTy;
175 LLConstant* infoInit;
176 LLConstant* info;
177
178 size_t index;
179
180 IrInterface(BaseClass* b);
181 };
182 114
183 #endif 115 #endif