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 <malloc.h>
38 #include <com/sun/star/uno/genfunc.hxx>
39 #include "com/sun/star/uno/RuntimeException.hpp"
42 #include "bridges/cpp_uno/shared/bridge.hxx"
43 #include "bridges/cpp_uno/shared/types.hxx"
44 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
45 #include "bridges/cpp_uno/shared/vtables.hxx"
49 using namespace ::rtl
;
50 using namespace ::com::sun::star::uno
;
55 //==================================================================================================
56 // The call instruction within the asm section of callVirtualMethod may throw
57 // exceptions. So that the compiler handles this correctly, it is important
58 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
59 // never happens at runtime), which in turn can throw exceptions, and (b)
60 // callVirtualMethod is not inlined at its call site (so that any exceptions are
61 // caught which are thrown from the instruction calling callVirtualMethod):
62 void callVirtualMethod(
63 void * pAdjustedThisPtr
,
64 sal_Int32 nVtableIndex
,
65 void * pRegisterReturn
,
66 typelib_TypeDescription
* pReturnTypeDescr
, bool bSimpleReturn
,
67 sal_Int32
* pStackLongs
,
68 sal_Int32 nStackLongs
) __attribute__((noinline
));
70 void callVirtualMethod(
71 void * pAdjustedThisPtr
,
72 sal_Int32 nVtableIndex
,
73 void * pRegisterReturn
,
74 typelib_TypeDescription
* pReturnTypeDescr
, bool bSimpleReturn
,
75 sal_Int32
* pStackLongs
,
76 sal_Int32 nStackLongs
)
78 // parameter list is mixed list of * and values
79 // reference parameters are pointers
81 OSL_ENSURE( pStackLongs
&& pAdjustedThisPtr
, "### null ptr!" );
82 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
83 OSL_ENSURE( nStackLongs
&& pStackLongs
, "### no stack in callVirtualMethod !" );
86 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
88 volatile long edx
= 0, eax
= 0; // for register returns
93 "mov %%eax, %%edx\n\t"
94 // stack padding to keep stack aligned:
97 "add %%esp, %%eax\n\t"
99 "sub %%eax, %%esp\n\t"
101 "mov %%edx, %%eax\n\t"
110 // do the actual call
112 "mov 0(%%edx), %%edx\n\t"
115 "add %%eax, %%edx\n\t"
116 "mov 0(%%edx), %%edx\n\t"
118 // save return registers
124 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
125 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
127 switch( pReturnTypeDescr
->eTypeClass
)
129 case typelib_TypeClass_VOID
:
131 case typelib_TypeClass_HYPER
:
132 case typelib_TypeClass_UNSIGNED_HYPER
:
133 ((long*)pRegisterReturn
)[1] = edx
;
134 case typelib_TypeClass_LONG
:
135 case typelib_TypeClass_UNSIGNED_LONG
:
136 case typelib_TypeClass_CHAR
:
137 case typelib_TypeClass_ENUM
:
138 ((long*)pRegisterReturn
)[0] = eax
;
140 case typelib_TypeClass_SHORT
:
141 case typelib_TypeClass_UNSIGNED_SHORT
:
142 *(unsigned short*)pRegisterReturn
= eax
;
144 case typelib_TypeClass_BOOLEAN
:
145 case typelib_TypeClass_BYTE
:
146 *(unsigned char*)pRegisterReturn
= eax
;
148 case typelib_TypeClass_FLOAT
:
149 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
151 case typelib_TypeClass_DOUBLE
:
152 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
155 sal_Int32
const nRetSize
= pReturnTypeDescr
->nSize
;
156 if (bSimpleReturn
&& nRetSize
<= 8 && nRetSize
> 0) {
158 static_cast<long *>(pRegisterReturn
)[1] = edx
;
159 static_cast<long *>(pRegisterReturn
)[0] = eax
;
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 ...
176 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
177 char * pCppStackStart
= pCppStack
;
180 typelib_TypeDescription
* pReturnTypeDescr
= 0;
181 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
182 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
184 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
185 bool bSimpleReturn
= true;
187 if (pReturnTypeDescr
)
189 bSimpleReturn
= CPPU_CURRENT_NAMESPACE::isSimpleReturnType(
193 pCppReturn
= pUnoReturn
; // direct way for simple types
197 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
199 ? alloca( pReturnTypeDescr
->nSize
)
200 : pUnoReturn
); // direct way
201 // complex return via ptr
202 *(void **)pCppStack
= pCppReturn
;
203 pCppStack
+= sizeof(void *);
207 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
208 + aVtableSlot
.offset
;
209 *(void**)pCppStack
= pAdjustedThisPtr
;
210 pCppStack
+= sizeof( void* );
213 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
215 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
216 // indizes of values this have to be converted (interface conversion cpp<=>uno)
217 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
218 // type descriptions for reconversions
219 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
221 sal_Int32 nTempIndizes
= 0;
223 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
225 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
226 typelib_TypeDescription
* pParamTypeDescr
= 0;
227 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
230 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
232 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
233 pThis
->getBridge()->getUno2Cpp() );
235 switch (pParamTypeDescr
->eTypeClass
)
237 case typelib_TypeClass_HYPER
:
238 case typelib_TypeClass_UNSIGNED_HYPER
:
239 case typelib_TypeClass_DOUBLE
:
240 pCppStack
+= sizeof(sal_Int32
); // extra long
245 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
247 else // ptr to complex value | ref
249 if (! rParam
.bIn
) // is pure out
251 // cpp out is constructed mem, uno out is not!
253 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
255 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
256 // will be released at reconversion
257 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
260 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
263 uno_copyAndConvertData(
264 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
265 pUnoArgs
[nPos
], pParamTypeDescr
,
266 pThis
->getBridge()->getUno2Cpp() );
268 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
269 // will be released at reconversion
270 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
274 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
276 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
279 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
284 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
286 pAdjustedThisPtr
, aVtableSlot
.index
,
287 pCppReturn
, pReturnTypeDescr
, bSimpleReturn
,
288 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
289 // NO exception occured...
292 // reconvert temporary params
293 for ( ; nTempIndizes
--; )
295 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
296 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
298 if (pParams
[nIndex
].bIn
)
300 if (pParams
[nIndex
].bOut
) // inout
302 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
303 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
304 pThis
->getBridge()->getCpp2Uno() );
309 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
310 pThis
->getBridge()->getCpp2Uno() );
312 // destroy temp cpp param => cpp: every param was constructed
313 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
315 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
318 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
320 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
321 pThis
->getBridge()->getCpp2Uno() );
322 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
327 #if OSL_DEBUG_LEVEL > 1
328 fprintf( stderr
, "caught C++ exception\n" );
330 // fill uno exception
331 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
334 for ( ; nTempIndizes
--; )
336 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
337 // destroy temp cpp param => cpp: every param was constructed
338 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
339 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
342 if (pReturnTypeDescr
)
343 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
349 namespace CPPU_CURRENT_NAMESPACE
{
350 bool isSimpleReturnType(typelib_TypeDescription
* pTD
, bool recursive
)
352 if (bridges::cpp_uno::shared::isSimpleType( pTD
))
354 // Only structs of exactly 1, 2, 4, or 8 bytes are returned through
355 // registers, see <http://developer.apple.com/documentation/DeveloperTools/
356 // Conceptual/LowLevelABI/Articles/IA32.html>:
357 if (pTD
->eTypeClass
== typelib_TypeClass_STRUCT
&&
358 (recursive
|| pTD
->nSize
<= 2 || pTD
->nSize
== 4 || pTD
->nSize
== 8))
360 typelib_CompoundTypeDescription
*const pCompTD
=
361 (typelib_CompoundTypeDescription
*) pTD
;
362 for ( sal_Int32 pos
= pCompTD
->nMembers
; pos
--; ) {
363 typelib_TypeDescription
* pMemberTD
= 0;
364 TYPELIB_DANGER_GET( &pMemberTD
, pCompTD
->ppTypeRefs
[pos
] );
365 bool const b
= isSimpleReturnType(pMemberTD
, true);
366 TYPELIB_DANGER_RELEASE( pMemberTD
);
376 //==================================================================================================
378 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
379 void unoInterfaceProxyDispatch(
380 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
381 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
384 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
385 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
387 switch (pMemberDescr
->eTypeClass
)
389 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
391 VtableSlot
aVtableSlot(
394 typelib_InterfaceAttributeTypeDescription
const * >(
398 // dependent dispatch
401 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
403 pReturn
, pArgs
, ppException
);
408 typelib_MethodParameter aParam
;
410 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
411 aParam
.bIn
= sal_True
;
412 aParam
.bOut
= sal_False
;
414 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
415 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
416 typelib_typedescriptionreference_new(
417 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
419 // dependent dispatch
420 aVtableSlot
.index
+= 1; // get, then set method
425 pReturn
, pArgs
, ppException
);
427 typelib_typedescriptionreference_release( pReturnTypeRef
);
432 case typelib_TypeClass_INTERFACE_METHOD
:
434 VtableSlot
aVtableSlot(
437 typelib_InterfaceMethodTypeDescription
const * >(
439 switch (aVtableSlot
.index
)
442 case 1: // acquire uno interface
443 (*pUnoI
->acquire
)( pUnoI
);
446 case 2: // release uno interface
447 (*pUnoI
->release
)( pUnoI
);
450 case 0: // queryInterface() opt
452 typelib_TypeDescription
* pTD
= 0;
453 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
456 uno_Interface
* pInterface
= 0;
457 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
458 pThis
->pBridge
->getUnoEnv(),
459 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
464 reinterpret_cast< uno_Any
* >( pReturn
),
465 &pInterface
, pTD
, 0 );
466 (*pInterface
->release
)( pInterface
);
467 TYPELIB_DANGER_RELEASE( pTD
);
471 TYPELIB_DANGER_RELEASE( pTD
);
473 } // else perform queryInterface()
475 // dependent dispatch
478 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
479 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
480 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
481 pReturn
, pArgs
, ppException
);
487 ::com::sun::star::uno::RuntimeException
aExc(
488 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
489 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
491 Type
const & rExcType
= ::getCppuType( &aExc
);
492 // binary identical null reference
493 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );