nss: upgrade to release 3.73
[LibreOffice.git] / bridges / source / cpp_uno / msvc_win32_arm64 / uno2cpp.cxx
bloba0c2adc6f6d8b8692d79ab6505ab1b10fb10a5e9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <cstring>
24 #include <exception>
25 #include <typeinfo>
27 #include <bridge.hxx>
28 #include <types.hxx>
29 #include <unointerfaceproxy.hxx>
30 #include <vtables.hxx>
31 #include <com/sun/star/uno/Exception.hpp>
32 #include <com/sun/star/uno/RuntimeException.hpp>
33 #include <com/sun/star/uno/genfunc.hxx>
34 #include <rtl/textenc.h>
35 #include <rtl/ustring.hxx>
36 #include <sal/alloca.h>
37 #include <sal/types.h>
38 #include <typelib/typeclass.h>
39 #include <typelib/typedescription.h>
40 #include <uno/any2.h>
41 #include <uno/data.h>
43 #include "abi.hxx"
44 #include <msvc/arm64.hxx>
46 namespace
48 extern "C" void callVirtualFunction(sal_uInt64* stack, sal_uInt64* frame, sal_uInt64 function,
49 void* ret);
51 void pushArgument(sal_uInt64 value, sal_uInt64* stack, sal_Int32& sp, sal_uInt64* regs,
52 sal_Int32& nregs)
54 (nregs != 8 ? regs[nregs++] : stack[sp++]) = value;
57 void call(bridges::cpp_uno::shared::UnoInterfaceProxy* pProxy,
58 bridges::cpp_uno::shared::VtableSlot slot, typelib_TypeDescriptionReference* returnType,
59 const sal_Int32 count, typelib_MethodParameter* parameters, void* returnValue,
60 void** arguments, uno_Any** exception)
62 static_assert(sizeof(sal_uInt64) == sizeof(void*));
63 typelib_TypeDescription* aReturnTD = nullptr;
64 TYPELIB_DANGER_GET(&aReturnTD, returnType);
65 const ReturnKind eRetKind = getReturnKind(aReturnTD);
66 const bool retConv = bridges::cpp_uno::shared::relatesToInterfaceType(aReturnTD);
67 void* ret = retConv ? alloca(aReturnTD->nSize) : returnValue;
69 sal_uInt64** thisPtr = reinterpret_cast<sal_uInt64**>(pProxy->getCppI()) + slot.offset;
71 sal_uInt64* gpr = static_cast<sal_uInt64*>(alloca((count + 16) * sizeof(sal_uInt64) + 32));
72 sal_uInt64* fpr = &gpr[8];
73 sal_uInt64* stack = &gpr[16];
74 sal_uInt64* frame = &gpr[16 + count];
75 void** cppArgs = static_cast<void**>(alloca(count * sizeof(void*)));
76 typelib_TypeDescription** ptds
77 = static_cast<typelib_TypeDescription**>(alloca(count * sizeof(typelib_TypeDescription*)));
79 sal_Int32 sp = 0;
80 sal_Int32 nGPR = 0;
81 sal_Int32 nFPR = 0;
82 gpr[nGPR++] = reinterpret_cast<sal_uInt64>(thisPtr);
84 for (sal_Int32 i = 0; i != count; ++i)
86 if (!parameters[i].bOut && bridges::cpp_uno::shared::isSimpleType(parameters[i].pTypeRef))
88 cppArgs[i] = 0;
89 switch (parameters[i].pTypeRef->eTypeClass)
91 case typelib_TypeClass_BOOLEAN:
92 pushArgument(*static_cast<sal_Bool*>(arguments[i]), stack, sp, gpr, nGPR);
93 break;
94 case typelib_TypeClass_BYTE:
95 pushArgument(*static_cast<sal_Int8*>(arguments[i]), stack, sp, gpr, nGPR);
96 break;
97 case typelib_TypeClass_SHORT:
98 pushArgument(*static_cast<sal_Int16*>(arguments[i]), stack, sp, gpr, nGPR);
99 break;
100 case typelib_TypeClass_UNSIGNED_SHORT:
101 pushArgument(*static_cast<sal_uInt16*>(arguments[i]), stack, sp, gpr, nGPR);
102 break;
103 case typelib_TypeClass_LONG:
104 case typelib_TypeClass_ENUM:
105 pushArgument(*static_cast<sal_Int32*>(arguments[i]), stack, sp, gpr, nGPR);
106 break;
107 case typelib_TypeClass_UNSIGNED_LONG:
108 pushArgument(*static_cast<sal_uInt32*>(arguments[i]), stack, sp, gpr, nGPR);
109 break;
110 case typelib_TypeClass_HYPER:
111 pushArgument(*static_cast<sal_Int64*>(arguments[i]), stack, sp, gpr, nGPR);
112 break;
113 case typelib_TypeClass_UNSIGNED_HYPER:
114 pushArgument(*static_cast<sal_uInt64*>(arguments[i]), stack, sp, gpr, nGPR);
115 break;
116 case typelib_TypeClass_FLOAT:
117 pushArgument(*static_cast<sal_uInt32*>(arguments[i]), stack, sp, fpr, nFPR);
118 break;
119 case typelib_TypeClass_DOUBLE:
120 pushArgument(*static_cast<sal_uInt64*>(arguments[i]), stack, sp, fpr, nFPR);
121 break;
122 case typelib_TypeClass_CHAR:
123 pushArgument(*static_cast<sal_Unicode*>(arguments[i]), stack, sp, gpr, nGPR);
124 break;
125 default:
126 assert(false);
129 else
131 typelib_TypeDescription* ptd = 0;
132 TYPELIB_DANGER_GET(&ptd, parameters[i].pTypeRef);
133 if (!parameters[i].bIn)
135 cppArgs[i] = alloca(ptd->nSize);
136 uno_constructData(cppArgs[i], ptd);
137 ptds[i] = ptd;
138 pushArgument(reinterpret_cast<sal_uInt64>(cppArgs[i]), stack, sp, gpr, nGPR);
140 else if (bridges::cpp_uno::shared::relatesToInterfaceType(ptd))
142 cppArgs[i] = alloca(ptd->nSize);
143 uno_copyAndConvertData(cppArgs[i], arguments[i], ptd,
144 pProxy->getBridge()->getUno2Cpp());
145 ptds[i] = ptd;
146 pushArgument(reinterpret_cast<sal_uInt64>(cppArgs[i]), stack, sp, gpr, nGPR);
148 else
150 cppArgs[i] = 0;
151 pushArgument(reinterpret_cast<sal_uInt64>(arguments[i]), stack, sp, gpr, nGPR);
152 TYPELIB_DANGER_RELEASE(ptd);
157 __try
159 callVirtualFunction(stack, frame, (*thisPtr)[slot.index], ret);
161 __except (msvc_filterCppException(GetExceptionInformation(), *exception,
162 pProxy->getBridge()->getCpp2Uno()))
164 for (sal_Int32 i = 0; i != count; ++i)
166 if (cppArgs[i] != 0)
168 uno_destructData(cppArgs[i], ptds[i],
169 reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
170 TYPELIB_DANGER_RELEASE(ptds[i]);
173 TYPELIB_DANGER_RELEASE(aReturnTD);
174 return;
177 *exception = 0;
178 for (sal_Int32 i = 0; i != count; ++i)
180 if (cppArgs[i] != 0)
182 if (parameters[i].bOut)
184 if (parameters[i].bIn)
186 uno_destructData(arguments[i], ptds[i], 0);
188 uno_copyAndConvertData(arguments[i], cppArgs[i], ptds[i],
189 pProxy->getBridge()->getCpp2Uno());
191 uno_destructData(cppArgs[i], ptds[i],
192 reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
193 TYPELIB_DANGER_RELEASE(ptds[i]);
197 switch (eRetKind)
199 case RETURN_KIND_REG:
200 switch (aReturnTD->eTypeClass)
202 case typelib_TypeClass_VOID:
203 break;
204 case typelib_TypeClass_BOOLEAN:
205 case typelib_TypeClass_BYTE:
206 case typelib_TypeClass_SHORT:
207 case typelib_TypeClass_UNSIGNED_SHORT:
208 case typelib_TypeClass_LONG:
209 case typelib_TypeClass_UNSIGNED_LONG:
210 case typelib_TypeClass_HYPER:
211 case typelib_TypeClass_UNSIGNED_HYPER:
212 case typelib_TypeClass_CHAR:
213 case typelib_TypeClass_ENUM:
214 case typelib_TypeClass_STRUCT:
215 std::memcpy(ret, gpr, aReturnTD->nSize);
216 break;
217 case typelib_TypeClass_FLOAT:
218 case typelib_TypeClass_DOUBLE:
219 std::memcpy(ret, fpr, aReturnTD->nSize);
220 break;
221 default:
222 assert(false);
224 break;
225 case RETURN_KIND_HFA_FLOAT:
226 switch (aReturnTD->nSize)
228 case 16:
229 std::memcpy(static_cast<char*>(ret) + 12, fpr + 3, 4);
230 [[fallthrough]];
231 case 12:
232 std::memcpy(static_cast<char*>(ret) + 8, fpr + 2, 4);
233 [[fallthrough]];
234 case 8:
235 std::memcpy(static_cast<char*>(ret) + 4, fpr + 1, 4);
236 [[fallthrough]];
237 case 4:
238 std::memcpy(ret, fpr, 4);
239 break;
240 default:
241 assert(false);
243 break;
244 case RETURN_KIND_HFA_DOUBLE:
245 std::memcpy(ret, fpr, aReturnTD->nSize);
246 break;
247 case RETURN_KIND_INDIRECT:
248 break;
251 if (retConv)
253 uno_copyAndConvertData(returnValue, ret, aReturnTD, pProxy->getBridge()->getCpp2Uno());
254 uno_destructData(ret, aReturnTD, reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
256 TYPELIB_DANGER_RELEASE(aReturnTD);
260 namespace bridges::cpp_uno::shared
262 void unoInterfaceProxyDispatch(uno_Interface* pUnoI, typelib_TypeDescription const* pMemberDescr,
263 void* pReturn, void** pArgs, uno_Any** ppException)
265 UnoInterfaceProxy* pProxy = static_cast<UnoInterfaceProxy*>(pUnoI);
266 switch (pMemberDescr->eTypeClass)
268 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
270 typelib_InterfaceAttributeTypeDescription const* atd
271 = reinterpret_cast<typelib_InterfaceAttributeTypeDescription const*>(pMemberDescr);
272 VtableSlot slot(getVtableSlot(atd));
273 if (pReturn != 0)
274 { // getter
275 call(pProxy, slot, atd->pAttributeTypeRef, 0, 0, pReturn, pArgs, ppException);
277 else
278 { // setter
279 typelib_MethodParameter param = { 0, atd->pAttributeTypeRef, true, false };
280 typelib_TypeDescriptionReference* pReturnTD = nullptr;
281 typelib_typedescriptionreference_new(&pReturnTD, typelib_TypeClass_VOID,
282 OUString("void").pData);
283 slot.index += 1;
284 call(pProxy, slot, pReturnTD, 1, &param, pReturn, pArgs, ppException);
285 typelib_typedescriptionreference_release(pReturnTD);
287 break;
289 case typelib_TypeClass_INTERFACE_METHOD:
291 typelib_InterfaceMethodTypeDescription const* mtd
292 = reinterpret_cast<typelib_InterfaceMethodTypeDescription const*>(pMemberDescr);
293 VtableSlot slot(getVtableSlot(mtd));
294 switch (slot.index)
296 case 1:
297 pUnoI->acquire(pUnoI);
298 *ppException = 0;
299 break;
300 case 2:
301 pUnoI->release(pUnoI);
302 *ppException = 0;
303 break;
304 case 0:
306 typelib_TypeDescription* td = 0;
307 TYPELIB_DANGER_GET(
308 &td, (reinterpret_cast<css::uno::Type*>(pArgs[0])->getTypeLibType()));
309 if (td != 0)
311 uno_Interface* ifc = 0;
312 pProxy->pBridge->getUnoEnv()->getRegisteredInterface(
313 pProxy->pBridge->getUnoEnv(), reinterpret_cast<void**>(&ifc),
314 pProxy->oid.pData,
315 reinterpret_cast<typelib_InterfaceTypeDescription*>(td));
316 if (ifc != 0)
318 uno_any_construct(reinterpret_cast<uno_Any*>(pReturn), &ifc, td, 0);
319 ifc->release(ifc);
320 TYPELIB_DANGER_RELEASE(td);
321 *ppException = 0;
322 break;
324 TYPELIB_DANGER_RELEASE(td);
327 [[fallthrough]];
328 default:
329 call(pProxy, slot, mtd->pReturnTypeRef, mtd->nParams, mtd->pParams, pReturn,
330 pArgs, ppException);
331 break;
333 break;
335 default:
336 assert(false);
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */