comparison gen/typinf.cpp @ 113:27b9f749d9fe trunk

[svn r117] Initial working implementation of interfaces. Groundwork for all the different types of class/interface casts laid out.
author lindquist
date Sat, 24 Nov 2007 06:33:00 +0100
parents e8da7856a260
children 5ba6d286c941
comparison
equal deleted inserted replaced
112:368547b1cbe6 113:27b9f749d9fe
1027 1027
1028 /* ========================================================================= */ 1028 /* ========================================================================= */
1029 1029
1030 void TypeInfoInterfaceDeclaration::llvmDeclare() 1030 void TypeInfoInterfaceDeclaration::llvmDeclare()
1031 { 1031 {
1032 assert(0 && "TypeInfoTupleDeclaration"); 1032 Logger::println("TypeInfoInterfaceDeclaration::llvmDeclare() %s", toChars());
1033 LOG_SCOPE;
1034
1035 // init typeinfo class
1036 ClassDeclaration* base = Type::typeinfointerface;
1037 assert(base);
1038 DtoResolveClass(base);
1039
1040 // get type of typeinfo class
1041 const llvm::StructType* stype = isaStruct(base->type->llvmType->get());
1042
1043 // create the symbol
1044 llvmValue = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,NULL,toChars(),gIR->module);
1033 } 1045 }
1034 1046
1035 void TypeInfoInterfaceDeclaration::llvmDefine() 1047 void TypeInfoInterfaceDeclaration::llvmDefine()
1036 { 1048 {
1037 assert(0 && "TypeInfoTupleDeclaration"); 1049 Logger::println("TypeInfoInterfaceDeclaration::llvmDefine() %s", toChars());
1050 LOG_SCOPE;
1051
1052 // init typeinfo class
1053 ClassDeclaration* base = Type::typeinfointerface;
1054 assert(base);
1055 DtoForceConstInitDsymbol(base);
1056
1057 // get type of typeinfo class
1058 const llvm::StructType* stype = isaStruct(base->type->llvmType->get());
1059
1060 // initializer vector
1061 std::vector<llvm::Constant*> sinits;
1062 // first is always the vtable
1063 sinits.push_back(base->llvmVtbl);
1064
1065 // get classinfo
1066 assert(tinfo->ty == Tclass);
1067 TypeClass *tc = (TypeClass *)tinfo;
1068 assert(tc->sym->llvmClass);
1069 sinits.push_back(tc->sym->llvmClass);
1070
1071 // create the symbol
1072 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits);
1073 isaGlobalVar(llvmValue)->setInitializer(tiInit);
1038 } 1074 }
1039 1075
1040 void TypeInfoInterfaceDeclaration::toDt(dt_t **pdt) 1076 void TypeInfoInterfaceDeclaration::toDt(dt_t **pdt)
1041 { 1077 {
1042 assert(0 && "TypeInfoInterfaceDeclaration"); 1078 assert(0);
1043
1044 /*
1045 //printf("TypeInfoInterfaceDeclaration::toDt() %s\n", tinfo->toChars());
1046 dtxoff(pdt, Type::typeinfointerface->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoInterface
1047 dtdword(pdt, 0); // monitor
1048
1049 assert(tinfo->ty == Tclass);
1050
1051 TypeClass *tc = (TypeClass *)tinfo;
1052 Symbol *s;
1053
1054 if (!tc->sym->vclassinfo)
1055 tc->sym->vclassinfo = new ClassInfoDeclaration(tc->sym);
1056 s = tc->sym->vclassinfo->toSymbol();
1057 dtxoff(pdt, s, 0, TYnptr); // ClassInfo for tinfo
1058 */
1059 } 1079 }
1060 1080
1061 /* ========================================================================= */ 1081 /* ========================================================================= */
1062 1082
1063 void TypeInfoTupleDeclaration::llvmDeclare() 1083 void TypeInfoTupleDeclaration::llvmDeclare()
1064 { 1084 {
1065 assert(0 && "TypeInfoTupleDeclaration"); 1085 Logger::println("TypeInfoTupleDeclaration::llvmDeclare() %s", toChars());
1086 LOG_SCOPE;
1087
1088 // init typeinfo class
1089 ClassDeclaration* base = Type::typeinfotypelist;
1090 assert(base);
1091 DtoResolveClass(base);
1092
1093 // get type of typeinfo class
1094 const llvm::StructType* stype = isaStruct(base->type->llvmType->get());
1095
1096 // create the symbol
1097 llvmValue = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::WeakLinkage,NULL,toChars(),gIR->module);
1066 } 1098 }
1067 1099
1068 void TypeInfoTupleDeclaration::llvmDefine() 1100 void TypeInfoTupleDeclaration::llvmDefine()
1069 { 1101 {
1070 assert(0 && "TypeInfoTupleDeclaration"); 1102 Logger::println("TypeInfoTupleDeclaration::llvmDefine() %s", toChars());
1103 LOG_SCOPE;
1104
1105 // init typeinfo class
1106 ClassDeclaration* base = Type::typeinfotypelist;
1107 assert(base);
1108 DtoForceConstInitDsymbol(base);
1109
1110 // get type of typeinfo class
1111 const llvm::StructType* stype = isaStruct(base->type->llvmType->get());
1112
1113 // initializer vector
1114 std::vector<llvm::Constant*> sinits;
1115 // first is always the vtable
1116 sinits.push_back(base->llvmVtbl);
1117
1118 // create elements array
1119 assert(tinfo->ty == Ttuple);
1120 TypeTuple *tu = (TypeTuple *)tinfo;
1121
1122 size_t dim = tu->arguments->dim;
1123 std::vector<llvm::Constant*> arrInits;
1124
1125 const llvm::Type* tiTy = Type::typeinfo->type->llvmType->get();
1126 tiTy = llvm::PointerType::get(tiTy);
1127
1128 for (size_t i = 0; i < dim; i++)
1129 {
1130 Argument *arg = (Argument *)tu->arguments->data[i];
1131 arg->type->getTypeInfo(NULL);
1132 DtoForceDeclareDsymbol(arg->type->vtinfo);
1133 assert(arg->type->vtinfo->llvmValue);
1134 llvm::Constant* c = isaConstant(arg->type->vtinfo->llvmValue);
1135 c = llvm::ConstantExpr::getBitCast(c, tiTy);
1136 arrInits.push_back(c);
1137 }
1138
1139 // build array type
1140 const llvm::ArrayType* arrTy = llvm::ArrayType::get(tiTy, dim);
1141 llvm::Constant* arrC = llvm::ConstantArray::get(arrTy, arrInits);
1142
1143 // build the slice
1144 llvm::Constant* slice = DtoConstSlice(DtoConstSize_t(dim), arrC);
1145 sinits.push_back(slice);
1146
1147 // create the symbol
1148 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits);
1149 isaGlobalVar(llvmValue)->setInitializer(tiInit);
1071 } 1150 }
1072 1151
1073 void TypeInfoTupleDeclaration::toDt(dt_t **pdt) 1152 void TypeInfoTupleDeclaration::toDt(dt_t **pdt)
1074 { 1153 {
1075 assert(0 && "TypeInfoTupleDeclaration"); 1154 assert(0);
1076 1155 }
1077 /*
1078 //printf("TypeInfoTupleDeclaration::toDt() %s\n", tinfo->toChars());
1079 dtxoff(pdt, Type::typeinfotypelist->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoInterface
1080 dtdword(pdt, 0); // monitor
1081
1082 assert(tinfo->ty == Ttuple);
1083
1084 TypeTuple *tu = (TypeTuple *)tinfo;
1085
1086 size_t dim = tu->arguments->dim;
1087 dtdword(pdt, dim); // elements.length
1088
1089 dt_t *d = NULL;
1090 for (size_t i = 0; i < dim; i++)
1091 { Argument *arg = (Argument *)tu->arguments->data[i];
1092 Expression *e = arg->type->getTypeInfo(NULL);
1093 e = e->optimize(WANTvalue);
1094 e->toDt(&d);
1095 }
1096
1097 Symbol *s;
1098 s = static_sym();
1099 s->Sdt = d;
1100 outdata(s);
1101
1102 dtxoff(pdt, s, 0, TYnptr); // elements.ptr
1103 */
1104 }