diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-15 17:04:28 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-15 20:26:10 +0200 |
commit | d6ee76c90bbdda6a4ed236953610d6e3ebfe69e0 (patch) | |
tree | b55567afec71190dd1faddc70373fc5f6ccdc2a4 | |
parent | 393543d02ac5b8908ff80f75e0460bbb4fb901aa (diff) |
shiboken6: Simplify CppGenerator::getArgumentType()
The function took an argument position where a value of 0 means return
type. The return type code branch was not used; it is only ever
invoked for arguments.
Remove the return type code path and change the function to take the
argument index. Failing to build a meta type for an argument is a hard
failure; change the function to return the meta type and to throw on
failure.
Task-number: PYSIDE-1660
Change-Id: I0dd89ecde595c71b42df7464d03204bafe74d17c
Reviewed-by: Christian Tismer <tismer@stackless.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 43 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.h | 11 |
2 files changed, 16 insertions, 38 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index d35d2a5f5..3a8f2c2a2 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -2511,41 +2511,25 @@ void CppGenerator::writeArgumentConversion(TextStream &s, writeUnusedVariableCast(s, argName); } -static const QStringList &knownPythonTypes() -{ - static const QStringList result = { - pyBoolT(), pyIntT(), pyFloatT(), pyLongT(), - cPyObjectT(), QLatin1String("PyString"), - cPyBufferT(), cPySequenceT(), - QLatin1String("PyTuple"), cPyListT(), - QLatin1String("PyDict"), QLatin1String("PyObject*"), - QLatin1String("PyObject *"), QLatin1String("PyTupleObject*")}; - return result; -} - -std::optional<AbstractMetaType> - CppGenerator::getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos) +AbstractMetaType + CppGenerator::getArgumentType(const AbstractMetaFunctionCPtr &func, int index) { - if (argPos < 0 || argPos > func->arguments().size()) { + if (index < 0 || index >= func->arguments().size()) { qCWarning(lcShiboken).noquote().nospace() << "Argument index for function '" << func->signature() << "' out of range."; return {}; } - QString typeReplaced = func->typeReplaced(argPos); + QString typeReplaced = func->typeReplaced(index + 1); if (typeReplaced.isEmpty()) { - if (argPos == 0) - return func->type(); - auto argType = func->arguments().at(argPos - 1).type(); + auto argType = func->arguments().at(index).type(); return argType.viewOn() ? *argType.viewOn() : argType; } auto argType = AbstractMetaType::fromString(typeReplaced); - if (!argType.has_value() && !knownPythonTypes().contains(typeReplaced)) { - qCWarning(lcShiboken, "%s", - qPrintable(msgUnknownTypeInArgumentTypeReplacement(typeReplaced, func.data()))); - } - return argType; + if (!argType.has_value()) + throw Exception(msgUnknownTypeInArgumentTypeReplacement(typeReplaced, func.data())); + return argType.value(); } static inline QString arrayHandleType(const AbstractMetaTypeList &nestedArrayTypes) @@ -3052,14 +3036,14 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s, } if (hasConversionRule) continue; - auto argType = getArgumentType(func, argIdx + 1); - if (!argType.has_value() || (mayHaveUnunsedArguments && !func->injectedCodeUsesArgument(argIdx))) + if (mayHaveUnunsedArguments && !func->injectedCodeUsesArgument(argIdx)) continue; + auto argType = getArgumentType(func, argIdx); int argPos = argIdx - removedArgs; QString argName = QLatin1String(CPP_ARG) + QString::number(argPos); QString pyArgName = usePyArgs ? pythonArgsAt(argPos) : QLatin1String(PYTHON_ARG); QString defaultValue = guessScopeForDefaultValue(func, arg); - writeArgumentConversion(s, argType.value(), argName, pyArgName, + writeArgumentConversion(s, argType, argName, pyArgName, func->implementingClass(), defaultValue, func->isUserAdded()); } @@ -4943,10 +4927,7 @@ void CppGenerator::writeRichCompareFunction(TextStream &s, const auto func = od->referenceFunction(); if (func->isStatic()) continue; - auto argTypeO = getArgumentType(func, 1); - if (!argTypeO.has_value()) - continue; - auto argType = argTypeO.value(); + auto argType = getArgumentType(func, 0); if (!first) { s << " else "; } else { diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index 4fd12aa69..8f6a19ced 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -165,14 +165,11 @@ private: * If the argument type was modified in the type system, this method will * try to build a new type based on the type name defined in the type system. * \param func The function which owns the argument. - * \param argPos Argument position in the function signature. - * Note that the position 0 represents the return value, and the function - * parameters start counting on 1. - * \param newType It is set to true if the type returned is a new object that must be deallocated. - * \return The type of the argument indicated by \p argPos. + * \param index Argument index in the function signature. + * \return The type of the argument indicated by \p index. */ - static std::optional<AbstractMetaType> - getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos); + static AbstractMetaType + getArgumentType(const AbstractMetaFunctionCPtr &func, int index); void writePythonToCppTypeConversion(TextStream &s, const AbstractMetaType &type, |