diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-06-01 15:21:36 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-06-04 11:07:36 +0200 |
| commit | 987cf3c516f06b269b60364222ff97be7a7f3755 (patch) | |
| tree | 8974f44063b7599e241d946c6bf634fa9f9d6cab /tools/qtpy2cpp_lib/formatter.py | |
| parent | fba2f8dad8e1d313b4bab13950bb7607b2b8e1da (diff) | |
qtpy2cpp: Improve function definitions
- Handle type annotations in function definitions with some heuristics
how to pass typical Qt classes.
- Fix the formatting of default parameters.
- Handle Slot decorators.
- Ignore the above elements later when the parser traverses them
Introduce concenience functions for checking visitor scope.
Pick-to: 6.3
Task-number: PYSIDE-1945
Change-Id: I489088025b0d6a76d43da6154af4db58b748adbe
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'tools/qtpy2cpp_lib/formatter.py')
| -rw-r--r-- | tools/qtpy2cpp_lib/formatter.py | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/tools/qtpy2cpp_lib/formatter.py b/tools/qtpy2cpp_lib/formatter.py index 9b454af80..07f733424 100644 --- a/tools/qtpy2cpp_lib/formatter.py +++ b/tools/qtpy2cpp_lib/formatter.py @@ -6,9 +6,29 @@ import ast +from .qt import ClassFlag, qt_class_flags + CLOSING = {"{": "}", "(": ")", "[": "]"} # Closing parenthesis for C++ +def _fix_function_argument_type(type, for_return): + """Fix function argument/return qualifiers using some heuristics for Qt.""" + if type == "float": + return "double" + if type == "str": + type = "QString" + if not type.startswith("Q"): + return type + flags = qt_class_flags(type) + if flags & ClassFlag.PASS_BY_VALUE: + return type + if flags & ClassFlag.PASS_BY_CONSTREF: + return type if for_return else f"const {type} &" + if flags & ClassFlag.PASS_BY_REF: + return type if for_return else f"{type} &" + return type + " *" # Assume pointer by default + + def to_string(node): """Helper to retrieve a string from the (Lists of)Name/Attribute aggregated into some nodes""" @@ -70,8 +90,17 @@ def format_for_loop(f_node): return result +def format_name_constant(node): + """Format a ast.NameConstant.""" + if node.value is None: + return "nullptr" + return "true" if node.value else "false" + + def format_literal(node): """Returns the value of number/string literals""" + if isinstance(node, ast.NameConstant): + return format_name_constant(node) if isinstance(node, ast.Num): return str(node.n) if isinstance(node, ast.Str): @@ -125,10 +154,16 @@ def format_function_def_arguments(function_def_node): if result: result += ', ' if a.arg != 'self': + if a.annotation and isinstance(a.annotation, ast.Name): + result += _fix_function_argument_type(a.annotation.id, False) + ' ' result += a.arg if default_values[i]: result += ' = ' - result += format_literal(default_values[i]) + default_value = default_values[i] + if isinstance(default_value, ast.Attribute): + result += format_reference(default_value) + else: + result += format_literal(default_value) return result @@ -218,7 +253,10 @@ class CppFormatter(Indenter): name = '~' + class_context warn = False else: - name = 'void ' + f_node.name + return_type = "void" + if f_node.returns and isinstance(f_node.returns, ast.Name): + return_type = _fix_function_argument_type(f_node.returns.id, True) + name = return_type + " " + f_node.name self.indent_string(f'{name}({arguments})') if warn: self._output_file.write(' /* FIXME: types */') |
