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 #include <malloc.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
27cdf0e10cSrcweir #include <uno/data.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
30cdf0e10cSrcweir #include "bridges/cpp_uno/shared/types.hxx"
31cdf0e10cSrcweir #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
32cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtables.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "share.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //#define BRDEBUG
37cdf0e10cSrcweir #ifdef BRDEBUG
38cdf0e10cSrcweir #include <stdio.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::rtl;
43cdf0e10cSrcweir using namespace ::com::sun::star::uno;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir   //==================================================================================================
callVirtualMethod(void * pAdjustedThisPtr,sal_Int32 nVtableIndex,void * pRegisterReturn,typelib_TypeClass eReturnType,char * pPT,sal_Int32 * pStackLongs,sal_Int32)50cdf0e10cSrcweir   static void callVirtualMethod(
51cdf0e10cSrcweir 	  void * pAdjustedThisPtr,
52cdf0e10cSrcweir 	  sal_Int32 nVtableIndex,
53cdf0e10cSrcweir 	  void * pRegisterReturn,
54cdf0e10cSrcweir 	  typelib_TypeClass eReturnType,
55cdf0e10cSrcweir 	  char * pPT,
56cdf0e10cSrcweir 	  sal_Int32 * pStackLongs,
57cdf0e10cSrcweir 	  sal_Int32 /*nStackLongs*/)
58cdf0e10cSrcweir   {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	// parameter list is mixed list of * and values
61cdf0e10cSrcweir 	// reference parameters are pointers
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	unsigned long * mfunc;        // actual function to be invoked
64cdf0e10cSrcweir 	void (*ptr)();
65cdf0e10cSrcweir 	int gpr[4];                   // storage for gpregisters, map to a0-a3
66cdf0e10cSrcweir 	int off;                      // offset used to find function
67cdf0e10cSrcweir 	int nw;                       // number of words mapped
68cdf0e10cSrcweir 	long *p;                      // pointer to parameter overflow area
69cdf0e10cSrcweir 	int c;                        // character of parameter type being decoded
70cdf0e10cSrcweir 	int iret, iret2;              // temporary function return values
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	// never called
73cdf0e10cSrcweir 	if (! pAdjustedThisPtr ) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #ifdef BRDEBUG
76cdf0e10cSrcweir 	fprintf(stderr,"in CallVirtualMethod\n");
77cdf0e10cSrcweir #endif
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	// Because of the MIPS O32 calling conventions we could be passing
80cdf0e10cSrcweir 	// parameters in both register types and on the stack. To create the
81cdf0e10cSrcweir 	// stack parameter area we need we now simply allocate local
82cdf0e10cSrcweir 	// variable storage param[] that is at least the size of the parameter stack
83cdf0e10cSrcweir 	// (more than enough space) which we can overwrite the parameters into.
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	/* p = sp - 512; new sp will be p - 16, but we don't change sp
86cdf0e10cSrcweir 	 * at this time to avoid breaking ABI--not sure whether changing sp will break
87*e883eaddSJohn Bampton 	 * references to local variables. For the same reason, we use absolute value.
88cdf0e10cSrcweir 	 */
89cdf0e10cSrcweir 	__asm__ __volatile__ (
90cdf0e10cSrcweir 		"addiu $2,$29,-512\n\t"
91cdf0e10cSrcweir 		"move %0,$2\n\t"
92cdf0e10cSrcweir 		:"=r"(p): : "$2","$29" );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir #ifdef BRDEBUG
95cdf0e10cSrcweir 	 if (nStackLongs * 4 > 512 )
96cdf0e10cSrcweir 		 fprintf(stderr,"too many arguments");
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	// now begin to load the C++ function arguments into storage
100cdf0e10cSrcweir 	nw = 0;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	// now we need to parse the entire signature string */
103cdf0e10cSrcweir 	// until we get the END indicator */
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	// treat complex return pointer like any other parameter //
106cdf0e10cSrcweir 
107cdf0e10cSrcweir #ifdef BRDEBUG
108cdf0e10cSrcweir 	fprintf(stderr,"overflow area pointer p=%p\n",p);
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	/* Let's figure out what is really going on here*/
11107a3d7f1SPedro Giffuni 	fprintf(stderr,"callVirtualMethod parameters string is %s\n",pPT);
112cdf0e10cSrcweir 	int k = nStackLongs;
113cdf0e10cSrcweir 	long * q = (long *)pStackLongs;
114cdf0e10cSrcweir 	while (k > 0) {
115cdf0e10cSrcweir 	  fprintf(stderr,"uno stack is: %x\n",(unsigned int)*q);
116cdf0e10cSrcweir 	  k--;
117cdf0e10cSrcweir 	  q++;
118cdf0e10cSrcweir 	}
119cdf0e10cSrcweir #endif
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	/* parse the argument list up to the ending ) */
122cdf0e10cSrcweir 	while (*pPT != 'X') {
123cdf0e10cSrcweir 	  c = *pPT;
124cdf0e10cSrcweir 	  switch (c) {
125cdf0e10cSrcweir 		case 'D':                   /* type is double */
126cdf0e10cSrcweir 		  /* treat the same as long long */
127cdf0e10cSrcweir 		case 'H':                /* type is long long */
128cdf0e10cSrcweir 		  if (nw & 1) nw++; 	/* note even elements gpr[] will map to
129cdf0e10cSrcweir 							   odd registers*/
130cdf0e10cSrcweir 		  if (nw < 4) {
131cdf0e10cSrcweir 			gpr[nw++] = *pStackLongs;
132cdf0e10cSrcweir 			gpr[nw++] = *(pStackLongs+1);
133cdf0e10cSrcweir 		  } else {
134cdf0e10cSrcweir 			if (((long) p) & 4)
135cdf0e10cSrcweir 			  p++;
136cdf0e10cSrcweir 			*p++ = *pStackLongs;
137cdf0e10cSrcweir 			*p++ = *(pStackLongs+1);
138cdf0e10cSrcweir 		  }
139cdf0e10cSrcweir 		  pStackLongs += 2;
140cdf0e10cSrcweir 		  break;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 		case 'S':
143cdf0e10cSrcweir 		  if (nw < 4) {
144cdf0e10cSrcweir 			gpr[nw++] = *((unsigned short*)pStackLongs);
145cdf0e10cSrcweir 		  } else {
146cdf0e10cSrcweir 			*p++ = *((unsigned short *)pStackLongs);
147cdf0e10cSrcweir 		  }
148cdf0e10cSrcweir 		  pStackLongs += 1;
149cdf0e10cSrcweir 		  break;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		case 'B':
152cdf0e10cSrcweir 		  if (nw < 4) {
153cdf0e10cSrcweir 			gpr[nw++] = *((char *)pStackLongs);
154cdf0e10cSrcweir 		  } else {
155cdf0e10cSrcweir 			*p++ = *((char *)pStackLongs);
156cdf0e10cSrcweir 		  }
157cdf0e10cSrcweir 		  pStackLongs += 1;
158cdf0e10cSrcweir 		  break;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		default:
161cdf0e10cSrcweir 		  if (nw < 4) {
162cdf0e10cSrcweir 			gpr[nw++] = *pStackLongs;
163cdf0e10cSrcweir 		  } else {
164cdf0e10cSrcweir 			*p++ = *pStackLongs;
165cdf0e10cSrcweir 		  }
166cdf0e10cSrcweir 		  pStackLongs += 1;
167cdf0e10cSrcweir 		  break;
168cdf0e10cSrcweir 	  }
169cdf0e10cSrcweir 	  pPT++;
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	/* figure out the address of the function we need to invoke */
173cdf0e10cSrcweir 	off = nVtableIndex;
174cdf0e10cSrcweir 	off = off * 4;                         // 4 bytes per slot
175cdf0e10cSrcweir 	mfunc = *((unsigned long **)pAdjustedThisPtr);    // get the address of the vtable
176cdf0e10cSrcweir 	mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
177cdf0e10cSrcweir 	mfunc = *((unsigned long **)mfunc);                 // the function is stored at the address
178cdf0e10cSrcweir 	ptr = (void (*)())mfunc;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir #ifdef BRDEBUG
181cdf0e10cSrcweir 	fprintf(stderr,"calling function %p\n",mfunc);
182cdf0e10cSrcweir #endif
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	/* Set up the machine registers and invoke the function */
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	__asm__ __volatile__ (
187cdf0e10cSrcweir 		"lw	$4,	0(%0)\n\t"
188cdf0e10cSrcweir 		"lw	$5,	4(%0)\n\t"
189cdf0e10cSrcweir 		"lw	$6,	8(%0)\n\t"
190cdf0e10cSrcweir 		"lw	$7,	12(%0)\n\t"
191cdf0e10cSrcweir 		: : "r" (gpr)
192cdf0e10cSrcweir 		: "$4", "$5", "$6", "$7"
193cdf0e10cSrcweir 		);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	__asm__ __volatile__ ("addiu $29,$29,-528\r\n":::"$29");
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	(*ptr)();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	__asm__ __volatile__ ("addiu $29,$29,528\r\n":::"$29");
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	__asm__ __volatile__ (
202cdf0e10cSrcweir 		"sw $2,%0 \n\t"
203cdf0e10cSrcweir 		"sw $3,%1 \n\t"
204cdf0e10cSrcweir 		: "=m" (iret), "=m" (iret2) : );
205cdf0e10cSrcweir 	register float fret asm("$f0");
206cdf0e10cSrcweir 	register double	dret asm("$f0");
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	switch( eReturnType )
209cdf0e10cSrcweir 	{
210cdf0e10cSrcweir 	  case typelib_TypeClass_HYPER:
211cdf0e10cSrcweir 	  case typelib_TypeClass_UNSIGNED_HYPER:
212cdf0e10cSrcweir 	  	((long*)pRegisterReturn)[1] = iret2;	// fall through
213cdf0e10cSrcweir 	  case typelib_TypeClass_LONG:
214cdf0e10cSrcweir 	  case typelib_TypeClass_UNSIGNED_LONG:
215cdf0e10cSrcweir 	  case typelib_TypeClass_ENUM:
216cdf0e10cSrcweir 		((long*)pRegisterReturn)[0] = iret;
217cdf0e10cSrcweir 		break;
218cdf0e10cSrcweir 	  case typelib_TypeClass_CHAR:
219cdf0e10cSrcweir 	  case typelib_TypeClass_SHORT:
220cdf0e10cSrcweir 	  case typelib_TypeClass_UNSIGNED_SHORT:
221cdf0e10cSrcweir 		*(unsigned short*)pRegisterReturn = (unsigned short)iret;
222cdf0e10cSrcweir 		break;
223cdf0e10cSrcweir 	  case typelib_TypeClass_BOOLEAN:
224cdf0e10cSrcweir 	  case typelib_TypeClass_BYTE:
225cdf0e10cSrcweir 		*(unsigned char*)pRegisterReturn = (unsigned char)iret;
226cdf0e10cSrcweir 		break;
227cdf0e10cSrcweir 	  case typelib_TypeClass_FLOAT:
228cdf0e10cSrcweir 		*(float*)pRegisterReturn = fret;
229cdf0e10cSrcweir 		break;
230cdf0e10cSrcweir 	  case typelib_TypeClass_DOUBLE:
231cdf0e10cSrcweir 		*(double*)pRegisterReturn = dret;
232cdf0e10cSrcweir 		break;
233cdf0e10cSrcweir 	  default:
234cdf0e10cSrcweir 		break;
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir   }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir   //==================================================================================================
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)240cdf0e10cSrcweir   static void cpp_call(
241cdf0e10cSrcweir 	  bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
242cdf0e10cSrcweir 	  bridges::cpp_uno::shared::VtableSlot  aVtableSlot,
243cdf0e10cSrcweir 	  typelib_TypeDescriptionReference * pReturnTypeRef,
244cdf0e10cSrcweir 	  sal_Int32 nParams, typelib_MethodParameter * pParams,
245cdf0e10cSrcweir 	  void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
246cdf0e10cSrcweir   {
247cdf0e10cSrcweir 	// max space for: [complex ret ptr], values|ptr ...
248cdf0e10cSrcweir 	char * pCppStack		=
249cdf0e10cSrcweir 	  (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
250cdf0e10cSrcweir 	char * pCppStackStart	= pCppStack;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	// need to know parameter types for callVirtualMethod so generate a signature string
253cdf0e10cSrcweir 	char * pParamType = (char *) alloca(nParams+2);
254cdf0e10cSrcweir 	char * pPT = pParamType;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir #ifdef BRDEBUG
257cdf0e10cSrcweir   fprintf(stderr,"in cpp_call\n");
258cdf0e10cSrcweir #endif
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	// return
261cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
262cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
263cdf0e10cSrcweir 	// OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	if (pReturnTypeDescr)
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 	  if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
270cdf0e10cSrcweir 	  {
271cdf0e10cSrcweir 		pCppReturn = pUnoReturn; // direct way for simple types
272cdf0e10cSrcweir 	  }
273cdf0e10cSrcweir 	  else
274cdf0e10cSrcweir 	  {
275cdf0e10cSrcweir 		// complex return via ptr
276cdf0e10cSrcweir 		pCppReturn = *(void **)pCppStack =
277cdf0e10cSrcweir 		  (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
278cdf0e10cSrcweir 		   ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
279cdf0e10cSrcweir 		*pPT++ = 'I'; //signify that a complex return type on stack
280cdf0e10cSrcweir 		pCppStack += sizeof(void *);
281cdf0e10cSrcweir 	  }
282cdf0e10cSrcweir 	}
283cdf0e10cSrcweir 	// push this
284cdf0e10cSrcweir 	void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
285cdf0e10cSrcweir 	*(void**)pCppStack = pAdjustedThisPtr;
286cdf0e10cSrcweir 	pCppStack += sizeof( void* );
287cdf0e10cSrcweir 	*pPT++ = 'I';
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	// stack space
290cdf0e10cSrcweir 	// OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
291cdf0e10cSrcweir 	// args
292cdf0e10cSrcweir 	void ** pCppArgs  = (void **)alloca( 3 * sizeof(void *) * nParams );
293cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
294cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
295cdf0e10cSrcweir 	// type descriptions for reconversions
296cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	sal_Int32 nTempIndizes   = 0;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
301cdf0e10cSrcweir 	{
302cdf0e10cSrcweir 	  const typelib_MethodParameter & rParam = pParams[nPos];
303cdf0e10cSrcweir 	  typelib_TypeDescription * pParamTypeDescr = 0;
304cdf0e10cSrcweir 	  TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	  if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
307cdf0e10cSrcweir 	  {
308cdf0e10cSrcweir 		uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
309cdf0e10cSrcweir 			pThis->getBridge()->getUno2Cpp() );
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 		switch (pParamTypeDescr->eTypeClass)
312cdf0e10cSrcweir 		{
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 		  // we need to know type of each param so that we know whether to use
315cdf0e10cSrcweir 		  // gpr or fpr to pass in parameters:
316cdf0e10cSrcweir 		  // Key: I - int, long, pointer, etc means pass in gpr
317cdf0e10cSrcweir 		  //      B - byte value passed in gpr
318cdf0e10cSrcweir 		  //      S - short value passed in gpr
319cdf0e10cSrcweir 		  //      F - float value pass in fpr
320cdf0e10cSrcweir 		  //      D - double value pass in fpr
321cdf0e10cSrcweir 		  //      H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
322cdf0e10cSrcweir 		  //      X - indicates end of parameter description string
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 		  case typelib_TypeClass_LONG:
325cdf0e10cSrcweir 		  case typelib_TypeClass_UNSIGNED_LONG:
326cdf0e10cSrcweir 		  case typelib_TypeClass_ENUM:
327cdf0e10cSrcweir 			*pPT++ = 'I';
328cdf0e10cSrcweir 			break;
329cdf0e10cSrcweir 		  case typelib_TypeClass_SHORT:
330cdf0e10cSrcweir 		  case typelib_TypeClass_CHAR:
331cdf0e10cSrcweir 		  case typelib_TypeClass_UNSIGNED_SHORT:
332cdf0e10cSrcweir 			*pPT++ = 'S';
333cdf0e10cSrcweir 			break;
334cdf0e10cSrcweir 		  case typelib_TypeClass_BOOLEAN:
335cdf0e10cSrcweir 		  case typelib_TypeClass_BYTE:
336cdf0e10cSrcweir 			*pPT++ = 'B';
337cdf0e10cSrcweir 			break;
338cdf0e10cSrcweir 		  case typelib_TypeClass_FLOAT:
339cdf0e10cSrcweir 			*pPT++ = 'F';
340cdf0e10cSrcweir 			break;
341cdf0e10cSrcweir 		  case typelib_TypeClass_DOUBLE:
342cdf0e10cSrcweir 			*pPT++ = 'D';
343cdf0e10cSrcweir 			pCppStack += sizeof(sal_Int32); // extra long
344cdf0e10cSrcweir 			break;
345cdf0e10cSrcweir 		  case typelib_TypeClass_HYPER:
346cdf0e10cSrcweir 		  case typelib_TypeClass_UNSIGNED_HYPER:
347cdf0e10cSrcweir 			*pPT++ = 'H';
348cdf0e10cSrcweir 			pCppStack += sizeof(sal_Int32); // extra long
349cdf0e10cSrcweir 			break;
350cdf0e10cSrcweir 		  default:
351cdf0e10cSrcweir 			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 			  *(void **)pCppStack = 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 			  *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
374cdf0e10cSrcweir 			  pUnoArgs[nPos], pParamTypeDescr,
375cdf0e10cSrcweir 			  pThis->getBridge()->getUno2Cpp() );
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 		  pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
378cdf0e10cSrcweir 		  // will be released at reconversion
379cdf0e10cSrcweir 		  ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
380cdf0e10cSrcweir 		}
381cdf0e10cSrcweir 		else // direct way
382cdf0e10cSrcweir 		{
383cdf0e10cSrcweir 		  *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
384cdf0e10cSrcweir 		  // no longer needed
385cdf0e10cSrcweir 		  TYPELIB_DANGER_RELEASE( pParamTypeDescr );
386cdf0e10cSrcweir 		}
387cdf0e10cSrcweir 		// KBH: FIXME: is this the right way to pass these
388cdf0e10cSrcweir 		*pPT++='I';
389cdf0e10cSrcweir 	  }
390cdf0e10cSrcweir 	  pCppStack += sizeof(sal_Int32); // standard parameter length
391cdf0e10cSrcweir 	}
392cdf0e10cSrcweir 
393cdf0e10cSrcweir 	// terminate the signature string
394cdf0e10cSrcweir 	*pPT++='X';
395cdf0e10cSrcweir 	*pPT=0;
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	try
398cdf0e10cSrcweir 	{
399cdf0e10cSrcweir 	  OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
400cdf0e10cSrcweir 	  callVirtualMethod(
401cdf0e10cSrcweir 		  pAdjustedThisPtr, aVtableSlot.index,
402cdf0e10cSrcweir 		  pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
403cdf0e10cSrcweir 		  (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
40407a3d7f1SPedro Giffuni 	  // NO exception occurred...
405cdf0e10cSrcweir 	  *ppUnoExc = 0;
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 	  // reconvert temporary params
408cdf0e10cSrcweir 	  for ( ; nTempIndizes--; )
409cdf0e10cSrcweir 	  {
410cdf0e10cSrcweir 		sal_Int32 nIndex = pTempIndizes[nTempIndizes];
411cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 		if (pParams[nIndex].bIn)
414cdf0e10cSrcweir 		{
415cdf0e10cSrcweir 		  if (pParams[nIndex].bOut) // inout
416cdf0e10cSrcweir 		  {
417cdf0e10cSrcweir 			uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
418cdf0e10cSrcweir 			uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
419cdf0e10cSrcweir 				pThis->getBridge()->getCpp2Uno() );
420cdf0e10cSrcweir 		  }
421cdf0e10cSrcweir 		}
422cdf0e10cSrcweir 		else // pure out
423cdf0e10cSrcweir 		{
424cdf0e10cSrcweir 		  uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
425cdf0e10cSrcweir 			  pThis->getBridge()->getCpp2Uno() );
426cdf0e10cSrcweir 		}
427cdf0e10cSrcweir 		// destroy temp cpp param => cpp: every param was constructed
428cdf0e10cSrcweir 		uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pParamTypeDescr );
431cdf0e10cSrcweir 	  }
432cdf0e10cSrcweir 	  // return value
433cdf0e10cSrcweir 	  if (pCppReturn && pUnoReturn != pCppReturn)
434cdf0e10cSrcweir 	  {
435cdf0e10cSrcweir 		uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
436cdf0e10cSrcweir 			pThis->getBridge()->getCpp2Uno() );
437cdf0e10cSrcweir 		uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
438cdf0e10cSrcweir 	  }
439cdf0e10cSrcweir 	}
440cdf0e10cSrcweir 	catch (...)
441cdf0e10cSrcweir 	{
442cdf0e10cSrcweir 	  // fill uno exception
443cdf0e10cSrcweir 	  fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
444cdf0e10cSrcweir 		  *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 	  // temporary params
447cdf0e10cSrcweir 	  for ( ; nTempIndizes--; )
448cdf0e10cSrcweir 	  {
449cdf0e10cSrcweir 		sal_Int32 nIndex = pTempIndizes[nTempIndizes];
450cdf0e10cSrcweir 		// destroy temp cpp param => cpp: every param was constructed
451cdf0e10cSrcweir 		uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
452cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
453cdf0e10cSrcweir 	  }
454cdf0e10cSrcweir 	  // return type
455cdf0e10cSrcweir 	  if (pReturnTypeDescr)
456cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir   }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 
463cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared {
464cdf0e10cSrcweir 
465cdf0e10cSrcweir //==================================================================================================
unoInterfaceProxyDispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberDescr,void * pReturn,void * pArgs[],uno_Any ** ppException)466cdf0e10cSrcweir void unoInterfaceProxyDispatch(
467cdf0e10cSrcweir 	uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
468cdf0e10cSrcweir     void * pReturn, void * pArgs[], uno_Any ** ppException )
469cdf0e10cSrcweir {
470cdf0e10cSrcweir   // is my surrogate
471cdf0e10cSrcweir   bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
472cdf0e10cSrcweir 	= static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
473cdf0e10cSrcweir   //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
474cdf0e10cSrcweir 
475cdf0e10cSrcweir #ifdef BRDEBUG
476cdf0e10cSrcweir   fprintf(stderr,"in dispatch\n");
477cdf0e10cSrcweir #endif
478cdf0e10cSrcweir 
479cdf0e10cSrcweir   switch (pMemberDescr->eTypeClass)
480cdf0e10cSrcweir   {
481cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
482cdf0e10cSrcweir 	  {
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 		VtableSlot aVtableSlot(
485cdf0e10cSrcweir 			getVtableSlot(
486cdf0e10cSrcweir 			  reinterpret_cast<
487cdf0e10cSrcweir 			  typelib_InterfaceAttributeTypeDescription const * >(
488cdf0e10cSrcweir 				pMemberDescr)));
489cdf0e10cSrcweir 
490cdf0e10cSrcweir 		if (pReturn)
491cdf0e10cSrcweir 		{
492cdf0e10cSrcweir 		  // dependent dispatch
493cdf0e10cSrcweir 		  cpp_call(
494cdf0e10cSrcweir 			  pThis, aVtableSlot,
495cdf0e10cSrcweir 			  ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
496cdf0e10cSrcweir 			  0, 0, // no params
497cdf0e10cSrcweir 			  pReturn, pArgs, ppException );
498cdf0e10cSrcweir 		}
499cdf0e10cSrcweir 		else
500cdf0e10cSrcweir 		{
501cdf0e10cSrcweir 		  // is SET
502cdf0e10cSrcweir 		  typelib_MethodParameter aParam;
503cdf0e10cSrcweir 		  aParam.pTypeRef =
504cdf0e10cSrcweir 			((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
505cdf0e10cSrcweir 		  aParam.bIn		= sal_True;
506cdf0e10cSrcweir 		  aParam.bOut		= sal_False;
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 		  typelib_TypeDescriptionReference * pReturnTypeRef = 0;
509cdf0e10cSrcweir 		  OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
510cdf0e10cSrcweir 		  typelib_typedescriptionreference_new(
511cdf0e10cSrcweir 			  &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 		  // dependent dispatch
514cdf0e10cSrcweir 		  aVtableSlot.index += 1; //get then set method
515cdf0e10cSrcweir 		  cpp_call(
516cdf0e10cSrcweir 			  pThis, aVtableSlot,
517cdf0e10cSrcweir 			  pReturnTypeRef,
518cdf0e10cSrcweir 			  1, &aParam,
519cdf0e10cSrcweir 			  pReturn, pArgs, ppException );
520cdf0e10cSrcweir 
521cdf0e10cSrcweir 		  typelib_typedescriptionreference_release( pReturnTypeRef );
522cdf0e10cSrcweir 		}
523cdf0e10cSrcweir 
524cdf0e10cSrcweir 		break;
525cdf0e10cSrcweir 	  }
526cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
527cdf0e10cSrcweir 	  {
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 		VtableSlot aVtableSlot(
530cdf0e10cSrcweir 			getVtableSlot(
531cdf0e10cSrcweir 			  reinterpret_cast<
532cdf0e10cSrcweir 			  typelib_InterfaceMethodTypeDescription const * >(
533cdf0e10cSrcweir 				pMemberDescr)));
534cdf0e10cSrcweir 		switch (aVtableSlot.index)
535cdf0e10cSrcweir 		{
536cdf0e10cSrcweir 		  // standard calls
537cdf0e10cSrcweir 		  case 1: // acquire uno interface
538cdf0e10cSrcweir 			(*pUnoI->acquire)( pUnoI );
539cdf0e10cSrcweir 			*ppException = 0;
540cdf0e10cSrcweir 			break;
541cdf0e10cSrcweir 		  case 2: // release uno interface
542cdf0e10cSrcweir 			(*pUnoI->release)( pUnoI );
543cdf0e10cSrcweir 			*ppException = 0;
544cdf0e10cSrcweir 			break;
545cdf0e10cSrcweir 		  case 0: // queryInterface() opt
546cdf0e10cSrcweir 			{
547cdf0e10cSrcweir 			  typelib_TypeDescription * pTD = 0;
548cdf0e10cSrcweir 			  TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
549cdf0e10cSrcweir 			  if (pTD)
550cdf0e10cSrcweir 			  {
551cdf0e10cSrcweir 				uno_Interface * pInterface = 0;
552cdf0e10cSrcweir 				(*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
553cdf0e10cSrcweir 																	   pThis->pBridge->getUnoEnv(),
554cdf0e10cSrcweir 																	   (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 				if (pInterface)
557cdf0e10cSrcweir 				{
558cdf0e10cSrcweir 				  ::uno_any_construct(
559cdf0e10cSrcweir 					  reinterpret_cast< uno_Any * >( pReturn ),
560cdf0e10cSrcweir 					  &pInterface, pTD, 0 );
561cdf0e10cSrcweir 				  (*pInterface->release)( pInterface );
562cdf0e10cSrcweir 				  TYPELIB_DANGER_RELEASE( pTD );
563cdf0e10cSrcweir 				  *ppException = 0;
564cdf0e10cSrcweir 				  break;
565cdf0e10cSrcweir 				}
566cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pTD );
567cdf0e10cSrcweir 			  }
568cdf0e10cSrcweir 			} // else perform queryInterface()
569cdf0e10cSrcweir 		  default:
570cdf0e10cSrcweir 			// dependent dispatch
571cdf0e10cSrcweir 			cpp_call(
572cdf0e10cSrcweir 				pThis, aVtableSlot,
573cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
574cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
575cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
576cdf0e10cSrcweir 				pReturn, pArgs, ppException );
577cdf0e10cSrcweir 		}
578cdf0e10cSrcweir 		break;
579cdf0e10cSrcweir 	  }
580cdf0e10cSrcweir 	default:
581cdf0e10cSrcweir 	  {
582cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException aExc(
583cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
584cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 		Type const & rExcType = ::getCppuType( &aExc );
587cdf0e10cSrcweir 		// binary identical null reference
588cdf0e10cSrcweir 		::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
589cdf0e10cSrcweir 	  }
590cdf0e10cSrcweir   }
591cdf0e10cSrcweir }
592cdf0e10cSrcweir }}}
593cdf0e10cSrcweir 
594