tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_ios / uno2cpp.cxx
blob2614fd041ae9e1f48cf1a50251391a82a685534f
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 #ifdef __arm64
22 #include <sal/config.h>
24 #include <exception>
25 #include <typeinfo>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <o3tl/runtimetooustring.hxx>
31 #include "bridge.hxx"
32 #include "types.hxx"
33 #include "unointerfaceproxy.hxx"
34 #include "vtables.hxx"
36 #include "share.hxx"
38 using namespace ::com::sun::star::uno;
40 namespace arm
42 bool is_hfa_struct(const typelib_TypeDescription * type)
44 const typelib_CompoundTypeDescription * p
45 = reinterpret_cast< const typelib_CompoundTypeDescription * >(type);
46 if (p->nMembers >= 4)
47 return false;
48 for (sal_Int32 i = 0; i < p->nMembers; ++i)
50 if ((p->ppTypeRefs[i]->eTypeClass != typelib_TypeClass_FLOAT &&
51 p->ppTypeRefs[i]->eTypeClass != typelib_TypeClass_DOUBLE) ||
52 p->ppTypeRefs[i]->eTypeClass != p->ppTypeRefs[0]->eTypeClass)
53 return false;
55 return true;
58 bool return_in_x8( typelib_TypeDescriptionReference *pTypeRef )
60 if (bridges::cpp_uno::shared::isSimpleType(pTypeRef))
61 return false;
62 else if (pTypeRef->eTypeClass == typelib_TypeClass_STRUCT || pTypeRef->eTypeClass == typelib_TypeClass_EXCEPTION)
64 typelib_TypeDescription * pTypeDescr = 0;
65 TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
67 // A Composite Type not larger than 16 bytes is returned in x0, x1
68 bool bRet = pTypeDescr->nSize > 16;
70 if (is_hfa_struct(pTypeDescr))
71 bRet = false;
73 TYPELIB_DANGER_RELEASE( pTypeDescr );
74 return bRet;
76 return true;
80 void MapReturn(sal_uInt64 *pGPR, double *pFPR, typelib_TypeDescriptionReference *pReturnType, sal_uInt64 *pRegisterReturn)
82 switch( pReturnType->eTypeClass )
84 case typelib_TypeClass_HYPER:
85 case typelib_TypeClass_UNSIGNED_HYPER:
86 pRegisterReturn[1] = pGPR[1];
87 [[fallthrough]];
88 case typelib_TypeClass_LONG:
89 case typelib_TypeClass_UNSIGNED_LONG:
90 case typelib_TypeClass_ENUM:
91 case typelib_TypeClass_CHAR:
92 case typelib_TypeClass_SHORT:
93 case typelib_TypeClass_UNSIGNED_SHORT:
94 case typelib_TypeClass_BOOLEAN:
95 case typelib_TypeClass_BYTE:
96 pRegisterReturn[0] = pGPR[0];
97 break;
98 case typelib_TypeClass_FLOAT:
99 *(float*)pRegisterReturn = *(float*)&pFPR[0];
100 break;
101 case typelib_TypeClass_DOUBLE:
102 *(double*)pRegisterReturn = pFPR[0];
103 break;
104 case typelib_TypeClass_STRUCT:
105 case typelib_TypeClass_EXCEPTION:
106 if (!arm::return_in_x8(pReturnType))
108 pRegisterReturn[0] = pGPR[0];
109 pRegisterReturn[1] = pGPR[1];
111 break;
112 default:
113 break;
117 namespace
119 void callVirtualMethod(
120 void *pThis,
121 sal_Int32 nVtableIndex,
122 void *pRegisterReturn,
123 typelib_TypeDescriptionReference *pReturnType,
124 sal_uInt64 *pStack,
125 int nStack,
126 sal_uInt64 *pGPR,
127 double *pFPR)
129 // never called
130 if (! pThis)
131 CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
133 if ( nStack )
135 // 16-bytes aligned
136 sal_uInt32 nStackBytes = ( ( nStack + 3 ) >> 2 ) * 16;
137 sal_uInt32 *stack = (sal_uInt32 *) alloca( nStackBytes );
138 memcpy( stack, pStack, nStackBytes );
141 sal_uInt64 pMethod = *((sal_uInt64*)pThis);
142 pMethod += 8 * nVtableIndex;
143 pMethod = *((sal_uInt64 *)pMethod);
145 __asm__ __volatile__
147 // Assembly string
148 " ldp x0, x1, %[pgpr_0]\n"
149 " ldp x2, x3, %[pgpr_2]\n"
150 " ldp x4, x5, %[pgpr_4]\n"
151 " ldp x6, x7, %[pgpr_6]\n"
152 " ldr x8, %[pregisterreturn]\n"
153 " ldp d0, d1, %[pfpr_0]\n"
154 " ldp d2, d3, %[pfpr_2]\n"
155 " ldp d4, d5, %[pfpr_4]\n"
156 " ldp d6, d7, %[pfpr_6]\n"
157 " blr %[pmethod]\n"
158 " stp x0, x1, %[pgpr_0]\n"
159 " str d0, %[pfpr_0]\n"
160 // Input operands
161 :: [pgpr_0]"m" (pGPR[0]),
162 [pgpr_2]"m" (pGPR[2]),
163 [pgpr_4]"m" (pGPR[4]),
164 [pgpr_6]"m" (pGPR[6]),
165 [pregisterreturn]"m" (pRegisterReturn),
166 [pfpr_0]"m" (pFPR[0]),
167 [pfpr_2]"m" (pFPR[2]),
168 [pfpr_4]"m" (pFPR[4]),
169 [pfpr_6]"m" (pFPR[6]),
170 [pmethod]"r" (pMethod)
171 // Clobbers
172 : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17",
173 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"
176 MapReturn(pGPR, pFPR, pReturnType, (sal_uInt64 *) pRegisterReturn);
180 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
181 if ( nr < arm::MAX_GPR_REGS ) \
182 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
183 else \
184 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
186 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
187 if ( nr < arm::MAX_GPR_REGS ) \
188 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
189 else \
190 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
192 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
193 if ( nr < arm::MAX_GPR_REGS ) \
194 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
195 else \
196 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
198 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
199 if ( nr < arm::MAX_GPR_REGS ) \
200 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
201 else \
202 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
204 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
205 if ( nr < arm::MAX_FPR_REGS ) \
206 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
207 else \
208 *pDS++ = *reinterpret_cast<double *>( pSV );
210 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
211 INSERT_DOUBLE( pSV, nr, pGPR, pDS )
213 namespace {
214 static void cpp_call(
215 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
216 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
217 typelib_TypeDescriptionReference * pReturnTypeRef,
218 sal_Int32 nParams, typelib_MethodParameter * pParams,
219 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
221 // max space for: values|ptr ...
222 sal_uInt64 * pStack = (sal_uInt64 *)alloca( (nParams+2) * sizeof(sal_Int64) );
223 sal_uInt64 * pStackStart = pStack;
225 sal_uInt64 pGPR[arm::MAX_GPR_REGS];
226 int nGPR = 0;
228 // storage and counter for SIMD/FP registers
229 double pFPR[arm::MAX_FPR_REGS];
230 int nFPR = 0;
232 // return
233 typelib_TypeDescription * pReturnTypeDescr = 0;
234 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
235 assert( pReturnTypeDescr);
237 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
239 if (pReturnTypeDescr)
241 if (!arm::return_in_x8( pReturnTypeRef ) )
242 pCppReturn = pUnoReturn; // direct way for simple types
243 else
245 // complex return via x8
246 pCppReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
247 ? alloca( pReturnTypeDescr->nSize )
248 : pUnoReturn); // direct way
251 // push this
252 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
253 + aVtableSlot.offset;
254 INSERT_INT64( &pAdjustedThisPtr, nGPR, pGPR, pStack );
256 // stack space
257 // args
258 void ** pCppArgs = (void **)alloca( sizeof(void *) * nParams );
260 // indices of values this have to be converted (interface conversion cpp<=>uno)
261 int * pTempIndices = (int *)alloca( sizeof(int) * nParams );
263 // type descriptions for reconversions
264 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)alloca( sizeof(void *) * nParams );
266 sal_Int32 nTempIndices = 0;
268 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
270 const typelib_MethodParameter & rParam = pParams[nPos];
271 typelib_TypeDescription * pParamTypeDescr = 0;
272 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
274 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
276 uno_copyAndConvertData( pCppArgs[nPos] = alloca(8), pUnoArgs[nPos],
277 pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
279 switch (pParamTypeDescr->eTypeClass)
281 case typelib_TypeClass_HYPER:
282 case typelib_TypeClass_UNSIGNED_HYPER:
283 INSERT_INT64( pCppArgs[nPos], nGPR, pGPR, pStack );
284 break;
285 case typelib_TypeClass_LONG:
286 case typelib_TypeClass_UNSIGNED_LONG:
287 case typelib_TypeClass_ENUM:
288 INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
289 break;
290 case typelib_TypeClass_SHORT:
291 case typelib_TypeClass_CHAR:
292 case typelib_TypeClass_UNSIGNED_SHORT:
293 INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
294 break;
295 case typelib_TypeClass_BOOLEAN:
296 case typelib_TypeClass_BYTE:
297 INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
298 break;
299 case typelib_TypeClass_FLOAT:
300 INSERT_FLOAT( pCppArgs[nPos], nFPR, pFPR, pStack );
301 break;
302 case typelib_TypeClass_DOUBLE:
303 INSERT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
304 break;
305 default:
306 break;
308 // no longer needed
309 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
311 else // ptr to complex value | ref
313 if (! rParam.bIn) // is pure out
315 // cpp out is constructed mem, uno out is not!
316 uno_constructData(
317 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
318 pParamTypeDescr );
319 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
320 // will be released at reconversion
321 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
323 // is in/inout
324 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
326 uno_copyAndConvertData(
327 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
328 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
330 pTempIndices[nTempIndices] = nPos; // has to be reconverted
331 // will be released at reconversion
332 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
334 else // direct way
336 pCppArgs[nPos] = pUnoArgs[nPos];
337 // no longer needed
338 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
340 INSERT_INT64( &(pCppArgs[nPos]), nGPR, pGPR, pStack );
344 assert( nGPR <= arm::MAX_GPR_REGS );
345 assert( nFPR <= arm::MAX_FPR_REGS );
349 try {
350 callVirtualMethod(
351 pAdjustedThisPtr, aVtableSlot.index,
352 pCppReturn, pReturnTypeRef,
353 pStackStart,
354 (pStack - pStackStart),
355 pGPR,
356 pFPR);
357 } catch (css::uno::Exception &) {
358 throw;
359 } catch (std::exception & e) {
360 throw css::uno::RuntimeException(
361 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name()) + ": "
362 + o3tl::runtimeToOUString(e.what()));
363 } catch (...) {
364 throw css::uno::RuntimeException("C++ code threw unknown exception");
367 // NO exception occurred...
368 *ppUnoExc = 0;
370 // reconvert temporary params
371 for ( ; nTempIndices--; )
373 sal_Int32 nIndex = pTempIndices[nTempIndices];
374 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
376 if (pParams[nIndex].bIn)
378 if (pParams[nIndex].bOut) // inout
380 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
381 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
382 pThis->getBridge()->getCpp2Uno() );
385 else // pure out
387 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
388 pThis->getBridge()->getCpp2Uno() );
390 // destroy temp cpp param => cpp: every param was constructed
391 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
393 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
395 // return value
396 if (pCppReturn && pUnoReturn != pCppReturn)
398 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
399 pThis->getBridge()->getCpp2Uno() );
400 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
403 catch (...)
405 // fill uno exception
406 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc, pThis->getBridge()->getCpp2Uno());
408 // temporary params
409 for ( ; nTempIndices--; )
411 sal_Int32 nIndex = pTempIndices[nTempIndices];
412 // destroy temp cpp param => cpp: every param was constructed
413 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndices], cpp_release );
414 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
417 // return type
418 if (pReturnTypeDescr)
419 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
424 namespace bridges::cpp_uno::shared {
426 void unoInterfaceProxyDispatch(
427 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
428 void * pReturn, void * pArgs[], uno_Any ** ppException )
430 // is my surrogate
431 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
432 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
433 #if OSL_DEBUG_LEVEL > 0
434 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
435 #endif
437 switch (pMemberDescr->eTypeClass)
439 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
441 #if OSL_DEBUG_LEVEL > 0
442 // determine vtable call index
443 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
444 assert( nMemberPos < pTypeDescr->nAllMembers && "### member pos out of range!");
445 #endif
447 VtableSlot aVtableSlot(
448 getVtableSlot(
449 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>
450 (pMemberDescr)));
452 if (pReturn)
454 // dependent dispatch
455 cpp_call(
456 pThis, aVtableSlot,
457 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
458 0, 0, // no params
459 pReturn, pArgs, ppException );
461 else
463 // is SET
464 typelib_MethodParameter aParam;
465 aParam.pTypeRef =
466 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
467 aParam.bIn = sal_True;
468 aParam.bOut = sal_False;
470 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
471 OUString aVoidName("void");
472 typelib_typedescriptionreference_new(
473 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
475 // dependent dispatch
476 aVtableSlot.index += 1;
477 cpp_call(
478 pThis, aVtableSlot, // get, then set method
479 pReturnTypeRef,
480 1, &aParam,
481 pReturn, pArgs, ppException );
483 typelib_typedescriptionreference_release( pReturnTypeRef );
486 break;
488 case typelib_TypeClass_INTERFACE_METHOD:
490 #if OSL_DEBUG_LEVEL > 0
491 // determine vtable call index
492 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
493 assert(nMemberPos < pTypeDescr->nAllMembers && "### member pos out of range!");
494 #endif
496 VtableSlot aVtableSlot(
497 getVtableSlot(
498 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>
499 (pMemberDescr)));
501 switch (aVtableSlot.index)
503 // standard calls
504 case 1: // acquire uno interface
505 (*pUnoI->acquire)( pUnoI );
506 *ppException = 0;
507 break;
508 case 2: // release uno interface
509 (*pUnoI->release)( pUnoI );
510 *ppException = 0;
511 break;
512 case 0: // queryInterface() opt
514 typelib_TypeDescription * pTD = 0;
515 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
516 if (pTD)
518 uno_Interface * pInterface = 0;
519 (*pThis->getBridge()->getUnoEnv()->getRegisteredInterface)(
520 pThis->getBridge()->getUnoEnv(),
521 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
523 if (pInterface)
525 ::uno_any_construct(
526 reinterpret_cast< uno_Any * >( pReturn ),
527 &pInterface, pTD, 0 );
528 (*pInterface->release)( pInterface );
529 TYPELIB_DANGER_RELEASE( pTD );
530 *ppException = 0;
531 break;
533 TYPELIB_DANGER_RELEASE( pTD );
535 } // else perform queryInterface()
536 [[fallthrough]];
537 default:
538 // dependent dispatch
539 cpp_call(
540 pThis, aVtableSlot,
541 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
542 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
543 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
544 pReturn, pArgs, ppException );
546 break;
548 default:
550 ::com::sun::star::uno::RuntimeException aExc(
551 "illegal member type description!",
552 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
554 Type const & rExcType = cppu::UnoType<decltype(aExc)>::get();
555 // binary identical null reference
556 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
563 #endif
565 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */