comparison generator/dgenerator.cpp @ 298:adae77fdc1ea signals

Native QList implementation is now used throughout QtD
author eldar
date Sun, 06 Dec 2009 17:26:37 +0000
parents bc783e20da2b
children 4d8481385c71
comparison
equal deleted inserted replaced
297:bc783e20da2b 298:adae77fdc1ea
174 const ContainerTypeEntry* c_entry = static_cast<const ContainerTypeEntry*>(d_type->typeEntry()); 174 const ContainerTypeEntry* c_entry = static_cast<const ContainerTypeEntry*>(d_type->typeEntry());
175 Q_ASSERT(c_entry); 175 Q_ASSERT(c_entry);
176 176
177 if ((option & SkipTemplateParameters) == 0) { 177 if ((option & SkipTemplateParameters) == 0) {
178 QList<AbstractMetaType *> args = d_type->instantiations(); 178 QList<AbstractMetaType *> args = d_type->instantiations();
179 179 const ContainerTypeEntry *cte =
180 static_cast<const ContainerTypeEntry *>(d_type->typeEntry());
180 if (args.size() == 1) { // QVector or QList 181 if (args.size() == 1) { // QVector or QList
181 if(d_type->typeEntry()->name() == "QList") 182 if(cte->isQList())
182 s = "QList!(" + translateType(args.at(0), context, BoxedPrimitive) + ")"; 183 s = "QList!(" + translateType(args.at(0), context, BoxedPrimitive) + ")";
183 else 184 else
184 s = translateType(args.at(0), context, BoxedPrimitive) + "[]"; 185 s = translateType(args.at(0), context, BoxedPrimitive) + "[]";
185 } 186 }
186 else if(args.size() == 2) { // all sorts of maps 187 else if(args.size() == 2) { // all sorts of maps
591 AbstractMetaArgument *arg = arguments.at(i); 592 AbstractMetaArgument *arg = arguments.at(i);
592 593
593 if (!d_function->argumentRemoved(i+1)) { 594 if (!d_function->argumentRemoved(i+1)) {
594 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, i+1); 595 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, i+1);
595 if (owner != TypeSystem::InvalidOwnership) { 596 if (owner != TypeSystem::InvalidOwnership) {
596 s << INDENT << "if (" << arg->argumentName() << " !is null) {" << endl; 597 QString empty_condition = " !is null";
598 if (arg->type()->isContainer())
599 empty_condition = ".length != 0";
600 s << INDENT << "if (" << arg->argumentName() << empty_condition << ") {" << endl;
597 { 601 {
598 Indentation indent(INDENT); 602 Indentation indent(INDENT);
599 if (arg->type()->isContainer()) 603 if (arg->type()->isContainer())
600 ;// qtd2 writeOwnershipForContainer(s, owner, arg); 604 ;// qtd2 writeOwnershipForContainer(s, owner, arg);
601 else 605 else
647 for (int i=0; i<arguments.size() + 1; ++i) { 651 for (int i=0; i<arguments.size() + 1; ++i) {
648 referenceCounts = d_function->referenceCounts(d_function->implementingClass(), 652 referenceCounts = d_function->referenceCounts(d_function->implementingClass(),
649 i == 0 ? -1 : i); 653 i == 0 ? -1 : i);
650 654
651 foreach (ReferenceCount refCount, referenceCounts) 655 foreach (ReferenceCount refCount, referenceCounts)
652 writeReferenceCount(s, refCount, i == 0 ? "this" : arguments.at(i-1)->argumentName()); 656 writeReferenceCount(s, refCount, i == 0 ? "this" : arguments.at(i-1)->argumentName(), arguments.at(i-1)->type());
653 } 657 }
654 658
655 referenceCounts = d_function->referenceCounts(d_function->implementingClass(), 0); 659 referenceCounts = d_function->referenceCounts(d_function->implementingClass(), 0);
656 AbstractMetaType *return_type = d_function->type(); 660 AbstractMetaType *return_type = d_function->type();
657 QString new_return_type = QString(d_function->typeReplaced(0)).replace('$', '.'); 661 QString new_return_type = QString(d_function->typeReplaced(0)).replace('$', '.');
682 else if (return_type->typeEntry()->isStructInD()) 686 else if (return_type->typeEntry()->isStructInD())
683 s << INDENT << return_type->name() << " res;" << endl; 687 s << INDENT << return_type->name() << " res;" << endl;
684 688
685 if(return_type->isContainer()) 689 if(return_type->isContainer())
686 { 690 {
687 if(d_function->type()->typeEntry()->name() == "QList") 691 const ContainerTypeEntry *type =
692 static_cast<const ContainerTypeEntry *>(return_type->typeEntry());
693 if(type->isQList()) // QList is a native type now
688 s << INDENT << "auto res = " << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << ".opCall();" << endl; 694 s << INDENT << "auto res = " << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << ".opCall();" << endl;
689 else 695 else
690 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl; 696 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl;
691 } 697 }
692 } 698 }
819 s << arg_name << ".ptr"; 825 s << arg_name << ".ptr";
820 } else if(type->isContainer()) { 826 } else if(type->isContainer()) {
821 const ContainerTypeEntry *cte = 827 const ContainerTypeEntry *cte =
822 static_cast<const ContainerTypeEntry *>(te); 828 static_cast<const ContainerTypeEntry *>(te);
823 if(isLinearContainer(cte)) 829 if(isLinearContainer(cte))
824 s << QString("%1.ptr, %1.length").arg(arg_name); 830 s << QString("&%1").arg(arg_name);
825 } else if (type->typeEntry()->qualifiedCppName() == "QChar") { 831 } else if (type->typeEntry()->qualifiedCppName() == "QChar") {
826 s << arg_name; 832 s << arg_name;
827 } else if (type->isTargetLangString() || (te && te->qualifiedCppName() == "QString")) { 833 } else if (type->isTargetLangString() || (te && te->qualifiedCppName() == "QString")) {
828 s << arg_name; 834 s << arg_name;
829 } else if (type->isTargetLangEnum() || type->isTargetLangFlags()) { 835 } else if (type->isTargetLangEnum() || type->isTargetLangFlags()) {
923 if (return_type->isArray()) { 929 if (return_type->isArray()) {
924 s << "ret[0 .. " << return_type->arrayElementCount() << "];" << endl; 930 s << "ret[0 .. " << return_type->arrayElementCount() << "];" << endl;
925 } 931 }
926 932
927 foreach (ReferenceCount referenceCount, referenceCounts) { 933 foreach (ReferenceCount referenceCount, referenceCounts) {
928 writeReferenceCount(s, referenceCount, "__d_return_value"); 934 writeReferenceCount(s, referenceCount, "__d_return_value", return_type);
929 } 935 }
930 936
931 if (!returnImmediately) 937 if (!returnImmediately)
932 s << INDENT << "return __d_return_value;" << endl; 938 s << INDENT << "return __d_return_value;" << endl;
933 } 939 }
1013 const AbstractMetaClass *d_class = d_function->ownerClass(); 1019 const AbstractMetaClass *d_class = d_function->ownerClass();
1014 retrieveModifications(d_function, d_class, excluded_attributes, included_attributes); 1020 retrieveModifications(d_function, d_class, excluded_attributes, included_attributes);
1015 } 1021 }
1016 1022
1017 void DGenerator::writeReferenceCount(QTextStream &s, const ReferenceCount &refCount, 1023 void DGenerator::writeReferenceCount(QTextStream &s, const ReferenceCount &refCount,
1018 const QString &argumentName) 1024 const QString &argumentName, AbstractMetaType *argumentType)
1019 { 1025 {
1020 if (refCount.action == ReferenceCount::Ignore) 1026 if (refCount.action == ReferenceCount::Ignore)
1021 return; 1027 return;
1022 1028
1023 QString refCountVariableName = refCount.variableName; 1029 QString refCountVariableName = refCount.variableName;
1024 if (!refCount.declareVariable.isEmpty() && refCount.action != ReferenceCount::Set) { 1030 if (!refCount.declareVariable.isEmpty() && refCount.action != ReferenceCount::Set) {
1025 s << INDENT << "auto __rcTmp = " << refCountVariableName << ";" << endl; 1031 s << INDENT << "auto __rcTmp = " << refCountVariableName << ";" << endl;
1026 refCountVariableName = "__rcTmp"; 1032 refCountVariableName = "__rcTmp";
1027 } 1033 }
1034 QString empty_condition = " !is null";
1035 if (argumentType && argumentType->isContainer())
1036 // if (((const ContainerTypeEntry *)argumentType->typeEntry())->isQList())
1037 empty_condition = ".length != 0";
1028 1038
1029 if (refCount.action != ReferenceCount::Set) { 1039 if (refCount.action != ReferenceCount::Set) {
1030 s << INDENT << "if (" << argumentName << " !is null"; 1040 s << INDENT << "if (" << argumentName << empty_condition;
1031 1041
1032 if (!refCount.conditional.isEmpty()) 1042 if (!refCount.conditional.isEmpty())
1033 s << " && " << refCount.conditional; 1043 s << " && " << refCount.conditional;
1034 1044
1035 s << ") {" << endl; 1045 s << ") {" << endl;
1039 s << INDENT << "{" << endl; 1049 s << INDENT << "{" << endl;
1040 } 1050 }
1041 1051
1042 { 1052 {
1043 Indentation indent(INDENT); 1053 Indentation indent(INDENT);
1054 QString summand = argumentName;
1044 switch (refCount.action) { 1055 switch (refCount.action) {
1045 case ReferenceCount::Add: 1056 case ReferenceCount::Add:
1046 s << INDENT << refCountVariableName << " ~= cast(Object) " << argumentName << ";" << endl; 1057 s << INDENT << refCountVariableName << " ~= cast(Object) " << argumentName << ";" << endl;
1047 break; 1058 break;
1048 case ReferenceCount::AddAll: 1059 case ReferenceCount::AddAll:
1049 s << INDENT << refCountVariableName << " ~= " << argumentName << ";" << endl; 1060 if(isNativeContainer(argumentType))
1061 summand = argumentName + ".toArray()";
1062 s << INDENT << refCountVariableName << " ~= " << summand << ";" << endl;
1050 break; 1063 break;
1051 case ReferenceCount::Remove: 1064 case ReferenceCount::Remove:
1052 s << INDENT << "remove(" << refCountVariableName 1065 s << INDENT << "remove(" << refCountVariableName
1053 << ", cast(Object) " << argumentName << ");" << endl; 1066 << ", cast(Object) " << argumentName << ");" << endl;
1054 break; 1067 break;
1945 if (!m_isRecursive) { 1958 if (!m_isRecursive) {
1946 s << "public import qt.QGlobal;" << endl 1959 s << "public import qt.QGlobal;" << endl
1947 << "public import qt.core.Qt;" << endl 1960 << "public import qt.core.Qt;" << endl
1948 << "private import qt.QtdObject;" << endl 1961 << "private import qt.QtdObject;" << endl
1949 << "private import qt.core.QString;" << endl 1962 << "private import qt.core.QString;" << endl
1950 << "private import qt.qtd.Array;" << endl; 1963 << "private import qt.qtd.Array;" << endl
1964 << "private import qt.core.QList;" << endl;
1951 if (d_class->isQObject()) { 1965 if (d_class->isQObject()) {
1952 s << "public import qt.Signal;" << endl 1966 s << "public import qt.Signal;" << endl
1953 << "public import qt.qtd.MOC;" << endl 1967 << "public import qt.qtd.MOC;" << endl
1954 << "public import qt.core.QMetaObject;" << endl 1968 << "public import qt.core.QMetaObject;" << endl
1955 << "public import qt.qtd.Traits;" << endl; 1969 << "public import qt.qtd.Traits;" << endl;
2269 } 2283 }
2270 2284
2271 s << INDENT << "public alias void __isQtType_" << d_class->name() << ";" << endl << endl; 2285 s << INDENT << "public alias void __isQtType_" << d_class->name() << ";" << endl << endl;
2272 2286
2273 // construction of a native copy of a Value 2287 // construction of a native copy of a Value
2274 if (d_class->typeEntry()->isValue() && d_class->hasCloneOperator()) 2288 if (d_class->typeEntry()->isValue())
2275 { 2289 {
2276 AbstractMetaFunction *copyCtor = d_class->copyConstructor();
2277 if(copyCtor)
2278 s << INDENT << "static void* __constructNativeCopy(const void* orig) {" << endl 2290 s << INDENT << "static void* __constructNativeCopy(const void* orig) {" << endl
2279 << INDENT << " return " << copyCtor->marshalledName() << "(cast(void*)orig);" << endl 2291 << INDENT << " return qtd_" << d_class->name() << "_native_copy(orig);" << endl
2280 << INDENT << "}" << endl << endl 2292 << INDENT << "}" << endl << endl
2281 2293
2282 << INDENT << "static void* __constructPlacedNativeCopy(const void* orig, void* place) {" << endl 2294 << INDENT << "static void __constructPlacedNativeCopy(const void* orig, void* place) {" << endl
2283 << INDENT << " return qtd_" << d_class->name() << "_placed_copy(orig, place);" << endl 2295 << INDENT << " qtd_" << d_class->name() << "_placed_copy(orig, place);" << endl
2284 << INDENT << "}" << endl << endl; 2296 << INDENT << "}" << endl << endl;
2285 } 2297 }
2286 2298
2287 // Add dummy constructor for use when constructing subclasses 2299 // Add dummy constructor for use when constructing subclasses
2288 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) { 2300 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) {
2454 if (d_class->hasVirtualFunctions() 2466 if (d_class->hasVirtualFunctions()
2455 && (d_class->typeEntry()->isObject() && !d_class->typeEntry()->isQObject()) ) 2467 && (d_class->typeEntry()->isObject() && !d_class->typeEntry()->isQObject()) )
2456 s << endl << "extern (C) void *__" << d_class->name() << "_entity(void *q_ptr);" << endl << endl; 2468 s << endl << "extern (C) void *__" << d_class->name() << "_entity(void *q_ptr);" << endl << endl;
2457 } 2469 }
2458 2470
2459 if (d_class->typeEntry()->isValue() && d_class->hasCloneOperator()) 2471 if (d_class->typeEntry()->isValue())
2460 { 2472 {
2461 AbstractMetaFunction *copyCtor = d_class->copyConstructor(); 2473 {
2462 if(copyCtor) 2474 s << "private extern(C) void qtd_" << d_class->name() << "_placed_copy(const void* orig, void* place);" << endl
2463 s << "private extern(C) void* qtd_" << d_class->name() << "_placed_copy(const void* orig, void* place);" << endl << endl; 2475 << "private extern(C) void* qtd_" << d_class->name() << "_native_copy(const void* orig);" << endl;
2476 }
2464 } 2477 }
2465 2478
2466 // if (d_class->needsConversionFunc) 2479 // if (d_class->needsConversionFunc)
2467 writeConversionFunction(s, d_class); 2480 writeConversionFunction(s, d_class);
2468 2481
2964 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { 2977 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) {
2965 QString arg_name = argument->indexedName(); 2978 QString arg_name = argument->indexedName();
2966 AbstractMetaType *type = argument->type(); 2979 AbstractMetaType *type = argument->type();
2967 // if has QString argument we have to pass char* and str.length to QString constructor 2980 // if has QString argument we have to pass char* and str.length to QString constructor
2968 { 2981 {
2969 if(type->isEnum()) 2982 if (type->isContainer())
2983 {
2984 if ( ((const ContainerTypeEntry *)type->typeEntry())->isQList() ) {
2985 s << INDENT;
2986 s << "auto " << arg_name << "_d_ref = cast(" << translateType(type, implementor) << "*)" << arg_name << ";" << endl;
2987 }
2988 } else if(type->isEnum())
2970 s << INDENT << "auto " << arg_name << "_enum = cast(" 2989 s << INDENT << "auto " << arg_name << "_enum = cast("
2971 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";"; 2990 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";";
2972 else if (type->typeEntry()->qualifiedCppName() == "QChar") 2991 else if (type->typeEntry()->qualifiedCppName() == "QChar")
2973 s << INDENT << "auto " << arg_name << "_d_ref = cast(wchar" << QString(type->actualIndirections(), '*') 2992 s << INDENT << "auto " << arg_name << "_d_ref = cast(wchar" << QString(type->actualIndirections(), '*')
2974 << ") " << arg_name << ";"; 2993 << ") " << arg_name << ";";
3042 if (!modified_type.isEmpty()) 3061 if (!modified_type.isEmpty())
3043 modified_type = modified_type.replace('$', '.'); 3062 modified_type = modified_type.replace('$', '.');
3044 3063
3045 if (modified_type == "string" /* && type->fullName() == "char" */) 3064 if (modified_type == "string" /* && type->fullName() == "char" */)
3046 s << "fromStringz(" << arg_name << ")"; 3065 s << "fromStringz(" << arg_name << ")";
3047 else { 3066 else
3067 {
3048 if(type->isContainer() 3068 if(type->isContainer()
3049 || (type->isReference() && type->typeEntry()->isStructInD())) 3069 || (type->isReference() && type->typeEntry()->isStructInD()))
3050 s << "*"; 3070 s << "*";
3051 s << arg_name; 3071 s << arg_name;
3072 if (type->isContainer() && ((const ContainerTypeEntry *)type->typeEntry())->isQList() )
3073 s << "_d_ref";
3052 } 3074 }
3053 if (type->typeEntry()->isStructInD()) ; 3075 if (type->typeEntry()->isStructInD()) ;
3054 else if (type->isQObject() || type->isObject() 3076 else if (type->isQObject() || type->isObject()
3055 || (type->typeEntry()->isValue() && type->isNativePointer()) 3077 || (type->typeEntry()->isValue() && type->isNativePointer())
3056 || type->isValue() 3078 || type->isValue()
3082 if (f_type->typeEntry()->designatedInterface()) 3104 if (f_type->typeEntry()->designatedInterface())
3083 native_id = "__ptr_" + f_type->typeEntry()->designatedInterface()->name(); 3105 native_id = "__ptr_" + f_type->typeEntry()->designatedInterface()->name();
3084 s << INDENT << "return ret_value is null? null : ret_value." << native_id << ";" << endl; 3106 s << INDENT << "return ret_value is null? null : ret_value." << native_id << ";" << endl;
3085 } else if (f_type->isTargetLangString()) 3107 } else if (f_type->isTargetLangString())
3086 s << INDENT << "*ret_str = _d_str;" << endl; 3108 s << INDENT << "*ret_str = _d_str;" << endl;
3087 else if (f_type->isContainer()) 3109 else if (f_type->isContainer()) {
3088 s << INDENT << "*__d_arr_ptr = return_value.ptr;" << endl 3110 if (isNativeContainer(f_type))
3089 << INDENT << "*__d_arr_size = return_value.length;" << endl; 3111 s << INDENT << "*__d_arr = return_value;" << endl;
3090 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) 3112 else
3113 s << INDENT << "*__d_arr_ptr = return_value.ptr;" << endl
3114 << INDENT << "*__d_arr_size = return_value.length;" << endl;
3115 } else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD())
3091 ; 3116 ;
3092 else 3117 else
3093 s << INDENT << "return return_value;" << endl; 3118 s << INDENT << "return return_value;" << endl;
3094 } else 3119 } else
3095 s << INDENT << "return return_value;" << endl; 3120 s << INDENT << "return return_value;" << endl;