1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/config.h>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <o3tl/runtimetooustring.hxx>
33 #include "unointerfaceproxy.hxx"
34 #include "vtables.hxx"
38 using namespace ::com::sun::star::uno
;
42 bool is_hfa_struct(const typelib_TypeDescription
* type
)
44 const typelib_CompoundTypeDescription
* p
45 = reinterpret_cast< const typelib_CompoundTypeDescription
* >(type
);
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
)
58 bool return_in_x8( typelib_TypeDescriptionReference
*pTypeRef
)
60 if (bridges::cpp_uno::shared::isSimpleType(pTypeRef
))
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
))
73 TYPELIB_DANGER_RELEASE( pTypeDescr
);
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];
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];
98 case typelib_TypeClass_FLOAT
:
99 *(float*)pRegisterReturn
= *(float*)&pFPR
[0];
101 case typelib_TypeClass_DOUBLE
:
102 *(double*)pRegisterReturn
= pFPR
[0];
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];
119 void callVirtualMethod(
121 sal_Int32 nVtableIndex
,
122 void *pRegisterReturn
,
123 typelib_TypeDescriptionReference
*pReturnType
,
131 CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
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
);
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"
158 " stp x0, x1, %[pgpr_0]\n"
159 " str d0, %[pfpr_0]\n"
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
)
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 ); \
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 ); \
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 ); \
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 ); \
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 ); \
208 *pDS++ = *reinterpret_cast<double *>( pSV );
210 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
211 INSERT_DOUBLE( pSV, nr, pGPR, pDS )
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
];
228 // storage and counter for SIMD/FP registers
229 double pFPR
[arm::MAX_FPR_REGS
];
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
245 // complex return via x8
246 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
247 ? alloca( pReturnTypeDescr
->nSize
)
248 : pUnoReturn
); // direct way
252 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
253 + aVtableSlot
.offset
;
254 INSERT_INT64( &pAdjustedThisPtr
, nGPR
, pGPR
, pStack
);
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
);
285 case typelib_TypeClass_LONG
:
286 case typelib_TypeClass_UNSIGNED_LONG
:
287 case typelib_TypeClass_ENUM
:
288 INSERT_INT32( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
290 case typelib_TypeClass_SHORT
:
291 case typelib_TypeClass_CHAR
:
292 case typelib_TypeClass_UNSIGNED_SHORT
:
293 INSERT_INT16( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
295 case typelib_TypeClass_BOOLEAN
:
296 case typelib_TypeClass_BYTE
:
297 INSERT_INT8( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
299 case typelib_TypeClass_FLOAT
:
300 INSERT_FLOAT( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
302 case typelib_TypeClass_DOUBLE
:
303 INSERT_DOUBLE( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
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!
317 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
319 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
320 // will be released at reconversion
321 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
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
;
336 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
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
);
351 pAdjustedThisPtr
, aVtableSlot
.index
,
352 pCppReturn
, pReturnTypeRef
,
354 (pStack
- pStackStart
),
357 } catch (css::uno::Exception
&) {
359 } catch (std::exception
& e
) {
360 throw css::uno::RuntimeException(
361 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
362 + o3tl::runtimeToOUString(e
.what()));
364 throw css::uno::RuntimeException("C++ code threw unknown exception");
367 // NO exception occurred...
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() );
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
);
396 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
398 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
399 pThis
->getBridge()->getCpp2Uno() );
400 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
405 // fill uno exception
406 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
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
] );
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
)
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
;
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!");
447 VtableSlot
aVtableSlot(
449 reinterpret_cast<typelib_InterfaceAttributeTypeDescription
const *>
454 // dependent dispatch
457 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
459 pReturn
, pArgs
, ppException
);
464 typelib_MethodParameter aParam
;
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;
478 pThis
, aVtableSlot
, // get, then set method
481 pReturn
, pArgs
, ppException
);
483 typelib_typedescriptionreference_release( pReturnTypeRef
);
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!");
496 VtableSlot
aVtableSlot(
498 reinterpret_cast<typelib_InterfaceMethodTypeDescription
const *>
501 switch (aVtableSlot
.index
)
504 case 1: // acquire uno interface
505 (*pUnoI
->acquire
)( pUnoI
);
508 case 2: // release uno interface
509 (*pUnoI
->release
)( pUnoI
);
512 case 0: // queryInterface() opt
514 typelib_TypeDescription
* pTD
= 0;
515 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
518 uno_Interface
* pInterface
= 0;
519 (*pThis
->getBridge()->getUnoEnv()->getRegisteredInterface
)(
520 pThis
->getBridge()->getUnoEnv(),
521 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
526 reinterpret_cast< uno_Any
* >( pReturn
),
527 &pInterface
, pTD
, 0 );
528 (*pInterface
->release
)( pInterface
);
529 TYPELIB_DANGER_RELEASE( pTD
);
533 TYPELIB_DANGER_RELEASE( pTD
);
535 } // else perform queryInterface()
538 // dependent dispatch
541 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
542 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
543 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
544 pReturn
, pArgs
, ppException
);
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 );
565 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */