LibreOffice Module bridges (master) 1
gcc3_macosx_x86-64/uno2cpp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/alloca.h>
21
22#include <exception>
23#include <typeinfo>
24
25#include <rtl/alloc.h>
26
27#include <com/sun/star/uno/genfunc.hxx>
28#include <com/sun/star/uno/RuntimeException.hpp>
30#include <uno/data.h>
31
32#include <bridge.hxx>
33#include <types.hxx>
34#include <unointerfaceproxy.hxx>
35#include <vtables.hxx>
36
37#include "abi.hxx"
38#include "callvirtualmethod.hxx"
39#include "share.hxx"
40
41using namespace ::com::sun::star::uno;
42
43namespace {
44
45// Functions for easier insertion of values to registers or stack
46// pSV - pointer to the source
47// nr - order of the value [will be increased if stored to register]
48// pFPR, pGPR - pointer to the registers
49// pDS - pointer to the stack [will be increased if stored here]
50
51// The value in %xmm register is already prepared to be retrieved as a float,
52// thus we treat float and double the same
54 void const * pSV, sal_uInt32 & nr, double * pFPR, sal_uInt64 *& pDS)
55{
56 if ( nr < x86_64::MAX_SSE_REGS )
57 pFPR[nr++] = *static_cast<double const *>( pSV );
58 else
59 *pDS++ = *static_cast<sal_uInt64 const *>( pSV ); // verbatim!
60}
61
62void INSERT_INT64(
63 void const * pSV, sal_uInt32 & nr, sal_uInt64 * pGPR, sal_uInt64 *& pDS)
64{
65 if ( nr < x86_64::MAX_GPR_REGS )
66 pGPR[nr++] = *static_cast<sal_uInt64 const *>( pSV );
67 else
68 *pDS++ = *static_cast<sal_uInt64 const *>( pSV );
69}
70
71void INSERT_INT32(
72 void const * pSV, sal_uInt32 & nr, sal_uInt64 * pGPR, sal_uInt64 *& pDS)
73{
74 if ( nr < x86_64::MAX_GPR_REGS )
75 pGPR[nr++] = *static_cast<sal_uInt32 const *>( pSV );
76 else
77 *pDS++ = *static_cast<sal_uInt32 const *>( pSV );
78}
79
80void INSERT_INT16(
81 void const * pSV, sal_uInt32 & nr, sal_uInt64 * pGPR, sal_uInt64 *& pDS)
82{
83 if ( nr < x86_64::MAX_GPR_REGS )
84 pGPR[nr++] = *static_cast<sal_uInt16 const *>( pSV );
85 else
86 *pDS++ = *static_cast<sal_uInt16 const *>( pSV );
87}
88
89void INSERT_INT8(
90 void const * pSV, sal_uInt32 & nr, sal_uInt64 * pGPR, sal_uInt64 *& pDS)
91{
92 if ( nr < x86_64::MAX_GPR_REGS )
93 pGPR[nr++] = *static_cast<sal_uInt8 const *>( pSV );
94 else
95 *pDS++ = *static_cast<sal_uInt8 const *>( pSV );
96}
97
98}
99
100static void cpp_call(
103 typelib_TypeDescriptionReference * pReturnTypeRef,
104 sal_Int32 nParams, typelib_MethodParameter * pParams,
105 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
106{
107 // Maximum space for [complex ret ptr], values | ptr ...
108 // (but will be used less - some of the values will be in pGPR and pFPR)
109 sal_uInt64 *pStack = static_cast<sal_uInt64 *>(__builtin_alloca( (nParams + 3) * sizeof(sal_uInt64) ));
110 sal_uInt64 *pStackStart = pStack;
111
112 sal_uInt64 pGPR[x86_64::MAX_GPR_REGS];
113 sal_uInt32 nGPR = 0;
114
115 double pFPR[x86_64::MAX_SSE_REGS];
116 sal_uInt32 nFPR = 0;
117
118 // Return
119 typelib_TypeDescription * pReturnTypeDescr = nullptr;
120 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
121 assert(pReturnTypeDescr);
122
123 void * pCppReturn = nullptr; // if != 0 && != pUnoReturn, needs reconversion (see below)
124
125 bool bSimpleReturn = true;
126 if ( pReturnTypeDescr )
127 {
128 if ( x86_64::return_in_hidden_param( pReturnTypeRef ) )
129 bSimpleReturn = false;
130
131 if ( bSimpleReturn )
132 pCppReturn = pUnoReturn; // direct way for simple types
133 else
134 {
135 // complex return via ptr
136 pCppReturn = bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )?
137 __builtin_alloca( pReturnTypeDescr->nSize ) : pUnoReturn;
138 INSERT_INT64( &pCppReturn, nGPR, pGPR, pStack );
139 }
140 }
141
142 // Push "this" pointer
143 void * pAdjustedThisPtr = reinterpret_cast< void ** >( pThis->getCppI() ) + aVtableSlot.offset;
144 INSERT_INT64( &pAdjustedThisPtr, nGPR, pGPR, pStack );
145
146 // Args
147 void ** pCppArgs = static_cast<void **>(alloca( 3 * sizeof(void *) * nParams ));
148 // Indices of values this have to be converted (interface conversion cpp<=>uno)
149 sal_Int32 * pTempIndices = reinterpret_cast<sal_Int32 *>(pCppArgs + nParams);
150 // Type descriptions for reconversions
151 typelib_TypeDescription ** ppTempParamTypeDescr = reinterpret_cast<typelib_TypeDescription **>(pCppArgs + (2 * nParams));
152
153 sal_Int32 nTempIndices = 0;
154
155 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
156 {
157 const typelib_MethodParameter & rParam = pParams[nPos];
158 typelib_TypeDescription * pParamTypeDescr = nullptr;
159 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
160
161 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
162 {
163 uno_copyAndConvertData( pCppArgs[nPos] = alloca( 8 ), pUnoArgs[nPos], pParamTypeDescr,
164 pThis->getBridge()->getUno2Cpp() );
165
166 switch (pParamTypeDescr->eTypeClass)
167 {
168 case typelib_TypeClass_HYPER:
169 case typelib_TypeClass_UNSIGNED_HYPER:
170 INSERT_INT64( pCppArgs[nPos], nGPR, pGPR, pStack );
171 break;
172 case typelib_TypeClass_LONG:
173 case typelib_TypeClass_UNSIGNED_LONG:
174 case typelib_TypeClass_ENUM:
175 INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
176 break;
177 case typelib_TypeClass_SHORT:
178 case typelib_TypeClass_CHAR:
179 case typelib_TypeClass_UNSIGNED_SHORT:
180 INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
181 break;
182 case typelib_TypeClass_BOOLEAN:
183 case typelib_TypeClass_BYTE:
184 INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
185 break;
186 case typelib_TypeClass_FLOAT:
187 case typelib_TypeClass_DOUBLE:
188 INSERT_FLOAT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
189 break;
190 default:
191 break;
192 }
193
194 // no longer needed
195 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
196 }
197 else // ptr to complex value | ref
198 {
199 if (! rParam.bIn) // is pure out
200 {
201 // cpp out is constructed mem, uno out is not!
203 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
204 pParamTypeDescr );
205 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
206 // will be released at reconversion
207 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
208 }
209 // is in/inout
210 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
211 {
213 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
214 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
215
216 pTempIndices[nTempIndices] = nPos; // has to be reconverted
217 // will be released at reconversion
218 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
219 }
220 else // direct way
221 {
222 pCppArgs[nPos] = pUnoArgs[nPos];
223 // no longer needed
224 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
225 }
226 INSERT_INT64( &(pCppArgs[nPos]), nGPR, pGPR, pStack );
227 }
228 }
229
230 try
231 {
232 try {
234 pAdjustedThisPtr, aVtableSlot.index,
235 pCppReturn, pReturnTypeRef, bSimpleReturn,
236 pStackStart, ( pStack - pStackStart ),
237 pGPR, pFPR );
238 } catch (const Exception &) {
239 throw;
240 } catch (const std::exception & e) {
241 throw RuntimeException(
242 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name())
243 + ": " + o3tl::runtimeToOUString(e.what()));
244 } catch (...) {
245 throw RuntimeException("C++ code threw unknown exception");
246 }
247
248 *ppUnoExc = nullptr;
249
250 // reconvert temporary params
251 for ( ; nTempIndices--; )
252 {
253 sal_Int32 nIndex = pTempIndices[nTempIndices];
254 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
255
256 if (pParams[nIndex].bIn)
257 {
258 if (pParams[nIndex].bOut) // inout
259 {
260 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, nullptr ); // destroy uno value
261 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
262 pThis->getBridge()->getCpp2Uno() );
263 }
264 }
265 else // pure out
266 {
267 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
268 pThis->getBridge()->getCpp2Uno() );
269 }
270 // destroy temp cpp param => cpp: every param was constructed
271 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
272
273 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
274 }
275 // return value
276 if (pCppReturn && pUnoReturn != pCppReturn)
277 {
278 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
279 pThis->getBridge()->getCpp2Uno() );
280 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
281 }
282 }
283 catch (...)
284 {
285 // fill uno exception
287
288 // temporary params
289 for ( ; nTempIndices--; )
290 {
291 sal_Int32 nIndex = pTempIndices[nTempIndices];
292 // destroy temp cpp param => cpp: every param was constructed
293 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndices], cpp_release );
294 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
295 }
296 // return type
297 if (pReturnTypeDescr)
298 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
299 }
300}
301
302
303namespace bridges::cpp_uno::shared {
304
306 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
307 void * pReturn, void * pArgs[], uno_Any ** ppException )
308{
309 // is my surrogate
311 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
312#if OSL_DEBUG_LEVEL > 0
313 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
314#endif
315
316 switch (pMemberDescr->eTypeClass)
317 {
318 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
319 {
320#if OSL_DEBUG_LEVEL > 0
321 // determine vtable call index
322 sal_Int32 nMemberPos = reinterpret_cast<typelib_InterfaceMemberTypeDescription const *>(pMemberDescr)->nPosition;
323 assert(nMemberPos < pTypeDescr->nAllMembers);
324#endif
325 VtableSlot aVtableSlot(
327 reinterpret_cast<
328 typelib_InterfaceAttributeTypeDescription const * >(
329 pMemberDescr)));
330
331 if (pReturn)
332 {
333 // dependent dispatch
334 cpp_call(
335 pThis, aVtableSlot,
336 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberDescr)->pAttributeTypeRef,
337 0, nullptr, // no params
338 pReturn, pArgs, ppException );
339 }
340 else
341 {
342 // is SET
343 typelib_MethodParameter aParam;
344 aParam.pTypeRef =
345 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberDescr)->pAttributeTypeRef;
346 aParam.bIn = true;
347 aParam.bOut = false;
348
349 typelib_TypeDescriptionReference * pReturnTypeRef = nullptr;
350 OUString aVoidName("void");
352 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
353
354 // dependent dispatch
355 aVtableSlot.index += 1; // get, then set method
356 cpp_call(
357 pThis, aVtableSlot, // get, then set method
358 pReturnTypeRef,
359 1, &aParam,
360 pReturn, pArgs, ppException );
361
363 }
364
365 break;
366 }
367 case typelib_TypeClass_INTERFACE_METHOD:
368 {
369#if OSL_DEBUG_LEVEL > 0
370 // determine vtable call index
371 sal_Int32 nMemberPos = reinterpret_cast<typelib_InterfaceMemberTypeDescription const *>(pMemberDescr)->nPosition;
372 assert(nMemberPos < pTypeDescr->nAllMembers);
373#endif
374 VtableSlot aVtableSlot(
376 reinterpret_cast<
377 typelib_InterfaceMethodTypeDescription const * >(
378 pMemberDescr)));
379
380 switch (aVtableSlot.index)
381 {
382 // standard calls
383 case 1: // acquire uno interface
384 (*pUnoI->acquire)( pUnoI );
385 *ppException = nullptr;
386 break;
387 case 2: // release uno interface
388 (*pUnoI->release)( pUnoI );
389 *ppException = nullptr;
390 break;
391 case 0: // queryInterface() opt
392 {
393 typelib_TypeDescription * pTD = nullptr;
394 TYPELIB_DANGER_GET( &pTD, static_cast< Type * >( pArgs[0] )->getTypeLibType() );
395 if (pTD)
396 {
397 uno_Interface * pInterface = nullptr;
398 (*pThis->getBridge()->getUnoEnv()->getRegisteredInterface)(
399 pThis->getBridge()->getUnoEnv(),
400 reinterpret_cast<void **>(&pInterface), pThis->oid.pData, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
401
402 if (pInterface)
403 {
404 ::uno_any_construct(
405 static_cast< uno_Any * >( pReturn ),
406 &pInterface, pTD, nullptr );
407 (*pInterface->release)( pInterface );
408 TYPELIB_DANGER_RELEASE( pTD );
409 *ppException = nullptr;
410 break;
411 }
412 TYPELIB_DANGER_RELEASE( pTD );
413 }
414 [[fallthrough]]; // else perform queryInterface()
415 }
416 default:
417 // dependent dispatch
418 cpp_call(
419 pThis, aVtableSlot,
420 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->pReturnTypeRef,
421 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->nParams,
422 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->pParams,
423 pReturn, pArgs, ppException );
424 }
425 break;
426 }
427 default:
428 {
429 ::com::sun::star::uno::RuntimeException aExc(
430 "illegal member type description!",
432
433 Type const & rExcType = cppu::UnoType<decltype(aExc)>::get();
434 // binary identical null reference
435 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), nullptr );
436 }
437 }
438}
439
440}
441
442/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno_Mapping * getUno2Cpp()
Definition: bridge.hxx:73
uno_ExtEnvironment * getUnoEnv()
Definition: bridge.hxx:70
uno_Mapping * getCpp2Uno()
Definition: bridge.hxx:72
A uno proxy wrapping a cpp interface.
com::sun::star::uno::XInterface * getCppI()
typelib_InterfaceTypeDescription * pTypeDescr
void SAL_CALL uno_destructData(void *pValue, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_constructData(void *pMem, typelib_TypeDescription *pTypeDescr) SAL_THROW_EXTERN_C()
void SAL_CALL uno_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
char const * name
#define INSERT_INT32(pSV, nr, pGPR, pDS)
#define INSERT_INT8(pSV, nr, pGPR, pDS)
#define INSERT_INT16(pSV, nr, pGPR, pDS)
#define INSERT_INT64(pSV, nr, pGPR, pDS)
#define INSERT_FLOAT_DOUBLE(pSV, nr, pFPR, pDS)
static void cpp_call(bridges::cpp_uno::shared::UnoInterfaceProxy *pThis, bridges::cpp_uno::shared::VtableSlot aVtableSlot, typelib_TypeDescriptionReference *pReturnTypeRef, sal_Int32 nParams, typelib_MethodParameter *pParams, void *pUnoReturn, void *pUnoArgs[], uno_Any **ppUnoExc)
sal_Int32 nIndex
sal_uInt16 nPos
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
struct _uno_Any uno_Any
Definition: msvc/except.hxx:32
void fillUnoException(uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
void callVirtualMethod(void *pAdjustedThisPtr, sal_Int32 nVtableIndex, void *pRegisterReturn, typelib_TypeDescription *pReturnTypeDescr, bool bSimpleReturn, sal_Int32 *pStackLongs, sal_Int32 nStackLongs)
void unoInterfaceProxyDispatch(uno_Interface *pUnoI, typelib_TypeDescription const *pMemberDescr, void *pReturn, void **pArgs, uno_Any **ppException)
VtableSlot getVtableSlot(typelib_InterfaceAttributeTypeDescription const *ifcMember)
Calculates the vtable slot associated with an interface attribute member.
Definition: vtables.cxx:132
bool isSimpleType(typelib_TypeClass typeClass)
Determines whether a type is a "simple" type (VOID, BOOLEAN, BYTE, SHORT, UNSIGNED SHORT,...
Definition: types.cxx:28
bool relatesToInterfaceType(typelib_TypeDescription const *type)
Determines whether a type relates to an interface type (is itself an interface type,...
Definition: types.cxx:41
@ Exception
Type
OUString runtimeToOUString(char const *runtimeString)
bool return_in_hidden_param(typelib_TypeDescriptionReference *pTypeRef) noexcept
Does function that returns this type use a hidden parameter, or registers?
const sal_uInt32 MAX_SSE_REGS
const sal_uInt32 MAX_GPR_REGS
Represents a vtable slot of a C++ class.
Definition: vtables.hxx:60
sal_Int32 index
The index within the vtable.
Definition: vtables.hxx:76
sal_Int32 offset
The offset of the vtable.
Definition: vtables.hxx:68
void SAL_CALL typelib_typedescriptionreference_new(typelib_TypeDescriptionReference **ppTDR, typelib_TypeClass eTypeClass, rtl_uString *pTypeName) SAL_THROW_EXTERN_C()
void SAL_CALL typelib_typedescriptionreference_release(typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
unsigned char sal_uInt8