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>
37 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
43 #include "bridges/cpp_uno/shared/vtables.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_TypeClass eReturnType
,
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_TypeClass eReturnType
,
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( eReturnType
)
120 case typelib_TypeClass_HYPER
:
121 case typelib_TypeClass_UNSIGNED_HYPER
:
122 ((long*)pRegisterReturn
)[1] = edx
;
123 case typelib_TypeClass_LONG
:
124 case typelib_TypeClass_UNSIGNED_LONG
:
125 case typelib_TypeClass_CHAR
:
126 case typelib_TypeClass_ENUM
:
127 ((long*)pRegisterReturn
)[0] = eax
;
129 case typelib_TypeClass_SHORT
:
130 case typelib_TypeClass_UNSIGNED_SHORT
:
131 *(unsigned short*)pRegisterReturn
= eax
;
133 case typelib_TypeClass_BOOLEAN
:
134 case typelib_TypeClass_BYTE
:
135 *(unsigned char*)pRegisterReturn
= eax
;
137 case typelib_TypeClass_FLOAT
:
138 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
140 case typelib_TypeClass_DOUBLE
:
141 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
148 //==================================================================================================
149 static void cpp_call(
150 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
151 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
152 typelib_TypeDescriptionReference
* pReturnTypeRef
,
153 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
154 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
156 // max space for: [complex ret ptr], values|ptr ...
158 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
159 char * pCppStackStart
= pCppStack
;
162 typelib_TypeDescription
* pReturnTypeDescr
= 0;
163 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
164 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
166 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
168 if (pReturnTypeDescr
)
170 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
172 pCppReturn
= pUnoReturn
; // direct way for simple types
176 // complex return via ptr
177 pCppReturn
= *(void **)pCppStack
178 = (bridges::cpp_uno::shared::relatesToInterfaceType(
180 ? alloca( pReturnTypeDescr
->nSize
)
181 : pUnoReturn
); // direct way
182 pCppStack
+= sizeof(void *);
186 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
187 + aVtableSlot
.offset
;
188 *(void**)pCppStack
= pAdjustedThisPtr
;
189 pCppStack
+= sizeof( void* );
192 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
194 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
195 // indizes of values this have to be converted (interface conversion cpp<=>uno)
196 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
197 // type descriptions for reconversions
198 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
200 sal_Int32 nTempIndizes
= 0;
202 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
204 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
205 typelib_TypeDescription
* pParamTypeDescr
= 0;
206 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
209 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
211 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
212 pThis
->getBridge()->getUno2Cpp() );
214 switch (pParamTypeDescr
->eTypeClass
)
216 case typelib_TypeClass_HYPER
:
217 case typelib_TypeClass_UNSIGNED_HYPER
:
218 case typelib_TypeClass_DOUBLE
:
219 pCppStack
+= sizeof(sal_Int32
); // extra long
225 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
227 else // ptr to complex value | ref
229 if (! rParam
.bIn
) // is pure out
231 // cpp out is constructed mem, uno out is not!
233 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
235 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
236 // will be released at reconversion
237 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
240 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
243 uno_copyAndConvertData(
244 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
245 pUnoArgs
[nPos
], pParamTypeDescr
,
246 pThis
->getBridge()->getUno2Cpp() );
248 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
249 // will be released at reconversion
250 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
254 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
256 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
259 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
264 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
266 pAdjustedThisPtr
, aVtableSlot
.index
,
267 pCppReturn
, pReturnTypeDescr
->eTypeClass
,
268 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
269 // NO exception occured...
272 // reconvert temporary params
273 for ( ; nTempIndizes
--; )
275 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
276 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
278 if (pParams
[nIndex
].bIn
)
280 if (pParams
[nIndex
].bOut
) // inout
282 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
283 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
284 pThis
->getBridge()->getCpp2Uno() );
289 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
290 pThis
->getBridge()->getCpp2Uno() );
292 // destroy temp cpp param => cpp: every param was constructed
293 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
295 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
298 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
300 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
301 pThis
->getBridge()->getCpp2Uno() );
302 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
307 // fill uno exception
308 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
311 for ( ; nTempIndizes
--; )
313 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
314 // destroy temp cpp param => cpp: every param was constructed
315 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
316 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
319 if (pReturnTypeDescr
)
320 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
326 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
328 void unoInterfaceProxyDispatch(
329 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
330 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
333 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
334 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
336 switch (pMemberDescr
->eTypeClass
)
338 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
340 VtableSlot
aVtableSlot(
343 typelib_InterfaceAttributeTypeDescription
const * >(
347 // dependent dispatch
350 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
352 pReturn
, pArgs
, ppException
);
357 typelib_MethodParameter aParam
;
359 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
360 aParam
.bIn
= sal_True
;
361 aParam
.bOut
= sal_False
;
363 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
364 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
365 typelib_typedescriptionreference_new(
366 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
368 // dependent dispatch
369 aVtableSlot
.index
+= 1; // get, then set method
374 pReturn
, pArgs
, ppException
);
376 typelib_typedescriptionreference_release( pReturnTypeRef
);
381 case typelib_TypeClass_INTERFACE_METHOD
:
383 VtableSlot
aVtableSlot(
386 typelib_InterfaceMethodTypeDescription
const * >(
388 switch (aVtableSlot
.index
)
391 case 1: // acquire uno interface
392 (*pUnoI
->acquire
)( pUnoI
);
395 case 2: // release uno interface
396 (*pUnoI
->release
)( pUnoI
);
399 case 0: // queryInterface() opt
401 typelib_TypeDescription
* pTD
= 0;
402 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
405 uno_Interface
* pInterface
= 0;
406 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
407 pThis
->pBridge
->getUnoEnv(),
408 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
413 reinterpret_cast< uno_Any
* >( pReturn
),
414 &pInterface
, pTD
, 0 );
415 (*pInterface
->release
)( pInterface
);
416 TYPELIB_DANGER_RELEASE( pTD
);
420 TYPELIB_DANGER_RELEASE( pTD
);
422 } // else perform queryInterface()
424 // dependent dispatch
427 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
428 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
429 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
430 pReturn
, pArgs
, ppException
);
436 ::com::sun::star::uno::RuntimeException
aExc(
437 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
438 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
440 Type
const & rExcType
= ::getCppuType( &aExc
);
441 // binary identical null reference
442 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );