Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_intel / cpp2uno.cxx
blobea5b762d360837caf4edff1152d6ffc5cd7638a0
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: cpp2uno.cxx,v $
10 * $Revision: 1.14 $
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 <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include <uno/data.h>
37 #include <typelib/typedescription.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
44 #include "share.hxx"
46 using namespace ::com::sun::star::uno;
48 namespace
51 //==================================================================================================
52 void cpp2uno_call(
53 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
54 const typelib_TypeDescription * pMemberTypeDescr,
55 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
56 sal_Int32 nParams, typelib_MethodParameter * pParams,
57 void ** pCallStack,
58 void * pReturnValue )
60 // pCallStack: ret, [return ptr], this, params
61 char * pCppStack = (char *)(pCallStack +1);
63 // return
64 typelib_TypeDescription * pReturnTypeDescr = 0;
65 if (pReturnTypeRef)
66 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
68 void * pUnoReturn = 0;
69 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
71 if (pReturnTypeDescr)
73 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
75 pUnoReturn = pReturnValue; // direct way for simple types
77 else // complex return via ptr (pCppReturn)
79 pCppReturn = *(void **)pCppStack;
80 pCppStack += sizeof(void *);
82 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
83 pReturnTypeDescr )
84 ? alloca( pReturnTypeDescr->nSize )
85 : pCppReturn); // direct way
88 // pop this
89 pCppStack += sizeof( void* );
91 // stack space
92 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
93 // parameters
94 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
95 void ** pCppArgs = pUnoArgs + nParams;
96 // indizes of values this have to be converted (interface conversion cpp<=>uno)
97 sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
98 // type descriptions for reconversions
99 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
101 sal_Int32 nTempIndizes = 0;
103 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
105 const typelib_MethodParameter & rParam = pParams[nPos];
106 typelib_TypeDescription * pParamTypeDescr = 0;
107 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
109 if (!rParam.bOut
110 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
111 // value
113 pCppArgs[nPos] = pCppStack;
114 pUnoArgs[nPos] = pCppStack;
115 switch (pParamTypeDescr->eTypeClass)
117 case typelib_TypeClass_HYPER:
118 case typelib_TypeClass_UNSIGNED_HYPER:
119 case typelib_TypeClass_DOUBLE:
120 pCppStack += sizeof(sal_Int32); // extra long
121 break;
122 default:
123 break;
125 // no longer needed
126 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
128 else // ptr to complex value | ref
130 pCppArgs[nPos] = *(void **)pCppStack;
132 if (! rParam.bIn) // is pure out
134 // uno out is unconstructed mem!
135 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
136 pTempIndizes[nTempIndizes] = nPos;
137 // will be released at reconversion
138 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
140 // is in/inout
141 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
142 pParamTypeDescr ))
144 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
145 *(void **)pCppStack, pParamTypeDescr,
146 pThis->getBridge()->getCpp2Uno() );
147 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
148 // will be released at reconversion
149 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
151 else // direct way
153 pUnoArgs[nPos] = *(void **)pCppStack;
154 // no longer needed
155 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
158 pCppStack += sizeof(sal_Int32); // standard parameter length
161 // ExceptionHolder
162 uno_Any aUnoExc; // Any will be constructed by callee
163 uno_Any * pUnoExc = &aUnoExc;
165 // invoke uno dispatch call
166 (*pThis->getUnoI()->pDispatcher)(
167 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
169 // in case an exception occured...
170 if (pUnoExc)
172 // destruct temporary in/inout params
173 for ( ; nTempIndizes--; )
175 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
177 if (pParams[nIndex].bIn) // is in/inout => was constructed
178 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
179 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
181 if (pReturnTypeDescr)
182 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
184 CPPU_CURRENT_NAMESPACE::raiseException(
185 &aUnoExc, pThis->getBridge()->getUno2Cpp() );
186 // has to destruct the any
188 else // else no exception occured...
190 // temporary params
191 for ( ; nTempIndizes--; )
193 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
194 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
196 if (pParams[nIndex].bOut) // inout/out
198 // convert and assign
199 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
200 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
201 pThis->getBridge()->getUno2Cpp() );
203 // destroy temp uno param
204 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
206 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
208 // return
209 if (pCppReturn) // has complex return
211 if (pUnoReturn != pCppReturn) // needs reconversion
213 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
214 pThis->getBridge()->getUno2Cpp() );
215 // destroy temp uno return
216 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
218 // complex return ptr is set to eax
219 *static_cast< void ** >(pReturnValue) = pCppReturn;
221 if (pReturnTypeDescr)
223 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
229 //==================================================================================================
230 extern "C" void cpp_vtable_call(
231 int nFunctionIndex, int nVtableOffset, void** pCallStack,
232 void * pReturnValue )
234 OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
236 // pCallStack: ret adr, [ret *], this, params
237 void * pThis;
238 if( nFunctionIndex & 0x80000000 )
240 nFunctionIndex &= 0x7fffffff;
241 pThis = pCallStack[2];
243 else
245 pThis = pCallStack[1];
247 pThis = static_cast< char * >(pThis) - nVtableOffset;
248 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
249 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
250 pThis);
252 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
254 OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
255 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
257 throw RuntimeException(
258 rtl::OUString::createFromAscii("illegal vtable index!"),
259 (XInterface *)pThis );
262 // determine called method
263 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
264 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
266 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
268 switch (aMemberDescr.get()->eTypeClass)
270 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
272 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
274 // is GET method
275 cpp2uno_call(
276 pCppI, aMemberDescr.get(),
277 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
278 0, 0, // no params
279 pCallStack, pReturnValue );
281 else
283 // is SET method
284 typelib_MethodParameter aParam;
285 aParam.pTypeRef =
286 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
287 aParam.bIn = sal_True;
288 aParam.bOut = sal_False;
290 cpp2uno_call(
291 pCppI, aMemberDescr.get(),
292 0, // indicates void return
293 1, &aParam,
294 pCallStack, pReturnValue );
296 break;
298 case typelib_TypeClass_INTERFACE_METHOD:
300 // is METHOD
301 switch (nFunctionIndex)
303 case 1: // acquire()
304 pCppI->acquireProxy(); // non virtual call!
305 break;
306 case 2: // release()
307 pCppI->releaseProxy(); // non virtual call!
308 break;
309 case 0: // queryInterface() opt
311 typelib_TypeDescription * pTD = 0;
312 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[3] )->getTypeLibType() );
313 if (pTD)
315 XInterface * pInterface = 0;
316 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
317 pCppI->getBridge()->getCppEnv(),
318 (void **)&pInterface, pCppI->getOid().pData,
319 (typelib_InterfaceTypeDescription *)pTD );
321 if (pInterface)
323 ::uno_any_construct(
324 reinterpret_cast< uno_Any * >( pCallStack[1] ),
325 &pInterface, pTD, cpp_acquire );
326 pInterface->release();
327 TYPELIB_DANGER_RELEASE( pTD );
328 *static_cast< void ** >(pReturnValue) = pCallStack[1];
329 break;
331 TYPELIB_DANGER_RELEASE( pTD );
333 } // else perform queryInterface()
334 default:
335 cpp2uno_call(
336 pCppI, aMemberDescr.get(),
337 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
338 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
339 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
340 pCallStack, pReturnValue );
342 break;
344 default:
346 throw RuntimeException(
347 rtl::OUString::createFromAscii("no member description found!"),
348 (XInterface *)pThis );
353 //==================================================================================================
354 extern "C" void privateSnippetExecutorGeneral();
355 extern "C" void privateSnippetExecutorVoid();
356 extern "C" void privateSnippetExecutorHyper();
357 extern "C" void privateSnippetExecutorFloat();
358 extern "C" void privateSnippetExecutorDouble();
359 extern "C" void privateSnippetExecutorClass();
360 extern "C" typedef void (*PrivateSnippetExecutor)();
362 int const codeSnippetSize = 16;
364 unsigned char * codeSnippet(
365 unsigned char * code, sal_PtrDiff writetoexecdiff, sal_Int32 functionIndex, sal_Int32 vtableOffset,
366 typelib_TypeClass returnTypeClass)
368 if (!bridges::cpp_uno::shared::isSimpleType(returnTypeClass)) {
369 functionIndex |= 0x80000000;
371 PrivateSnippetExecutor exec;
372 switch (returnTypeClass) {
373 case typelib_TypeClass_VOID:
374 exec = privateSnippetExecutorVoid;
375 break;
376 case typelib_TypeClass_HYPER:
377 case typelib_TypeClass_UNSIGNED_HYPER:
378 exec = privateSnippetExecutorHyper;
379 break;
380 case typelib_TypeClass_FLOAT:
381 exec = privateSnippetExecutorFloat;
382 break;
383 case typelib_TypeClass_DOUBLE:
384 exec = privateSnippetExecutorDouble;
385 break;
386 case typelib_TypeClass_STRING:
387 case typelib_TypeClass_TYPE:
388 case typelib_TypeClass_ANY:
389 case typelib_TypeClass_SEQUENCE:
390 case typelib_TypeClass_STRUCT:
391 case typelib_TypeClass_INTERFACE:
392 exec = privateSnippetExecutorClass;
393 break;
394 default:
395 exec = privateSnippetExecutorGeneral;
396 break;
398 unsigned char * p = code;
399 OSL_ASSERT(sizeof (sal_Int32) == 4);
400 // mov function_index, %eax:
401 *p++ = 0xB8;
402 *reinterpret_cast< sal_Int32 * >(p) = functionIndex;
403 p += sizeof (sal_Int32);
404 // mov vtable_offset, %edx:
405 *p++ = 0xBA;
406 *reinterpret_cast< sal_Int32 * >(p) = vtableOffset;
407 p += sizeof (sal_Int32);
408 // jmp privateSnippetExecutor:
409 *p++ = 0xE9;
410 *reinterpret_cast< sal_Int32 * >(p)
411 = ((unsigned char *) exec) - p - sizeof (sal_Int32) - writetoexecdiff;
412 p += sizeof (sal_Int32);
413 OSL_ASSERT(p - code <= codeSnippetSize);
414 return code + codeSnippetSize;
419 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
421 bridges::cpp_uno::shared::VtableFactory::Slot *
422 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block) {
423 return static_cast< Slot * >(block) + 2;
426 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
427 sal_Int32 slotCount)
429 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
432 bridges::cpp_uno::shared::VtableFactory::Slot *
433 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
434 void * block, sal_Int32 slotCount)
436 Slot * slots = mapBlockToVtable(block);
437 slots[-2].fn = 0;
438 slots[-1].fn = 0;
439 return slots + slotCount;
442 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
443 Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
444 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
445 sal_Int32 functionCount, sal_Int32 vtableOffset)
447 (*slots) -= functionCount;
448 Slot * s = *slots;
449 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
450 typelib_TypeDescription * member = 0;
451 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
452 OSL_ASSERT(member != 0);
453 switch (member->eTypeClass) {
454 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
455 // Getter:
456 (s++)->fn = code + writetoexecdiff;
457 code = codeSnippet(
458 code, writetoexecdiff, functionOffset++, vtableOffset,
459 reinterpret_cast< typelib_InterfaceAttributeTypeDescription * >(
460 member)->pAttributeTypeRef->eTypeClass);
461 // Setter:
462 if (!reinterpret_cast<
463 typelib_InterfaceAttributeTypeDescription * >(
464 member)->bReadOnly)
466 (s++)->fn = code + writetoexecdiff;
467 code = codeSnippet(
468 code, writetoexecdiff, functionOffset++, vtableOffset,
469 typelib_TypeClass_VOID);
471 break;
473 case typelib_TypeClass_INTERFACE_METHOD:
474 (s++)->fn = code + writetoexecdiff;
475 code = codeSnippet(
476 code, writetoexecdiff, functionOffset++, vtableOffset,
477 reinterpret_cast< typelib_InterfaceMethodTypeDescription * >(
478 member)->pReturnTypeRef->eTypeClass);
479 break;
481 default:
482 OSL_ASSERT(false);
483 break;
485 TYPELIB_DANGER_RELEASE(member);
487 return code;
490 void bridges::cpp_uno::shared::VtableFactory::flushCode(
491 unsigned char const *, unsigned char const *)