Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / mingw_intel / uno2cpp.cxx
blob4c864c5245bb1dbbf29bb7a46e7fe4cdea612504
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include <uno/data.h>
37 #include <sal/alloca.h>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/vtables.hxx"
44 #include "share.hxx"
45 #include "smallstruct.hxx"
47 using namespace ::rtl;
48 using namespace ::com::sun::star::uno;
50 namespace
53 //==================================================================================================
54 // The call instruction within the asm section of callVirtualMethod may throw
55 // exceptions. So that the compiler handles this correctly, it is important
56 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
57 // never happens at runtime), which in turn can throw exceptions, and (b)
58 // callVirtualMethod is not inlined at its call site (so that any exceptions are
59 // caught which are thrown from the instruction calling callVirtualMethod):
60 void callVirtualMethod(
61 void * pAdjustedThisPtr,
62 sal_Int32 nVtableIndex,
63 void * pRegisterReturn,
64 typelib_TypeDescription const * returnType,
65 sal_Int32 * pStackLongs,
66 sal_Int32 nStackLongs ) __attribute__((noinline));
68 void callVirtualMethod(
69 void * pAdjustedThisPtr,
70 sal_Int32 nVtableIndex,
71 void * pRegisterReturn,
72 typelib_TypeDescription const * returnType,
73 sal_Int32 * pStackLongs,
74 sal_Int32 nStackLongs )
76 // parameter list is mixed list of * and values
77 // reference parameters are pointers
79 OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
80 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
81 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
83 // never called
84 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
86 volatile long edx = 0, eax = 0; // for register returns
87 void * stackptr;
88 asm volatile (
89 "mov %%esp, %6\n\t"
90 // copy values
91 "mov %0, %%eax\n\t"
92 "mov %%eax, %%edx\n\t"
93 "dec %%edx\n\t"
94 "shl $2, %%edx\n\t"
95 "add %1, %%edx\n"
96 "Lcopy:\n\t"
97 "pushl 0(%%edx)\n\t"
98 "sub $4, %%edx\n\t"
99 "dec %%eax\n\t"
100 "jne Lcopy\n\t"
101 // do the actual call
102 "mov %2, %%edx\n\t"
103 "mov 0(%%edx), %%edx\n\t"
104 "mov %3, %%eax\n\t"
105 "shl $2, %%eax\n\t"
106 "add %%eax, %%edx\n\t"
107 "mov 0(%%edx), %%edx\n\t"
108 "call *%%edx\n\t"
109 // save return registers
110 "mov %%eax, %4\n\t"
111 "mov %%edx, %5\n\t"
112 // cleanup stack
113 "mov %6, %%esp\n\t"
115 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
116 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
117 : "eax", "edx" );
118 switch( returnType->eTypeClass )
120 case typelib_TypeClass_VOID:
121 break;
122 case typelib_TypeClass_HYPER:
123 case typelib_TypeClass_UNSIGNED_HYPER:
124 ((long*)pRegisterReturn)[1] = edx;
125 case typelib_TypeClass_LONG:
126 case typelib_TypeClass_UNSIGNED_LONG:
127 case typelib_TypeClass_CHAR:
128 case typelib_TypeClass_ENUM:
129 ((long*)pRegisterReturn)[0] = eax;
130 break;
131 case typelib_TypeClass_SHORT:
132 case typelib_TypeClass_UNSIGNED_SHORT:
133 *(unsigned short*)pRegisterReturn = eax;
134 break;
135 case typelib_TypeClass_BOOLEAN:
136 case typelib_TypeClass_BYTE:
137 *(unsigned char*)pRegisterReturn = eax;
138 break;
139 case typelib_TypeClass_FLOAT:
140 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
141 break;
142 case typelib_TypeClass_DOUBLE:
143 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
144 break;
145 case typelib_TypeClass_STRUCT:
146 if (bridges::cpp_uno::shared::isSmallStruct(returnType)) {
147 if (returnType->nSize <= 1) {
148 *(unsigned char*)pRegisterReturn = eax;
150 else if (returnType->nSize <= 2) {
151 *(unsigned short*)pRegisterReturn = eax;
153 else if (returnType->nSize <= 8) {
154 ((long*)pRegisterReturn)[0] = eax;
155 if (returnType->nSize > 4) {
156 ((long*)pRegisterReturn)[1] = edx;
160 break;
161 default:
162 break;
166 //==================================================================================================
167 static void cpp_call(
168 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
169 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
170 typelib_TypeDescriptionReference * pReturnTypeRef,
171 sal_Int32 nParams, typelib_MethodParameter * pParams,
172 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
174 // max space for: [complex ret ptr], values|ptr ...
175 char * pCppStack =
176 #ifdef BROKEN_ALLOCA
177 (char *)malloc( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
178 #else
179 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
180 #endif
181 char * pCppStackStart = pCppStack;
183 // return
184 typelib_TypeDescription * pReturnTypeDescr = 0;
185 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
186 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
188 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
190 if (pReturnTypeDescr)
192 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
194 pCppReturn = pUnoReturn; // direct way for simple types
196 else
198 // complex return via ptr
199 pCppReturn
200 = (bridges::cpp_uno::shared::relatesToInterfaceType(
201 pReturnTypeDescr )
202 #ifdef BROKEN_ALLOCA
203 ? malloc( pReturnTypeDescr->nSize )
204 #else
205 ? alloca( pReturnTypeDescr->nSize )
206 #endif
207 : pUnoReturn); // direct way
208 if (!bridges::cpp_uno::shared::isSmallStruct(pReturnTypeDescr)) {
209 *(void **)pCppStack = pCppReturn;
210 pCppStack += sizeof(void *);
214 // push this
215 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
216 + aVtableSlot.offset;
217 *(void**)pCppStack = pAdjustedThisPtr;
218 pCppStack += sizeof( void* );
220 // stack space
221 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
222 // args
223 #ifdef BROKEN_ALLOCA
224 void ** pCppArgs = (void **)malloc( 3 * sizeof(void *) * nParams );
225 #else
226 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
227 #endif
228 // indizes of values this have to be converted (interface conversion cpp<=>uno)
229 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
230 // type descriptions for reconversions
231 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
233 sal_Int32 nTempIndizes = 0;
235 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
237 const typelib_MethodParameter & rParam = pParams[nPos];
238 typelib_TypeDescription * pParamTypeDescr = 0;
239 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
241 if (!rParam.bOut
242 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
244 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
245 pThis->getBridge()->getUno2Cpp() );
247 switch (pParamTypeDescr->eTypeClass)
249 case typelib_TypeClass_HYPER:
250 case typelib_TypeClass_UNSIGNED_HYPER:
251 case typelib_TypeClass_DOUBLE:
252 pCppStack += sizeof(sal_Int32); // extra long
253 break;
254 default:
255 break;
257 // no longer needed
258 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
260 else // ptr to complex value | ref
262 if (! rParam.bIn) // is pure out
264 // cpp out is constructed mem, uno out is not!
265 uno_constructData(
266 #ifdef BROKEN_ALLOCA
267 *(void **)pCppStack = pCppArgs[nPos] = malloc( pParamTypeDescr->nSize ),
268 #else
269 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
270 #endif
271 pParamTypeDescr );
272 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
273 // will be released at reconversion
274 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
276 // is in/inout
277 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
278 pParamTypeDescr ))
280 uno_copyAndConvertData(
281 #ifdef BROKEN_ALLOCA
282 *(void **)pCppStack = pCppArgs[nPos] = malloc( pParamTypeDescr->nSize ),
283 #else
284 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
285 #endif
286 pUnoArgs[nPos], pParamTypeDescr,
287 pThis->getBridge()->getUno2Cpp() );
289 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
290 // will be released at reconversion
291 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
293 else // direct way
295 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
296 // no longer needed
297 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
300 pCppStack += sizeof(sal_Int32); // standard parameter length
305 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
306 callVirtualMethod(
307 pAdjustedThisPtr, aVtableSlot.index,
308 pCppReturn, pReturnTypeDescr,
309 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
310 // NO exception occured...
311 *ppUnoExc = 0;
313 // reconvert temporary params
314 for ( ; nTempIndizes--; )
316 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
317 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
319 if (pParams[nIndex].bIn)
321 if (pParams[nIndex].bOut) // inout
323 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
324 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
325 pThis->getBridge()->getCpp2Uno() );
328 else // pure out
330 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
331 pThis->getBridge()->getCpp2Uno() );
333 // destroy temp cpp param => cpp: every param was constructed
334 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
335 #ifdef BROKEN_ALLOCA
336 free( pCppArgs[nIndex] );
337 #endif
339 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
341 // return value
342 if (pCppReturn && pUnoReturn != pCppReturn)
344 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
345 pThis->getBridge()->getCpp2Uno() );
346 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
349 catch (...)
351 // fill uno exception
352 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
354 // temporary params
355 for ( ; nTempIndizes--; )
357 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
358 // destroy temp cpp param => cpp: every param was constructed
359 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
360 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
361 #ifdef BROKEN_ALLOCA
362 free( pCppArgs[nIndex] );
363 #endif
365 // return type
366 if (pReturnTypeDescr)
367 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
369 if (pCppReturn && pUnoReturn != pCppReturn)
371 #ifdef BROKEN_ALLOCA
372 free( pCppReturn );
373 #endif
375 #ifdef BROKEN_ALLOCA
376 free( pCppStackStart );
377 #endif
382 namespace bridges { namespace cpp_uno { namespace shared {
384 void unoInterfaceProxyDispatch(
385 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
386 void * pReturn, void * pArgs[], uno_Any ** ppException )
388 // is my surrogate
389 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
390 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
392 switch (pMemberDescr->eTypeClass)
394 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
396 VtableSlot aVtableSlot(
397 getVtableSlot(
398 reinterpret_cast<
399 typelib_InterfaceAttributeTypeDescription const * >(
400 pMemberDescr)));
401 if (pReturn)
403 // dependent dispatch
404 cpp_call(
405 pThis, aVtableSlot,
406 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
407 0, 0, // no params
408 pReturn, pArgs, ppException );
410 else
412 // is SET
413 typelib_MethodParameter aParam;
414 aParam.pTypeRef =
415 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
416 aParam.bIn = sal_True;
417 aParam.bOut = sal_False;
419 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
420 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
421 typelib_typedescriptionreference_new(
422 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
424 // dependent dispatch
425 aVtableSlot.index += 1; // get, then set method
426 cpp_call(
427 pThis, aVtableSlot,
428 pReturnTypeRef,
429 1, &aParam,
430 pReturn, pArgs, ppException );
432 typelib_typedescriptionreference_release( pReturnTypeRef );
435 break;
437 case typelib_TypeClass_INTERFACE_METHOD:
439 VtableSlot aVtableSlot(
440 getVtableSlot(
441 reinterpret_cast<
442 typelib_InterfaceMethodTypeDescription const * >(
443 pMemberDescr)));
444 switch (aVtableSlot.index)
446 // standard calls
447 case 1: // acquire uno interface
448 (*pUnoI->acquire)( pUnoI );
449 *ppException = 0;
450 break;
451 case 2: // release uno interface
452 (*pUnoI->release)( pUnoI );
453 *ppException = 0;
454 break;
455 case 0: // queryInterface() opt
457 typelib_TypeDescription * pTD = 0;
458 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
459 if (pTD)
461 uno_Interface * pInterface = 0;
462 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
463 pThis->pBridge->getUnoEnv(),
464 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
466 if (pInterface)
468 ::uno_any_construct(
469 reinterpret_cast< uno_Any * >( pReturn ),
470 &pInterface, pTD, 0 );
471 (*pInterface->release)( pInterface );
472 TYPELIB_DANGER_RELEASE( pTD );
473 *ppException = 0;
474 break;
476 TYPELIB_DANGER_RELEASE( pTD );
478 } // else perform queryInterface()
479 default:
480 // dependent dispatch
481 cpp_call(
482 pThis, aVtableSlot,
483 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
484 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
485 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
486 pReturn, pArgs, ppException );
488 break;
490 default:
492 ::com::sun::star::uno::RuntimeException aExc(
493 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
494 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
496 Type const & rExcType = ::getCppuType( &aExc );
497 // binary identical null reference
498 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
503 } } }