comparison gen/classes.cpp @ 103:855adfdb8d38 trunk

[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
author lindquist
date Sun, 18 Nov 2007 08:25:07 +0100
parents 027b8d8b71ec
children 5b5194b25f33
comparison
equal deleted inserted replaced
102:027b8d8b71ec 103:855adfdb8d38
44 44
45 // first resolve the base class 45 // first resolve the base class
46 if (cd->baseClass) { 46 if (cd->baseClass) {
47 DtoResolveClass(cd->baseClass); 47 DtoResolveClass(cd->baseClass);
48 } 48 }
49 // resolve typeinfo
50 //DtoResolveClass(ClassDeclaration::typeinfo);
51 // resolve classinfo
52 //DtoResolveClass(ClassDeclaration::classinfo);
49 53
50 Logger::println("DtoResolveClass(%s)", cd->toPrettyChars()); 54 Logger::println("DtoResolveClass(%s)", cd->toPrettyChars());
51 LOG_SCOPE; 55 LOG_SCOPE;
52 56
53 assert(cd->type->ty == Tclass); 57 assert(cd->type->ty == Tclass);
87 91
88 llvm::PATypeHolder& spa = irstruct->recty; 92 llvm::PATypeHolder& spa = irstruct->recty;
89 llvm::cast<llvm::OpaqueType>(spa.get())->refineAbstractTypeTo(structtype); 93 llvm::cast<llvm::OpaqueType>(spa.get())->refineAbstractTypeTo(structtype);
90 structtype = isaStruct(spa.get()); 94 structtype = isaStruct(spa.get());
91 95
92 ts->llvmType = new llvm::PATypeHolder(structtype); 96 if (!ts->llvmType)
97 ts->llvmType = new llvm::PATypeHolder(structtype);
98 else
99 *ts->llvmType = structtype;
93 100
94 bool needs_definition = false; 101 bool needs_definition = false;
95 if (cd->parent->isModule()) { 102 if (cd->parent->isModule()) {
96 gIR->module->addTypeName(cd->mangle(), ts->llvmType->get()); 103 gIR->module->addTypeName(cd->mangle(), ts->llvmType->get());
97 needs_definition = (cd->getModule() == gIR->dmodule); 104 needs_definition = (cd->getModule() == gIR->dmodule);
116 TypeFunction* tf = (TypeFunction*)fd->type; 123 TypeFunction* tf = (TypeFunction*)fd->type;
117 const llvm::Type* fpty = llvm::PointerType::get(tf->llvmType->get()); 124 const llvm::Type* fpty = llvm::PointerType::get(tf->llvmType->get());
118 sinits_ty.push_back(fpty); 125 sinits_ty.push_back(fpty);
119 } 126 }
120 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { 127 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) {
121 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); 128 //Logger::println("*** ClassDeclaration in vtable: %s", cd->toChars());
129 const llvm::Type* cinfoty;
130 if (cd != ClassDeclaration::classinfo) {
131 cd = ClassDeclaration::classinfo;
132 DtoResolveClass(cd);
133 cinfoty = cd->type->llvmType->get();
134 }
135 else {
136 cinfoty = ts->llvmType->get();
137 }
138 const llvm::Type* cty = llvm::PointerType::get(cd->type->llvmType->get());
122 sinits_ty.push_back(cty); 139 sinits_ty.push_back(cty);
123 } 140 }
124 else 141 else
125 assert(0); 142 assert(0);
126 } 143 }
190 gIR->structs.pop_back(); 207 gIR->structs.pop_back();
191 208
192 gIR->constInitList.push_back(cd); 209 gIR->constInitList.push_back(cd);
193 if (needs_definition) 210 if (needs_definition)
194 gIR->defineList.push_back(cd); 211 gIR->defineList.push_back(cd);
212
213 // classinfo
214 DtoDeclareClassInfo(cd);
195 } 215 }
196 216
197 ////////////////////////////////////////////////////////////////////////////////////////// 217 //////////////////////////////////////////////////////////////////////////////////////////
198 218
199 void DtoConstInitClass(ClassDeclaration* cd) 219 void DtoConstInitClass(ClassDeclaration* cd)
234 assert(cd->type->ty == Tclass); 254 assert(cd->type->ty == Tclass);
235 TypeClass* ts = (TypeClass*)cd->type; 255 TypeClass* ts = (TypeClass*)cd->type;
236 const llvm::StructType* structtype = isaStruct(ts->llvmType->get()); 256 const llvm::StructType* structtype = isaStruct(ts->llvmType->get());
237 257
238 // generate initializer 258 // generate initializer
239 Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n'; 259 /*Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n';
240 260
241 for(size_t i=0; i<structtype->getNumElements(); ++i) { 261 for(size_t i=0; i<structtype->getNumElements(); ++i) {
242 Logger::cout() << "s#" << i << " = " << *structtype->getElementType(i) << '\n'; 262 Logger::cout() << "s#" << i << " = " << *structtype->getElementType(i) << '\n';
243 } 263 }
244 264
245 for(size_t i=0; i<fieldinits.size(); ++i) { 265 for(size_t i=0; i<fieldinits.size(); ++i) {
246 Logger::cout() << "i#" << i << " = " << *fieldinits[i]->getType() << '\n'; 266 Logger::cout() << "i#" << i << " = " << *fieldinits[i]->getType() << '\n';
247 } 267 }*/
248 268
249 llvm::Constant* _init = llvm::ConstantStruct::get(structtype, fieldinits); 269 llvm::Constant* _init = llvm::ConstantStruct::get(structtype, fieldinits);
250 assert(_init); 270 assert(_init);
251 cd->llvmInitZ = _init; 271 cd->llvmInitZ = _init;
252 272
263 DtoForceDeclareDsymbol(fd); 283 DtoForceDeclareDsymbol(fd);
264 assert(fd->llvmValue); 284 assert(fd->llvmValue);
265 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); 285 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue);
266 sinits.push_back(c); 286 sinits.push_back(c);
267 } 287 }
268 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { 288 else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) {
269 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); 289 assert(cd->llvmClass);
270 llvm::Constant* c = llvm::Constant::getNullValue(cty); 290 llvm::Constant* c = cd->llvmClass;
271 sinits.push_back(c); 291 sinits.push_back(c);
272 } 292 }
273 else 293 else
274 assert(0); 294 assert(0);
275 } 295 }
286 llvm::Constant* cvtblInit = llvm::ConstantStruct::get(svtbl_ty, sinits); 306 llvm::Constant* cvtblInit = llvm::ConstantStruct::get(svtbl_ty, sinits);
287 cd->llvmConstVtbl = llvm::cast<llvm::ConstantStruct>(cvtblInit); 307 cd->llvmConstVtbl = llvm::cast<llvm::ConstantStruct>(cvtblInit);
288 308
289 gIR->classes.pop_back(); 309 gIR->classes.pop_back();
290 gIR->structs.pop_back(); 310 gIR->structs.pop_back();
291
292 DtoDeclareClassInfo(cd);
293 } 311 }
294 312
295 ////////////////////////////////////////////////////////////////////////////////////////// 313 //////////////////////////////////////////////////////////////////////////////////////////
296 314
297 void DtoDefineClass(ClassDeclaration* cd) 315 void DtoDefineClass(ClassDeclaration* cd)