merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_netbsd_intel / uno2cpp.cxx
blobd829b4187c4f6edda9158c0103c46156a79d0c34
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.6 $
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 <sal/alloca.h>
35 #include <rtl/alloc.h>
37 #include <uno/data.h>
38 #include <bridges/cpp_uno/bridge.hxx>
39 #include <bridges/cpp_uno/type_misc.hxx>
41 #include "share.hxx"
44 using namespace ::rtl;
45 using namespace ::com::sun::star::uno;
47 namespace CPPU_CURRENT_NAMESPACE
50 void dummy_can_throw_anything( char const * );
52 //==================================================================================================
53 // The call instruction within the asm section of callVirtualMethod may throw
54 // exceptions. So that the compiler handles this correctly, it is important
55 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
56 // never happens at runtime), which in turn can throw exceptions, and (b)
57 // callVirtualMethod is not inlined at its call site (so that any exceptions are
58 // caught which are thrown from the instruction calling callVirtualMethod):
59 void callVirtualMethod(
60 void * pThis,
61 sal_Int32 nVtableIndex,
62 void * pRegisterReturn,
63 typelib_TypeClass eReturnType,
64 sal_Int32 * pStackLongs,
65 sal_Int32 nStackLongs ) __attribute__((noinline));
67 void callVirtualMethod(
68 void * pThis,
69 sal_Int32 nVtableIndex,
70 void * pRegisterReturn,
71 typelib_TypeClass eReturnType,
72 sal_Int32 * pStackLongs,
73 sal_Int32 nStackLongs )
75 // parameter list is mixed list of * and values
76 // reference parameters are pointers
78 OSL_ENSURE( pStackLongs && pThis, "### null ptr!" );
79 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
80 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
82 // never called
83 if (! pThis) dummy_can_throw_anything("xxx"); // address something
85 volatile long edx = 0, eax = 0; // for register returns
86 void * stackptr;
87 asm volatile (
88 "mov %%esp, %6\n\t"
89 // copy values
90 "mov %0, %%eax\n\t"
91 "mov %%eax, %%edx\n\t"
92 "dec %%edx\n\t"
93 "shl $2, %%edx\n\t"
94 "add %1, %%edx\n"
95 "Lcopy:\n\t"
96 "pushl 0(%%edx)\n\t"
97 "sub $4, %%edx\n\t"
98 "dec %%eax\n\t"
99 "jne Lcopy\n\t"
100 // do the actual call
101 "mov %2, %%edx\n\t"
102 "mov 0(%%edx), %%edx\n\t"
103 "mov %3, %%eax\n\t"
104 "shl $2, %%eax\n\t"
105 "add %%eax, %%edx\n\t"
106 "mov 0(%%edx), %%edx\n\t"
107 "call *%%edx\n\t"
108 // save return registers
109 "mov %%eax, %4\n\t"
110 "mov %%edx, %5\n\t"
111 // cleanup stack
112 "mov %6, %%esp\n\t"
114 : "m"(nStackLongs), "m"(pStackLongs), "m"(pThis), "m"(nVtableIndex),
115 "m"(eax), "m"(edx), "m"(stackptr)
116 : "eax", "edx" );
117 switch( eReturnType )
119 case typelib_TypeClass_HYPER:
120 case typelib_TypeClass_UNSIGNED_HYPER:
121 ((long*)pRegisterReturn)[1] = edx;
122 case typelib_TypeClass_LONG:
123 case typelib_TypeClass_UNSIGNED_LONG:
124 case typelib_TypeClass_CHAR:
125 case typelib_TypeClass_ENUM:
126 ((long*)pRegisterReturn)[0] = eax;
127 break;
128 case typelib_TypeClass_SHORT:
129 case typelib_TypeClass_UNSIGNED_SHORT:
130 *(unsigned short*)pRegisterReturn = eax;
131 break;
132 case typelib_TypeClass_BOOLEAN:
133 case typelib_TypeClass_BYTE:
134 *(unsigned char*)pRegisterReturn = eax;
135 break;
136 case typelib_TypeClass_FLOAT:
137 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
138 break;
139 case typelib_TypeClass_DOUBLE:
140 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
141 break;
145 //==================================================================================================
146 static void cpp_call(
147 cppu_unoInterfaceProxy * pThis,
148 sal_Int32 nVtableCall,
149 typelib_TypeDescriptionReference * pReturnTypeRef,
150 sal_Int32 nParams, typelib_MethodParameter * pParams,
151 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
153 // max space for: [complex ret ptr], values|ptr ...
154 char * pCppStack =
155 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
156 char * pCppStackStart = pCppStack;
158 // return
159 typelib_TypeDescription * pReturnTypeDescr = 0;
160 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
161 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
163 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
165 if (pReturnTypeDescr)
167 if (cppu_isSimpleType( pReturnTypeDescr ))
169 pCppReturn = pUnoReturn; // direct way for simple types
171 else
173 // complex return via ptr
174 pCppReturn = *(void **)pCppStack = (cppu_relatesToInterface( pReturnTypeDescr )
175 ? alloca( pReturnTypeDescr->nSize )
176 : pUnoReturn); // direct way
177 pCppStack += sizeof(void *);
180 // push this
181 *(void**)pCppStack = pThis->pCppI;
182 pCppStack += sizeof( void* );
184 // stack space
185 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
186 // args
187 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
188 // indizes of values this have to be converted (interface conversion cpp<=>uno)
189 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
190 // type descriptions for reconversions
191 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
193 sal_Int32 nTempIndizes = 0;
195 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
197 const typelib_MethodParameter & rParam = pParams[nPos];
198 typelib_TypeDescription * pParamTypeDescr = 0;
199 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
201 if (!rParam.bOut && cppu_isSimpleType( pParamTypeDescr ))
203 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
204 &pThis->pBridge->aUno2Cpp );
206 switch (pParamTypeDescr->eTypeClass)
208 case typelib_TypeClass_HYPER:
209 case typelib_TypeClass_UNSIGNED_HYPER:
210 case typelib_TypeClass_DOUBLE:
211 pCppStack += sizeof(sal_Int32); // extra long
213 // no longer needed
214 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
216 else // ptr to complex value | ref
218 if (! rParam.bIn) // is pure out
220 // cpp out is constructed mem, uno out is not!
221 uno_constructData(
222 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
223 pParamTypeDescr );
224 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
225 // will be released at reconversion
226 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
228 // is in/inout
229 else if (cppu_relatesToInterface( pParamTypeDescr ))
231 uno_copyAndConvertData(
232 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
233 pUnoArgs[nPos], pParamTypeDescr, &pThis->pBridge->aUno2Cpp );
235 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
236 // will be released at reconversion
237 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
239 else // direct way
241 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
242 // no longer needed
243 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
246 pCppStack += sizeof(sal_Int32); // standard parameter length
251 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
252 callVirtualMethod(
253 pThis->pCppI, nVtableCall,
254 pCppReturn, pReturnTypeDescr->eTypeClass,
255 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
256 // NO exception occured...
257 *ppUnoExc = 0;
259 // reconvert temporary params
260 for ( ; nTempIndizes--; )
262 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
263 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
265 if (pParams[nIndex].bIn)
267 if (pParams[nIndex].bOut) // inout
269 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
270 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
271 &pThis->pBridge->aCpp2Uno );
274 else // pure out
276 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
277 &pThis->pBridge->aCpp2Uno );
279 // destroy temp cpp param => cpp: every param was constructed
280 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
282 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
284 // return value
285 if (pCppReturn && pUnoReturn != pCppReturn)
287 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
288 &pThis->pBridge->aCpp2Uno );
289 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
292 catch (...)
294 // fill uno exception
295 fillUnoException( __cxa_get_globals()->caughtExceptions, *ppUnoExc, &pThis->pBridge->aCpp2Uno );
297 // temporary params
298 for ( ; nTempIndizes--; )
300 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
301 // destroy temp cpp param => cpp: every param was constructed
302 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
303 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
305 // return type
306 if (pReturnTypeDescr)
307 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
312 //==================================================================================================
313 void SAL_CALL cppu_unoInterfaceProxy_dispatch(
314 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
315 void * pReturn, void * pArgs[], uno_Any ** ppException ) throw ()
317 // is my surrogate
318 cppu_unoInterfaceProxy * pThis = (cppu_unoInterfaceProxy *)pUnoI;
319 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
321 switch (pMemberDescr->eTypeClass)
323 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
325 // determine vtable call index
326 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
327 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
329 sal_Int32 nVtableCall = pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos];
330 OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
332 if (pReturn)
334 // dependent dispatch
335 cpp_call(
336 pThis, nVtableCall,
337 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
338 0, 0, // no params
339 pReturn, pArgs, ppException );
341 else
343 // is SET
344 typelib_MethodParameter aParam;
345 aParam.pTypeRef =
346 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
347 aParam.bIn = sal_True;
348 aParam.bOut = sal_False;
350 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
351 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
352 typelib_typedescriptionreference_new(
353 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
355 // dependent dispatch
356 cpp_call(
357 pThis, nVtableCall +1, // get, then set method
358 pReturnTypeRef,
359 1, &aParam,
360 pReturn, pArgs, ppException );
362 typelib_typedescriptionreference_release( pReturnTypeRef );
365 break;
367 case typelib_TypeClass_INTERFACE_METHOD:
369 // determine vtable call index
370 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
371 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
373 sal_Int32 nVtableCall = pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos];
374 OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
376 switch (nVtableCall)
378 // standard calls
379 case 1: // acquire uno interface
380 (*pUnoI->acquire)( pUnoI );
381 *ppException = 0;
382 break;
383 case 2: // release uno interface
384 (*pUnoI->release)( pUnoI );
385 *ppException = 0;
386 break;
387 case 0: // queryInterface() opt
389 typelib_TypeDescription * pTD = 0;
390 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
391 if (pTD)
393 uno_Interface * pInterface = 0;
394 (*pThis->pBridge->pUnoEnv->getRegisteredInterface)(
395 pThis->pBridge->pUnoEnv,
396 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
398 if (pInterface)
400 ::uno_any_construct(
401 reinterpret_cast< uno_Any * >( pReturn ),
402 &pInterface, pTD, 0 );
403 (*pInterface->release)( pInterface );
404 TYPELIB_DANGER_RELEASE( pTD );
405 *ppException = 0;
406 break;
408 TYPELIB_DANGER_RELEASE( pTD );
410 } // else perform queryInterface()
411 default:
412 // dependent dispatch
413 cpp_call(
414 pThis, nVtableCall,
415 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
416 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
417 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
418 pReturn, pArgs, ppException );
420 break;
422 default:
424 ::com::sun::star::uno::RuntimeException aExc(
425 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
426 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
428 Type const & rExcType = ::getCppuType( &aExc );
429 // binary identical null reference
430 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );