merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_os2_intel / uno2cpp.cxx
blobc1df7c20d83f26cf1a6ad76ba1353cb431c79b81
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 #include <malloc.h>
32 #include <sal/alloca.h>
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include <uno/data.h>
38 #include "bridges/cpp_uno/shared/bridge.hxx"
39 #include "bridges/cpp_uno/shared/types.hxx"
40 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/vtables.hxx"
43 #include "share.hxx"
45 using namespace ::rtl;
46 using namespace ::com::sun::star::uno;
48 namespace
51 //==================================================================================================
52 // The call instruction within the asm section of callVirtualMethod may throw
53 // exceptions. So that the compiler handles this correctly, it is important
54 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
55 // never happens at runtime), which in turn can throw exceptions, and (b)
56 // callVirtualMethod is not inlined at its call site (so that any exceptions are
57 // caught which are thrown from the instruction calling callVirtualMethod):
58 static void callVirtualMethod(
59 void * pAdjustedThisPtr,
60 sal_Int32 nVtableIndex,
61 void * pRegisterReturn,
62 typelib_TypeClass eReturnType,
63 sal_Int32 * pStackLongs,
64 sal_Int32 nStackLongs );
65 // __attribute__((noinline));
67 //==================================================================================================
68 static 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 /* figure out the address of the function we need to invoke */
87 unsigned long * mfunc; // actual function to be invoked
88 int off; // offset used to find function
89 void (*ptr)();
90 off = nVtableIndex;
91 off = off * 4; // 4 bytes per slot
92 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable
93 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
94 mfunc = *((unsigned long **)mfunc); // the function is stored at the address
95 ptr = (void (*)())mfunc;
97 volatile long edx = 0, eax = 0; // for register returns
98 void * stackptr;
99 asm volatile (
100 "mov %%esp, %6\n\t"
101 // copy values
102 "mov %0, %%eax\n\t"
103 "mov %%eax, %%edx\n\t"
104 "dec %%edx\n\t"
105 "shl $2, %%edx\n\t"
106 "add %1, %%edx\n"
107 "Lcopy:\n\t"
108 "pushl 0(%%edx)\n\t"
109 "sub $4, %%edx\n\t"
110 "dec %%eax\n\t"
111 "jne Lcopy\n\t"
113 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
114 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
115 : "eax", "edx" );
117 (*ptr)();
119 asm volatile (
120 // save return registers
121 "mov %%eax, %4\n\t"
122 "mov %%edx, %5\n\t"
123 // cleanup stack
124 "mov %6, %%esp\n\t"
126 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
127 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
128 : "eax", "edx" );
129 switch( eReturnType )
131 case typelib_TypeClass_HYPER:
132 case typelib_TypeClass_UNSIGNED_HYPER:
133 ((long*)pRegisterReturn)[1] = edx;
134 case typelib_TypeClass_LONG:
135 case typelib_TypeClass_UNSIGNED_LONG:
136 case typelib_TypeClass_CHAR:
137 case typelib_TypeClass_ENUM:
138 ((long*)pRegisterReturn)[0] = eax;
139 break;
140 case typelib_TypeClass_SHORT:
141 case typelib_TypeClass_UNSIGNED_SHORT:
142 *(unsigned short*)pRegisterReturn = eax;
143 break;
144 case typelib_TypeClass_BOOLEAN:
145 case typelib_TypeClass_BYTE:
146 *(unsigned char*)pRegisterReturn = eax;
147 break;
148 case typelib_TypeClass_FLOAT:
149 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
150 break;
151 case typelib_TypeClass_DOUBLE:
152 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
153 break;
157 //==================================================================================================
158 static void cpp_call(
159 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
160 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
161 typelib_TypeDescriptionReference * pReturnTypeRef,
162 sal_Int32 nParams, typelib_MethodParameter * pParams,
163 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
165 // max space for: [complex ret ptr], values|ptr ...
166 char * pCppStack =
167 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
168 char * pCppStackStart = pCppStack;
170 // return
171 typelib_TypeDescription * pReturnTypeDescr = 0;
172 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
173 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
175 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
177 if (pReturnTypeDescr)
179 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
181 pCppReturn = pUnoReturn; // direct way for simple types
183 else
185 // complex return via ptr
186 pCppReturn = *(void **)pCppStack
187 = (bridges::cpp_uno::shared::relatesToInterfaceType(
188 pReturnTypeDescr )
189 ? alloca( pReturnTypeDescr->nSize )
190 : pUnoReturn); // direct way
191 pCppStack += sizeof(void *);
194 // push this
195 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
196 + aVtableSlot.offset;
197 *(void**)pCppStack = pAdjustedThisPtr;
198 pCppStack += sizeof( void* );
200 // stack space
201 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
202 // args
203 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
204 // indizes of values this have to be converted (interface conversion cpp<=>uno)
205 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
206 // type descriptions for reconversions
207 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
209 sal_Int32 nTempIndizes = 0;
211 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
213 const typelib_MethodParameter & rParam = pParams[nPos];
214 typelib_TypeDescription * pParamTypeDescr = 0;
215 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
217 if (!rParam.bOut
218 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
220 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
221 pThis->getBridge()->getUno2Cpp() );
223 switch (pParamTypeDescr->eTypeClass)
225 case typelib_TypeClass_HYPER:
226 case typelib_TypeClass_UNSIGNED_HYPER:
227 case typelib_TypeClass_DOUBLE:
228 pCppStack += sizeof(sal_Int32); // extra long
230 // no longer needed
231 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
233 else // ptr to complex value | ref
235 if (! rParam.bIn) // is pure out
237 // cpp out is constructed mem, uno out is not!
238 uno_constructData(
239 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
240 pParamTypeDescr );
241 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
242 // will be released at reconversion
243 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
245 // is in/inout
246 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
247 pParamTypeDescr ))
249 uno_copyAndConvertData(
250 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
251 pUnoArgs[nPos], pParamTypeDescr,
252 pThis->getBridge()->getUno2Cpp() );
254 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
255 // will be released at reconversion
256 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
258 else // direct way
260 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
261 // no longer needed
262 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
265 pCppStack += sizeof(sal_Int32); // standard parameter length
270 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
271 callVirtualMethod(
272 pAdjustedThisPtr, aVtableSlot.index,
273 pCppReturn, pReturnTypeDescr->eTypeClass,
274 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
275 // NO exception occured...
276 *ppUnoExc = 0;
278 // reconvert temporary params
279 for ( ; nTempIndizes--; )
281 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
282 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
284 if (pParams[nIndex].bIn)
286 if (pParams[nIndex].bOut) // inout
288 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
289 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
290 pThis->getBridge()->getCpp2Uno() );
293 else // pure out
295 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
296 pThis->getBridge()->getCpp2Uno() );
298 // destroy temp cpp param => cpp: every param was constructed
299 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
301 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
303 // return value
304 if (pCppReturn && pUnoReturn != pCppReturn)
306 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
307 pThis->getBridge()->getCpp2Uno() );
308 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
311 catch (...)
313 // fill uno exception
314 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
316 // temporary params
317 for ( ; nTempIndizes--; )
319 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
320 // destroy temp cpp param => cpp: every param was constructed
321 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
322 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
324 // return type
325 if (pReturnTypeDescr)
326 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
332 namespace bridges { namespace cpp_uno { namespace shared {
334 void unoInterfaceProxyDispatch(
335 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
336 void * pReturn, void * pArgs[], uno_Any ** ppException )
338 // is my surrogate
339 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
340 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
341 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
343 switch (pMemberDescr->eTypeClass)
345 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
347 VtableSlot aVtableSlot(
348 getVtableSlot(
349 reinterpret_cast<
350 typelib_InterfaceAttributeTypeDescription const * >(
351 pMemberDescr)));
352 if (pReturn)
354 // dependent dispatch
355 cpp_call(
356 pThis, aVtableSlot,
357 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
358 0, 0, // no params
359 pReturn, pArgs, ppException );
361 else
363 // is SET
364 typelib_MethodParameter aParam;
365 aParam.pTypeRef =
366 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
367 aParam.bIn = sal_True;
368 aParam.bOut = sal_False;
370 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
371 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
372 typelib_typedescriptionreference_new(
373 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
375 // dependent dispatch
376 aVtableSlot.index += 1; // get, then set method
377 cpp_call(
378 pThis, aVtableSlot,
379 pReturnTypeRef,
380 1, &aParam,
381 pReturn, pArgs, ppException );
383 typelib_typedescriptionreference_release( pReturnTypeRef );
386 break;
388 case typelib_TypeClass_INTERFACE_METHOD:
390 VtableSlot aVtableSlot(
391 getVtableSlot(
392 reinterpret_cast<
393 typelib_InterfaceMethodTypeDescription const * >(
394 pMemberDescr)));
395 switch (aVtableSlot.index)
397 // standard calls
398 case 1: // acquire uno interface
399 (*pUnoI->acquire)( pUnoI );
400 *ppException = 0;
401 break;
402 case 2: // release uno interface
403 (*pUnoI->release)( pUnoI );
404 *ppException = 0;
405 break;
406 case 0: // queryInterface() opt
408 typelib_TypeDescription * pTD = 0;
409 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
410 if (pTD)
412 uno_Interface * pInterface = 0;
413 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
414 pThis->pBridge->getUnoEnv(),
415 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
417 if (pInterface)
419 ::uno_any_construct(
420 reinterpret_cast< uno_Any * >( pReturn ),
421 &pInterface, pTD, 0 );
422 (*pInterface->release)( pInterface );
423 TYPELIB_DANGER_RELEASE( pTD );
424 *ppException = 0;
425 break;
427 TYPELIB_DANGER_RELEASE( pTD );
429 } // else perform queryInterface()
430 default:
431 // dependent dispatch
432 cpp_call(
433 pThis, aVtableSlot,
434 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
435 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
436 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
437 pReturn, pArgs, ppException );
439 break;
441 default:
443 ::com::sun::star::uno::RuntimeException aExc(
444 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
445 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
447 Type const & rExcType = ::getCppuType( &aExc );
448 // binary identical null reference
449 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
454 } } }