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"
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include <sal/alloca.h>
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"
45 #include "smallstruct.hxx"
47 using namespace ::rtl
;
48 using namespace ::com::sun::star::uno
;
53 //==================================================================================================
54 // The call instruction within the asm section of callVirtualMethod may throw
55 // exceptions. So that the compiler handles this correctly, it is important
56 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
57 // never happens at runtime), which in turn can throw exceptions, and (b)
58 // callVirtualMethod is not inlined at its call site (so that any exceptions are
59 // caught which are thrown from the instruction calling callVirtualMethod):
60 void callVirtualMethod(
61 void * pAdjustedThisPtr
,
62 sal_Int32 nVtableIndex
,
63 void * pRegisterReturn
,
64 typelib_TypeDescription
const * returnType
,
65 sal_Int32
* pStackLongs
,
66 sal_Int32 nStackLongs
) __attribute__((noinline
));
68 void callVirtualMethod(
69 void * pAdjustedThisPtr
,
70 sal_Int32 nVtableIndex
,
71 void * pRegisterReturn
,
72 typelib_TypeDescription
const * returnType
,
73 sal_Int32
* pStackLongs
,
74 sal_Int32 nStackLongs
)
76 // parameter list is mixed list of * and values
77 // reference parameters are pointers
79 OSL_ENSURE( pStackLongs
&& pAdjustedThisPtr
, "### null ptr!" );
80 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
81 OSL_ENSURE( nStackLongs
&& pStackLongs
, "### no stack in callVirtualMethod !" );
84 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
86 volatile long edx
= 0, eax
= 0; // for register returns
92 "mov %%eax, %%edx\n\t"
101 // do the actual call
103 "mov 0(%%edx), %%edx\n\t"
106 "add %%eax, %%edx\n\t"
107 "mov 0(%%edx), %%edx\n\t"
109 // save return registers
115 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
116 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
118 switch( returnType
->eTypeClass
)
120 case typelib_TypeClass_VOID
:
122 case typelib_TypeClass_HYPER
:
123 case typelib_TypeClass_UNSIGNED_HYPER
:
124 ((long*)pRegisterReturn
)[1] = edx
;
125 case typelib_TypeClass_LONG
:
126 case typelib_TypeClass_UNSIGNED_LONG
:
127 case typelib_TypeClass_CHAR
:
128 case typelib_TypeClass_ENUM
:
129 ((long*)pRegisterReturn
)[0] = eax
;
131 case typelib_TypeClass_SHORT
:
132 case typelib_TypeClass_UNSIGNED_SHORT
:
133 *(unsigned short*)pRegisterReturn
= eax
;
135 case typelib_TypeClass_BOOLEAN
:
136 case typelib_TypeClass_BYTE
:
137 *(unsigned char*)pRegisterReturn
= eax
;
139 case typelib_TypeClass_FLOAT
:
140 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
142 case typelib_TypeClass_DOUBLE
:
143 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
145 case typelib_TypeClass_STRUCT
:
146 if (bridges::cpp_uno::shared::isSmallStruct(returnType
)) {
147 if (returnType
->nSize
<= 1) {
148 *(unsigned char*)pRegisterReturn
= eax
;
150 else if (returnType
->nSize
<= 2) {
151 *(unsigned short*)pRegisterReturn
= eax
;
153 else if (returnType
->nSize
<= 8) {
154 ((long*)pRegisterReturn
)[0] = eax
;
155 if (returnType
->nSize
> 4) {
156 ((long*)pRegisterReturn
)[1] = edx
;
166 //==================================================================================================
167 static void cpp_call(
168 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
169 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
170 typelib_TypeDescriptionReference
* pReturnTypeRef
,
171 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
172 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
174 // max space for: [complex ret ptr], values|ptr ...
177 (char *)malloc( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
179 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
181 char * pCppStackStart
= pCppStack
;
184 typelib_TypeDescription
* pReturnTypeDescr
= 0;
185 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
186 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
188 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
190 if (pReturnTypeDescr
)
192 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
194 pCppReturn
= pUnoReturn
; // direct way for simple types
198 // complex return via ptr
200 = (bridges::cpp_uno::shared::relatesToInterfaceType(
203 ? malloc( pReturnTypeDescr
->nSize
)
205 ? alloca( pReturnTypeDescr
->nSize
)
207 : pUnoReturn
); // direct way
208 if (!bridges::cpp_uno::shared::isSmallStruct(pReturnTypeDescr
)) {
209 *(void **)pCppStack
= pCppReturn
;
210 pCppStack
+= sizeof(void *);
215 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
216 + aVtableSlot
.offset
;
217 *(void**)pCppStack
= pAdjustedThisPtr
;
218 pCppStack
+= sizeof( void* );
221 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
224 void ** pCppArgs
= (void **)malloc( 3 * sizeof(void *) * nParams
);
226 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
228 // indizes of values this have to be converted (interface conversion cpp<=>uno)
229 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
230 // type descriptions for reconversions
231 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
233 sal_Int32 nTempIndizes
= 0;
235 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
237 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
238 typelib_TypeDescription
* pParamTypeDescr
= 0;
239 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
242 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
244 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
245 pThis
->getBridge()->getUno2Cpp() );
247 switch (pParamTypeDescr
->eTypeClass
)
249 case typelib_TypeClass_HYPER
:
250 case typelib_TypeClass_UNSIGNED_HYPER
:
251 case typelib_TypeClass_DOUBLE
:
252 pCppStack
+= sizeof(sal_Int32
); // extra long
258 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
260 else // ptr to complex value | ref
262 if (! rParam
.bIn
) // is pure out
264 // cpp out is constructed mem, uno out is not!
267 *(void **)pCppStack
= pCppArgs
[nPos
] = malloc( pParamTypeDescr
->nSize
),
269 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
272 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
273 // will be released at reconversion
274 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
277 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
280 uno_copyAndConvertData(
282 *(void **)pCppStack
= pCppArgs
[nPos
] = malloc( pParamTypeDescr
->nSize
),
284 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
286 pUnoArgs
[nPos
], pParamTypeDescr
,
287 pThis
->getBridge()->getUno2Cpp() );
289 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
290 // will be released at reconversion
291 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
295 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
297 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
300 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
305 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
307 pAdjustedThisPtr
, aVtableSlot
.index
,
308 pCppReturn
, pReturnTypeDescr
,
309 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
310 // NO exception occured...
313 // reconvert temporary params
314 for ( ; nTempIndizes
--; )
316 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
317 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
319 if (pParams
[nIndex
].bIn
)
321 if (pParams
[nIndex
].bOut
) // inout
323 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
324 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
325 pThis
->getBridge()->getCpp2Uno() );
330 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
331 pThis
->getBridge()->getCpp2Uno() );
333 // destroy temp cpp param => cpp: every param was constructed
334 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
336 free( pCppArgs
[nIndex
] );
339 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
342 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
344 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
345 pThis
->getBridge()->getCpp2Uno() );
346 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
351 // fill uno exception
352 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
355 for ( ; nTempIndizes
--; )
357 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
358 // destroy temp cpp param => cpp: every param was constructed
359 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
360 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
362 free( pCppArgs
[nIndex
] );
366 if (pReturnTypeDescr
)
367 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
369 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
376 free( pCppStackStart
);
382 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
384 void unoInterfaceProxyDispatch(
385 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
386 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
389 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
390 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
392 switch (pMemberDescr
->eTypeClass
)
394 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
396 VtableSlot
aVtableSlot(
399 typelib_InterfaceAttributeTypeDescription
const * >(
403 // dependent dispatch
406 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
408 pReturn
, pArgs
, ppException
);
413 typelib_MethodParameter aParam
;
415 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
416 aParam
.bIn
= sal_True
;
417 aParam
.bOut
= sal_False
;
419 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
420 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
421 typelib_typedescriptionreference_new(
422 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
424 // dependent dispatch
425 aVtableSlot
.index
+= 1; // get, then set method
430 pReturn
, pArgs
, ppException
);
432 typelib_typedescriptionreference_release( pReturnTypeRef
);
437 case typelib_TypeClass_INTERFACE_METHOD
:
439 VtableSlot
aVtableSlot(
442 typelib_InterfaceMethodTypeDescription
const * >(
444 switch (aVtableSlot
.index
)
447 case 1: // acquire uno interface
448 (*pUnoI
->acquire
)( pUnoI
);
451 case 2: // release uno interface
452 (*pUnoI
->release
)( pUnoI
);
455 case 0: // queryInterface() opt
457 typelib_TypeDescription
* pTD
= 0;
458 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
461 uno_Interface
* pInterface
= 0;
462 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
463 pThis
->pBridge
->getUnoEnv(),
464 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
469 reinterpret_cast< uno_Any
* >( pReturn
),
470 &pInterface
, pTD
, 0 );
471 (*pInterface
->release
)( pInterface
);
472 TYPELIB_DANGER_RELEASE( pTD
);
476 TYPELIB_DANGER_RELEASE( pTD
);
478 } // else perform queryInterface()
480 // dependent dispatch
483 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
484 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
485 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
486 pReturn
, pArgs
, ppException
);
492 ::com::sun::star::uno::RuntimeException
aExc(
493 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
494 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
496 Type
const & rExcType
= ::getCppuType( &aExc
);
497 // binary identical null reference
498 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );