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 ************************************************************************/
32 #include <sal/alloca.h>
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
38 #include "bridges/cpp_uno/shared/bridge.hxx"
39 #include "bridges/cpp_uno/shared/types.hxx"
40 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/vtables.hxx"
45 using namespace ::rtl
;
46 using namespace ::com::sun::star::uno
;
51 //==================================================================================================
52 // The call instruction within the asm section of callVirtualMethod may throw
53 // exceptions. So that the compiler handles this correctly, it is important
54 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
55 // never happens at runtime), which in turn can throw exceptions, and (b)
56 // callVirtualMethod is not inlined at its call site (so that any exceptions are
57 // caught which are thrown from the instruction calling callVirtualMethod):
58 static void callVirtualMethod(
59 void * pAdjustedThisPtr
,
60 sal_Int32 nVtableIndex
,
61 void * pRegisterReturn
,
62 typelib_TypeClass eReturnType
,
63 sal_Int32
* pStackLongs
,
64 sal_Int32 nStackLongs
);
65 // __attribute__((noinline));
67 //==================================================================================================
68 static 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 /* figure out the address of the function we need to invoke */
87 unsigned long * mfunc
; // actual function to be invoked
88 int off
; // offset used to find function
91 off
= off
* 4; // 4 bytes per slot
92 mfunc
= *((unsigned long **)pAdjustedThisPtr
); // get the address of the vtable
93 mfunc
= (unsigned long *)((char *)mfunc
+ off
); // get the address from the vtable entry at offset
94 mfunc
= *((unsigned long **)mfunc
); // the function is stored at the address
95 ptr
= (void (*)())mfunc
;
97 volatile long edx
= 0, eax
= 0; // for register returns
103 "mov %%eax, %%edx\n\t"
113 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
114 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
120 // save return registers
126 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
127 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
129 switch( eReturnType
)
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
) );
157 //==================================================================================================
158 static void cpp_call(
159 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
160 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
161 typelib_TypeDescriptionReference
* pReturnTypeRef
,
162 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
163 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
165 // max space for: [complex ret ptr], values|ptr ...
167 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
168 char * pCppStackStart
= pCppStack
;
171 typelib_TypeDescription
* pReturnTypeDescr
= 0;
172 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
173 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
175 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
177 if (pReturnTypeDescr
)
179 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
181 pCppReturn
= pUnoReturn
; // direct way for simple types
185 // complex return via ptr
186 pCppReturn
= *(void **)pCppStack
187 = (bridges::cpp_uno::shared::relatesToInterfaceType(
189 ? alloca( pReturnTypeDescr
->nSize
)
190 : pUnoReturn
); // direct way
191 pCppStack
+= sizeof(void *);
195 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
196 + aVtableSlot
.offset
;
197 *(void**)pCppStack
= pAdjustedThisPtr
;
198 pCppStack
+= sizeof( void* );
201 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
203 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
204 // indizes of values this have to be converted (interface conversion cpp<=>uno)
205 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
206 // type descriptions for reconversions
207 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
209 sal_Int32 nTempIndizes
= 0;
211 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
213 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
214 typelib_TypeDescription
* pParamTypeDescr
= 0;
215 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
218 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
220 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
221 pThis
->getBridge()->getUno2Cpp() );
223 switch (pParamTypeDescr
->eTypeClass
)
225 case typelib_TypeClass_HYPER
:
226 case typelib_TypeClass_UNSIGNED_HYPER
:
227 case typelib_TypeClass_DOUBLE
:
228 pCppStack
+= sizeof(sal_Int32
); // extra long
231 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
233 else // ptr to complex value | ref
235 if (! rParam
.bIn
) // is pure out
237 // cpp out is constructed mem, uno out is not!
239 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
241 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
242 // will be released at reconversion
243 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
246 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
249 uno_copyAndConvertData(
250 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
251 pUnoArgs
[nPos
], pParamTypeDescr
,
252 pThis
->getBridge()->getUno2Cpp() );
254 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
255 // will be released at reconversion
256 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
260 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
262 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
265 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
270 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
272 pAdjustedThisPtr
, aVtableSlot
.index
,
273 pCppReturn
, pReturnTypeDescr
->eTypeClass
,
274 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
275 // NO exception occured...
278 // reconvert temporary params
279 for ( ; nTempIndizes
--; )
281 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
282 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
284 if (pParams
[nIndex
].bIn
)
286 if (pParams
[nIndex
].bOut
) // inout
288 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
289 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
290 pThis
->getBridge()->getCpp2Uno() );
295 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
296 pThis
->getBridge()->getCpp2Uno() );
298 // destroy temp cpp param => cpp: every param was constructed
299 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
301 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
304 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
306 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
307 pThis
->getBridge()->getCpp2Uno() );
308 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
313 // fill uno exception
314 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
317 for ( ; nTempIndizes
--; )
319 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
320 // destroy temp cpp param => cpp: every param was constructed
321 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
322 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
325 if (pReturnTypeDescr
)
326 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
332 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
334 void unoInterfaceProxyDispatch(
335 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
336 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
339 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
340 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
341 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
343 switch (pMemberDescr
->eTypeClass
)
345 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
347 VtableSlot
aVtableSlot(
350 typelib_InterfaceAttributeTypeDescription
const * >(
354 // dependent dispatch
357 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
359 pReturn
, pArgs
, ppException
);
364 typelib_MethodParameter aParam
;
366 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
367 aParam
.bIn
= sal_True
;
368 aParam
.bOut
= sal_False
;
370 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
371 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
372 typelib_typedescriptionreference_new(
373 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
375 // dependent dispatch
376 aVtableSlot
.index
+= 1; // get, then set method
381 pReturn
, pArgs
, ppException
);
383 typelib_typedescriptionreference_release( pReturnTypeRef
);
388 case typelib_TypeClass_INTERFACE_METHOD
:
390 VtableSlot
aVtableSlot(
393 typelib_InterfaceMethodTypeDescription
const * >(
395 switch (aVtableSlot
.index
)
398 case 1: // acquire uno interface
399 (*pUnoI
->acquire
)( pUnoI
);
402 case 2: // release uno interface
403 (*pUnoI
->release
)( pUnoI
);
406 case 0: // queryInterface() opt
408 typelib_TypeDescription
* pTD
= 0;
409 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
412 uno_Interface
* pInterface
= 0;
413 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
414 pThis
->pBridge
->getUnoEnv(),
415 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
420 reinterpret_cast< uno_Any
* >( pReturn
),
421 &pInterface
, pTD
, 0 );
422 (*pInterface
->release
)( pInterface
);
423 TYPELIB_DANGER_RELEASE( pTD
);
427 TYPELIB_DANGER_RELEASE( pTD
);
429 } // else perform queryInterface()
431 // dependent dispatch
434 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
435 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
436 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
437 pReturn
, pArgs
, ppException
);
443 ::com::sun::star::uno::RuntimeException
aExc(
444 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
445 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
447 Type
const & rExcType
= ::getCppuType( &aExc
);
448 // binary identical null reference
449 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );