comparison generator/dgenerator.cpp @ 130:7ae9bc9d6935

reference counting for return values. applied to QMenu
author eldar
date Sun, 07 Jun 2009 11:42:29 +0000
parents e416c5215366
children 4d1c5d1d1bbf
comparison
equal deleted inserted replaced
129:9c97adeef763 130:7ae9bc9d6935
673 673
674 if(return_type->isContainer()) 674 if(return_type->isContainer())
675 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl; 675 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl;
676 } 676 }
677 677
678 //returning string or a struct 678 // returning string or a struct
679 bool return_in_arg = return_type && (return_type->isTargetLangString() || 679 bool return_in_arg = return_type && (return_type->isTargetLangString() ||
680 return_type->name() == "QModelIndex" || 680 return_type->name() == "QModelIndex" ||
681 return_type->isContainer() || 681 return_type->isContainer() ||
682 return_type->typeEntry()->isStructInD()); 682 return_type->typeEntry()->isStructInD());
683 683
684 // bool flag showing if we return value immediately, without any conversions
685 // which is commpon for primitive types, initially set up to return_in_arg, because in that case
686 // we don't need type conversions
687 bool returnImmediately = return_in_arg;
684 688
685 s << INDENT; 689 s << INDENT;
686 if ( (has_return_type && d_function->argumentReplaced(0).isEmpty() ) || d_function->isConstructor()) { //qtd 690 if ( (has_return_type && d_function->argumentReplaced(0).isEmpty() ) || d_function->isConstructor()) { //qtd
687 if(d_function->type() && d_function->type()->isQObject()) { // qtd 691 if(d_function->type() && d_function->type()->isQObject()) { // qtd
688 s << "void *__qt_return_value = "; 692 s << "void *__qt_return_value = ";
699 return_type->typeEntry()->isInterface()) ) { 703 return_type->typeEntry()->isInterface()) ) {
700 s << "void* __qt_return_value = "; 704 s << "void* __qt_return_value = ";
701 } else if (return_type && return_type->isArray()) { 705 } else if (return_type && return_type->isArray()) {
702 s << return_type->arrayElementType()->name() + "* __qt_return_value = "; 706 s << return_type->arrayElementType()->name() + "* __qt_return_value = ";
703 } else { 707 } else {
708 returnImmediately = true;
704 s << "return "; 709 s << "return ";
705 } 710 }
706 711
707 if (return_type && return_type->isTargetLangEnum()) { 712 if (return_type && return_type->isTargetLangEnum()) {
708 s << "cast(" << return_type->typeEntry()->qualifiedTargetLangName() << ") "; 713 s << "cast(" << return_type->typeEntry()->qualifiedTargetLangName() << ") ";
838 } 843 }
839 844
840 // qtd2 if (return_type && (/* qtdreturn_type->isTargetLangEnum() ||*/ return_type->isTargetLangFlags())) 845 // qtd2 if (return_type && (/* qtdreturn_type->isTargetLangEnum() ||*/ return_type->isTargetLangFlags()))
841 // s << ")"; 846 // s << ")";
842 847
843 foreach (ReferenceCount referenceCount, referenceCounts) {
844 writeReferenceCount(s, referenceCount, "__qt_return_value");
845 }
846
847 s << ";" << endl; 848 s << ";" << endl;
848 849
849 // return value marschalling
850 if(return_type) {
851 if ( ( has_return_type && d_function->argumentReplaced(0).isEmpty() )) // qtd
852 if(return_type->isQObject())
853 s << INDENT << "return qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl;
854
855 if (return_type->isValue() && !return_type->typeEntry()->isStructInD())
856 s << INDENT << "return new " << return_type->name() << "(__qt_return_value, false);" << endl;
857
858 if (return_type->isVariant())
859 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl;
860
861 if (return_type->isNativePointer() && return_type->typeEntry()->isValue())
862 s << INDENT << "return new " << return_type->name() << "(__qt_return_value, true);" << endl;
863
864 if (return_type->isObject()) {
865 if(d_function->storeResult())
866 s << INDENT << QString("__m_%1.nativeId = __qt_return_value;").arg(d_function->name()) << endl
867 << INDENT << QString("return __m_%1;").arg(d_function->name()) << endl;
868 else
869 s << INDENT << "return qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl;
870 s << endl;
871 }
872
873 if (return_type->isArray()) {
874 s << INDENT << "return __qt_return_value[0 .. " << return_type->arrayElementCount() << "];" << endl;
875 }
876 }
877 writeInjectedCode(s, d_function, CodeSnip::End);
878 /* qtd2 850 /* qtd2
879 if (needs_return_variable) { 851 if (needs_return_variable) {
880 if (owner != TypeSystem::InvalidOwnership) { 852 if (owner != TypeSystem::InvalidOwnership) {
881 s << INDENT << "if (__qt_return_value != null) {" << endl; 853 s << INDENT << "if (__qt_return_value != null) {" << endl;
882 if (return_type->isContainer()) 854 if (return_type->isContainer())
891 if (d_function->isConstructor()) { 863 if (d_function->isConstructor()) {
892 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, -1); 864 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, -1);
893 if (owner != TypeSystem::InvalidOwnership && d_function->isConstructor()) 865 if (owner != TypeSystem::InvalidOwnership && d_function->isConstructor())
894 s << INDENT << "this." << function_call_for_ownership(owner) << ";" << endl; 866 s << INDENT << "this." << function_call_for_ownership(owner) << ";" << endl;
895 } 867 }
868
869 // return value marschalling
870 if(return_type) {
871 if (!returnImmediately && !d_function->storeResult()) {
872 s << INDENT;
873 QString modified_type = d_function->typeReplaced(0);
874 if (modified_type.isEmpty())
875 s << translateType(d_function->type(), d_function->implementingClass());
876 else
877 s << modified_type.replace('$', '.');
878 s << " __d_return_value = ";
879 }
880
881 if ( ( has_return_type && d_function->argumentReplaced(0).isEmpty() )) // qtd
882 if(return_type->isQObject())
883 s << "qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl;
884
885 if (return_type->isValue() && !return_type->typeEntry()->isStructInD())
886 s << "new " << return_type->name() << "(__qt_return_value, false);" << endl;
887
888 if (return_type->isVariant())
889 s << "new QVariant(__qt_return_value, false);" << endl;
890
891 if (return_type->isNativePointer() && return_type->typeEntry()->isValue())
892 s << "new " << return_type->name() << "(__qt_return_value, true);" << endl;
893
894 if (return_type->isObject()) {
895 if(d_function->storeResult())
896 s << INDENT << QString("__m_%1.nativeId = __qt_return_value;").arg(d_function->name()) << endl
897 << INDENT << QString("return __m_%1;").arg(d_function->name()) << endl;
898 else
899 s << "qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl;
900 s << endl;
901 }
902
903 if (return_type->isArray()) {
904 s << "__qt_return_value[0 .. " << return_type->arrayElementCount() << "];" << endl;
905 }
906
907 foreach (ReferenceCount referenceCount, referenceCounts) {
908 writeReferenceCount(s, referenceCount, "__d_return_value");
909 }
910
911 if (!returnImmediately && !d_function->storeResult())
912 s << INDENT << "return __d_return_value;" << endl;
913 }
914 writeInjectedCode(s, d_function, CodeSnip::End);
896 915
897 if(return_in_arg) // qtd 916 if(return_in_arg) // qtd
898 s << INDENT << "return res;" << endl; 917 s << INDENT << "return res;" << endl;
899 } 918 }
900 919