diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-04 14:49:23 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-05 10:39:46 +0200 |
commit | 7a3d79fb13d9f1ef31e7ee3e1e2691abf6d615e4 (patch) | |
tree | 2685d89f33b2ccb8e686393a3dae5e607ec3a7b8 | |
parent | d410d3b621a9273be7a466a7ab3eb278fc225cdf (diff) |
shiboken6: Refactor the code generating the type check
- Use pyInVariable in more places.
- Replace placeholder string formatting by concatenation/QTextStream.
- Simplify the code appending the "pyIn" variable to check function
and handle the constants true/false.
- Remove unused code path replacing "%in", which is handled
elsewhere (conversion rules).
Task-number: PYSIDE-1660
Change-Id: Ib7d3a9ca940860bf99f6731284c01e1a61df2618
Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 41 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/shibokengenerator.cpp | 24 |
2 files changed, 31 insertions, 34 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 8f86cbaff..284553901 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -1569,34 +1569,27 @@ return result;)"; const AbstractMetaClass *sourceClass = conv->ownerClass(); typeCheck = u"PyObject_TypeCheck(pyIn, "_qs + cpythonTypeNameExt(sourceClass->typeEntry()) + u')'; - toCppConv = QLatin1Char('*') + cpythonWrapperCPtr(sourceClass->typeEntry(), QLatin1String("pyIn")); + toCppConv = QLatin1Char('*') + cpythonWrapperCPtr(sourceClass->typeEntry(), + pyInVariable); } else { // Constructor that does implicit conversion. const auto &firstArg = conv->arguments().constFirst(); if (firstArg.isTypeModified() || conv->isModifiedToArray(1)) continue; const AbstractMetaType &sourceType = firstArg.type(); - typeCheck = cpythonCheckFunction(sourceType); - bool isUserPrimitiveWithoutTargetLangName = sourceType.isUserPrimitive() - && !sourceType.typeEntry()->hasTargetLangApiType(); - if (!sourceType.isWrapperType() - && !isUserPrimitiveWithoutTargetLangName - && !sourceType.typeEntry()->isEnum() - && !sourceType.typeEntry()->isFlags() - && !sourceType.typeEntry()->isContainer()) { - typeCheck += QLatin1Char('('); - } if (sourceType.isWrapperType()) { - typeCheck += QLatin1String("pyIn)"); - toCppConv = (sourceType.referenceType() == LValueReference - || !sourceType.isPointerToWrapperType()) - ? QLatin1String(" *") : QString(); - toCppConv += cpythonWrapperCPtr(sourceType.typeEntry(), QLatin1String("pyIn")); - } else if (typeCheck.contains(QLatin1String("%in"))) { - typeCheck.replace(QLatin1String("%in"), QLatin1String("pyIn")); - typeCheck.append(QLatin1Char(')')); - } else { - typeCheck += QLatin1String("pyIn)"); + if (sourceType.referenceType() == LValueReference + || !sourceType.isPointerToWrapperType()) { + toCppConv = u" *"_qs; + } + toCppConv += cpythonWrapperCPtr(sourceType.typeEntry(), pyInVariable); + } + + typeCheck = cpythonCheckFunction(sourceType); + if (typeCheck.endsWith(u", ")) { + typeCheck += pyInVariable + u')'; + } else if (typeCheck != u"true" && typeCheck != u"false") { + typeCheck += u'(' + pyInVariable + u')'; } if (sourceType.isUserPrimitive() @@ -1607,13 +1600,15 @@ return result;)"; StringStream pc(TextStream::Language::Cpp); pc << getFullTypeNameWithoutModifiers(sourceType) << " cppIn" << minimalConstructorExpression(api(), sourceType) << ";\n"; - writeToCppConversion(pc, sourceType, nullptr, QLatin1String("pyIn"), QLatin1String("cppIn")); + writeToCppConversion(pc, sourceType, nullptr, pyInVariable, + u"cppIn"_qs); pc << ';'; toCppPreConv = pc.toString(); toCppConv.append(QLatin1String("cppIn")); } else if (!sourceType.isWrapperType()) { StringStream tcc(TextStream::Language::Cpp); - writeToCppConversion(tcc, sourceType, metaClass, QLatin1String("pyIn"), QLatin1String("/*BOZO-1061*/")); + writeToCppConversion(tcc, sourceType, metaClass, pyInVariable, + u"/*BOZO-1061*/"_qs); toCppConv = tcc.toString(); } } diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index bd0729caa..796c60be7 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -1027,11 +1027,11 @@ QString ShibokenGenerator::cpythonCheckFunction(AbstractMetaType metaType) const || type == ContainerTypeEntry::SetContainer) { const AbstractMetaType &type = metaType.instantiations().constFirst(); if (type.isPointerToWrapperType()) { - typeCheck += u"checkSequenceTypes("_qs + cpythonTypeNameExt(type) + u", "_qs; + typeCheck += u"checkSequenceTypes("_qs + cpythonTypeNameExt(type) + + u", "_qs; } else if (type.isWrapperType()) { - typeCheck += QLatin1String("convertibleSequenceTypes("); - typeCheck += cpythonTypeNameExt(type); - typeCheck += QLatin1String(", "); + typeCheck += u"convertibleSequenceTypes("_qs + + cpythonTypeNameExt(type) + u", "_qs; } else { typeCheck += u"convertibleSequenceTypes("_qs + converterObject(type) + u", "_qs; @@ -1051,14 +1051,16 @@ QString ShibokenGenerator::cpythonCheckFunction(AbstractMetaType metaType) const const AbstractMetaType &firstType = metaType.instantiations().constFirst(); const AbstractMetaType &secondType = metaType.instantiations().constLast(); if (firstType.isPointerToWrapperType() && secondType.isPointerToWrapperType()) { - typeCheck += QString::fromLatin1("check%1Types(%2, %3, ") - .arg(pyType, cpythonTypeNameExt(firstType), cpythonTypeNameExt(secondType)); + QTextStream(&typeCheck) << "check" << pyType << "Types(" + << cpythonTypeNameExt(firstType) << ", " + << cpythonTypeNameExt(secondType) << ", "; } else { - typeCheck += QString::fromLatin1("convertible%1Types(%2, %3, %4, %5, ") - .arg(pyType, converterObject(firstType), - firstType.isPointerToWrapperType() ? QLatin1String("true") : QLatin1String("false"), - converterObject(secondType), - secondType.isPointerToWrapperType() ? QLatin1String("true") : QLatin1String("false")); + QTextStream(&typeCheck) << "convertible" << pyType << "Types(" + << converterObject(firstType) << ", " + << (firstType.isPointerToWrapperType() ? "true" : "false") + << ", " << converterObject(secondType) << ", " + << (secondType.isPointerToWrapperType() ? "true" :"false") + << ", "; } } return typeCheck; |