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 <sal/alloca.h>
35 #include <rtl/alloc.h>
38 #include <bridges/cpp_uno/bridge.hxx>
39 #include <bridges/cpp_uno/type_misc.hxx>
44 using namespace ::rtl
;
45 using namespace ::com::sun::star::uno
;
47 namespace CPPU_CURRENT_NAMESPACE
50 void dummy_can_throw_anything( char const * );
52 //==================================================================================================
53 // The call instruction within the asm section of callVirtualMethod may throw
54 // exceptions. So that the compiler handles this correctly, it is important
55 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
56 // never happens at runtime), which in turn can throw exceptions, and (b)
57 // callVirtualMethod is not inlined at its call site (so that any exceptions are
58 // caught which are thrown from the instruction calling callVirtualMethod):
59 void callVirtualMethod(
61 sal_Int32 nVtableIndex
,
62 void * pRegisterReturn
,
63 typelib_TypeClass eReturnType
,
64 sal_Int32
* pStackLongs
,
65 sal_Int32 nStackLongs
) __attribute__((noinline
));
67 void callVirtualMethod(
69 sal_Int32 nVtableIndex
,
70 void * pRegisterReturn
,
71 typelib_TypeClass eReturnType
,
72 sal_Int32
* pStackLongs
,
73 sal_Int32 nStackLongs
)
75 // parameter list is mixed list of * and values
76 // reference parameters are pointers
78 OSL_ENSURE( pStackLongs
&& pThis
, "### null ptr!" );
79 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
80 OSL_ENSURE( nStackLongs
&& pStackLongs
, "### no stack in callVirtualMethod !" );
83 if (! pThis
) dummy_can_throw_anything("xxx"); // address something
85 volatile long edx
= 0, eax
= 0; // for register returns
91 "mov %%eax, %%edx\n\t"
100 // do the actual call
102 "mov 0(%%edx), %%edx\n\t"
105 "add %%eax, %%edx\n\t"
106 "mov 0(%%edx), %%edx\n\t"
108 // save return registers
114 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pThis
), "m"(nVtableIndex
),
115 "m"(eax
), "m"(edx
), "m"(stackptr
)
117 switch( eReturnType
)
119 case typelib_TypeClass_HYPER
:
120 case typelib_TypeClass_UNSIGNED_HYPER
:
121 ((long*)pRegisterReturn
)[1] = edx
;
122 case typelib_TypeClass_LONG
:
123 case typelib_TypeClass_UNSIGNED_LONG
:
124 case typelib_TypeClass_CHAR
:
125 case typelib_TypeClass_ENUM
:
126 ((long*)pRegisterReturn
)[0] = eax
;
128 case typelib_TypeClass_SHORT
:
129 case typelib_TypeClass_UNSIGNED_SHORT
:
130 *(unsigned short*)pRegisterReturn
= eax
;
132 case typelib_TypeClass_BOOLEAN
:
133 case typelib_TypeClass_BYTE
:
134 *(unsigned char*)pRegisterReturn
= eax
;
136 case typelib_TypeClass_FLOAT
:
137 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
139 case typelib_TypeClass_DOUBLE
:
140 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
145 //==================================================================================================
146 static void cpp_call(
147 cppu_unoInterfaceProxy
* pThis
,
148 sal_Int32 nVtableCall
,
149 typelib_TypeDescriptionReference
* pReturnTypeRef
,
150 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
151 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
153 // max space for: [complex ret ptr], values|ptr ...
155 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
156 char * pCppStackStart
= pCppStack
;
159 typelib_TypeDescription
* pReturnTypeDescr
= 0;
160 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
161 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
163 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
165 if (pReturnTypeDescr
)
167 if (cppu_isSimpleType( pReturnTypeDescr
))
169 pCppReturn
= pUnoReturn
; // direct way for simple types
173 // complex return via ptr
174 pCppReturn
= *(void **)pCppStack
= (cppu_relatesToInterface( pReturnTypeDescr
)
175 ? alloca( pReturnTypeDescr
->nSize
)
176 : pUnoReturn
); // direct way
177 pCppStack
+= sizeof(void *);
181 *(void**)pCppStack
= pThis
->pCppI
;
182 pCppStack
+= sizeof( void* );
185 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
187 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
188 // indizes of values this have to be converted (interface conversion cpp<=>uno)
189 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
190 // type descriptions for reconversions
191 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
193 sal_Int32 nTempIndizes
= 0;
195 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
197 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
198 typelib_TypeDescription
* pParamTypeDescr
= 0;
199 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
201 if (!rParam
.bOut
&& cppu_isSimpleType( pParamTypeDescr
))
203 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
204 &pThis
->pBridge
->aUno2Cpp
);
206 switch (pParamTypeDescr
->eTypeClass
)
208 case typelib_TypeClass_HYPER
:
209 case typelib_TypeClass_UNSIGNED_HYPER
:
210 case typelib_TypeClass_DOUBLE
:
211 pCppStack
+= sizeof(sal_Int32
); // extra long
214 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
216 else // ptr to complex value | ref
218 if (! rParam
.bIn
) // is pure out
220 // cpp out is constructed mem, uno out is not!
222 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
224 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
225 // will be released at reconversion
226 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
229 else if (cppu_relatesToInterface( pParamTypeDescr
))
231 uno_copyAndConvertData(
232 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
233 pUnoArgs
[nPos
], pParamTypeDescr
, &pThis
->pBridge
->aUno2Cpp
);
235 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
236 // will be released at reconversion
237 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
241 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
243 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
246 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
251 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
253 pThis
->pCppI
, nVtableCall
,
254 pCppReturn
, pReturnTypeDescr
->eTypeClass
,
255 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
256 // NO exception occured...
259 // reconvert temporary params
260 for ( ; nTempIndizes
--; )
262 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
263 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
265 if (pParams
[nIndex
].bIn
)
267 if (pParams
[nIndex
].bOut
) // inout
269 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
270 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
271 &pThis
->pBridge
->aCpp2Uno
);
276 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
277 &pThis
->pBridge
->aCpp2Uno
);
279 // destroy temp cpp param => cpp: every param was constructed
280 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
282 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
285 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
287 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
288 &pThis
->pBridge
->aCpp2Uno
);
289 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
294 // fill uno exception
295 fillUnoException( __cxa_get_globals()->caughtExceptions
, *ppUnoExc
, &pThis
->pBridge
->aCpp2Uno
);
298 for ( ; nTempIndizes
--; )
300 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
301 // destroy temp cpp param => cpp: every param was constructed
302 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
303 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
306 if (pReturnTypeDescr
)
307 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
312 //==================================================================================================
313 void SAL_CALL
cppu_unoInterfaceProxy_dispatch(
314 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
315 void * pReturn
, void * pArgs
[], uno_Any
** ppException
) throw ()
318 cppu_unoInterfaceProxy
* pThis
= (cppu_unoInterfaceProxy
*)pUnoI
;
319 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
321 switch (pMemberDescr
->eTypeClass
)
323 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
325 // determine vtable call index
326 sal_Int32 nMemberPos
= ((typelib_InterfaceMemberTypeDescription
*)pMemberDescr
)->nPosition
;
327 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### member pos out of range!" );
329 sal_Int32 nVtableCall
= pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
];
330 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
334 // dependent dispatch
337 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
339 pReturn
, pArgs
, ppException
);
344 typelib_MethodParameter aParam
;
346 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
347 aParam
.bIn
= sal_True
;
348 aParam
.bOut
= sal_False
;
350 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
351 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
352 typelib_typedescriptionreference_new(
353 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
355 // dependent dispatch
357 pThis
, nVtableCall
+1, // get, then set method
360 pReturn
, pArgs
, ppException
);
362 typelib_typedescriptionreference_release( pReturnTypeRef
);
367 case typelib_TypeClass_INTERFACE_METHOD
:
369 // determine vtable call index
370 sal_Int32 nMemberPos
= ((typelib_InterfaceMemberTypeDescription
*)pMemberDescr
)->nPosition
;
371 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### member pos out of range!" );
373 sal_Int32 nVtableCall
= pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
];
374 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
379 case 1: // acquire uno interface
380 (*pUnoI
->acquire
)( pUnoI
);
383 case 2: // release uno interface
384 (*pUnoI
->release
)( pUnoI
);
387 case 0: // queryInterface() opt
389 typelib_TypeDescription
* pTD
= 0;
390 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
393 uno_Interface
* pInterface
= 0;
394 (*pThis
->pBridge
->pUnoEnv
->getRegisteredInterface
)(
395 pThis
->pBridge
->pUnoEnv
,
396 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
401 reinterpret_cast< uno_Any
* >( pReturn
),
402 &pInterface
, pTD
, 0 );
403 (*pInterface
->release
)( pInterface
);
404 TYPELIB_DANGER_RELEASE( pTD
);
408 TYPELIB_DANGER_RELEASE( pTD
);
410 } // else perform queryInterface()
412 // dependent dispatch
415 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
416 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
417 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
418 pReturn
, pArgs
, ppException
);
424 ::com::sun::star::uno::RuntimeException
aExc(
425 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
426 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
428 Type
const & rExcType
= ::getCppuType( &aExc
);
429 // binary identical null reference
430 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );