merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_solaris_intel / uno2cpp.cxx
blob4b6fb4ef426a4180902b52a1e8e04681a78d8d64
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.7 $
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>
35 #include <sal/alloca.h>
37 #include <com/sun/star/uno/genfunc.hxx>
38 #include "com/sun/star/uno/RuntimeException.hpp"
39 #include <uno/data.h>
41 #include "bridges/cpp_uno/shared/bridge.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
44 #include "bridges/cpp_uno/shared/vtables.hxx"
46 #include "share.hxx"
48 using namespace ::rtl;
49 using namespace ::com::sun::star::uno;
51 namespace
54 //==================================================================================================
55 static void callVirtualMethod(
56 void * pAdjustedThisPtr,
57 sal_Int32 nVtableIndex,
58 void * pRegisterReturn,
59 typelib_TypeClass eReturnType,
60 sal_Int32 * pStackLongs,
61 sal_Int32 nStackLongs )
63 // parameter list is mixed list of * and values
64 // reference parameters are pointers
66 OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
67 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
68 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
70 // never called
71 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
73 volatile long edx = 0, eax = 0; // for register returns
74 void * stackptr;
75 asm volatile (
76 "mov %%esp, %6\n\t"
77 // copy values
78 "mov %0, %%eax\n\t"
79 "mov %%eax, %%edx\n\t"
80 "dec %%edx\n\t"
81 "shl $2, %%edx\n\t"
82 "add %1, %%edx\n"
83 "Lcopy:\n\t"
84 "pushl 0(%%edx)\n\t"
85 "sub $4, %%edx\n\t"
86 "dec %%eax\n\t"
87 "jne Lcopy\n\t"
88 // do the actual call
89 "mov %2, %%edx\n\t"
90 "mov 0(%%edx), %%edx\n\t"
91 "mov %3, %%eax\n\t"
92 "shl $2, %%eax\n\t"
93 "add %%eax, %%edx\n\t"
94 "mov 0(%%edx), %%edx\n\t"
95 "call *%%edx\n\t"
96 // save return registers
97 "mov %%eax, %4\n\t"
98 "mov %%edx, %5\n\t"
99 // cleanup stack
100 "mov %6, %%esp\n\t"
102 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
103 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
104 : "eax", "edx" );
105 switch( eReturnType )
107 case typelib_TypeClass_HYPER:
108 case typelib_TypeClass_UNSIGNED_HYPER:
109 ((long*)pRegisterReturn)[1] = edx;
110 case typelib_TypeClass_LONG:
111 case typelib_TypeClass_UNSIGNED_LONG:
112 case typelib_TypeClass_CHAR:
113 case typelib_TypeClass_ENUM:
114 ((long*)pRegisterReturn)[0] = eax;
115 break;
116 case typelib_TypeClass_SHORT:
117 case typelib_TypeClass_UNSIGNED_SHORT:
118 *(unsigned short*)pRegisterReturn = eax;
119 break;
120 case typelib_TypeClass_BOOLEAN:
121 case typelib_TypeClass_BYTE:
122 *(unsigned char*)pRegisterReturn = eax;
123 break;
124 case typelib_TypeClass_FLOAT:
125 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
126 break;
127 case typelib_TypeClass_DOUBLE:
128 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
129 break;
133 //==================================================================================================
134 static void cpp_call(
135 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
136 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
137 typelib_TypeDescriptionReference * pReturnTypeRef,
138 sal_Int32 nParams, typelib_MethodParameter * pParams,
139 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
141 // max space for: [complex ret ptr], values|ptr ...
142 char * pCppStack =
143 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
144 char * pCppStackStart = pCppStack;
146 // return
147 typelib_TypeDescription * pReturnTypeDescr = 0;
148 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
149 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
151 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
153 if (pReturnTypeDescr)
155 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
157 pCppReturn = pUnoReturn; // direct way for simple types
159 else
161 // complex return via ptr
162 pCppReturn = *(void **)pCppStack
163 = (bridges::cpp_uno::shared::relatesToInterfaceType(
164 pReturnTypeDescr )
165 ? alloca( pReturnTypeDescr->nSize )
166 : pUnoReturn); // direct way
167 pCppStack += sizeof(void *);
170 // push this
171 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
172 + aVtableSlot.offset;
173 *(void**)pCppStack = pAdjustedThisPtr;
174 pCppStack += sizeof( void* );
176 // stack space
177 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
178 // args
179 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
180 // indizes of values this have to be converted (interface conversion cpp<=>uno)
181 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
182 // type descriptions for reconversions
183 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
185 sal_Int32 nTempIndizes = 0;
187 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
189 const typelib_MethodParameter & rParam = pParams[nPos];
190 typelib_TypeDescription * pParamTypeDescr = 0;
191 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
193 if (!rParam.bOut
194 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
196 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
197 pThis->getBridge()->getUno2Cpp() );
199 switch (pParamTypeDescr->eTypeClass)
201 case typelib_TypeClass_HYPER:
202 case typelib_TypeClass_UNSIGNED_HYPER:
203 case typelib_TypeClass_DOUBLE:
204 pCppStack += sizeof(sal_Int32); // extra long
206 // no longer needed
207 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
209 else // ptr to complex value | ref
211 if (! rParam.bIn) // is pure out
213 // cpp out is constructed mem, uno out is not!
214 uno_constructData(
215 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
216 pParamTypeDescr );
217 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
218 // will be released at reconversion
219 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
221 // is in/inout
222 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
223 pParamTypeDescr ))
225 uno_copyAndConvertData(
226 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
227 pUnoArgs[nPos], pParamTypeDescr,
228 pThis->getBridge()->getUno2Cpp() );
230 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
231 // will be released at reconversion
232 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
234 else // direct way
236 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
237 // no longer needed
238 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
241 pCppStack += sizeof(sal_Int32); // standard parameter length
246 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
247 callVirtualMethod(
248 pAdjustedThisPtr, aVtableSlot.index,
249 pCppReturn, pReturnTypeDescr->eTypeClass,
250 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
251 // NO exception occured...
252 *ppUnoExc = 0;
254 // reconvert temporary params
255 for ( ; nTempIndizes--; )
257 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
258 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
260 if (pParams[nIndex].bIn)
262 if (pParams[nIndex].bOut) // inout
264 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
265 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
266 pThis->getBridge()->getCpp2Uno() );
269 else // pure out
271 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
272 pThis->getBridge()->getCpp2Uno() );
274 // destroy temp cpp param => cpp: every param was constructed
275 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
277 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
279 // return value
280 if (pCppReturn && pUnoReturn != pCppReturn)
282 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
283 pThis->getBridge()->getCpp2Uno() );
284 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
287 catch (...)
289 // fill uno exception
290 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
292 // temporary params
293 for ( ; nTempIndizes--; )
295 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
296 // destroy temp cpp param => cpp: every param was constructed
297 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
298 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
300 // return type
301 if (pReturnTypeDescr)
302 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
308 namespace bridges { namespace cpp_uno { namespace shared {
310 void unoInterfaceProxyDispatch(
311 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
312 void * pReturn, void * pArgs[], uno_Any ** ppException )
314 // is my surrogate
315 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
316 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
317 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
319 switch (pMemberDescr->eTypeClass)
321 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
323 VtableSlot aVtableSlot(
324 getVtableSlot(
325 reinterpret_cast<
326 typelib_InterfaceAttributeTypeDescription const * >(
327 pMemberDescr)));
328 if (pReturn)
330 // dependent dispatch
331 cpp_call(
332 pThis, aVtableSlot,
333 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
334 0, 0, // no params
335 pReturn, pArgs, ppException );
337 else
339 // is SET
340 typelib_MethodParameter aParam;
341 aParam.pTypeRef =
342 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
343 aParam.bIn = sal_True;
344 aParam.bOut = sal_False;
346 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
347 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
348 typelib_typedescriptionreference_new(
349 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
351 // dependent dispatch
352 aVtableSlot.index += 1; // get, then set method
353 cpp_call(
354 pThis, aVtableSlot,
355 pReturnTypeRef,
356 1, &aParam,
357 pReturn, pArgs, ppException );
359 typelib_typedescriptionreference_release( pReturnTypeRef );
362 break;
364 case typelib_TypeClass_INTERFACE_METHOD:
366 VtableSlot aVtableSlot(
367 getVtableSlot(
368 reinterpret_cast<
369 typelib_InterfaceMethodTypeDescription const * >(
370 pMemberDescr)));
371 switch (aVtableSlot.index)
373 // standard calls
374 case 1: // acquire uno interface
375 (*pUnoI->acquire)( pUnoI );
376 *ppException = 0;
377 break;
378 case 2: // release uno interface
379 (*pUnoI->release)( pUnoI );
380 *ppException = 0;
381 break;
382 case 0: // queryInterface() opt
384 typelib_TypeDescription * pTD = 0;
385 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
386 if (pTD)
388 uno_Interface * pInterface = 0;
389 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
390 pThis->pBridge->getUnoEnv(),
391 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
393 if (pInterface)
395 ::uno_any_construct(
396 reinterpret_cast< uno_Any * >( pReturn ),
397 &pInterface, pTD, 0 );
398 (*pInterface->release)( pInterface );
399 TYPELIB_DANGER_RELEASE( pTD );
400 *ppException = 0;
401 break;
403 TYPELIB_DANGER_RELEASE( pTD );
405 } // else perform queryInterface()
406 default:
407 // dependent dispatch
408 cpp_call(
409 pThis, aVtableSlot,
410 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
411 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
412 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
413 pReturn, pArgs, ppException );
415 break;
417 default:
419 ::com::sun::star::uno::RuntimeException aExc(
420 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
421 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
423 Type const & rExcType = ::getCppuType( &aExc );
424 // binary identical null reference
425 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
430 } } }