[PATCH] widl: Handle C++ aggregate returns in a MSVC compatible way.

Henri Verbeet hverbeet at codeweavers.com
Thu Jul 20 07:18:33 CDT 2017


This mainly affects 64-bit winelib applications, and potentially mingw-w64
usage of the Wine headers. As explained in commit
fabfa59aea5c5b3201142382038beb3e76cb7567, MSVC returns aggregates through an
implicit parameter immediately after the this/interface pointer. GCC's
"ms_abi" attribute is supposed to match this, but unfortunately current
versions of g++ (confirmed up to at least g++ 6.3) place the implicit return
pointer before the this/interface pointer. See also
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52792.

This commit takes an approach similar to the earlier commit
fabfa59aea5c5b3201142382038beb3e76cb7567: For ABI compatibility the return
pointer is listed as an explicit parameter in the vtbl entry, while an inline
helper is provided for source code compatibility.

Signed-off-by: Henri Verbeet <hverbeet at codeweavers.com>
---
 tools/widl/header.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/tools/widl/header.c b/tools/widl/header.c
index e48b488..0481368 100644
--- a/tools/widl/header.c
+++ b/tools/widl/header.c
@@ -1043,14 +1043,52 @@ static void write_cpp_method_def(FILE *header, const type_t *iface)
     const var_t *func = stmt->u.var;
     if (!is_callas(func->attrs)) {
       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
+      const var_list_t *args = type_get_function_args(func->type);
+      int aggregate_ret = is_aggregate_return(func);
+      const var_t *arg;
+
       if (!callconv) callconv = "STDMETHODCALLTYPE";
       indent(header, 0);
       fprintf(header, "virtual ");
       write_type_decl_left(header, type_function_get_rettype(func->type));
+      if (aggregate_ret)
+        fprintf(header, " *");
       fprintf(header, " %s %s(\n", callconv, get_name(func));
-      write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
-      fprintf(header, ") = 0;\n");
-      fprintf(header, "\n");
+      if (aggregate_ret) {
+        ++indentation;
+        indent(header, 0);
+        write_type_decl_left(header, type_function_get_rettype(func->type));
+        fprintf(header, " *__ret");
+        if (args)
+          fprintf(header, ",\n");
+        --indentation;
+      }
+      if (!aggregate_ret || args)
+        write_args(header, args, iface->name, 2, TRUE);
+      fprintf(header, ") = 0;\n\n");
+
+      if (aggregate_ret) {
+        indent(header, 0);
+        write_type_decl_left(header, type_function_get_rettype(func->type));
+        fprintf(header, " %s %s(\n", callconv, get_name(func));
+        write_args(header, args, iface->name, 2, TRUE);
+        fprintf(header, ")\n");
+        indent(header, 0);
+        fprintf(header, "{\n");
+        ++indentation;
+        indent(header, 0);
+        write_type_decl_left(header, type_function_get_rettype(func->type));
+        fprintf(header, " __ret;\n");
+        indent(header, 0);
+        fprintf(header, "return *%s(&__ret", get_name(func));
+        if (args)
+            LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
+                fprintf(header, ", %s", arg->name);
+        fprintf(header, ");\n");
+        --indentation;
+        indent(header, 0);
+        fprintf(header, "}\n\n");
+      }
     }
   }
 }
-- 
2.1.4




More information about the wine-patches mailing list