LibreOffice Module bridges (master) 1
gcc3_linux_intel/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/config.h>
21
22#include <exception>
23#include <typeinfo>
24
25#include <sal/alloca.h>
26
27#include <com/sun/star/uno/genfunc.hxx>
28#include <com/sun/star/uno/Exception.hpp>
29#include <com/sun/star/uno/RuntimeException.hpp>
31#include <uno/data.h>
32
33#include <bridge.hxx>
34#include <types.hxx>
35#include <unointerfaceproxy.hxx>
36#include <vtables.hxx>
37
38#include "callvirtualmethod.hxx"
39#include "share.hxx"
40
41using namespace ::com::sun::star::uno;
42
43namespace
44{
45
46void cpp_call(
49 typelib_TypeDescriptionReference * pReturnTypeRef,
50 sal_Int32 nParams, typelib_MethodParameter * pParams,
51 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
52{
53 // max space for: [complex ret ptr], values|ptr ...
54 char * pCppStack =
55 static_cast<char *>(alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) ));
56 char * pCppStackStart = pCppStack;
57
58 // return
59 typelib_TypeDescription * pReturnTypeDescr = nullptr;
60 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
61 assert(pReturnTypeDescr);
62
63 void * pCppReturn = nullptr; // if != 0 && != pUnoReturn, needs reconversion
64 bool bSimpleReturn = true;
65
66 if (pReturnTypeDescr)
67 {
68 bSimpleReturn = x86::isSimpleReturnType(pReturnTypeDescr);
69 if (bSimpleReturn)
70 {
71 pCppReturn = pUnoReturn; // direct way for simple types
72 }
73 else
74 {
75 // complex return via ptr
76 pCppReturn = *reinterpret_cast<void **>(pCppStack)
78 pReturnTypeDescr )
79 ? alloca( pReturnTypeDescr->nSize )
80 : pUnoReturn); // direct way
81 pCppStack += sizeof(void *);
82 }
83 }
84 // push this
85 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
86 + aVtableSlot.offset;
87 *reinterpret_cast<void **>(pCppStack) = pAdjustedThisPtr;
88 pCppStack += sizeof( void* );
89
90 // stack space
91 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
92 // args
93 void ** pCppArgs = static_cast<void **>(alloca( 3 * sizeof(void *) * nParams ));
94 // indices of values this have to be converted (interface conversion cpp<=>uno)
95 sal_Int32 * pTempIndices = reinterpret_cast<sal_Int32 *>(pCppArgs + nParams);
96 // type descriptions for reconversions
97 typelib_TypeDescription ** ppTempParamTypeDescr = reinterpret_cast<typelib_TypeDescription **>(pCppArgs + (2 * nParams));
98
99 sal_Int32 nTempIndices = 0;
100
101 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
102 {
103 const typelib_MethodParameter & rParam = pParams[nPos];
104 typelib_TypeDescription * pParamTypeDescr = nullptr;
105 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
106
107 if (!rParam.bOut
108 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
109 {
110 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
111 pThis->getBridge()->getUno2Cpp() );
112
113 switch (pParamTypeDescr->eTypeClass)
114 {
115 case typelib_TypeClass_HYPER:
116 case typelib_TypeClass_UNSIGNED_HYPER:
117 case typelib_TypeClass_DOUBLE:
118 pCppStack += sizeof(sal_Int32); // extra long
119 break;
120 default:
121 break;
122 }
123 // no longer needed
124 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
125 }
126 else // ptr to complex value | ref
127 {
128 if (! rParam.bIn) // is pure out
129 {
130 // cpp out is constructed mem, uno out is not!
132 *reinterpret_cast<void **>(pCppStack) = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
133 pParamTypeDescr );
134 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
135 // will be released at reconversion
136 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
137 }
138 // is in/inout
140 pParamTypeDescr ))
141 {
143 *reinterpret_cast<void **>(pCppStack) = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
144 pUnoArgs[nPos], pParamTypeDescr,
145 pThis->getBridge()->getUno2Cpp() );
146
147 pTempIndices[nTempIndices] = nPos; // has to be reconverted
148 // will be released at reconversion
149 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
150 }
151 else // direct way
152 {
153 *reinterpret_cast<void **>(pCppStack) = pCppArgs[nPos] = pUnoArgs[nPos];
154 // no longer needed
155 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
156 }
157 }
158 pCppStack += sizeof(sal_Int32); // standard parameter length
159 }
160
161 try
162 {
163 assert( !( (pCppStack - pCppStackStart ) & 3) && "UNALIGNED STACK !!! (Please DO panic)" );
164 try {
166 pAdjustedThisPtr, aVtableSlot.index,
167 pCppReturn, pReturnTypeDescr, bSimpleReturn,
168 reinterpret_cast<sal_Int32 *>(pCppStackStart), (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
169 } catch (css::uno::Exception &) {
170 throw;
171 } catch (std::exception & e) {
172 throw css::uno::RuntimeException(
173 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name()) + ": "
174 + o3tl::runtimeToOUString(e.what()));
175 } catch (...) {
176 throw css::uno::RuntimeException("C++ code threw unknown exception");
177 }
178 // NO exception occurred...
179 *ppUnoExc = nullptr;
180
181 // reconvert temporary params
182 for ( ; nTempIndices--; )
183 {
184 sal_Int32 nIndex = pTempIndices[nTempIndices];
185 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
186
187 if (pParams[nIndex].bIn)
188 {
189 if (pParams[nIndex].bOut) // inout
190 {
191 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, nullptr ); // destroy uno value
192 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
193 pThis->getBridge()->getCpp2Uno() );
194 }
195 }
196 else // pure out
197 {
198 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
199 pThis->getBridge()->getCpp2Uno() );
200 }
201 // destroy temp cpp param => cpp: every param was constructed
202 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
203
204 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
205 }
206 // return value
207 if (pCppReturn && pUnoReturn != pCppReturn)
208 {
209 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
210 pThis->getBridge()->getCpp2Uno() );
211 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
212 }
213 }
214 catch (...)
215 {
216 // fill uno exception
218
219 // temporary params
220 for ( ; nTempIndices--; )
221 {
222 sal_Int32 nIndex = pTempIndices[nTempIndices];
223 // destroy temp cpp param => cpp: every param was constructed
224 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndices], cpp_release );
225 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
226 }
227 // return type
228 if (pReturnTypeDescr)
229 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
230 }
231}
232
233}
234
235namespace x86
236{
237 bool isSimpleReturnType(typelib_TypeDescription * pTD, bool recursive)
238 {
240 return true;
241#if defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) || \
242 defined(MACOSX) || defined(DRAGONFLY)
243 // Only structs of exactly 1, 2, 4, or 8 bytes are returned through
244 // registers, see <http://developer.apple.com/documentation/DeveloperTools/
245 // Conceptual/LowLevelABI/Articles/IA32.html>:
246 if (pTD->eTypeClass == typelib_TypeClass_STRUCT &&
247 (recursive || pTD->nSize <= 2 || pTD->nSize == 4 || pTD->nSize == 8))
248 {
249 typelib_CompoundTypeDescription *const pCompTD =
250 (typelib_CompoundTypeDescription *) pTD;
251 for ( sal_Int32 pos = pCompTD->nMembers; pos--; ) {
252 typelib_TypeDescription * pMemberTD = 0;
253 TYPELIB_DANGER_GET( &pMemberTD, pCompTD->ppTypeRefs[pos] );
254 bool const b = isSimpleReturnType(pMemberTD, true);
255 TYPELIB_DANGER_RELEASE( pMemberTD );
256 if (! b)
257 return false;
258 }
259 return true;
260 }
261#else
262 (void)recursive;
263#endif
264 return false;
265 }
266}
267
268namespace bridges::cpp_uno::shared {
269
271 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
272 void * pReturn, void * pArgs[], uno_Any ** ppException )
273{
274 // is my surrogate
276 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
277
278 switch (pMemberDescr->eTypeClass)
279 {
280 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
281 {
282 VtableSlot aVtableSlot(
284 reinterpret_cast<
285 typelib_InterfaceAttributeTypeDescription const * >(
286 pMemberDescr)));
287 if (pReturn)
288 {
289 // dependent dispatch
290 cpp_call(
291 pThis, aVtableSlot,
292 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberDescr)->pAttributeTypeRef,
293 0, nullptr, // no params
294 pReturn, pArgs, ppException );
295 }
296 else
297 {
298 // is SET
299 typelib_MethodParameter aParam;
300 aParam.pTypeRef =
301 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberDescr)->pAttributeTypeRef;
302 aParam.bIn = true;
303 aParam.bOut = false;
304
305 typelib_TypeDescriptionReference * pReturnTypeRef = nullptr;
306 OUString aVoidName("void");
308 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
309
310 // dependent dispatch
311 aVtableSlot.index += 1; // get, then set method
312 cpp_call(
313 pThis, aVtableSlot,
314 pReturnTypeRef,
315 1, &aParam,
316 pReturn, pArgs, ppException );
317
319 }
320
321 break;
322 }
323 case typelib_TypeClass_INTERFACE_METHOD:
324 {
325 VtableSlot aVtableSlot(
327 reinterpret_cast<
328 typelib_InterfaceMethodTypeDescription const * >(
329 pMemberDescr)));
330 switch (aVtableSlot.index)
331 {
332 // standard calls
333 case 1: // acquire uno interface
334 (*pUnoI->acquire)( pUnoI );
335 *ppException = nullptr;
336 break;
337 case 2: // release uno interface
338 (*pUnoI->release)( pUnoI );
339 *ppException = nullptr;
340 break;
341 case 0: // queryInterface() opt
342 {
343 typelib_TypeDescription * pTD = nullptr;
344 TYPELIB_DANGER_GET( &pTD, static_cast< Type * >( pArgs[0] )->getTypeLibType() );
345 if (pTD)
346 {
347 uno_Interface * pInterface = nullptr;
348 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
349 pThis->pBridge->getUnoEnv(),
350 reinterpret_cast<void **>(&pInterface), pThis->oid.pData, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
351
352 if (pInterface)
353 {
354 ::uno_any_construct(
355 static_cast< uno_Any * >( pReturn ),
356 &pInterface, pTD, nullptr );
357 (*pInterface->release)( pInterface );
358 TYPELIB_DANGER_RELEASE( pTD );
359 *ppException = nullptr;
360 break;
361 }
362 TYPELIB_DANGER_RELEASE( pTD );
363 }
364 [[fallthrough]]; // else perform queryInterface()
365 }
366 default:
367 // dependent dispatch
368 cpp_call(
369 pThis, aVtableSlot,
370 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->pReturnTypeRef,
371 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->nParams,
372 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberDescr)->pParams,
373 pReturn, pArgs, ppException );
374 }
375 break;
376 }
377 default:
378 {
379 ::com::sun::star::uno::RuntimeException aExc(
380 "illegal member type description!",
382
383 Type const & rExcType = cppu::UnoType<decltype(aExc)>::get();
384 // binary identical null reference
385 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), nullptr );
386 }
387 }
388}
389
390}
391
392/* 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()
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
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
Type
OUString runtimeToOUString(char const *runtimeString)
bool isSimpleReturnType(typelib_TypeDescription *pTD, bool recursive=false)
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()
size_t pos