comparison generator/dgenerator.cpp @ 33:cf8a415f3f32

gui package is fully wrapped now. lots of fixes in the generator
author eldar
date Sat, 16 May 2009 17:12:06 +0000
parents 5015aede8edd
children 5917a613d118
comparison
equal deleted inserted replaced
32:a293cd68d56c 33:cf8a415f3f32
69 << "signed char" << "unsigned short" << "QBool" << "unsigned int" 69 << "signed char" << "unsigned short" << "QBool" << "unsigned int"
70 << "Qt::HANDLE" << "QChar" << "java.lang.JObjectWrapper" << "void" 70 << "Qt::HANDLE" << "QChar" << "java.lang.JObjectWrapper" << "void"
71 << "QLatin1String" << "unsigned long long" << "signed int" 71 << "QLatin1String" << "unsigned long long" << "signed int"
72 << "signed short" << "Array" << "GLuint" << "GLenum" << "GLint" 72 << "signed short" << "Array" << "GLuint" << "GLenum" << "GLint"
73 << "unsigned long" << "ulong" << "long" << "QByteRef" 73 << "unsigned long" << "ulong" << "long" << "QByteRef"
74 << "QStringList" << "QList" << "QVector" << "QPair"; 74 << "QStringList" << "QList" << "QVector" << "QPair"
75 << "QSet";
75 } 76 }
76 77
77 QString DGenerator::fileNameForClass(const AbstractMetaClass *d_class) const 78 QString DGenerator::fileNameForClass(const AbstractMetaClass *d_class) const
78 { 79 {
79 return QString("%1.d").arg(d_class->name()); 80 return QString("%1.d").arg(d_class->name());
123 if (context != 0 && d_type != 0 && context->typeEntry()->isGenericClass() && d_type->originalTemplateType() != 0) 124 if (context != 0 && d_type != 0 && context->typeEntry()->isGenericClass() && d_type->originalTemplateType() != 0)
124 d_type = d_type->originalTemplateType(); 125 d_type = d_type->originalTemplateType();
125 126
126 if (!d_type) { 127 if (!d_type) {
127 s = "void"; 128 s = "void";
128 } else if (d_type->typeEntry() && d_type->typeEntry()->qualifiedCppName() == "QString") { 129 } else if (d_type->typeEntry()->qualifiedCppName() == "QChar")
130 s = "wchar" + QString(d_type->actualIndirections(), '*');
131 else if (d_type->typeEntry() && d_type->typeEntry()->qualifiedCppName() == "QString") {
129 s = "string"; 132 s = "string";
130 } else if (d_type->isArray()) { 133 } else if (d_type->isArray()) {
131 s = translateType(d_type->arrayElementType(), context) + "[]"; 134 s = translateType(d_type->arrayElementType(), context) + "[]";
132 } else if (d_type->isEnum() /* qtd2 || d_type->isFlags() */) { 135 } else if (d_type->isEnum() /* qtd2 || d_type->isFlags() */) {
133 if (( d_type->isEnum() && ((EnumTypeEntry *)d_type->typeEntry())->forceInteger() ) 136 if (( d_type->isEnum() && ((EnumTypeEntry *)d_type->typeEntry())->forceInteger() )
453 */ 456 */
454 457
455 CppImplGenerator::writeFinalFunctionArguments(s, d_function, true); // qtd 458 CppImplGenerator::writeFinalFunctionArguments(s, d_function, true); // qtd
456 459
457 // Make sure people don't call the private functions 460 // Make sure people don't call the private functions
461 // qtd remember name QNoImplementationException
458 if (d_function->isEmptyFunction()) { 462 if (d_function->isEmptyFunction()) {
459 s << endl 463 s << endl
460 << INDENT << "{" << endl 464 << INDENT << "{" << endl
461 << INDENT << "// qtd2 throw new qt.QNoImplementationException();" << endl 465 << INDENT << " throw new Exception(\"No Implementation Exception\");" << endl
462 << INDENT << "}" << endl << endl; 466 << INDENT << "}" << endl << endl;
463 } else { 467 } else {
464 s << ";" << endl; 468 s << ";" << endl;
465 } 469 }
466 } 470 }
656 } 660 }
657 661
658 // bool needs_return_variable = has_return_type 662 // bool needs_return_variable = has_return_type
659 // && (owner != TypeSystem::InvalidOwnership || referenceCounts.size() > 0 || has_code_injections_at_the_end); 663 // && (owner != TypeSystem::InvalidOwnership || referenceCounts.size() > 0 || has_code_injections_at_the_end);
660 664
661 if(d_function->type()) { // qtd 665 if(return_type) { // qtd
662 if (d_function->type()->isTargetLangString()) 666 if (return_type->isTargetLangString())
663 s << INDENT << "string res;" << endl; 667 s << INDENT << "string res;" << endl;
664 668
665 if(d_function->type()->name() == "QModelIndex") 669 if(return_type->name() == "QModelIndex")
666 s << INDENT << "QModelIndex res;" << endl; 670 s << INDENT << "QModelIndex res;" << endl;
667 671
668 if(d_function->type()->isContainer()) 672 if(return_type->isContainer())
669 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl; 673 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl;
670 } 674 }
671 675
672 s << INDENT; 676 s << INDENT;
673 if ( (has_return_type && d_function->argumentReplaced(0).isEmpty() ) || d_function->isConstructor()) { //qtd 677 if ( (has_return_type && d_function->argumentReplaced(0).isEmpty() ) || d_function->isConstructor()) { //qtd
684 s << new_return_type; 688 s << new_return_type;
685 689
686 s << " __qt_return_value = "; 690 s << " __qt_return_value = ";
687 }*/ else if (d_function->isConstructor()) { // qtd 691 }*/ else if (d_function->isConstructor()) { // qtd
688 s << "void* __qt_return_value = "; 692 s << "void* __qt_return_value = ";
689 } else if (d_function->type() && d_function->type()->isValue() && !d_function->type()->typeEntry()->isStructInD()) { 693 } else if (return_type && return_type->isValue() && !return_type->typeEntry()->isStructInD()) {
690 s << "void* __qt_return_value = "; 694 s << "void* __qt_return_value = ";
691 } else if (d_function->type() && d_function->type()->isVariant()) 695 } else if (return_type && return_type->isVariant()) {
692 s << "void* __qt_return_value = "; 696 s << "void* __qt_return_value = ";
693 else if ( d_function->type() && ( d_function->type()->isObject() || 697 } else if (return_type && ( return_type->isObject() ||
694 (d_function->type()->isNativePointer() && d_function->type()->typeEntry()->isValue()) || 698 (return_type->isNativePointer() && return_type->typeEntry()->isValue()) ||
695 d_function->type()->typeEntry()->isInterface()) ) { 699 return_type->typeEntry()->isInterface()) ) {
696 s << "void* __qt_return_value = "; 700 s << "void* __qt_return_value = ";
701 } else if (return_type && return_type->isArray()) {
702 s << return_type->arrayElementType()->name() + "* __qt_return_value = ";
697 } else { 703 } else {
698 s << "return "; 704 s << "return ";
699 } 705 }
700 706
701 if (return_type && return_type->isTargetLangEnum()) { 707 if (return_type && return_type->isTargetLangEnum()) {
786 s << arg_name << " is null ? null : " << arg_name << ".nativeId"; 792 s << arg_name << " is null ? null : " << arg_name << ".nativeId";
787 else if (te->designatedInterface()) 793 else if (te->designatedInterface())
788 s << arg_name << " is null ? null : " << arg_name << ".__ptr_" << te->designatedInterface()->name(); 794 s << arg_name << " is null ? null : " << arg_name << ".__ptr_" << te->designatedInterface()->name();
789 else if (modified_type == "string" /* && type->fullName() == "char" */) { 795 else if (modified_type == "string" /* && type->fullName() == "char" */) {
790 s << "toStringz(" << arg_name << ")"; 796 s << "toStringz(" << arg_name << ")";
791 } else if(type->isContainer()) { 797 } else if (type->isArray())
798 s << arg_name << ".ptr";
799 else if(type->isContainer()) {
792 const ContainerTypeEntry *cte = 800 const ContainerTypeEntry *cte =
793 static_cast<const ContainerTypeEntry *>(te); 801 static_cast<const ContainerTypeEntry *>(te);
794 if(isLinearContainer(cte)) 802 if(isLinearContainer(cte))
795 s << QString("%1.ptr, %1.length").arg(arg_name); 803 s << QString("%1.ptr, %1.length").arg(arg_name);
796 } else if (type->isTargetLangString() || (te && te->qualifiedCppName() == "QString")) 804 } else if (type->typeEntry()->qualifiedCppName() == "QChar")
797 s << QString("%1.ptr, %1.length").arg(arg_name); 805 s << arg_name;
806 else if (type->isTargetLangString() || (te && te->qualifiedCppName() == "QString"))
807 s << arg_name;
798 else if (type->isTargetLangEnum() || type->isTargetLangFlags()) { 808 else if (type->isTargetLangEnum() || type->isTargetLangFlags()) {
799 s << arg_name; 809 s << arg_name;
800 // qtd s << arg->argumentName() << ".value()"; 810 // qtd s << arg->argumentName() << ".value()";
801 } else if (!type->hasNativeId() && !(te->isValue() && type->isNativePointer())) { // qtd2 hack for QStyleOption not being a nativeId based for some reason 811 } else if (!type->hasNativeId() && !(te->isValue() && type->isNativePointer())) { // qtd2 hack for QStyleOption not being a nativeId based for some reason
802 s << arg_name; 812 s << arg_name;
839 } 849 }
840 850
841 s << ";" << endl; 851 s << ";" << endl;
842 852
843 // return value marschalling 853 // return value marschalling
844 if(d_function->type()) { 854 if(return_type) {
845 if ( ( has_return_type && d_function->argumentReplaced(0).isEmpty() )/* || d_function->isConstructor()*/) // qtd 855 if ( ( has_return_type && d_function->argumentReplaced(0).isEmpty() )/* || d_function->isConstructor()*/) // qtd
846 if(d_function->type()->isQObject()) { 856 if(d_function->type()->isQObject()) {
847 857
848 QString type_name = d_function->type()->name(); 858 QString type_name = d_function->type()->name();
849 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); 859 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry());
860 << INDENT << "} else" << endl 870 << INDENT << "} else" << endl
861 << INDENT << " return cast(" << d_function->type()->name() << ") d_obj;" << endl; 871 << INDENT << " return cast(" << d_function->type()->name() << ") d_obj;" << endl;
862 } 872 }
863 873
864 874
865 if (d_function->type()->isValue() && !d_function->type()->typeEntry()->isStructInD()) 875 if (return_type->isValue() && !return_type->typeEntry()->isStructInD())
866 s << INDENT << "return new " << d_function->type()->name() << "(__qt_return_value, false);" << endl; 876 s << INDENT << "return new " << d_function->type()->name() << "(__qt_return_value, false);" << endl;
867 877
868 if (d_function->type()->isVariant()) 878 if (return_type->isVariant())
869 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl; 879 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl;
870 880
871 if (d_function->type()->isNativePointer() && d_function->type()->typeEntry()->isValue()) 881 if (return_type->isNativePointer() && return_type->typeEntry()->isValue())
872 s << INDENT << "return new " << d_function->type()->name() << "(__qt_return_value, true);" << endl; 882 s << INDENT << "return new " << return_type->name() << "(__qt_return_value, true);" << endl;
873 883
874 if (d_function->type()->isObject()) { 884 if (return_type->isObject()) {
875 if(d_function->storeResult()) 885 if(d_function->storeResult())
876 s << INDENT << QString("__m_%1.nativeId = __qt_return_value;").arg(d_function->name()) << endl 886 s << INDENT << QString("__m_%1.nativeId = __qt_return_value;").arg(d_function->name()) << endl
877 << INDENT << QString("return __m_%1;").arg(d_function->name()) << endl; 887 << INDENT << QString("return __m_%1;").arg(d_function->name()) << endl;
878 else { 888 else {
879 QString type_name = d_function->type()->name(); 889 QString type_name = return_type->name();
880 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); 890 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry());
881 if(ctype->isAbstract()) 891 if(ctype->isAbstract())
882 type_name = type_name + "_ConcreteWrapper"; 892 type_name = type_name + "_ConcreteWrapper";
883 893
884 QString return_type_name = d_function->type()->name(); 894 QString return_type_name = d_function->type()->name();
885 if(d_function->type()->typeEntry()->designatedInterface()) 895 if(return_type->typeEntry()->designatedInterface())
886 return_type_name = d_function->type()->typeEntry()->designatedInterface()->name(); 896 return_type_name = d_function->type()->typeEntry()->designatedInterface()->name();
887 897
888 AbstractMetaClass *classForTypeEntry = NULL; 898 AbstractMetaClass *classForTypeEntry = NULL;
889 // search in AbstractMetaClass list for return type 899 // search in AbstractMetaClass list for return type
890 // find a better way to perform TypeEntry -> AbstractMetaClass lookup, maybe create hash before generation 900 // find a better way to perform TypeEntry -> AbstractMetaClass lookup, maybe create hash before generation
897 classForTypeEntry = ClassFromEntry::get(d_function->type()->typeEntry()); 907 classForTypeEntry = ClassFromEntry::get(d_function->type()->typeEntry());
898 908
899 // if class has virtual functions then it has classname_entity function so 909 // if class has virtual functions then it has classname_entity function so
900 // we can look for D Object pointer. otherwise create new wrapper 910 // we can look for D Object pointer. otherwise create new wrapper
901 if (classForTypeEntry != NULL && classForTypeEntry->hasVirtualFunctions()) { 911 if (classForTypeEntry != NULL && classForTypeEntry->hasVirtualFunctions()) {
902 s << INDENT << "void* d_obj = __" << d_function->type()->name() << "_entity(__qt_return_value);" << endl 912 s << INDENT << "void* d_obj = __" << return_type->name() << "_entity(__qt_return_value);" << endl
903 << INDENT << "if (d_obj !is null) {" << endl 913 << INDENT << "if (d_obj !is null) {" << endl
904 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl 914 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl
905 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl 915 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl
906 << INDENT << "} else {" << endl 916 << INDENT << "} else {" << endl
907 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl 917 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl
914 << INDENT << "return return_value;" << endl; 924 << INDENT << "return return_value;" << endl;
915 } 925 }
916 } 926 }
917 s << endl; 927 s << endl;
918 } 928 }
929
930 if (return_type->isArray()) {
931 s << INDENT << "return __qt_return_value[0 .. " << return_type->arrayElementCount() << "];" << endl;
932 }
919 } 933 }
920 writeInjectedCode(s, d_function, CodeSnip::End); 934 writeInjectedCode(s, d_function, CodeSnip::End);
921 /* qtd2 935 /* qtd2
922 if (needs_return_variable) { 936 if (needs_return_variable) {
923 if (owner != TypeSystem::InvalidOwnership) { 937 if (owner != TypeSystem::InvalidOwnership) {
1042 1056
1043 { 1057 {
1044 Indentation indent(INDENT); 1058 Indentation indent(INDENT);
1045 switch (refCount.action) { 1059 switch (refCount.action) {
1046 case ReferenceCount::Add: 1060 case ReferenceCount::Add:
1061 s << INDENT << refCountVariableName << " ~= cast(Object) " << argumentName << ";" << endl;
1062 break;
1047 case ReferenceCount::AddAll: 1063 case ReferenceCount::AddAll:
1048 s << INDENT << refCountVariableName << " ~= " << argumentName << ";" << endl; 1064 s << INDENT << refCountVariableName << " ~= " << argumentName << ";" << endl;
1049 break; 1065 break;
1050 case ReferenceCount::Remove: 1066 case ReferenceCount::Remove:
1051 s << INDENT << "remove(" << refCountVariableName 1067 s << INDENT << "remove(" << refCountVariableName
1052 << ", " << argumentName << ");" << endl; 1068 << ", cast(Object) " << argumentName << ");" << endl;
1053 break; 1069 break;
1054 case ReferenceCount::Set: 1070 case ReferenceCount::Set:
1055 { 1071 {
1056 if (refCount.declareVariable.isEmpty()) 1072 if (refCount.declareVariable.isEmpty())
1057 s << INDENT << refCount.variableName << " = cast(Object) " << argumentName << ";" << endl; 1073 s << INDENT << refCount.variableName << " = cast(Object) " << argumentName << ";" << endl;
1530 if (type->designatedInterface()) 1546 if (type->designatedInterface())
1531 return type; 1547 return type;
1532 else if (type->isEnum()) { 1548 else if (type->isEnum()) {
1533 const EnumTypeEntry *te = static_cast<const EnumTypeEntry *>(type); 1549 const EnumTypeEntry *te = static_cast<const EnumTypeEntry *>(type);
1534 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier()); 1550 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier());
1535 typeEntriesEnums << ownerTe; 1551 if(ownerTe)
1552 typeEntriesEnums << ownerTe;
1536 return NULL; 1553 return NULL;
1537 // return ownerTe; 1554 // return ownerTe;
1538 } else if (type->isFlags()) { 1555 } else if (type->isFlags()) {
1539 const FlagsTypeEntry *te = static_cast<const FlagsTypeEntry *>(type); 1556 const FlagsTypeEntry *te = static_cast<const FlagsTypeEntry *>(type);
1540 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier()); 1557 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier());
1659 if (typeEntry->isNamespace() || typeEntry->name() == "QObject") 1676 if (typeEntry->isNamespace() || typeEntry->name() == "QObject")
1660 visibility = "public"; 1677 visibility = "public";
1661 if(d_class->baseClass() && d_class->baseClass()->typeEntry() == typeEntry) 1678 if(d_class->baseClass() && d_class->baseClass()->typeEntry() == typeEntry)
1662 visibility = "public";*/ 1679 visibility = "public";*/
1663 QString visibility = "public"; 1680 QString visibility = "public";
1664 s << QString("%1 import ").arg(visibility) << typeEntry->javaPackage() << "." << typeEntry->name() << ";" << endl; 1681 s << QString("%1 import ").arg(visibility) << typeEntry->javaPackage() << "." << typeEntry->targetLangName() << ";" << endl;
1665 } 1682 }
1666 1683
1667 void DGenerator::writeRequiredImports(QTextStream &s, const AbstractMetaClass *d_class) 1684 void DGenerator::writeRequiredImports(QTextStream &s, const AbstractMetaClass *d_class)
1668 { 1685 {
1669 foreach (const TypeEntry *typeEntry, typeEntriesEnums) { 1686 foreach (const TypeEntry *typeEntry, typeEntriesEnums) {
2094 s << "public "; break; 2111 s << "public "; break;
2095 default: 2112 default:
2096 s << "protected"; // friendly 2113 s << "protected"; // friendly
2097 } 2114 }
2098 2115
2116 } // qtd2
2117
2099 if (isStatic) 2118 if (isStatic)
2100 s << "static "; 2119 s << "static ";
2101
2102 } // qtd2
2103 2120
2104 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) { 2121 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) {
2105 s << "Object[] " << variableName << ";" << endl; 2122 s << "Object[] " << variableName << ";" << endl;
2106 /* 2123 /*
2107 if (threadSafe) 2124 if (threadSafe)
2799 // if has QString argument we have to pass char* and str.length to QString constructor 2816 // if has QString argument we have to pass char* and str.length to QString constructor
2800 { 2817 {
2801 if(type->isEnum()) 2818 if(type->isEnum())
2802 s << INDENT << "auto " << arg_name << "_enum = cast(" 2819 s << INDENT << "auto " << arg_name << "_enum = cast("
2803 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";"; 2820 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";";
2821 else if (type->typeEntry()->qualifiedCppName() == "QChar")
2822 s << INDENT << "auto " << arg_name << "_d_ref = cast(wchar" << QString(type->actualIndirections(), '*')
2823 << ") " << arg_name << ";";
2804 else if (type->isTargetLangString()) 2824 else if (type->isTargetLangString())
2805 s << INDENT << "string " << arg_name << "_d_ref = toString(" 2825 s << INDENT << "string " << arg_name << "_d_ref = toString("
2806 << arg_name << "[0.." << arg_name << "_size]);"; 2826 << arg_name << "[0.." << arg_name << "_size]);";
2807 else if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") { 2827 else if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") {
2808 s << INDENT << "scope " << arg_name << "_d_qstr = new QString(" << arg_name << ", true);" << endl 2828 s << INDENT << "auto " << arg_name << "_d_qstr = QString(" << arg_name << ", true);" << endl
2809 << INDENT << "string " << arg_name << "_d_ref = " << arg_name << "_d_qstr.toNativeString();"; 2829 << INDENT << "string " << arg_name << "_d_ref = " << arg_name << "_d_qstr.toNativeString();";
2810 } else if(type->isVariant()) 2830 } else if(type->isVariant())
2811 s << INDENT << "scope " << arg_name << "_d_ref = new QVariant(" << arg_name << ", true);"; 2831 s << INDENT << "scope " << arg_name << "_d_ref = new QVariant(" << arg_name << ", true);";
2812 else if (type->typeEntry()->isStructInD()) 2832 else if (type->typeEntry()->isStructInD())
2813 continue; 2833 continue;