Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_solaris_intel / uno2cpp.cxx
bloba97a736c8042f4522e84b017b23d4d1d63761285
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <malloc.h>
31 #include <sal/alloca.h>
33 #include <com/sun/star/uno/genfunc.hxx>
34 #include "com/sun/star/uno/RuntimeException.hpp"
35 #include <uno/data.h>
37 #include "bridges/cpp_uno/shared/bridge.hxx"
38 #include "bridges/cpp_uno/shared/types.hxx"
39 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
40 #include "bridges/cpp_uno/shared/vtables.hxx"
42 #include "share.hxx"
44 using namespace ::rtl;
45 using namespace ::com::sun::star::uno;
47 namespace
50 //==================================================================================================
51 static void callVirtualMethod(
52 void * pAdjustedThisPtr,
53 sal_Int32 nVtableIndex,
54 void * pRegisterReturn,
55 typelib_TypeClass eReturnType,
56 sal_Int32 * pStackLongs,
57 sal_Int32 nStackLongs )
59 // parameter list is mixed list of * and values
60 // reference parameters are pointers
62 OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
63 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
64 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
66 // never called
67 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
69 volatile long edx = 0, eax = 0; // for register returns
70 void * stackptr;
71 asm volatile (
72 "mov %%esp, %6\n\t"
73 // copy values
74 "mov %0, %%eax\n\t"
75 "mov %%eax, %%edx\n\t"
76 "dec %%edx\n\t"
77 "shl $2, %%edx\n\t"
78 "add %1, %%edx\n"
79 "Lcopy:\n\t"
80 "pushl 0(%%edx)\n\t"
81 "sub $4, %%edx\n\t"
82 "dec %%eax\n\t"
83 "jne Lcopy\n\t"
84 // do the actual call
85 "mov %2, %%edx\n\t"
86 "mov 0(%%edx), %%edx\n\t"
87 "mov %3, %%eax\n\t"
88 "shl $2, %%eax\n\t"
89 "add %%eax, %%edx\n\t"
90 "mov 0(%%edx), %%edx\n\t"
91 "call *%%edx\n\t"
92 // save return registers
93 "mov %%eax, %4\n\t"
94 "mov %%edx, %5\n\t"
95 // cleanup stack
96 "mov %6, %%esp\n\t"
98 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
99 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
100 : "eax", "edx" );
101 switch( eReturnType )
103 case typelib_TypeClass_HYPER:
104 case typelib_TypeClass_UNSIGNED_HYPER:
105 ((long*)pRegisterReturn)[1] = edx;
106 case typelib_TypeClass_LONG:
107 case typelib_TypeClass_UNSIGNED_LONG:
108 case typelib_TypeClass_CHAR:
109 case typelib_TypeClass_ENUM:
110 ((long*)pRegisterReturn)[0] = eax;
111 break;
112 case typelib_TypeClass_SHORT:
113 case typelib_TypeClass_UNSIGNED_SHORT:
114 *(unsigned short*)pRegisterReturn = eax;
115 break;
116 case typelib_TypeClass_BOOLEAN:
117 case typelib_TypeClass_BYTE:
118 *(unsigned char*)pRegisterReturn = eax;
119 break;
120 case typelib_TypeClass_FLOAT:
121 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
122 break;
123 case typelib_TypeClass_DOUBLE:
124 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
125 break;
129 //==================================================================================================
130 static void cpp_call(
131 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
132 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
133 typelib_TypeDescriptionReference * pReturnTypeRef,
134 sal_Int32 nParams, typelib_MethodParameter * pParams,
135 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
137 // max space for: [complex ret ptr], values|ptr ...
138 char * pCppStack =
139 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
140 char * pCppStackStart = pCppStack;
142 // return
143 typelib_TypeDescription * pReturnTypeDescr = 0;
144 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
145 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
147 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
149 if (pReturnTypeDescr)
151 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
153 pCppReturn = pUnoReturn; // direct way for simple types
155 else
157 // complex return via ptr
158 pCppReturn = *(void **)pCppStack
159 = (bridges::cpp_uno::shared::relatesToInterfaceType(
160 pReturnTypeDescr )
161 ? alloca( pReturnTypeDescr->nSize )
162 : pUnoReturn); // direct way
163 pCppStack += sizeof(void *);
166 // push this
167 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
168 + aVtableSlot.offset;
169 *(void**)pCppStack = pAdjustedThisPtr;
170 pCppStack += sizeof( void* );
172 // stack space
173 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
174 // args
175 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
176 // indizes of values this have to be converted (interface conversion cpp<=>uno)
177 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
178 // type descriptions for reconversions
179 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
181 sal_Int32 nTempIndizes = 0;
183 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
185 const typelib_MethodParameter & rParam = pParams[nPos];
186 typelib_TypeDescription * pParamTypeDescr = 0;
187 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
189 if (!rParam.bOut
190 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
192 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
193 pThis->getBridge()->getUno2Cpp() );
195 switch (pParamTypeDescr->eTypeClass)
197 case typelib_TypeClass_HYPER:
198 case typelib_TypeClass_UNSIGNED_HYPER:
199 case typelib_TypeClass_DOUBLE:
200 pCppStack += sizeof(sal_Int32); // extra long
202 // no longer needed
203 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
205 else // ptr to complex value | ref
207 if (! rParam.bIn) // is pure out
209 // cpp out is constructed mem, uno out is not!
210 uno_constructData(
211 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
212 pParamTypeDescr );
213 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
214 // will be released at reconversion
215 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
217 // is in/inout
218 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
219 pParamTypeDescr ))
221 uno_copyAndConvertData(
222 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
223 pUnoArgs[nPos], pParamTypeDescr,
224 pThis->getBridge()->getUno2Cpp() );
226 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
227 // will be released at reconversion
228 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
230 else // direct way
232 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
233 // no longer needed
234 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
237 pCppStack += sizeof(sal_Int32); // standard parameter length
242 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
243 callVirtualMethod(
244 pAdjustedThisPtr, aVtableSlot.index,
245 pCppReturn, pReturnTypeDescr->eTypeClass,
246 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
247 // NO exception occurred...
248 *ppUnoExc = 0;
250 // reconvert temporary params
251 for ( ; nTempIndizes--; )
253 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
254 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
256 if (pParams[nIndex].bIn)
258 if (pParams[nIndex].bOut) // inout
260 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
261 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
262 pThis->getBridge()->getCpp2Uno() );
265 else // pure out
267 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
268 pThis->getBridge()->getCpp2Uno() );
270 // destroy temp cpp param => cpp: every param was constructed
271 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
273 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
275 // return value
276 if (pCppReturn && pUnoReturn != pCppReturn)
278 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
279 pThis->getBridge()->getCpp2Uno() );
280 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
283 catch (...)
285 // fill uno exception
286 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
288 // temporary params
289 for ( ; nTempIndizes--; )
291 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
292 // destroy temp cpp param => cpp: every param was constructed
293 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
294 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
296 // return type
297 if (pReturnTypeDescr)
298 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
304 namespace bridges { namespace cpp_uno { namespace shared {
306 void unoInterfaceProxyDispatch(
307 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
308 void * pReturn, void * pArgs[], uno_Any ** ppException )
310 // is my surrogate
311 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
312 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
313 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
315 switch (pMemberDescr->eTypeClass)
317 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
319 VtableSlot aVtableSlot(
320 getVtableSlot(
321 reinterpret_cast<
322 typelib_InterfaceAttributeTypeDescription const * >(
323 pMemberDescr)));
324 if (pReturn)
326 // dependent dispatch
327 cpp_call(
328 pThis, aVtableSlot,
329 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
330 0, 0, // no params
331 pReturn, pArgs, ppException );
333 else
335 // is SET
336 typelib_MethodParameter aParam;
337 aParam.pTypeRef =
338 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
339 aParam.bIn = sal_True;
340 aParam.bOut = sal_False;
342 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
343 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
344 typelib_typedescriptionreference_new(
345 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
347 // dependent dispatch
348 aVtableSlot.index += 1; // get, then set method
349 cpp_call(
350 pThis, aVtableSlot,
351 pReturnTypeRef,
352 1, &aParam,
353 pReturn, pArgs, ppException );
355 typelib_typedescriptionreference_release( pReturnTypeRef );
358 break;
360 case typelib_TypeClass_INTERFACE_METHOD:
362 VtableSlot aVtableSlot(
363 getVtableSlot(
364 reinterpret_cast<
365 typelib_InterfaceMethodTypeDescription const * >(
366 pMemberDescr)));
367 switch (aVtableSlot.index)
369 // standard calls
370 case 1: // acquire uno interface
371 (*pUnoI->acquire)( pUnoI );
372 *ppException = 0;
373 break;
374 case 2: // release uno interface
375 (*pUnoI->release)( pUnoI );
376 *ppException = 0;
377 break;
378 case 0: // queryInterface() opt
380 typelib_TypeDescription * pTD = 0;
381 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
382 if (pTD)
384 uno_Interface * pInterface = 0;
385 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
386 pThis->pBridge->getUnoEnv(),
387 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
389 if (pInterface)
391 ::uno_any_construct(
392 reinterpret_cast< uno_Any * >( pReturn ),
393 &pInterface, pTD, 0 );
394 (*pInterface->release)( pInterface );
395 TYPELIB_DANGER_RELEASE( pTD );
396 *ppException = 0;
397 break;
399 TYPELIB_DANGER_RELEASE( pTD );
401 } // else perform queryInterface()
402 default:
403 // dependent dispatch
404 cpp_call(
405 pThis, aVtableSlot,
406 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
407 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
408 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
409 pReturn, pArgs, ppException );
411 break;
413 default:
415 ::com::sun::star::uno::RuntimeException aExc(
416 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
417 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
419 Type const & rExcType = ::getCppuType( &aExc );
420 // binary identical null reference
421 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
426 } } }
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */