161dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
361dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
461dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
561dff127SAndrew Rist  * distributed with this work for additional information
661dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
761dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
861dff127SAndrew Rist  * "License"); you may not use this file except in compliance
961dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
1061dff127SAndrew Rist  *
1161dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1261dff127SAndrew Rist  *
1361dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1461dff127SAndrew Rist  * software distributed under the License is distributed on an
1561dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1661dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
1761dff127SAndrew Rist  * specific language governing permissions and limitations
1861dff127SAndrew Rist  * under the License.
1961dff127SAndrew Rist  *
2061dff127SAndrew Rist  *************************************************************/
2161dff127SAndrew Rist 
2261dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
27942d46e3SPedro Giffuni #include <typeinfo>
28de2c434cSPedro Giffuni #include <exception>
29de2c434cSPedro Giffuni #include <cstddef>
30de2c434cSPedro Giffuni #include <cxxabi.h>
31*610a3988SDamjan Jovanovic #include <stdio.h>
32cdf0e10cSrcweir #include <stdlib.h>
33cdf0e10cSrcweir #include <string.h>
34942d46e3SPedro Giffuni 
35942d46e3SPedro Giffuni #include "rtl/alloc.h"
36942d46e3SPedro Giffuni #include "rtl/ustrbuf.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
39cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
40cdf0e10cSrcweir #include <uno/data.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <bridges/cpp_uno/shared/bridge.hxx>
43cdf0e10cSrcweir #include <bridges/cpp_uno/shared/types.hxx>
44cdf0e10cSrcweir #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
45cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtables.hxx"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include "abi.hxx"
48cdf0e10cSrcweir #include "share.hxx"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace ::rtl;
51cdf0e10cSrcweir using namespace ::com::sun::star::uno;
52de2c434cSPedro Giffuni #ifdef __GLIBCXX__
53de2c434cSPedro Giffuni using CPPU_CURRENT_NAMESPACE::__cxa_get_globals;
54de2c434cSPedro Giffuni #else
55de2c434cSPedro Giffuni using __cxxabiv1::__cxa_get_globals;
56de2c434cSPedro Giffuni #endif
57cdf0e10cSrcweir 
58cdf0e10cSrcweir //==================================================================================================
59cdf0e10cSrcweir static void callVirtualMethod(void * pThis, sal_uInt32 nVtableIndex,
6081293574SPedro Giffuni                               void * pRegisterReturn, typelib_TypeDescriptionReference * pReturnTypeRef, bool bSimpleReturn,
61cdf0e10cSrcweir                               sal_uInt64 *pStack, sal_uInt32 nStack,
62cdf0e10cSrcweir                               sal_uInt64 *pGPR, sal_uInt32 nGPR,
63cdf0e10cSrcweir                               double *pFPR, sal_uInt32 nFPR) __attribute__((noinline));
64cdf0e10cSrcweir 
callVirtualMethod(void * pThis,sal_uInt32 nVtableIndex,void * pRegisterReturn,typelib_TypeDescriptionReference * pReturnTypeRef,bool bSimpleReturn,sal_uInt64 * pStack,sal_uInt32 nStack,sal_uInt64 * pGPR,sal_uInt32 nGPR,double * pFPR,sal_uInt32 nFPR)65cdf0e10cSrcweir static void callVirtualMethod(void * pThis, sal_uInt32 nVtableIndex,
6681293574SPedro Giffuni                               void * pRegisterReturn, typelib_TypeDescriptionReference * pReturnTypeRef, bool bSimpleReturn,
67cdf0e10cSrcweir                               sal_uInt64 *pStack, sal_uInt32 nStack,
68cdf0e10cSrcweir                               sal_uInt64 *pGPR, sal_uInt32 nGPR,
69cdf0e10cSrcweir                               double *pFPR, sal_uInt32 nFPR)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
72cdf0e10cSrcweir     // Let's figure out what is really going on here
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         fprintf( stderr, "= callVirtualMethod() =\nGPR's (%d): ", nGPR );
7581293574SPedro Giffuni         for ( unsigned int i = 0; i < nGPR; ++i )
76cdf0e10cSrcweir             fprintf( stderr, "0x%lx, ", pGPR[i] );
77cdf0e10cSrcweir         fprintf( stderr, "\nFPR's (%d): ", nFPR );
7881293574SPedro Giffuni         for ( unsigned int i = 0; i < nFPR; ++i )
79cdf0e10cSrcweir             fprintf( stderr, "%f, ", pFPR[i] );
80cdf0e10cSrcweir         fprintf( stderr, "\nStack (%d): ", nStack );
8181293574SPedro Giffuni         for ( unsigned int i = 0; i < nStack; ++i )
82cdf0e10cSrcweir             fprintf( stderr, "0x%lx, ", pStack[i] );
83cdf0e10cSrcweir         fprintf( stderr, "\n" );
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir #endif
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     // The call instruction within the asm section of callVirtualMethod may throw
88cdf0e10cSrcweir     // exceptions.  So that the compiler handles this correctly, it is important
89cdf0e10cSrcweir     // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
90cdf0e10cSrcweir     // never happens at runtime), which in turn can throw exceptions, and (b)
91cdf0e10cSrcweir     // callVirtualMethod is not inlined at its call site (so that any exceptions are
92cdf0e10cSrcweir     // caught which are thrown from the instruction calling callVirtualMethod):
93cdf0e10cSrcweir     if ( !pThis )
94cdf0e10cSrcweir         CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything( "xxx" ); // address something
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     // Should not happen, but...
97cdf0e10cSrcweir     if ( nFPR > x86_64::MAX_SSE_REGS )
98cdf0e10cSrcweir         nFPR = x86_64::MAX_SSE_REGS;
99cdf0e10cSrcweir     if ( nGPR > x86_64::MAX_GPR_REGS )
100cdf0e10cSrcweir         nGPR = x86_64::MAX_GPR_REGS;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // Get pointer to method
103cdf0e10cSrcweir     sal_uInt64 pMethod = *((sal_uInt64 *)pThis);
104cdf0e10cSrcweir     pMethod += 8 * nVtableIndex;
105cdf0e10cSrcweir     pMethod = *((sal_uInt64 *)pMethod);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     // Load parameters to stack, if necessary
108fea2173fSHerbert Dürr     sal_uInt64* pCallStack = NULL;
109cdf0e10cSrcweir     if ( nStack )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         // 16-bytes aligned
112cdf0e10cSrcweir         sal_uInt32 nStackBytes = ( ( nStack + 1 ) >> 1 ) * 16;
113fea2173fSHerbert Dürr         pCallStack = (sal_uInt64*) __builtin_alloca( nStackBytes );
114cdf0e10cSrcweir         memcpy( pCallStack, pStack, nStackBytes );
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     // Return values
118cdf0e10cSrcweir     sal_uInt64 rax;
119cdf0e10cSrcweir     sal_uInt64 rdx;
120cdf0e10cSrcweir     double xmm0;
12181293574SPedro Giffuni     double xmm1;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     asm volatile (
124cdf0e10cSrcweir         // Fill the xmm registers
125cdf0e10cSrcweir         "movq %2, %%rax\n\t"
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         "movsd   (%%rax), %%xmm0\n\t"
128cdf0e10cSrcweir         "movsd  8(%%rax), %%xmm1\n\t"
129cdf0e10cSrcweir         "movsd 16(%%rax), %%xmm2\n\t"
130cdf0e10cSrcweir         "movsd 24(%%rax), %%xmm3\n\t"
131cdf0e10cSrcweir         "movsd 32(%%rax), %%xmm4\n\t"
132cdf0e10cSrcweir         "movsd 40(%%rax), %%xmm5\n\t"
133cdf0e10cSrcweir         "movsd 48(%%rax), %%xmm6\n\t"
134cdf0e10cSrcweir         "movsd 56(%%rax), %%xmm7\n\t"
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         // Fill the general purpose registers
137cdf0e10cSrcweir         "movq %1, %%rax\n\t"
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         "movq    (%%rax), %%rdi\n\t"
140cdf0e10cSrcweir         "movq   8(%%rax), %%rsi\n\t"
141cdf0e10cSrcweir         "movq  16(%%rax), %%rdx\n\t"
142cdf0e10cSrcweir         "movq  24(%%rax), %%rcx\n\t"
143cdf0e10cSrcweir         "movq  32(%%rax), %%r8\n\t"
144cdf0e10cSrcweir         "movq  40(%%rax), %%r9\n\t"
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         // Perform the call
147cdf0e10cSrcweir         "movq %0, %%r11\n\t"
148cdf0e10cSrcweir         "movq %3, %%rax\n\t"
149cdf0e10cSrcweir         "call *%%r11\n\t"
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         // Fill the return values
152cdf0e10cSrcweir         "movq   %%rax, %4\n\t"
153cdf0e10cSrcweir         "movq   %%rdx, %5\n\t"
154cdf0e10cSrcweir         "movsd %%xmm0, %6\n\t"
15581293574SPedro Giffuni         "movsd %%xmm1, %7\n\t"
156cdf0e10cSrcweir         :
157cdf0e10cSrcweir         : "m" ( pMethod ), "m" ( pGPR ), "m" ( pFPR ), "m" ( nFPR ),
158fea2173fSHerbert Dürr           "m" ( rax ), "m" ( rdx ), "m" ( xmm0 ), "m" ( xmm1 ),
159fea2173fSHerbert Dürr           "m" (pCallStack) // dummy input to prevent the compiler from optimizing the alloca out
160cdf0e10cSrcweir         : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r11"
161cdf0e10cSrcweir     );
162cdf0e10cSrcweir 
16381293574SPedro Giffuni     switch (pReturnTypeRef->eTypeClass)
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir     case typelib_TypeClass_HYPER:
166cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_HYPER:
167cdf0e10cSrcweir         *reinterpret_cast<sal_uInt64 *>( pRegisterReturn ) = rax;
168cdf0e10cSrcweir         break;
169cdf0e10cSrcweir     case typelib_TypeClass_LONG:
170cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_LONG:
171cdf0e10cSrcweir     case typelib_TypeClass_ENUM:
172cdf0e10cSrcweir         *reinterpret_cast<sal_uInt32 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt32*>( &rax );
173cdf0e10cSrcweir         break;
174cdf0e10cSrcweir     case typelib_TypeClass_CHAR:
175cdf0e10cSrcweir     case typelib_TypeClass_SHORT:
176cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_SHORT:
177cdf0e10cSrcweir         *reinterpret_cast<sal_uInt16 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt16*>( &rax );
178cdf0e10cSrcweir         break;
179cdf0e10cSrcweir     case typelib_TypeClass_BOOLEAN:
180cdf0e10cSrcweir     case typelib_TypeClass_BYTE:
181cdf0e10cSrcweir         *reinterpret_cast<sal_uInt8 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt8*>( &rax );
182cdf0e10cSrcweir         break;
183cdf0e10cSrcweir     case typelib_TypeClass_FLOAT:
184cdf0e10cSrcweir     case typelib_TypeClass_DOUBLE:
185cdf0e10cSrcweir         *reinterpret_cast<double *>( pRegisterReturn ) = xmm0;
186cdf0e10cSrcweir         break;
187cdf0e10cSrcweir     default:
188cdf0e10cSrcweir         {
18981293574SPedro Giffuni             sal_Int32 const nRetSize = pReturnTypeRef->pType->nSize;
190cdf0e10cSrcweir             if (bSimpleReturn && nRetSize <= 16 && nRetSize > 0)
191cdf0e10cSrcweir             {
19281293574SPedro Giffuni                 sal_uInt64 longs[2];
19381293574SPedro Giffuni                 longs[0] = rax;
19481293574SPedro Giffuni                 longs[1] = rdx;
19581293574SPedro Giffuni 
19681293574SPedro Giffuni                 double doubles[2];
19781293574SPedro Giffuni                 doubles[0] = xmm0;
19881293574SPedro Giffuni                 doubles[1] = xmm1;
19981293574SPedro Giffuni                 x86_64::fill_struct( pReturnTypeRef, &longs[0], &doubles[0], pRegisterReturn);
200cdf0e10cSrcweir             }
201cdf0e10cSrcweir             break;
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir //==================================================================================================
207cdf0e10cSrcweir 
208cdf0e10cSrcweir // Macros for easier insertion of values to registers or stack
209cdf0e10cSrcweir // pSV - pointer to the source
210cdf0e10cSrcweir // nr - order of the value [will be increased if stored to register]
211cdf0e10cSrcweir // pFPR, pGPR - pointer to the registers
212cdf0e10cSrcweir // pDS - pointer to the stack [will be increased if stored here]
213cdf0e10cSrcweir 
214cdf0e10cSrcweir // The value in %xmm register is already prepared to be retrieved as a float,
215cdf0e10cSrcweir // thus we treat float and double the same
216cdf0e10cSrcweir #define INSERT_FLOAT_DOUBLE( pSV, nr, pFPR, pDS ) \
217cdf0e10cSrcweir 	if ( nr < x86_64::MAX_SSE_REGS ) \
218cdf0e10cSrcweir 		pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
219cdf0e10cSrcweir 	else \
220cdf0e10cSrcweir 		*pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
221cdf0e10cSrcweir 
222cdf0e10cSrcweir #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
223cdf0e10cSrcweir 	if ( nr < x86_64::MAX_GPR_REGS ) \
224cdf0e10cSrcweir 		pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
225cdf0e10cSrcweir 	else \
226cdf0e10cSrcweir 		*pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
229cdf0e10cSrcweir 	if ( nr < x86_64::MAX_GPR_REGS ) \
230cdf0e10cSrcweir 		pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
231cdf0e10cSrcweir 	else \
232cdf0e10cSrcweir 		*pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
235cdf0e10cSrcweir 	if ( nr < x86_64::MAX_GPR_REGS ) \
236cdf0e10cSrcweir 		pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
237cdf0e10cSrcweir 	else \
238cdf0e10cSrcweir 		*pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
241cdf0e10cSrcweir 	if ( nr < x86_64::MAX_GPR_REGS ) \
242cdf0e10cSrcweir 		pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
243cdf0e10cSrcweir 	else \
244cdf0e10cSrcweir 		*pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir //==================================================================================================
247cdf0e10cSrcweir 
248942d46e3SPedro Giffuni namespace {
249942d46e3SPedro Giffuni 
appendCString(OUStringBuffer & buffer,char const * text)250942d46e3SPedro Giffuni void appendCString(OUStringBuffer & buffer, char const * text) {
251942d46e3SPedro Giffuni     if (text != 0) {
252942d46e3SPedro Giffuni         buffer.append(
253942d46e3SPedro Giffuni             OStringToOUString(OString(text), RTL_TEXTENCODING_ISO_8859_1));
254942d46e3SPedro Giffuni             // use 8859-1 to avoid conversion failure
255942d46e3SPedro Giffuni     }
256942d46e3SPedro Giffuni }
257942d46e3SPedro Giffuni 
258942d46e3SPedro Giffuni }
259942d46e3SPedro Giffuni 
cpp_call(bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,bridges::cpp_uno::shared::VtableSlot aVtableSlot,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void * pUnoReturn,void * pUnoArgs[],uno_Any ** ppUnoExc)260cdf0e10cSrcweir static void cpp_call(
261cdf0e10cSrcweir 	bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
262cdf0e10cSrcweir 	bridges::cpp_uno::shared::VtableSlot aVtableSlot,
263cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef,
264cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
265cdf0e10cSrcweir 	void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	// Maxium space for [complex ret ptr], values | ptr ...
268cdf0e10cSrcweir 	// (but will be used less - some of the values will be in pGPR and pFPR)
269cdf0e10cSrcweir   	sal_uInt64 *pStack = (sal_uInt64 *)__builtin_alloca( (nParams + 3) * sizeof(sal_uInt64) );
270cdf0e10cSrcweir   	sal_uInt64 *pStackStart = pStack;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	sal_uInt64 pGPR[x86_64::MAX_GPR_REGS];
273cdf0e10cSrcweir 	sal_uInt32 nGPR = 0;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	double pFPR[x86_64::MAX_SSE_REGS];
276cdf0e10cSrcweir 	sal_uInt32 nFPR = 0;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	// Return
279cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
280cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
281cdf0e10cSrcweir 	OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion (see below)
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	bool bSimpleReturn = true;
286cdf0e10cSrcweir 	if ( pReturnTypeDescr )
287cdf0e10cSrcweir 	{
288cdf0e10cSrcweir 		if ( x86_64::return_in_hidden_param( pReturnTypeRef ) )
289cdf0e10cSrcweir 			bSimpleReturn = false;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 		if ( bSimpleReturn )
292cdf0e10cSrcweir 			pCppReturn = pUnoReturn; // direct way for simple types
293cdf0e10cSrcweir 		else
294cdf0e10cSrcweir 		{
295cdf0e10cSrcweir 			// complex return via ptr
296cdf0e10cSrcweir 			pCppReturn = bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )?
297cdf0e10cSrcweir 						 __builtin_alloca( pReturnTypeDescr->nSize ) : pUnoReturn;
298cdf0e10cSrcweir 			INSERT_INT64( &pCppReturn, nGPR, pGPR, pStack );
299cdf0e10cSrcweir 		}
300cdf0e10cSrcweir 	}
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	// Push "this" pointer
303cdf0e10cSrcweir 	void * pAdjustedThisPtr = reinterpret_cast< void ** >( pThis->getCppI() ) + aVtableSlot.offset;
304cdf0e10cSrcweir 	INSERT_INT64( &pAdjustedThisPtr, nGPR, pGPR, pStack );
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	// Args
307cdf0e10cSrcweir 	void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
308cdf0e10cSrcweir 	// Indizes of values this have to be converted (interface conversion cpp<=>uno)
309cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
310cdf0e10cSrcweir 	// Type descriptions for reconversions
311cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	sal_Int32 nTempIndizes = 0;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
316cdf0e10cSrcweir 	{
317cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
318cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = 0;
319cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 		if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
322cdf0e10cSrcweir 		{
323cdf0e10cSrcweir 			uno_copyAndConvertData( pCppArgs[nPos] = alloca( 8 ), pUnoArgs[nPos], pParamTypeDescr,
324cdf0e10cSrcweir 									pThis->getBridge()->getUno2Cpp() );
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 			switch (pParamTypeDescr->eTypeClass)
327cdf0e10cSrcweir 			{
328cdf0e10cSrcweir 			case typelib_TypeClass_HYPER:
329cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_HYPER:
330cdf0e10cSrcweir 				INSERT_INT64( pCppArgs[nPos], nGPR, pGPR, pStack );
331cdf0e10cSrcweir 				break;
332cdf0e10cSrcweir 			case typelib_TypeClass_LONG:
333cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_LONG:
334cdf0e10cSrcweir 			case typelib_TypeClass_ENUM:
335cdf0e10cSrcweir 				INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
336cdf0e10cSrcweir 				break;
337cdf0e10cSrcweir 			case typelib_TypeClass_SHORT:
338cdf0e10cSrcweir 			case typelib_TypeClass_CHAR:
339cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_SHORT:
340cdf0e10cSrcweir 				INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
341cdf0e10cSrcweir 				break;
342cdf0e10cSrcweir 			case typelib_TypeClass_BOOLEAN:
343cdf0e10cSrcweir 			case typelib_TypeClass_BYTE:
344cdf0e10cSrcweir 				INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
345cdf0e10cSrcweir 				break;
346cdf0e10cSrcweir 			case typelib_TypeClass_FLOAT:
347cdf0e10cSrcweir 			case typelib_TypeClass_DOUBLE:
348cdf0e10cSrcweir 				INSERT_FLOAT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
349cdf0e10cSrcweir 				break;
35081293574SPedro Giffuni 			default:
35181293574SPedro Giffuni 				break;
352cdf0e10cSrcweir 			}
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 			// no longer needed
355cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
356cdf0e10cSrcweir 		}
357cdf0e10cSrcweir 		else // ptr to complex value | ref
358cdf0e10cSrcweir 		{
359cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
360cdf0e10cSrcweir 			{
361cdf0e10cSrcweir 				// cpp out is constructed mem, uno out is not!
362cdf0e10cSrcweir 				uno_constructData(
363cdf0e10cSrcweir 					pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
364cdf0e10cSrcweir 					pParamTypeDescr );
365cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
366cdf0e10cSrcweir 				// will be released at reconversion
367cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
368cdf0e10cSrcweir 			}
369cdf0e10cSrcweir 			// is in/inout
370cdf0e10cSrcweir 			else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
371cdf0e10cSrcweir 			{
372cdf0e10cSrcweir 				uno_copyAndConvertData(
373cdf0e10cSrcweir 					pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
374cdf0e10cSrcweir 					pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
377cdf0e10cSrcweir 				// will be released at reconversion
378cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
379cdf0e10cSrcweir 			}
380cdf0e10cSrcweir 			else // direct way
381cdf0e10cSrcweir 			{
382cdf0e10cSrcweir 				pCppArgs[nPos] = pUnoArgs[nPos];
383cdf0e10cSrcweir 				// no longer needed
384cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
385cdf0e10cSrcweir 			}
386cdf0e10cSrcweir 			INSERT_INT64( &(pCppArgs[nPos]), nGPR, pGPR, pStack );
387cdf0e10cSrcweir 		}
388cdf0e10cSrcweir 	}
389cdf0e10cSrcweir 
390cdf0e10cSrcweir 	try
391cdf0e10cSrcweir 	{
392942d46e3SPedro Giffuni         try {
393942d46e3SPedro Giffuni             callVirtualMethod(
394942d46e3SPedro Giffuni                 pAdjustedThisPtr, aVtableSlot.index,
395942d46e3SPedro Giffuni                 pCppReturn, pReturnTypeRef, bSimpleReturn,
396942d46e3SPedro Giffuni                 pStackStart, ( pStack - pStackStart ),
397942d46e3SPedro Giffuni                 pGPR, nGPR,
398942d46e3SPedro Giffuni                 pFPR, nFPR );
399942d46e3SPedro Giffuni         } catch (Exception &) {
400942d46e3SPedro Giffuni             throw;
401942d46e3SPedro Giffuni         } catch (std::exception & e) {
402942d46e3SPedro Giffuni             OUStringBuffer buf;
403942d46e3SPedro Giffuni             buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("C++ code threw "));
404942d46e3SPedro Giffuni             appendCString(buf, typeid(e).name());
405942d46e3SPedro Giffuni             buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(": "));
406942d46e3SPedro Giffuni             appendCString(buf, e.what());
407942d46e3SPedro Giffuni             throw RuntimeException(
408942d46e3SPedro Giffuni                 buf.makeStringAndClear(), Reference< XInterface >());
409942d46e3SPedro Giffuni         } catch (...) {
410942d46e3SPedro Giffuni             throw RuntimeException(
411942d46e3SPedro Giffuni                 OUString(
412942d46e3SPedro Giffuni                     RTL_CONSTASCII_USTRINGPARAM(
413942d46e3SPedro Giffuni                         "C++ code threw unknown exception")),
414942d46e3SPedro Giffuni                 Reference< XInterface >());
415942d46e3SPedro Giffuni         }
416942d46e3SPedro Giffuni 
417cdf0e10cSrcweir 		// NO exception occured...
418cdf0e10cSrcweir 		*ppUnoExc = 0;
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 		// reconvert temporary params
421cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
422cdf0e10cSrcweir 		{
423cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
424cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 			if (pParams[nIndex].bIn)
427cdf0e10cSrcweir 			{
428cdf0e10cSrcweir 				if (pParams[nIndex].bOut) // inout
429cdf0e10cSrcweir 				{
430cdf0e10cSrcweir 					uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
431cdf0e10cSrcweir 					uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
432cdf0e10cSrcweir 											pThis->getBridge()->getCpp2Uno() );
433cdf0e10cSrcweir 				}
434cdf0e10cSrcweir 			}
435cdf0e10cSrcweir 			else // pure out
436cdf0e10cSrcweir 			{
437cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
438cdf0e10cSrcweir 										pThis->getBridge()->getCpp2Uno() );
439cdf0e10cSrcweir 			}
440cdf0e10cSrcweir 			// destroy temp cpp param => cpp: every param was constructed
441cdf0e10cSrcweir 			uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
444cdf0e10cSrcweir 		}
445cdf0e10cSrcweir 		// return value
446cdf0e10cSrcweir 		if (pCppReturn && pUnoReturn != pCppReturn)
447cdf0e10cSrcweir 		{
448cdf0e10cSrcweir 			uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
449cdf0e10cSrcweir 									pThis->getBridge()->getCpp2Uno() );
450cdf0e10cSrcweir 			uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
451cdf0e10cSrcweir 		}
452cdf0e10cSrcweir 	}
453cdf0e10cSrcweir  	catch (...)
454cdf0e10cSrcweir  	{
455cdf0e10cSrcweir   		// fill uno exception
456de2c434cSPedro Giffuni 		CPPU_CURRENT_NAMESPACE::fillUnoException( __cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 		// temporary params
459cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
460cdf0e10cSrcweir 		{
461cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
462cdf0e10cSrcweir 			// destroy temp cpp param => cpp: every param was constructed
463cdf0e10cSrcweir 			uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
464cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
465cdf0e10cSrcweir 		}
466cdf0e10cSrcweir 		// return type
467cdf0e10cSrcweir 		if (pReturnTypeDescr)
468cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
469cdf0e10cSrcweir 	}
470cdf0e10cSrcweir }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir //==================================================================================================
473cdf0e10cSrcweir 
474cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared {
475cdf0e10cSrcweir 
unoInterfaceProxyDispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberDescr,void * pReturn,void * pArgs[],uno_Any ** ppException)476cdf0e10cSrcweir void unoInterfaceProxyDispatch(
477cdf0e10cSrcweir 	uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
478cdf0e10cSrcweir 	void * pReturn, void * pArgs[], uno_Any ** ppException )
479cdf0e10cSrcweir {
480cdf0e10cSrcweir 	// is my surrogate
481cdf0e10cSrcweir 	bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
482cdf0e10cSrcweir 		= static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
48381293574SPedro Giffuni #if OSL_DEBUG_LEVEL > 0
484cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
48581293574SPedro Giffuni #endif
486cdf0e10cSrcweir 
487cdf0e10cSrcweir 	switch (pMemberDescr->eTypeClass)
488cdf0e10cSrcweir 	{
489cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
490cdf0e10cSrcweir 	{
49181293574SPedro Giffuni #if OSL_DEBUG_LEVEL > 0
492cdf0e10cSrcweir 		// determine vtable call index
493cdf0e10cSrcweir 		sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
494cdf0e10cSrcweir 		OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
49581293574SPedro Giffuni #endif
496cdf0e10cSrcweir 		VtableSlot aVtableSlot(
497cdf0e10cSrcweir 				getVtableSlot(
498cdf0e10cSrcweir 					reinterpret_cast<
499cdf0e10cSrcweir 					typelib_InterfaceAttributeTypeDescription const * >(
500cdf0e10cSrcweir 						pMemberDescr)));
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 		if (pReturn)
503cdf0e10cSrcweir 		{
504cdf0e10cSrcweir 			// dependent dispatch
505cdf0e10cSrcweir 			cpp_call(
506cdf0e10cSrcweir 				pThis, aVtableSlot,
507cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
508cdf0e10cSrcweir 				0, 0, // no params
509cdf0e10cSrcweir 				pReturn, pArgs, ppException );
510cdf0e10cSrcweir 		}
511cdf0e10cSrcweir 		else
512cdf0e10cSrcweir 		{
513cdf0e10cSrcweir 			// is SET
514cdf0e10cSrcweir 			typelib_MethodParameter aParam;
515cdf0e10cSrcweir 			aParam.pTypeRef =
516cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
517cdf0e10cSrcweir 			aParam.bIn		= sal_True;
518cdf0e10cSrcweir 			aParam.bOut		= sal_False;
519cdf0e10cSrcweir 
520cdf0e10cSrcweir 			typelib_TypeDescriptionReference * pReturnTypeRef = 0;
521cdf0e10cSrcweir 			OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
522cdf0e10cSrcweir 			typelib_typedescriptionreference_new(
523cdf0e10cSrcweir 				&pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 			// dependent dispatch
526cdf0e10cSrcweir 			aVtableSlot.index += 1; // get, then set method
527cdf0e10cSrcweir 			cpp_call(
528cdf0e10cSrcweir 				pThis, aVtableSlot, // get, then set method
529cdf0e10cSrcweir 				pReturnTypeRef,
530cdf0e10cSrcweir 				1, &aParam,
531cdf0e10cSrcweir 				pReturn, pArgs, ppException );
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 			typelib_typedescriptionreference_release( pReturnTypeRef );
534cdf0e10cSrcweir 		}
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 		break;
537cdf0e10cSrcweir 	}
538cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
539cdf0e10cSrcweir 	{
54081293574SPedro Giffuni #if OSL_DEBUG_LEVEL > 0
541cdf0e10cSrcweir 		// determine vtable call index
542cdf0e10cSrcweir 		sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
543cdf0e10cSrcweir 		OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
54481293574SPedro Giffuni #endif
545cdf0e10cSrcweir 		VtableSlot aVtableSlot(
546cdf0e10cSrcweir 				getVtableSlot(
547cdf0e10cSrcweir 					reinterpret_cast<
548cdf0e10cSrcweir 					typelib_InterfaceMethodTypeDescription const * >(
549cdf0e10cSrcweir 						pMemberDescr)));
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 		switch (aVtableSlot.index)
552cdf0e10cSrcweir 		{
553cdf0e10cSrcweir 			// standard calls
554cdf0e10cSrcweir 		case 1: // acquire uno interface
555cdf0e10cSrcweir 			(*pUnoI->acquire)( pUnoI );
556cdf0e10cSrcweir 			*ppException = 0;
557cdf0e10cSrcweir 			break;
558cdf0e10cSrcweir 		case 2: // release uno interface
559cdf0e10cSrcweir 			(*pUnoI->release)( pUnoI );
560cdf0e10cSrcweir 			*ppException = 0;
561cdf0e10cSrcweir 			break;
562cdf0e10cSrcweir 		case 0: // queryInterface() opt
563cdf0e10cSrcweir 		{
564cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
565cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
566cdf0e10cSrcweir 			if (pTD)
567cdf0e10cSrcweir 			{
568cdf0e10cSrcweir                 uno_Interface * pInterface = 0;
569cdf0e10cSrcweir                 (*pThis->getBridge()->getUnoEnv()->getRegisteredInterface)(
570cdf0e10cSrcweir                     pThis->getBridge()->getUnoEnv(),
571cdf0e10cSrcweir                     (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
572cdf0e10cSrcweir 
573cdf0e10cSrcweir                 if (pInterface)
574cdf0e10cSrcweir                 {
575cdf0e10cSrcweir                     ::uno_any_construct(
576cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( pReturn ),
577cdf0e10cSrcweir                         &pInterface, pTD, 0 );
578cdf0e10cSrcweir                     (*pInterface->release)( pInterface );
579cdf0e10cSrcweir                     TYPELIB_DANGER_RELEASE( pTD );
580cdf0e10cSrcweir                     *ppException = 0;
581cdf0e10cSrcweir                     break;
582cdf0e10cSrcweir                 }
583cdf0e10cSrcweir                 TYPELIB_DANGER_RELEASE( pTD );
584cdf0e10cSrcweir             }
585cdf0e10cSrcweir 		} // else perform queryInterface()
586cdf0e10cSrcweir 		default:
587cdf0e10cSrcweir 			// dependent dispatch
588cdf0e10cSrcweir 			cpp_call(
589cdf0e10cSrcweir 				pThis, aVtableSlot,
590cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
591cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
592cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
593cdf0e10cSrcweir 				pReturn, pArgs, ppException );
594cdf0e10cSrcweir 		}
595cdf0e10cSrcweir 		break;
596cdf0e10cSrcweir 	}
597cdf0e10cSrcweir 	default:
598cdf0e10cSrcweir 	{
599cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException aExc(
600cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
601cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 		Type const & rExcType = ::getCppuType( &aExc );
604cdf0e10cSrcweir 		// binary identical null reference
605cdf0e10cSrcweir 		::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
606cdf0e10cSrcweir 	}
607cdf0e10cSrcweir 	}
608cdf0e10cSrcweir }
609cdf0e10cSrcweir 
610cdf0e10cSrcweir } } }
611