1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
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"
36 #include <com/sun/star/uno/genfunc.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/vtables.hxx"
50 using namespace ::rtl
;
51 using namespace ::com::sun::star::uno
;
56 invoke_count_words(char * pPT
)
58 sal_Int32 overflow
= 0, gpr
= 0, fpr
= 0;
59 int c
; // character of parameter type being decoded
64 case 'D': /* type is double */
65 if (fpr
< 2) fpr
++; else overflow
+=2;
68 case 'F': /* type is float */
69 if (fpr
< 2) fpr
++; else overflow
++;
72 case 'H': /* type is long long */
73 if (gpr
< 4) gpr
+=2; else gpr
=5, overflow
+=2;
80 if (gpr
< 5) gpr
++; else overflow
++;
84 if (gpr
< 5) gpr
++; else overflow
++;
89 /* Round up number of overflow words to ensure stack
90 stays aligned to 8 bytes. */
91 return (overflow
+ 1) & ~1;
95 //invoke_copy_to_stack(sal_Int32 paramCount, sal_Int32 * pStackLongs, char * pPT, sal_Int32* d_ov, sal_Int32 overflow)
96 invoke_copy_to_stack(sal_Int32
* pStackLongs
, char * pPT
, sal_Int32
* d_ov
, sal_Int32 overflow
)
98 sal_Int32
*d_gpr
= d_ov
+ overflow
;
99 sal_Int64
*d_fpr
= (sal_Int64
*)(d_gpr
+ 5);
100 sal_Int32 gpr
= 0, fpr
= 0;
103 while (*pPT
!= 'X') {
106 case 'D': /* type is double */
108 *((double*) d_fpr
) = *((double *)pStackLongs
), d_fpr
++, fpr
++;
110 *((double*) d_ov
) = *((double *)pStackLongs
), d_ov
+=2;
115 case 'F': /* type is float */
117 *((sal_Int64
*) d_fpr
) = 0;
118 *((float*) d_fpr
) = *((float *)pStackLongs
), d_fpr
++, fpr
++;
121 *((sal_Int64
*) d_ov
) = 0;
122 *((float*) d_ov
) = *((float *)pStackLongs
), d_ov
++;
128 case 'H': /* type is long long */
130 *((sal_Int64
*) d_gpr
) = *((sal_Int64
*) pStackLongs
), d_gpr
+=2, gpr
+=2;
133 *((sal_Int64
*) d_ov
) = *((sal_Int64
*) pStackLongs
), d_ov
+=2, gpr
=5;
140 *((sal_uInt32
*)d_gpr
) = *((unsigned short*)pStackLongs
), d_gpr
++, gpr
++;
142 *((sal_uInt32
*)d_ov
) = *((unsigned short*)pStackLongs
), d_ov
++;
148 *((sal_Int32
*)d_gpr
) = *((signed short*)pStackLongs
), d_gpr
++, gpr
++;
150 *((sal_Int32
*)d_ov
) = *((signed short*)pStackLongs
), d_ov
++;
156 *((sal_uInt32
*)d_gpr
) = *((unsigned char*)pStackLongs
), d_gpr
++, gpr
++;
158 *((sal_uInt32
*)d_ov
) = *((unsigned char*)pStackLongs
), d_ov
++;
164 *((sal_Int32
*)d_gpr
) = *((signed char*)pStackLongs
), d_gpr
++, gpr
++;
166 *((sal_Int32
*)d_ov
) = *((signed char*)pStackLongs
), d_ov
++;
172 *((sal_Int32
*)d_gpr
) = *pStackLongs
, d_gpr
++, gpr
++;
174 *((sal_Int32
*)d_ov
) = *pStackLongs
, d_ov
++;
182 //==================================================================================================
183 static void callVirtualMethod(
185 sal_Int32 nVtableIndex
,
186 void * pRegisterReturn
,
187 typelib_TypeClass eReturnType
,
189 sal_Int32
* pStackLongs
,
190 sal_Int32 nStackLongs
)
193 // parameter list is mixed list of * and values
194 // reference parameters are pointers
196 // the basic idea here is to use gpr[5] as a storage area for
197 // the future values of registers r2 to r6 needed for the call,
198 // and similarly fpr[2] as a storage area for the future values
199 // of floating point registers f0 to f2
201 sal_Int32
*vtable
= *(sal_Int32
**)pThis
;
202 // sal_Int32 method = vtable[nVtableIndex + 2];
203 sal_Int32 method
= vtable
[nVtableIndex
];
204 sal_Int32 overflow
= invoke_count_words (pPT
);
206 volatile double dret
; // temporary function return values
208 volatile int iret
, iret2
;
210 void * dummy
= alloca(32); // dummy alloca to force r11 usage for exception handling
237 "a" (invoke_copy_to_stack
),
240 : "2", "3", "4", "5", "6", "7", "memory"
244 (*(void (*)())method
)();
255 : "=f" (fret
), "=f" (dret
), "=r" (iret
), "=r" (iret2
)
258 switch( eReturnType
)
260 case typelib_TypeClass_HYPER
:
261 case typelib_TypeClass_UNSIGNED_HYPER
:
262 // ((long*)pRegisterReturn)[0] = iret;
263 ((long*)pRegisterReturn
)[1] = iret2
;
264 case typelib_TypeClass_LONG
:
265 case typelib_TypeClass_UNSIGNED_LONG
:
266 case typelib_TypeClass_ENUM
:
267 ((long*)pRegisterReturn
)[0] = iret
;
269 case typelib_TypeClass_CHAR
:
270 case typelib_TypeClass_SHORT
:
271 case typelib_TypeClass_UNSIGNED_SHORT
:
272 *(unsigned short*)pRegisterReturn
= (unsigned short)iret
;
274 case typelib_TypeClass_BOOLEAN
:
275 case typelib_TypeClass_BYTE
:
276 *(unsigned char*)pRegisterReturn
= (unsigned char)iret
;
278 case typelib_TypeClass_FLOAT
:
279 *(float*)pRegisterReturn
= fret
;
281 case typelib_TypeClass_DOUBLE
:
282 *(double*)pRegisterReturn
= dret
;
288 //============================================================================
289 static void cpp_call(
290 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
291 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
292 typelib_TypeDescriptionReference
* pReturnTypeRef
,
293 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
294 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
296 // max space for: [complex ret ptr], values|ptr ...
298 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
299 char * pCppStackStart
= pCppStack
;
301 // need to know parameter types for callVirtualMethod so generate a signature string
302 char * pParamType
= (char *) alloca(nParams
+2);
303 char * pPT
= pParamType
;
306 typelib_TypeDescription
* pReturnTypeDescr
= 0;
307 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
308 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
310 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
312 if (pReturnTypeDescr
)
314 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
316 pCppReturn
= pUnoReturn
; // direct way for simple types
320 // complex return via ptr
321 pCppReturn
= *(void **)pCppStack
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
322 ? alloca( pReturnTypeDescr
->nSize
)
323 : pUnoReturn
); // direct way
324 *pPT
++ = 'I'; //signify that a complex return type on stack
325 pCppStack
+= sizeof(void *);
328 // push "this" pointer
329 void * pAdjustedThisPtr
= reinterpret_cast< void ** >( pThis
->getCppI() ) + aVtableSlot
.offset
;
330 *(void**)pCppStack
= pAdjustedThisPtr
;
331 pCppStack
+= sizeof( void* );
335 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
337 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
338 // indizes of values this have to be converted (interface conversion cpp<=>uno)
339 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
340 // type descriptions for reconversions
341 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
343 sal_Int32 nTempIndizes
= 0;
345 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
347 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
348 typelib_TypeDescription
* pParamTypeDescr
= 0;
349 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
351 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
353 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
354 pThis
->getBridge()->getUno2Cpp() );
356 switch (pParamTypeDescr
->eTypeClass
)
359 // we need to know type of each param so that we know whether to use
360 // gpr or fpr to pass in parameters:
361 // Key: I - int, long, pointer, etc means pass in gpr
362 // B - byte value passed in gpr
363 // S - short value passed in gpr
364 // F - float value pass in fpr
365 // D - double value pass in fpr
366 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
367 // X - indicates end of parameter description string
369 case typelib_TypeClass_LONG
:
370 case typelib_TypeClass_UNSIGNED_LONG
:
371 case typelib_TypeClass_ENUM
:
374 case typelib_TypeClass_SHORT
:
377 case typelib_TypeClass_CHAR
:
378 case typelib_TypeClass_UNSIGNED_SHORT
:
381 case typelib_TypeClass_BOOLEAN
:
384 case typelib_TypeClass_BYTE
:
387 case typelib_TypeClass_FLOAT
:
390 case typelib_TypeClass_DOUBLE
:
392 pCppStack
+= sizeof(sal_Int32
); // extra long
394 case typelib_TypeClass_HYPER
:
395 case typelib_TypeClass_UNSIGNED_HYPER
:
397 pCppStack
+= sizeof(sal_Int32
); // extra long
401 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
403 else // ptr to complex value | ref
405 if (! rParam
.bIn
) // is pure out
407 // cpp out is constructed mem, uno out is not!
409 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
411 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
412 // will be released at reconversion
413 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
416 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
418 uno_copyAndConvertData(
419 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
420 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
422 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
423 // will be released at reconversion
424 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
428 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
430 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
432 // KBH: FIXME: is this the right way to pass these
435 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
438 // terminate the signature string
444 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
446 pAdjustedThisPtr
, aVtableSlot
.index
,
447 pCppReturn
, pReturnTypeDescr
->eTypeClass
, pParamType
,
448 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
449 // NO exception occured...
452 // reconvert temporary params
453 for ( ; nTempIndizes
--; )
455 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
456 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
458 if (pParams
[nIndex
].bIn
)
460 if (pParams
[nIndex
].bOut
) // inout
462 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
463 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
464 pThis
->getBridge()->getCpp2Uno() );
469 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
470 pThis
->getBridge()->getCpp2Uno() );
472 // destroy temp cpp param => cpp: every param was constructed
473 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
475 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
478 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
480 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
481 pThis
->getBridge()->getCpp2Uno() );
482 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
487 // fill uno exception
488 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
489 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
493 for ( ; nTempIndizes
--; )
495 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
496 // destroy temp cpp param => cpp: every param was constructed
497 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
498 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
501 if (pReturnTypeDescr
)
502 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
507 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
509 void unoInterfaceProxyDispatch(
510 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
511 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
514 fprintf(stderr
, "unoInterfaceProxyDispatch\n");
519 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
520 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
521 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
523 switch (pMemberDescr
->eTypeClass
)
525 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
528 VtableSlot
aVtableSlot(
531 typelib_InterfaceAttributeTypeDescription
const * >(
536 // dependent dispatch
539 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
541 pReturn
, pArgs
, ppException
);
546 typelib_MethodParameter aParam
;
548 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
549 aParam
.bIn
= sal_True
;
550 aParam
.bOut
= sal_False
;
552 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
553 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
554 typelib_typedescriptionreference_new(
555 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
557 // dependent dispatch
558 aVtableSlot
.index
+= 1; //get then set method
563 pReturn
, pArgs
, ppException
);
565 typelib_typedescriptionreference_release( pReturnTypeRef
);
570 case typelib_TypeClass_INTERFACE_METHOD
:
573 VtableSlot
aVtableSlot(
576 typelib_InterfaceMethodTypeDescription
const * >(
578 switch (aVtableSlot
.index
)
581 case 1: // acquire uno interface
582 (*pUnoI
->acquire
)( pUnoI
);
585 case 2: // release uno interface
586 (*pUnoI
->release
)( pUnoI
);
589 case 0: // queryInterface() opt
591 typelib_TypeDescription
* pTD
= 0;
592 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
595 uno_Interface
* pInterface
= 0;
596 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
597 pThis
->pBridge
->getUnoEnv(),
598 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
603 reinterpret_cast< uno_Any
* >( pReturn
),
604 &pInterface
, pTD
, 0 );
605 (*pInterface
->release
)( pInterface
);
606 TYPELIB_DANGER_RELEASE( pTD
);
610 TYPELIB_DANGER_RELEASE( pTD
);
612 } // else perform queryInterface()
614 // dependent dispatch
617 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
618 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
619 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
620 pReturn
, pArgs
, ppException
);
626 ::com::sun::star::uno::RuntimeException
aExc(
627 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
628 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
630 Type
const & rExcType
= ::getCppuType( &aExc
);
631 // binary identical null reference
632 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
638 /* vi:set tabstop=4 shiftwidth=4 expandtab: */