Mercurial > projects > ldc
annotate gen/typinf.cpp @ 86:fd32135dca3e trunk
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
Lots of bugfixes.
Added support for special foreach on strings.
Added std.array, std.utf, std.ctype and std.uni to phobos.
Changed all the .c files in the gen dir to .cpp (it *is* C++ after all)
author | lindquist |
---|---|
date | Sat, 03 Nov 2007 14:44:58 +0100 |
parents | gen/typinf.c@3587401b6eeb |
children | ccca1c13e13a |
rev | line source |
---|---|
1 | 1 |
2 | |
3 // Copyright (c) 1999-2004 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <cstdio> | |
12 #include <cassert> | |
13 | |
52 | 14 #include "gen/llvm.h" |
15 | |
1 | 16 #include "mars.h" |
17 #include "module.h" | |
18 #include "mtype.h" | |
19 #include "scope.h" | |
20 #include "init.h" | |
21 #include "expression.h" | |
22 #include "attrib.h" | |
23 #include "declaration.h" | |
24 #include "template.h" | |
25 #include "id.h" | |
26 #include "enum.h" | |
27 #include "import.h" | |
28 #include "aggregate.h" | |
29 | |
52 | 30 #include "gen/irstate.h" |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
31 #include "gen/logger.h" |
52 | 32 #include "gen/runtime.h" |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
33 #include "gen/tollvm.h" |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
34 #include "gen/arrays.h" |
1 | 35 |
36 /******************************************* | |
37 * Get a canonicalized form of the TypeInfo for use with the internal | |
38 * runtime library routines. Canonicalized in that static arrays are | |
39 * represented as dynamic arrays, enums are represented by their | |
40 * underlying type, etc. This reduces the number of TypeInfo's needed, | |
41 * so we can use the custom internal ones more. | |
42 */ | |
43 | |
44 Expression *Type::getInternalTypeInfo(Scope *sc) | |
45 { TypeInfoDeclaration *tid; | |
46 Expression *e; | |
47 Type *t; | |
48 static TypeInfoDeclaration *internalTI[TMAX]; | |
49 | |
50 //printf("Type::getInternalTypeInfo() %s\n", toChars()); | |
51 t = toBasetype(); | |
52 switch (t->ty) | |
53 { | |
54 case Tsarray: | |
55 t = t->next->arrayOf(); // convert to corresponding dynamic array type | |
56 break; | |
57 | |
58 case Tclass: | |
59 if (((TypeClass *)t)->sym->isInterfaceDeclaration()) | |
60 break; | |
61 goto Linternal; | |
62 | |
63 case Tarray: | |
64 if (t->next->ty != Tclass) | |
65 break; | |
66 goto Linternal; | |
67 | |
68 case Tfunction: | |
69 case Tdelegate: | |
70 case Tpointer: | |
71 Linternal: | |
72 tid = internalTI[t->ty]; | |
73 if (!tid) | |
74 { tid = new TypeInfoDeclaration(t, 1); | |
75 internalTI[t->ty] = tid; | |
76 } | |
77 e = new VarExp(0, tid); | |
52 | 78 //e = e->addressOf(sc); |
1 | 79 e->type = tid->type; // do this so we don't get redundant dereference |
80 return e; | |
81 | |
82 default: | |
83 break; | |
84 } | |
85 //printf("\tcalling getTypeInfo() %s\n", t->toChars()); | |
86 return t->getTypeInfo(sc); | |
87 } | |
88 | |
89 | |
90 /**************************************************** | |
91 * Get the exact TypeInfo. | |
92 */ | |
93 | |
94 Expression *Type::getTypeInfo(Scope *sc) | |
95 { | |
96 Expression *e; | |
97 Type *t; | |
98 | |
99 //printf("Type::getTypeInfo() %p, %s\n", this, toChars()); | |
100 t = merge(); // do this since not all Type's are merge'd | |
101 if (!t->vtinfo) | |
102 { t->vtinfo = t->getTypeInfoDeclaration(); | |
103 assert(t->vtinfo); | |
104 | |
105 /* If this has a custom implementation in std/typeinfo, then | |
106 * do not generate a COMDAT for it. | |
107 */ | |
108 if (!t->builtinTypeInfo()) | |
109 { // Generate COMDAT | |
110 if (sc) // if in semantic() pass | |
111 { // Find module that will go all the way to an object file | |
112 Module *m = sc->module->importedFrom; | |
113 m->members->push(t->vtinfo); | |
114 } | |
115 else // if in obj generation pass | |
116 { | |
117 t->vtinfo->toObjFile(); | |
118 } | |
119 } | |
120 } | |
121 e = new VarExp(0, t->vtinfo); | |
122 //e = e->addressOf(sc); | |
123 e->type = t->vtinfo->type; // do this so we don't get redundant dereference | |
124 return e; | |
125 } | |
126 | |
52 | 127 enum RET TypeFunction::retStyle() |
128 { | |
129 return RETstack; | |
130 } | |
1 | 131 |
132 TypeInfoDeclaration *Type::getTypeInfoDeclaration() | |
133 { | |
134 //printf("Type::getTypeInfoDeclaration() %s\n", toChars()); | |
135 return new TypeInfoDeclaration(this, 0); | |
136 } | |
137 | |
52 | 138 TypeInfoDeclaration *TypeTypedef::getTypeInfoDeclaration() |
1 | 139 { |
52 | 140 return new TypeInfoTypedefDeclaration(this); |
1 | 141 } |
142 | |
52 | 143 TypeInfoDeclaration *TypePointer::getTypeInfoDeclaration() |
144 { | |
145 return new TypeInfoPointerDeclaration(this); | |
146 } | |
1 | 147 |
148 TypeInfoDeclaration *TypeDArray::getTypeInfoDeclaration() | |
149 { | |
150 return new TypeInfoArrayDeclaration(this); | |
151 } | |
152 | |
153 TypeInfoDeclaration *TypeSArray::getTypeInfoDeclaration() | |
154 { | |
155 return new TypeInfoStaticArrayDeclaration(this); | |
156 } | |
157 | |
158 TypeInfoDeclaration *TypeAArray::getTypeInfoDeclaration() | |
159 { | |
160 return new TypeInfoAssociativeArrayDeclaration(this); | |
161 } | |
162 | |
52 | 163 TypeInfoDeclaration *TypeStruct::getTypeInfoDeclaration() |
164 { | |
165 return new TypeInfoStructDeclaration(this); | |
166 } | |
1 | 167 |
168 TypeInfoDeclaration *TypeClass::getTypeInfoDeclaration() | |
169 { | |
170 if (sym->isInterfaceDeclaration()) | |
171 return new TypeInfoInterfaceDeclaration(this); | |
172 else | |
173 return new TypeInfoClassDeclaration(this); | |
174 } | |
175 | |
176 TypeInfoDeclaration *TypeEnum::getTypeInfoDeclaration() | |
177 { | |
178 return new TypeInfoEnumDeclaration(this); | |
179 } | |
180 | |
181 TypeInfoDeclaration *TypeFunction::getTypeInfoDeclaration() | |
182 { | |
183 return new TypeInfoFunctionDeclaration(this); | |
184 } | |
185 | |
186 TypeInfoDeclaration *TypeDelegate::getTypeInfoDeclaration() | |
187 { | |
188 return new TypeInfoDelegateDeclaration(this); | |
189 } | |
190 | |
191 TypeInfoDeclaration *TypeTuple::getTypeInfoDeclaration() | |
192 { | |
193 return new TypeInfoTupleDeclaration(this); | |
194 } | |
195 | |
196 | |
197 /* ========================================================================= */ | |
198 | |
199 /* These decide if there's an instance for them already in std.typeinfo, | |
200 * because then the compiler doesn't need to build one. | |
201 */ | |
202 | |
203 int Type::builtinTypeInfo() | |
204 { | |
205 return 0; | |
206 } | |
207 | |
208 int TypeBasic::builtinTypeInfo() | |
209 { | |
210 return 1; | |
211 } | |
212 | |
213 int TypeDArray::builtinTypeInfo() | |
214 { | |
52 | 215 return next->isTypeBasic() != NULL; |
1 | 216 } |
217 | |
218 /* ========================================================================= */ | |
219 | |
220 /*************************************** | |
221 * Create a static array of TypeInfo references | |
222 * corresponding to an array of Expression's. | |
223 * Used to supply hidden _arguments[] value for variadic D functions. | |
224 */ | |
225 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
54
diff
changeset
|
226 Expression *createTypeInfoArray(Scope *sc, Expression *exps[], int dim) |
1 | 227 { |
228 assert(0); | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
54
diff
changeset
|
229 return NULL; |
1 | 230 } |
231 | |
52 | 232 /* ========================================================================= */ |
233 | |
234 ////////////////////////////////////////////////////////////////////////////// | |
235 // MAGIC PLACE | |
236 ////////////////////////////////////////////////////////////////////////////// | |
237 | |
238 void TypeInfoDeclaration::toObjFile() | |
239 { | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
54
diff
changeset
|
240 if (llvmTouched) return; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
54
diff
changeset
|
241 else llvmTouched = true; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
54
diff
changeset
|
242 |
52 | 243 Logger::println("TypeInfoDeclaration::toObjFile()"); |
244 LOG_SCOPE; | |
65 | 245 |
52 | 246 Logger::println("type = '%s'", tinfo->toChars()); |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
247 Logger::println("typeinfo mangle: %s", mangle()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
248 |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
249 // this is a declaration of a builtin __initZ var |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
250 if (tinfo->builtinTypeInfo()) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
251 llvmValue = LLVM_D_GetRuntimeGlobal(gIR->module, mangle()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
252 assert(llvmValue); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
253 Logger::cout() << "Got typeinfo var:" << '\n' << *llvmValue << '\n'; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
254 } |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
255 // custom typedef |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
256 else { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
257 toDt(NULL); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
258 } |
52 | 259 } |
260 | |
261 /* ========================================================================= */ | |
262 | |
263 void TypeInfoDeclaration::toDt(dt_t **pdt) | |
264 { | |
265 assert(0 && "TypeInfoDeclaration"); | |
266 } | |
267 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
268 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
269 |
52 | 270 void TypeInfoTypedefDeclaration::toDt(dt_t **pdt) |
271 { | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
272 Logger::println("TypeInfoTypedefDeclaration::toDt() %s", toChars()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
273 LOG_SCOPE; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
274 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
275 ClassDeclaration* base = Type::typeinfotypedef; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
276 base->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
277 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
278 llvm::Constant* initZ = base->llvmInitZ; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
279 assert(initZ); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
280 const llvm::StructType* stype = llvm::cast<llvm::StructType>(initZ->getType()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
281 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
282 std::vector<llvm::Constant*> sinits; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
283 sinits.push_back(initZ->getOperand(0)); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
284 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
285 assert(tinfo->ty == Ttypedef); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
286 TypeTypedef *tc = (TypeTypedef *)tinfo; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
287 TypedefDeclaration *sd = tc->sym; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
288 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
289 // TypeInfo base |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
290 //const llvm::PointerType* basept = llvm::cast<llvm::PointerType>(initZ->getOperand(1)->getType()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
291 //sinits.push_back(llvm::ConstantPointerNull::get(basept)); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
292 Logger::println("generating base typeinfo"); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
293 //sd->basetype = sd->basetype->merge(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
294 sd->basetype->getTypeInfo(NULL); // generate vtinfo |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
295 assert(sd->basetype->vtinfo); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
296 if (!sd->basetype->vtinfo->llvmValue) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
297 sd->basetype->vtinfo->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
298 assert(llvm::isa<llvm::Constant>(sd->basetype->vtinfo->llvmValue)); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
299 llvm::Constant* castbase = llvm::cast<llvm::Constant>(sd->basetype->vtinfo->llvmValue); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
300 castbase = llvm::ConstantExpr::getBitCast(castbase, initZ->getOperand(1)->getType()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
301 sinits.push_back(castbase); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
302 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
303 // char[] name |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
304 char *name = sd->toPrettyChars(); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
305 sinits.push_back(DtoConstString(name)); |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
306 assert(sinits.back()->getType() == initZ->getOperand(2)->getType()); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
307 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
308 // void[] init |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
309 const llvm::PointerType* initpt = llvm::PointerType::get(llvm::Type::Int8Ty); |
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
310 if (tinfo->isZeroInit() || !sd->init) // 0 initializer, or the same as the base type |
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
311 { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
312 sinits.push_back(DtoConstSlice(DtoConstSize_t(0), llvm::ConstantPointerNull::get(initpt))); |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
313 } |
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
314 else |
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
315 { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
316 llvm::Constant* ci = DtoConstInitializer(sd->basetype, sd->init); |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
317 std::string ciname(sd->mangle()); |
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
318 ciname.append("__init"); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
319 llvm::GlobalVariable* civar = new llvm::GlobalVariable(DtoType(sd->basetype),true,llvm::GlobalValue::InternalLinkage,ci,ciname,gIR->module); |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
320 llvm::Constant* cicast = llvm::ConstantExpr::getBitCast(civar, initpt); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
321 size_t cisize = gTargetData->getTypeSize(DtoType(sd->basetype)); |
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
322 sinits.push_back(DtoConstSlice(DtoConstSize_t(cisize), cicast)); |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
65
diff
changeset
|
323 } |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
324 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
325 // create the symbol |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
326 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits); |
73 | 327 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,tiInit,toChars(),gIR->module); |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
328 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
329 llvmValue = gvar; |
52 | 330 } |
331 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
332 /* ========================================================================= */ |
52 | 333 |
334 void TypeInfoEnumDeclaration::toDt(dt_t **pdt) | |
335 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
336 Logger::println("TypeInfoTypedefDeclaration::toDt() %s", toChars()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
337 LOG_SCOPE; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
338 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
339 ClassDeclaration* base = Type::typeinfoenum; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
340 base->toObjFile(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
341 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
342 llvm::Constant* initZ = base->llvmInitZ; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
343 assert(initZ); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
344 const llvm::StructType* stype = llvm::cast<llvm::StructType>(initZ->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
345 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
346 std::vector<llvm::Constant*> sinits; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
347 sinits.push_back(initZ->getOperand(0)); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
348 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
349 assert(tinfo->ty == Tenum); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
350 TypeEnum *tc = (TypeEnum *)tinfo; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
351 EnumDeclaration *sd = tc->sym; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
352 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
353 // TypeInfo base |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
354 //const llvm::PointerType* basept = llvm::cast<llvm::PointerType>(initZ->getOperand(1)->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
355 //sinits.push_back(llvm::ConstantPointerNull::get(basept)); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
356 Logger::println("generating base typeinfo"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
357 //sd->basetype = sd->basetype->merge(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
358 sd->memtype->getTypeInfo(NULL); // generate vtinfo |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
359 assert(sd->memtype->vtinfo); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
360 if (!sd->memtype->vtinfo->llvmValue) |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
361 sd->memtype->vtinfo->toObjFile(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
362 assert(llvm::isa<llvm::Constant>(sd->memtype->vtinfo->llvmValue)); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
363 llvm::Constant* castbase = llvm::cast<llvm::Constant>(sd->memtype->vtinfo->llvmValue); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
364 castbase = llvm::ConstantExpr::getBitCast(castbase, initZ->getOperand(1)->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
365 sinits.push_back(castbase); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
366 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
367 // char[] name |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
368 char *name = sd->toPrettyChars(); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
369 sinits.push_back(DtoConstString(name)); |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
370 assert(sinits.back()->getType() == initZ->getOperand(2)->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
371 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
372 // void[] init |
71
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
373 const llvm::PointerType* initpt = llvm::PointerType::get(llvm::Type::Int8Ty); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
374 if (tinfo->isZeroInit() || !sd->defaultval) // 0 initializer, or the same as the base type |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
375 { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
376 sinits.push_back(DtoConstSlice(DtoConstSize_t(0), llvm::ConstantPointerNull::get(initpt))); |
71
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
377 } |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
378 else |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
379 { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
380 const llvm::Type* memty = DtoType(sd->memtype); |
71
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
381 llvm::Constant* ci = llvm::ConstantInt::get(memty, sd->defaultval, !sd->memtype->isunsigned()); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
382 std::string ciname(sd->mangle()); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
383 ciname.append("__init"); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
384 llvm::GlobalVariable* civar = new llvm::GlobalVariable(memty,true,llvm::GlobalValue::InternalLinkage,ci,ciname,gIR->module); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
385 llvm::Constant* cicast = llvm::ConstantExpr::getBitCast(civar, initpt); |
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
386 size_t cisize = gTargetData->getTypeSize(memty); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
387 sinits.push_back(DtoConstSlice(DtoConstSize_t(cisize), cicast)); |
71
53d3086b5ad3
[svn r75] Fixed the init member in custom enum typeinfo. I don't see any way this could actually need emmision though :/
lindquist
parents:
69
diff
changeset
|
388 } |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
389 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
390 // create the symbol |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
391 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits); |
73 | 392 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,tiInit,toChars(),gIR->module); |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
393 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
394 llvmValue = gvar; |
52 | 395 } |
396 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
397 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
398 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
399 static llvm::Constant* LLVM_D_Create_TypeInfoBase(Type* basetype, TypeInfoDeclaration* tid, ClassDeclaration* cd) |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
400 { |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
401 ClassDeclaration* base = cd; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
402 base->toObjFile(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
403 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
404 llvm::Constant* initZ = base->llvmInitZ; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
405 assert(initZ); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
406 const llvm::StructType* stype = llvm::cast<llvm::StructType>(initZ->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
407 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
408 std::vector<llvm::Constant*> sinits; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
409 sinits.push_back(initZ->getOperand(0)); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
410 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
411 // TypeInfo base |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
412 Logger::println("generating base typeinfo"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
413 basetype->getTypeInfo(NULL); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
414 assert(basetype->vtinfo); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
415 if (!basetype->vtinfo->llvmValue) |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
416 basetype->vtinfo->toObjFile(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
417 assert(llvm::isa<llvm::Constant>(basetype->vtinfo->llvmValue)); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
418 llvm::Constant* castbase = llvm::cast<llvm::Constant>(basetype->vtinfo->llvmValue); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
419 castbase = llvm::ConstantExpr::getBitCast(castbase, initZ->getOperand(1)->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
420 sinits.push_back(castbase); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
421 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
422 // create the symbol |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
423 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits); |
73 | 424 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,tiInit,tid->toChars(),gIR->module); |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
425 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
426 tid->llvmValue = gvar; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
427 } |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
428 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
429 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
430 |
52 | 431 void TypeInfoPointerDeclaration::toDt(dt_t **pdt) |
432 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
433 Logger::println("TypeInfoPointerDeclaration::toDt() %s", toChars()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
434 LOG_SCOPE; |
52 | 435 |
436 assert(tinfo->ty == Tpointer); | |
437 TypePointer *tc = (TypePointer *)tinfo; | |
438 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
439 LLVM_D_Create_TypeInfoBase(tc->next, this, Type::typeinfopointer); |
52 | 440 } |
441 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
442 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
443 |
52 | 444 void TypeInfoArrayDeclaration::toDt(dt_t **pdt) |
445 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
446 Logger::println("TypeInfoArrayDeclaration::toDt() %s", toChars()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
447 LOG_SCOPE; |
52 | 448 |
449 assert(tinfo->ty == Tarray); | |
450 TypeDArray *tc = (TypeDArray *)tinfo; | |
451 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
452 LLVM_D_Create_TypeInfoBase(tc->next, this, Type::typeinfoarray); |
52 | 453 } |
454 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
455 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
456 |
52 | 457 void TypeInfoStaticArrayDeclaration::toDt(dt_t **pdt) |
458 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
459 assert(0 && "TypeInfoStaticArrayDeclaration"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
460 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
461 /* |
52 | 462 //printf("TypeInfoStaticArrayDeclaration::toDt()\n"); |
463 dtxoff(pdt, Type::typeinfostaticarray->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfo_StaticArray | |
464 dtdword(pdt, 0); // monitor | |
465 | |
466 assert(tinfo->ty == Tsarray); | |
467 | |
468 TypeSArray *tc = (TypeSArray *)tinfo; | |
469 | |
470 tc->next->getTypeInfo(NULL); | |
471 dtxoff(pdt, tc->next->vtinfo->toSymbol(), 0, TYnptr); // TypeInfo for array of type | |
472 | |
473 dtdword(pdt, tc->dim->toInteger()); // length | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
474 */ |
52 | 475 } |
476 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
477 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
478 |
52 | 479 void TypeInfoAssociativeArrayDeclaration::toDt(dt_t **pdt) |
480 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
481 assert(0 && "TypeInfoAssociativeArrayDeclaration"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
482 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
483 /* |
52 | 484 //printf("TypeInfoAssociativeArrayDeclaration::toDt()\n"); |
485 dtxoff(pdt, Type::typeinfoassociativearray->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfo_AssociativeArray | |
486 dtdword(pdt, 0); // monitor | |
487 | |
488 assert(tinfo->ty == Taarray); | |
489 | |
490 TypeAArray *tc = (TypeAArray *)tinfo; | |
491 | |
492 tc->next->getTypeInfo(NULL); | |
493 dtxoff(pdt, tc->next->vtinfo->toSymbol(), 0, TYnptr); // TypeInfo for array of type | |
494 | |
495 tc->index->getTypeInfo(NULL); | |
496 dtxoff(pdt, tc->index->vtinfo->toSymbol(), 0, TYnptr); // TypeInfo for array of type | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
497 */ |
52 | 498 } |
499 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
500 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
501 |
52 | 502 void TypeInfoFunctionDeclaration::toDt(dt_t **pdt) |
503 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
504 Logger::println("TypeInfoFunctionDeclaration::toDt() %s", toChars()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
505 LOG_SCOPE; |
52 | 506 |
507 assert(tinfo->ty == Tfunction); | |
508 TypeFunction *tc = (TypeFunction *)tinfo; | |
509 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
510 LLVM_D_Create_TypeInfoBase(tc->next, this, Type::typeinfofunction); |
52 | 511 } |
512 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
513 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
514 |
52 | 515 void TypeInfoDelegateDeclaration::toDt(dt_t **pdt) |
516 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
517 Logger::println("TypeInfoDelegateDeclaration::toDt() %s", toChars()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
518 LOG_SCOPE; |
52 | 519 |
520 assert(tinfo->ty == Tdelegate); | |
521 TypeDelegate *tc = (TypeDelegate *)tinfo; | |
522 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
523 LLVM_D_Create_TypeInfoBase(tc->next->next, this, Type::typeinfodelegate); |
52 | 524 } |
525 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
526 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
527 |
52 | 528 void TypeInfoStructDeclaration::toDt(dt_t **pdt) |
529 { | |
72 | 530 Logger::println("TypeInfoStructDeclaration::toDt() %s", toChars()); |
531 LOG_SCOPE; | |
532 | |
533 assert(tinfo->ty == Tstruct); | |
534 TypeStruct *tc = (TypeStruct *)tinfo; | |
535 StructDeclaration *sd = tc->sym; | |
536 | |
537 ClassDeclaration* base = Type::typeinfostruct; | |
538 base->toObjFile(); | |
539 | |
540 const llvm::StructType* stype = llvm::cast<llvm::StructType>(base->llvmType); | |
541 | |
542 std::vector<llvm::Constant*> sinits; | |
543 sinits.push_back(base->llvmVtbl); | |
544 | |
545 // char[] name | |
546 char *name = sd->toPrettyChars(); | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
547 sinits.push_back(DtoConstString(name)); |
73 | 548 Logger::println("************** A"); |
72 | 549 assert(sinits.back()->getType() == stype->getElementType(1)); |
550 | |
551 // void[] init | |
552 const llvm::PointerType* initpt = llvm::PointerType::get(llvm::Type::Int8Ty); | |
553 if (sd->zeroInit) // 0 initializer, or the same as the base type | |
554 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
555 sinits.push_back(DtoConstSlice(DtoConstSize_t(0), llvm::ConstantPointerNull::get(initpt))); |
72 | 556 } |
557 else | |
558 { | |
559 assert(sd->llvmInitZ); | |
560 size_t cisize = gTargetData->getTypeSize(tc->llvmType); | |
561 llvm::Constant* cicast = llvm::ConstantExpr::getBitCast(tc->llvmInit, initpt); | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
562 sinits.push_back(DtoConstSlice(DtoConstSize_t(cisize), cicast)); |
72 | 563 } |
564 | |
565 // toX functions ground work | |
566 FuncDeclaration *fd; | |
567 FuncDeclaration *fdx; | |
568 TypeFunction *tf; | |
569 Type *ta; | |
570 Dsymbol *s; | |
571 | |
572 static TypeFunction *tftohash; | |
573 static TypeFunction *tftostring; | |
574 | |
575 if (!tftohash) | |
576 { | |
577 Scope sc; | |
578 | |
579 tftohash = new TypeFunction(NULL, Type::thash_t, 0, LINKd); | |
580 tftohash = (TypeFunction *)tftohash->semantic(0, &sc); | |
581 | |
582 tftostring = new TypeFunction(NULL, Type::tchar->arrayOf(), 0, LINKd); | |
583 tftostring = (TypeFunction *)tftostring->semantic(0, &sc); | |
584 } | |
585 | |
586 TypeFunction *tfeqptr; | |
587 { | |
588 Scope sc; | |
589 Arguments *arguments = new Arguments; | |
590 Argument *arg = new Argument(STCin, tc->pointerTo(), NULL, NULL); | |
591 | |
592 arguments->push(arg); | |
593 tfeqptr = new TypeFunction(arguments, Type::tint32, 0, LINKd); | |
594 tfeqptr = (TypeFunction *)tfeqptr->semantic(0, &sc); | |
595 } | |
596 | |
597 #if 0 | |
598 TypeFunction *tfeq; | |
599 { | |
600 Scope sc; | |
601 Array *arguments = new Array; | |
602 Argument *arg = new Argument(In, tc, NULL, NULL); | |
603 | |
604 arguments->push(arg); | |
605 tfeq = new TypeFunction(arguments, Type::tint32, 0, LINKd); | |
606 tfeq = (TypeFunction *)tfeq->semantic(0, &sc); | |
607 } | |
608 #endif | |
609 | |
73 | 610 Logger::println("************** B"); |
72 | 611 const llvm::PointerType* ptty = llvm::cast<llvm::PointerType>(stype->getElementType(3)); |
612 | |
613 s = search_function(sd, Id::tohash); | |
614 fdx = s ? s->isFuncDeclaration() : NULL; | |
615 if (fdx) | |
616 { | |
617 fd = fdx->overloadExactMatch(tftohash); | |
618 if (fd) { | |
619 assert(fd->llvmValue != 0); | |
620 llvm::Constant* c = llvm::cast_or_null<llvm::Constant>(fd->llvmValue); | |
621 assert(c); | |
622 c = llvm::ConstantExpr::getBitCast(c, ptty); | |
623 sinits.push_back(c); | |
624 } | |
625 else { | |
626 //fdx->error("must be declared as extern (D) uint toHash()"); | |
627 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
628 } | |
629 } | |
630 else { | |
631 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
632 } | |
633 | |
634 s = search_function(sd, Id::eq); | |
635 fdx = s ? s->isFuncDeclaration() : NULL; | |
636 for (int i = 0; i < 2; i++) | |
637 { | |
73 | 638 Logger::println("************** C %d", i); |
72 | 639 ptty = llvm::cast<llvm::PointerType>(stype->getElementType(4+i)); |
640 if (fdx) | |
641 { | |
642 fd = fdx->overloadExactMatch(tfeqptr); | |
643 if (fd) { | |
644 assert(fd->llvmValue != 0); | |
645 llvm::Constant* c = llvm::cast_or_null<llvm::Constant>(fd->llvmValue); | |
646 assert(c); | |
647 c = llvm::ConstantExpr::getBitCast(c, ptty); | |
648 sinits.push_back(c); | |
649 } | |
650 else { | |
651 //fdx->error("must be declared as extern (D) int %s(%s*)", fdx->toChars(), sd->toChars()); | |
652 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
653 } | |
654 } | |
655 else { | |
656 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
657 } | |
658 | |
659 s = search_function(sd, Id::cmp); | |
660 fdx = s ? s->isFuncDeclaration() : NULL; | |
661 } | |
662 | |
73 | 663 Logger::println("************** D"); |
72 | 664 ptty = llvm::cast<llvm::PointerType>(stype->getElementType(6)); |
665 s = search_function(sd, Id::tostring); | |
666 fdx = s ? s->isFuncDeclaration() : NULL; | |
667 if (fdx) | |
668 { | |
669 fd = fdx->overloadExactMatch(tftostring); | |
670 if (fd) { | |
671 assert(fd->llvmValue != 0); | |
672 llvm::Constant* c = llvm::cast_or_null<llvm::Constant>(fd->llvmValue); | |
673 assert(c); | |
674 c = llvm::ConstantExpr::getBitCast(c, ptty); | |
675 sinits.push_back(c); | |
676 } | |
677 else { | |
678 //fdx->error("must be declared as extern (D) char[] toString()"); | |
679 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
680 } | |
681 } | |
682 else { | |
683 sinits.push_back(llvm::ConstantPointerNull::get(ptty)); | |
684 } | |
685 | |
686 // uint m_flags; | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
73
diff
changeset
|
687 sinits.push_back(DtoConstUint(tc->hasPointers())); |
72 | 688 |
689 // create the symbol | |
690 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits); | |
73 | 691 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,tiInit,toChars(),gIR->module); |
72 | 692 |
693 llvmValue = gvar; | |
52 | 694 } |
695 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
696 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
697 |
52 | 698 void TypeInfoClassDeclaration::toDt(dt_t **pdt) |
699 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
700 assert(0 && "TypeInfoClassDeclaration"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
701 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
702 /* |
52 | 703 //printf("TypeInfoClassDeclaration::toDt() %s\n", tinfo->toChars()); |
704 dtxoff(pdt, Type::typeinfoclass->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoClass | |
705 dtdword(pdt, 0); // monitor | |
706 | |
707 assert(tinfo->ty == Tclass); | |
708 | |
709 TypeClass *tc = (TypeClass *)tinfo; | |
710 Symbol *s; | |
711 | |
712 if (!tc->sym->vclassinfo) | |
713 tc->sym->vclassinfo = new ClassInfoDeclaration(tc->sym); | |
714 s = tc->sym->vclassinfo->toSymbol(); | |
715 dtxoff(pdt, s, 0, TYnptr); // ClassInfo for tinfo | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
716 */ |
52 | 717 } |
718 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
719 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
720 |
52 | 721 void TypeInfoInterfaceDeclaration::toDt(dt_t **pdt) |
722 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
723 assert(0 && "TypeInfoInterfaceDeclaration"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
724 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
725 /* |
52 | 726 //printf("TypeInfoInterfaceDeclaration::toDt() %s\n", tinfo->toChars()); |
727 dtxoff(pdt, Type::typeinfointerface->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoInterface | |
728 dtdword(pdt, 0); // monitor | |
729 | |
730 assert(tinfo->ty == Tclass); | |
731 | |
732 TypeClass *tc = (TypeClass *)tinfo; | |
733 Symbol *s; | |
734 | |
735 if (!tc->sym->vclassinfo) | |
736 tc->sym->vclassinfo = new ClassInfoDeclaration(tc->sym); | |
737 s = tc->sym->vclassinfo->toSymbol(); | |
738 dtxoff(pdt, s, 0, TYnptr); // ClassInfo for tinfo | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
739 */ |
52 | 740 } |
741 | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
742 /* ========================================================================= */ |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
743 |
52 | 744 void TypeInfoTupleDeclaration::toDt(dt_t **pdt) |
745 { | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
746 assert(0 && "TypeInfoTupleDeclaration"); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
747 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
748 /* |
52 | 749 //printf("TypeInfoTupleDeclaration::toDt() %s\n", tinfo->toChars()); |
750 dtxoff(pdt, Type::typeinfotypelist->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoInterface | |
751 dtdword(pdt, 0); // monitor | |
752 | |
753 assert(tinfo->ty == Ttuple); | |
754 | |
755 TypeTuple *tu = (TypeTuple *)tinfo; | |
756 | |
757 size_t dim = tu->arguments->dim; | |
758 dtdword(pdt, dim); // elements.length | |
759 | |
760 dt_t *d = NULL; | |
761 for (size_t i = 0; i < dim; i++) | |
762 { Argument *arg = (Argument *)tu->arguments->data[i]; | |
763 Expression *e = arg->type->getTypeInfo(NULL); | |
764 e = e->optimize(WANTvalue); | |
765 e->toDt(&d); | |
766 } | |
767 | |
768 Symbol *s; | |
769 s = static_sym(); | |
770 s->Sdt = d; | |
771 outdata(s); | |
772 | |
773 dtxoff(pdt, s, 0, TYnptr); // elements.ptr | |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
774 */ |
52 | 775 } |