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"
35 #include <sal/alloca.h>
37 #include <com/sun/star/uno/genfunc.hxx>
38 #include "com/sun/star/uno/RuntimeException.hpp"
41 #include "bridges/cpp_uno/shared/bridge.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
44 #include "bridges/cpp_uno/shared/vtables.hxx"
48 using namespace ::rtl
;
49 using namespace ::com::sun::star::uno
;
54 //==================================================================================================
55 static void callVirtualMethod(
56 void * pAdjustedThisPtr
,
57 sal_Int32 nVtableIndex
,
58 void * pRegisterReturn
,
59 typelib_TypeClass eReturnType
,
60 sal_Int32
* pStackLongs
,
61 sal_Int32 nStackLongs
)
63 // parameter list is mixed list of * and values
64 // reference parameters are pointers
66 OSL_ENSURE( pStackLongs
&& pAdjustedThisPtr
, "### null ptr!" );
67 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
68 OSL_ENSURE( nStackLongs
&& pStackLongs
, "### no stack in callVirtualMethod !" );
71 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
73 volatile long edx
= 0, eax
= 0; // for register returns
79 "mov %%eax, %%edx\n\t"
90 "mov 0(%%edx), %%edx\n\t"
93 "add %%eax, %%edx\n\t"
94 "mov 0(%%edx), %%edx\n\t"
96 // save return registers
102 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
103 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
105 switch( eReturnType
)
107 case typelib_TypeClass_HYPER
:
108 case typelib_TypeClass_UNSIGNED_HYPER
:
109 ((long*)pRegisterReturn
)[1] = edx
;
110 case typelib_TypeClass_LONG
:
111 case typelib_TypeClass_UNSIGNED_LONG
:
112 case typelib_TypeClass_CHAR
:
113 case typelib_TypeClass_ENUM
:
114 ((long*)pRegisterReturn
)[0] = eax
;
116 case typelib_TypeClass_SHORT
:
117 case typelib_TypeClass_UNSIGNED_SHORT
:
118 *(unsigned short*)pRegisterReturn
= eax
;
120 case typelib_TypeClass_BOOLEAN
:
121 case typelib_TypeClass_BYTE
:
122 *(unsigned char*)pRegisterReturn
= eax
;
124 case typelib_TypeClass_FLOAT
:
125 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
127 case typelib_TypeClass_DOUBLE
:
128 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
133 //==================================================================================================
134 static void cpp_call(
135 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
136 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
137 typelib_TypeDescriptionReference
* pReturnTypeRef
,
138 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
139 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
141 // max space for: [complex ret ptr], values|ptr ...
143 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
144 char * pCppStackStart
= pCppStack
;
147 typelib_TypeDescription
* pReturnTypeDescr
= 0;
148 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
149 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
151 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
153 if (pReturnTypeDescr
)
155 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
157 pCppReturn
= pUnoReturn
; // direct way for simple types
161 // complex return via ptr
162 pCppReturn
= *(void **)pCppStack
163 = (bridges::cpp_uno::shared::relatesToInterfaceType(
165 ? alloca( pReturnTypeDescr
->nSize
)
166 : pUnoReturn
); // direct way
167 pCppStack
+= sizeof(void *);
171 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
172 + aVtableSlot
.offset
;
173 *(void**)pCppStack
= pAdjustedThisPtr
;
174 pCppStack
+= sizeof( void* );
177 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
179 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
180 // indizes of values this have to be converted (interface conversion cpp<=>uno)
181 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
182 // type descriptions for reconversions
183 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
185 sal_Int32 nTempIndizes
= 0;
187 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
189 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
190 typelib_TypeDescription
* pParamTypeDescr
= 0;
191 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
194 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
196 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
197 pThis
->getBridge()->getUno2Cpp() );
199 switch (pParamTypeDescr
->eTypeClass
)
201 case typelib_TypeClass_HYPER
:
202 case typelib_TypeClass_UNSIGNED_HYPER
:
203 case typelib_TypeClass_DOUBLE
:
204 pCppStack
+= sizeof(sal_Int32
); // extra long
207 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
209 else // ptr to complex value | ref
211 if (! rParam
.bIn
) // is pure out
213 // cpp out is constructed mem, uno out is not!
215 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
217 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
218 // will be released at reconversion
219 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
222 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
225 uno_copyAndConvertData(
226 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
227 pUnoArgs
[nPos
], pParamTypeDescr
,
228 pThis
->getBridge()->getUno2Cpp() );
230 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
231 // will be released at reconversion
232 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
236 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
238 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
241 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
246 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
248 pAdjustedThisPtr
, aVtableSlot
.index
,
249 pCppReturn
, pReturnTypeDescr
->eTypeClass
,
250 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
251 // NO exception occured...
254 // reconvert temporary params
255 for ( ; nTempIndizes
--; )
257 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
258 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
260 if (pParams
[nIndex
].bIn
)
262 if (pParams
[nIndex
].bOut
) // inout
264 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
265 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
266 pThis
->getBridge()->getCpp2Uno() );
271 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
272 pThis
->getBridge()->getCpp2Uno() );
274 // destroy temp cpp param => cpp: every param was constructed
275 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
277 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
280 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
282 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
283 pThis
->getBridge()->getCpp2Uno() );
284 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
289 // fill uno exception
290 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
293 for ( ; nTempIndizes
--; )
295 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
296 // destroy temp cpp param => cpp: every param was constructed
297 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
298 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
301 if (pReturnTypeDescr
)
302 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
308 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
310 void unoInterfaceProxyDispatch(
311 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
312 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
315 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
316 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
317 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
319 switch (pMemberDescr
->eTypeClass
)
321 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
323 VtableSlot
aVtableSlot(
326 typelib_InterfaceAttributeTypeDescription
const * >(
330 // dependent dispatch
333 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
335 pReturn
, pArgs
, ppException
);
340 typelib_MethodParameter aParam
;
342 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
343 aParam
.bIn
= sal_True
;
344 aParam
.bOut
= sal_False
;
346 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
347 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
348 typelib_typedescriptionreference_new(
349 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
351 // dependent dispatch
352 aVtableSlot
.index
+= 1; // get, then set method
357 pReturn
, pArgs
, ppException
);
359 typelib_typedescriptionreference_release( pReturnTypeRef
);
364 case typelib_TypeClass_INTERFACE_METHOD
:
366 VtableSlot
aVtableSlot(
369 typelib_InterfaceMethodTypeDescription
const * >(
371 switch (aVtableSlot
.index
)
374 case 1: // acquire uno interface
375 (*pUnoI
->acquire
)( pUnoI
);
378 case 2: // release uno interface
379 (*pUnoI
->release
)( pUnoI
);
382 case 0: // queryInterface() opt
384 typelib_TypeDescription
* pTD
= 0;
385 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
388 uno_Interface
* pInterface
= 0;
389 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
390 pThis
->pBridge
->getUnoEnv(),
391 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
396 reinterpret_cast< uno_Any
* >( pReturn
),
397 &pInterface
, pTD
, 0 );
398 (*pInterface
->release
)( pInterface
);
399 TYPELIB_DANGER_RELEASE( pTD
);
403 TYPELIB_DANGER_RELEASE( pTD
);
405 } // else perform queryInterface()
407 // dependent dispatch
410 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
411 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
412 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
413 pReturn
, pArgs
, ppException
);
419 ::com::sun::star::uno::RuntimeException
aExc(
420 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
421 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
423 Type
const & rExcType
= ::getCppuType( &aExc
);
424 // binary identical null reference
425 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );