Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_intel / uno2cpp.cxx
blob7c09fa89c1969584969ec7f54c03d9f7006c1a26
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.11 $
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 <malloc.h>
36 #include <com/sun/star/uno/genfunc.hxx>
37 #include "com/sun/star/uno/RuntimeException.hpp"
38 #include <uno/data.h>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
43 #include "bridges/cpp_uno/shared/vtables.hxx"
45 #include "share.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_TypeClass eReturnType,
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_TypeClass eReturnType,
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( eReturnType )
120 case typelib_TypeClass_HYPER:
121 case typelib_TypeClass_UNSIGNED_HYPER:
122 ((long*)pRegisterReturn)[1] = edx;
123 case typelib_TypeClass_LONG:
124 case typelib_TypeClass_UNSIGNED_LONG:
125 case typelib_TypeClass_CHAR:
126 case typelib_TypeClass_ENUM:
127 ((long*)pRegisterReturn)[0] = eax;
128 break;
129 case typelib_TypeClass_SHORT:
130 case typelib_TypeClass_UNSIGNED_SHORT:
131 *(unsigned short*)pRegisterReturn = eax;
132 break;
133 case typelib_TypeClass_BOOLEAN:
134 case typelib_TypeClass_BYTE:
135 *(unsigned char*)pRegisterReturn = eax;
136 break;
137 case typelib_TypeClass_FLOAT:
138 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
139 break;
140 case typelib_TypeClass_DOUBLE:
141 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
142 break;
143 default:
144 break;
148 //==================================================================================================
149 static void cpp_call(
150 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
151 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
152 typelib_TypeDescriptionReference * pReturnTypeRef,
153 sal_Int32 nParams, typelib_MethodParameter * pParams,
154 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
156 // max space for: [complex ret ptr], values|ptr ...
157 char * pCppStack =
158 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
159 char * pCppStackStart = pCppStack;
161 // return
162 typelib_TypeDescription * pReturnTypeDescr = 0;
163 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
164 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
166 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
168 if (pReturnTypeDescr)
170 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
172 pCppReturn = pUnoReturn; // direct way for simple types
174 else
176 // complex return via ptr
177 pCppReturn = *(void **)pCppStack
178 = (bridges::cpp_uno::shared::relatesToInterfaceType(
179 pReturnTypeDescr )
180 ? alloca( pReturnTypeDescr->nSize )
181 : pUnoReturn); // direct way
182 pCppStack += sizeof(void *);
185 // push this
186 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
187 + aVtableSlot.offset;
188 *(void**)pCppStack = pAdjustedThisPtr;
189 pCppStack += sizeof( void* );
191 // stack space
192 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
193 // args
194 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
195 // indizes of values this have to be converted (interface conversion cpp<=>uno)
196 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
197 // type descriptions for reconversions
198 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
200 sal_Int32 nTempIndizes = 0;
202 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
204 const typelib_MethodParameter & rParam = pParams[nPos];
205 typelib_TypeDescription * pParamTypeDescr = 0;
206 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
208 if (!rParam.bOut
209 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
211 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
212 pThis->getBridge()->getUno2Cpp() );
214 switch (pParamTypeDescr->eTypeClass)
216 case typelib_TypeClass_HYPER:
217 case typelib_TypeClass_UNSIGNED_HYPER:
218 case typelib_TypeClass_DOUBLE:
219 pCppStack += sizeof(sal_Int32); // extra long
220 break;
221 default:
222 break;
224 // no longer needed
225 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
227 else // ptr to complex value | ref
229 if (! rParam.bIn) // is pure out
231 // cpp out is constructed mem, uno out is not!
232 uno_constructData(
233 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
234 pParamTypeDescr );
235 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
236 // will be released at reconversion
237 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
239 // is in/inout
240 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
241 pParamTypeDescr ))
243 uno_copyAndConvertData(
244 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
245 pUnoArgs[nPos], pParamTypeDescr,
246 pThis->getBridge()->getUno2Cpp() );
248 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
249 // will be released at reconversion
250 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
252 else // direct way
254 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
255 // no longer needed
256 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
259 pCppStack += sizeof(sal_Int32); // standard parameter length
264 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
265 callVirtualMethod(
266 pAdjustedThisPtr, aVtableSlot.index,
267 pCppReturn, pReturnTypeDescr->eTypeClass,
268 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
269 // NO exception occured...
270 *ppUnoExc = 0;
272 // reconvert temporary params
273 for ( ; nTempIndizes--; )
275 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
276 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
278 if (pParams[nIndex].bIn)
280 if (pParams[nIndex].bOut) // inout
282 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
283 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
284 pThis->getBridge()->getCpp2Uno() );
287 else // pure out
289 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
290 pThis->getBridge()->getCpp2Uno() );
292 // destroy temp cpp param => cpp: every param was constructed
293 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
295 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
297 // return value
298 if (pCppReturn && pUnoReturn != pCppReturn)
300 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
301 pThis->getBridge()->getCpp2Uno() );
302 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
305 catch (...)
307 // fill uno exception
308 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
310 // temporary params
311 for ( ; nTempIndizes--; )
313 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
314 // destroy temp cpp param => cpp: every param was constructed
315 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
316 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
318 // return type
319 if (pReturnTypeDescr)
320 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
326 namespace bridges { namespace cpp_uno { namespace shared {
328 void unoInterfaceProxyDispatch(
329 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
330 void * pReturn, void * pArgs[], uno_Any ** ppException )
332 // is my surrogate
333 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
334 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
336 switch (pMemberDescr->eTypeClass)
338 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
340 VtableSlot aVtableSlot(
341 getVtableSlot(
342 reinterpret_cast<
343 typelib_InterfaceAttributeTypeDescription const * >(
344 pMemberDescr)));
345 if (pReturn)
347 // dependent dispatch
348 cpp_call(
349 pThis, aVtableSlot,
350 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
351 0, 0, // no params
352 pReturn, pArgs, ppException );
354 else
356 // is SET
357 typelib_MethodParameter aParam;
358 aParam.pTypeRef =
359 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
360 aParam.bIn = sal_True;
361 aParam.bOut = sal_False;
363 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
364 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
365 typelib_typedescriptionreference_new(
366 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
368 // dependent dispatch
369 aVtableSlot.index += 1; // get, then set method
370 cpp_call(
371 pThis, aVtableSlot,
372 pReturnTypeRef,
373 1, &aParam,
374 pReturn, pArgs, ppException );
376 typelib_typedescriptionreference_release( pReturnTypeRef );
379 break;
381 case typelib_TypeClass_INTERFACE_METHOD:
383 VtableSlot aVtableSlot(
384 getVtableSlot(
385 reinterpret_cast<
386 typelib_InterfaceMethodTypeDescription const * >(
387 pMemberDescr)));
388 switch (aVtableSlot.index)
390 // standard calls
391 case 1: // acquire uno interface
392 (*pUnoI->acquire)( pUnoI );
393 *ppException = 0;
394 break;
395 case 2: // release uno interface
396 (*pUnoI->release)( pUnoI );
397 *ppException = 0;
398 break;
399 case 0: // queryInterface() opt
401 typelib_TypeDescription * pTD = 0;
402 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
403 if (pTD)
405 uno_Interface * pInterface = 0;
406 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
407 pThis->pBridge->getUnoEnv(),
408 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
410 if (pInterface)
412 ::uno_any_construct(
413 reinterpret_cast< uno_Any * >( pReturn ),
414 &pInterface, pTD, 0 );
415 (*pInterface->release)( pInterface );
416 TYPELIB_DANGER_RELEASE( pTD );
417 *ppException = 0;
418 break;
420 TYPELIB_DANGER_RELEASE( pTD );
422 } // else perform queryInterface()
423 default:
424 // dependent dispatch
425 cpp_call(
426 pThis, aVtableSlot,
427 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
428 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
429 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
430 pReturn, pArgs, ppException );
432 break;
434 default:
436 ::com::sun::star::uno::RuntimeException aExc(
437 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
438 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
440 Type const & rExcType = ::getCppuType( &aExc );
441 // binary identical null reference
442 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
447 } } }