LibreOffice Module bridges (master) 1
msvc_win32_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
21#include <malloc.h>
22
23#include <com/sun/star/uno/genfunc.hxx>
24#include <uno/data.h>
25
26#include <bridge.hxx>
27#include <types.hxx>
28#include <unointerfaceproxy.hxx>
29#include <vtables.hxx>
30
31#include <msvc/except.hxx>
32
33using namespace ::com::sun::star;
34
35namespace
36{
37
38inline void callVirtualMethod(
39 void * pAdjustedThisPtr, sal_Int32 nVtableIndex,
40 void * pRegisterReturn, typelib_TypeClass eReturnTypeClass,
41 sal_Int32 * pStackLongs, sal_Int32 nStackLongs )
42{
43 // parameter list is mixed list of * and values
44 // reference parameters are pointers
45
46 assert(pStackLongs && pAdjustedThisPtr);
47 static_assert( (sizeof(void *) == 4) &&
48 (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
49
50__asm
51 {
52 mov eax, nStackLongs
53 test eax, eax
54 je Lcall
55 // copy values
56 mov ecx, eax
57 shl eax, 2 // sizeof(sal_Int32) == 4
58 add eax, pStackLongs // params stack space
59Lcopy: sub eax, 4
60 push dword ptr [eax]
61 dec ecx
62 jne Lcopy
63Lcall:
64 // call
65 mov ecx, pAdjustedThisPtr
66 push ecx // this ptr
67 mov edx, [ecx] // pvft
68 mov eax, nVtableIndex
69 shl eax, 2 // sizeof(void *) == 4
70 add edx, eax
71 call [edx] // interface method call must be __cdecl!!!
72
73 // register return
74 mov ecx, eReturnTypeClass
75 cmp ecx, typelib_TypeClass_VOID
76 je Lcleanup
77 mov ebx, pRegisterReturn
78// int32
79 cmp ecx, typelib_TypeClass_LONG
80 je Lint32
81 cmp ecx, typelib_TypeClass_UNSIGNED_LONG
82 je Lint32
83 cmp ecx, typelib_TypeClass_ENUM
84 je Lint32
85// int8
86 cmp ecx, typelib_TypeClass_BOOLEAN
87 je Lint8
88 cmp ecx, typelib_TypeClass_BYTE
89 je Lint8
90// int16
91 cmp ecx, typelib_TypeClass_CHAR
92 je Lint16
93 cmp ecx, typelib_TypeClass_SHORT
94 je Lint16
95 cmp ecx, typelib_TypeClass_UNSIGNED_SHORT
96 je Lint16
97// float
98 cmp ecx, typelib_TypeClass_FLOAT
99 je Lfloat
100// double
101 cmp ecx, typelib_TypeClass_DOUBLE
102 je Ldouble
103// int64
104 cmp ecx, typelib_TypeClass_HYPER
105 je Lint64
106 cmp ecx, typelib_TypeClass_UNSIGNED_HYPER
107 je Lint64
108 jmp Lcleanup // no simple type
109Lint8:
110 mov byte ptr [ebx], al
111 jmp Lcleanup
112Lint16:
113 mov word ptr [ebx], ax
114 jmp Lcleanup
115Lfloat:
116 fstp dword ptr [ebx]
117 jmp Lcleanup
118Ldouble:
119 fstp qword ptr [ebx]
120 jmp Lcleanup
121Lint64:
122 mov dword ptr [ebx], eax
123 mov dword ptr [ebx+4], edx
124 jmp Lcleanup
125Lint32:
126 mov dword ptr [ebx], eax
127 jmp Lcleanup
128Lcleanup:
129 // cleanup stack (obsolete though because of function)
130 mov eax, nStackLongs
131 shl eax, 2 // sizeof(sal_Int32) == 4
132 add eax, 4 // this ptr
133 add esp, eax
134 }
135}
136
137void cpp_call(
140 typelib_TypeDescriptionReference * pReturnTypeRef,
141 sal_Int32 nParams,
142 typelib_MethodParameter * pParams,
143 void * pUnoReturn,
144 void * pUnoArgs[],
145 uno_Any ** ppUnoExc ) throw ()
146{
147 // max space for: [complex ret ptr], values|ptr ...
148 char * pCppStack = (char *)alloca( sizeof(sal_Int32) + (nParams * sizeof(sal_Int64)) );
149 char * pCppStackStart = pCppStack;
150
151 // return type
152 typelib_TypeDescription * pReturnTD = nullptr;
153 TYPELIB_DANGER_GET( &pReturnTD, pReturnTypeRef );
154 assert(pReturnTD);
155
156 void * pCppReturn = nullptr; // if != 0 && != pUnoReturn, needs reconversion
157
158 if (pReturnTD)
159 {
161 {
162 pCppReturn = pUnoReturn; // direct way for simple types
163 }
164 else
165 {
166 // complex return via ptr
167 pCppReturn = *(void **)pCppStack
169 pReturnTD )
170 ? alloca( pReturnTD->nSize )
171 : pUnoReturn); // direct way
172 pCppStack += sizeof(void *);
173 }
174 }
175
176 // stack space
177
178 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
179 // args
180 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
181 // indices of values this have to be converted (interface conversion cpp<=>uno)
182 sal_Int32 * pTempIndexes = (sal_Int32 *)(pCppArgs + nParams);
183 // type descriptions for reconversions
184 typelib_TypeDescription ** pTempParamTD = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
185
186 sal_Int32 nTempIndexes = 0;
187
188 for (int nPos = 0; nPos < nParams; ++nPos)
189 {
190 const typelib_MethodParameter & rParam = pParams[nPos];
191 typelib_TypeDescription * pParamTD = nullptr;
192 TYPELIB_DANGER_GET( &pParamTD, rParam.pTypeRef );
193
194 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType(pParamTD))
195 {
196 ::uno_copyAndConvertData(
197 pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTD,
198 pThis->getBridge()->getUno2Cpp() );
199
200 switch (pParamTD->eTypeClass)
201 {
202 case typelib_TypeClass_HYPER:
203 case typelib_TypeClass_UNSIGNED_HYPER:
204 case typelib_TypeClass_DOUBLE:
205 pCppStack += sizeof(sal_Int32); // extra long
206 break;
207 default:
208 break;
209 }
210 // no longer needed
211 TYPELIB_DANGER_RELEASE( pParamTD );
212 }
213 else // ptr to complex value | ref
214 {
215 if (! rParam.bIn) // is pure out
216 {
217 // C++ out is constructed mem, UNO out is not!
218 ::uno_constructData(
219 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTD->nSize ),
220 pParamTD );
221
222 // default constructed for C++ call
223 pTempIndexes[nTempIndexes] = nPos;
224
225 // will be released at reconversion
226 pTempParamTD[nTempIndexes++] = pParamTD;
227 }
228 // is in/inout
230 {
231 ::uno_copyAndConvertData(
232 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTD->nSize ),
233 pUnoArgs[nPos], pParamTD,
234 pThis->getBridge()->getUno2Cpp() );
235
236 // has to be reconverted
237 pTempIndexes[nTempIndexes] = nPos;
238
239 // will be released at reconversion
240 pTempParamTD[nTempIndexes++] = pParamTD;
241 }
242 else // direct way
243 {
244 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
245 // no longer needed
246 TYPELIB_DANGER_RELEASE( pParamTD );
247 }
248 }
249 pCppStack += sizeof(sal_Int32); // standard parameter length
250 }
251
252 __try
253 {
254 // pCppI is msci this pointer
256 reinterpret_cast< void ** >(pThis->getCppI()) + aVtableSlot.offset,
257 aVtableSlot.index,
258 pCppReturn, pReturnTD->eTypeClass,
259 (sal_Int32 *)pCppStackStart,
260 (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
261 }
262 __except (msvc_filterCppException(
263 GetExceptionInformation(),
264 *ppUnoExc, pThis->getBridge()->getCpp2Uno() ))
265 {
266 // *ppUnoExc was constructed by filter function
267 // temporary params
268 while (nTempIndexes--)
269 {
270 sal_Int32 nIndex = pTempIndexes[nTempIndexes];
271 // destroy temp C++ param => C++: every param was constructed
272 ::uno_destructData(
273 pCppArgs[nIndex], pTempParamTD[nTempIndexes],
274 uno::cpp_release );
275 TYPELIB_DANGER_RELEASE( pTempParamTD[nTempIndexes] );
276 }
277
278 // return type
279 if (pReturnTD)
280 TYPELIB_DANGER_RELEASE( pReturnTD );
281
282 return;
283 }
284
285 // NO exception occurred
286 *ppUnoExc = nullptr;
287
288 // reconvert temporary params
289 while (nTempIndexes--)
290 {
291 int nIndex = pTempIndexes[nTempIndexes];
292 typelib_TypeDescription * pParamTD =
293 pTempParamTD[nTempIndexes];
294
295 if (pParams[nIndex].bIn)
296 {
297 if (pParams[nIndex].bOut) // inout
298 {
299 ::uno_destructData(
300 pUnoArgs[nIndex], pParamTD, nullptr ); // destroy UNO value
301 ::uno_copyAndConvertData(
302 pUnoArgs[nIndex], pCppArgs[nIndex], pParamTD,
303 pThis->getBridge()->getCpp2Uno() );
304 }
305 }
306 else // pure out
307 {
308 ::uno_copyAndConvertData(
309 pUnoArgs[nIndex], pCppArgs[nIndex], pParamTD,
310 pThis->getBridge()->getCpp2Uno() );
311 }
312
313 // destroy temp C++ param => C++: every param was constructed
314 ::uno_destructData(
315 pCppArgs[nIndex], pParamTD, uno::cpp_release );
316
317 TYPELIB_DANGER_RELEASE( pParamTD );
318 }
319
320 // return value
321 if (pCppReturn && pUnoReturn != pCppReturn)
322 {
323 ::uno_copyAndConvertData(
324 pUnoReturn, pCppReturn, pReturnTD,
325 pThis->getBridge()->getCpp2Uno() );
326 ::uno_destructData(
327 pCppReturn, pReturnTD, uno::cpp_release );
328 }
329
330 // return type
331 if ( pReturnTD )
332 TYPELIB_DANGER_RELEASE( pReturnTD );
333}
334
335} // namespace
336
337namespace bridges::cpp_uno::shared {
338
340 uno_Interface * pUnoI,
341 const typelib_TypeDescription * pMemberTD,
342 void * pReturn,
343 void * pArgs[],
344 uno_Any ** ppException )
345{
346 // is my surrogate
348 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
349
350 switch (pMemberTD->eTypeClass)
351 {
352 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
353 {
354 VtableSlot aVtableSlot(
356 reinterpret_cast<
357 typelib_InterfaceAttributeTypeDescription const * >(
358 pMemberTD)));
359 if ( pReturn )
360 {
361 // is GET
362 cpp_call(
363 pThis, aVtableSlot,
364 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberTD)->pAttributeTypeRef,
365 0, nullptr, // no params
366 pReturn, pArgs, ppException );
367 }
368 else
369 {
370 // is SET
371 typelib_MethodParameter aParam;
372 aParam.pTypeRef =
373 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>(pMemberTD)->pAttributeTypeRef;
374 aParam.bIn = sal_True;
375 aParam.bOut = sal_False;
376
377 typelib_TypeDescriptionReference * pReturnTypeRef = nullptr;
378 OUString aVoidName("void");
380 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
381
382 aVtableSlot.index += 1; // get, then set method
383 cpp_call(
384 pThis, aVtableSlot,
385 pReturnTypeRef,
386 1, &aParam,
387 pReturn, pArgs, ppException );
388
390 }
391
392 break;
393 }
394 case typelib_TypeClass_INTERFACE_METHOD:
395 {
396 VtableSlot aVtableSlot(
398 reinterpret_cast<
399 typelib_InterfaceMethodTypeDescription const * >(
400 pMemberTD)));
401
402 switch (aVtableSlot.index)
403 {
404 case 1: // acquire UNO interface
405 (*pUnoI->acquire)( pUnoI );
406 *ppException = nullptr;
407 break;
408 case 2: // release UNO interface
409 (*pUnoI->release)( pUnoI );
410 *ppException = nullptr;
411 break;
412 case 0: // queryInterface() opt
413 {
414 typelib_TypeDescription * pTD = nullptr;
415 TYPELIB_DANGER_GET( &pTD, static_cast< uno::Type * >( pArgs[0] )->getTypeLibType() );
416
417 if ( pTD )
418 {
419 uno_Interface * pInterface = nullptr;
420 (*pThis->getBridge()->getUnoEnv()->getRegisteredInterface)(
421 pThis->getBridge()->getUnoEnv(),
422 reinterpret_cast<void **>(&pInterface), pThis->oid.pData, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
423
424 if ( pInterface )
425 {
426 ::uno_any_construct(
427 static_cast< uno_Any * >( pReturn ),
428 &pInterface, pTD, nullptr );
429 (*pInterface->release)( pInterface );
430 TYPELIB_DANGER_RELEASE( pTD );
431 *ppException = nullptr;
432 break;
433 }
434 TYPELIB_DANGER_RELEASE( pTD );
435 }
436 [[fallthrough]]; // else perform queryInterface()
437 }
438 default:
439 typelib_InterfaceMethodTypeDescription const* pMethodTD
440 = reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>(pMemberTD);
441
442 cpp_call(pThis, aVtableSlot, pMethodTD->pReturnTypeRef, pMethodTD->nParams, pMethodTD->pParams,
443 pReturn, pArgs, ppException);
444 }
445 break;
446 }
447 default:
448 {
449 uno::RuntimeException aExc("Illegal member type description!", uno::Reference<uno::XInterface>());
450
451 uno::Type const & rExcType = cppu::UnoType<decltype(aExc)>::get();
452 // binary identical null reference
453 ::uno_type_any_construct(*ppException, &aExc, rExcType.getTypeLibType(), nullptr);
454 }
455 }
456}
457
458}
459
460/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno_ExtEnvironment * getUnoEnv()
Definition: bridge.hxx:70
A uno proxy wrapping a cpp interface.
void callVirtualMethod(void *pThis, sal_uInt32 nVtableIndex, void *pRegisterReturn, typelib_TypeDescription *pReturnTypeDescr, bool bRegisterReturn, sal_uInt32 *pStack, sal_uInt32 nStack, sal_uInt32 *pGPR, double *pFPR) __attribute__((noinline))
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
int msvc_filterCppException(EXCEPTION_POINTERS *pPointers, uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
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
Represents a vtable slot of a C++ class.
Definition: vtables.hxx:60
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()
#define sal_True
#define sal_False