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: cpp2uno.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 <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include <typelib/typedescription.hxx>
38 #include <sal/alloca.h>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
46 #include "smallstruct.hxx"
48 using namespace ::com::sun::star::uno
;
53 //==================================================================================================
55 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
56 const typelib_TypeDescription
* pMemberTypeDescr
,
57 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
58 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
62 // pCallStack: ret, [return ptr], this, params
63 char * pCppStack
= (char *)(pCallStack
+1);
66 typelib_TypeDescription
* pReturnTypeDescr
= 0;
68 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
70 void * pUnoReturn
= 0;
71 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
75 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
77 pUnoReturn
= pReturnValue
; // direct way for simple types
79 else // complex return via ptr (pCppReturn)
81 if (!bridges::cpp_uno::shared::isSmallStruct(pReturnTypeDescr
)) {
82 pCppReturn
= *(void **)pCppStack
;
83 pCppStack
+= sizeof(void *);
86 pCppReturn
= pReturnValue
;
89 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
91 ? alloca( pReturnTypeDescr
->nSize
)
92 : pCppReturn
); // direct way
96 pCppStack
+= sizeof( void* );
99 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
101 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
102 void ** pCppArgs
= pUnoArgs
+ nParams
;
103 // indizes of values this have to be converted (interface conversion cpp<=>uno)
104 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
105 // type descriptions for reconversions
106 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
108 sal_Int32 nTempIndizes
= 0;
110 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
112 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
113 typelib_TypeDescription
* pParamTypeDescr
= 0;
114 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
117 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
120 pCppArgs
[nPos
] = pCppStack
;
121 pUnoArgs
[nPos
] = pCppStack
;
122 switch (pParamTypeDescr
->eTypeClass
)
124 case typelib_TypeClass_HYPER
:
125 case typelib_TypeClass_UNSIGNED_HYPER
:
126 case typelib_TypeClass_DOUBLE
:
127 pCppStack
+= sizeof(sal_Int32
); // extra long
133 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
135 else // ptr to complex value | ref
137 pCppArgs
[nPos
] = *(void **)pCppStack
;
139 if (! rParam
.bIn
) // is pure out
141 // uno out is unconstructed mem!
142 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
143 pTempIndizes
[nTempIndizes
] = nPos
;
144 // will be released at reconversion
145 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
148 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
151 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
152 *(void **)pCppStack
, pParamTypeDescr
,
153 pThis
->getBridge()->getCpp2Uno() );
154 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
155 // will be released at reconversion
156 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
160 pUnoArgs
[nPos
] = *(void **)pCppStack
;
162 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
165 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
169 uno_Any aUnoExc
; // Any will be constructed by callee
170 uno_Any
* pUnoExc
= &aUnoExc
;
172 // invoke uno dispatch call
173 (*pThis
->getUnoI()->pDispatcher
)(
174 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
176 // in case an exception occured...
179 // destruct temporary in/inout params
180 for ( ; nTempIndizes
--; )
182 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
184 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
185 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
186 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
188 if (pReturnTypeDescr
)
189 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
191 CPPU_CURRENT_NAMESPACE::raiseException(
192 &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
193 // has to destruct the any
195 else // else no exception occured...
198 for ( ; nTempIndizes
--; )
200 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
201 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
203 if (pParams
[nIndex
].bOut
) // inout/out
205 // convert and assign
206 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
207 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
208 pThis
->getBridge()->getUno2Cpp() );
210 // destroy temp uno param
211 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
213 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
216 if (pCppReturn
) // has complex return
218 if (pUnoReturn
!= pCppReturn
) // needs reconversion
220 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
221 pThis
->getBridge()->getUno2Cpp() );
222 // destroy temp uno return
223 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
225 if (pReturnValue
!= pCppReturn
)
226 // complex return ptr is set to eax
227 *static_cast< void ** >(pReturnValue
) = pCppReturn
;
229 if (pReturnTypeDescr
)
231 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
237 //==================================================================================================
238 extern "C" void cpp_vtable_call(
239 int nFunctionIndex
, int nVtableOffset
, void** pCallStack
,
240 void * pReturnValue
)
242 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
244 // pCallStack: ret adr, [ret *], this, params
246 if( nFunctionIndex
& 0x80000000 )
248 nFunctionIndex
&= 0x7fffffff;
249 pThis
= pCallStack
[2];
253 pThis
= pCallStack
[1];
255 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
256 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
257 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
260 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
262 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
263 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
265 throw RuntimeException(
266 rtl::OUString::createFromAscii("illegal vtable index!"),
267 (XInterface
*)pThis
);
270 // determine called method
271 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
272 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
274 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
276 switch (aMemberDescr
.get()->eTypeClass
)
278 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
280 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
284 pCppI
, aMemberDescr
.get(),
285 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
287 pCallStack
, pReturnValue
);
292 typelib_MethodParameter aParam
;
294 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
295 aParam
.bIn
= sal_True
;
296 aParam
.bOut
= sal_False
;
299 pCppI
, aMemberDescr
.get(),
300 0, // indicates void return
302 pCallStack
, pReturnValue
);
306 case typelib_TypeClass_INTERFACE_METHOD
:
309 switch (nFunctionIndex
)
312 pCppI
->acquireProxy(); // non virtual call!
315 pCppI
->releaseProxy(); // non virtual call!
317 case 0: // queryInterface() opt
319 typelib_TypeDescription
* pTD
= 0;
320 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[3] )->getTypeLibType() );
323 XInterface
* pInterface
= 0;
324 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
325 pCppI
->getBridge()->getCppEnv(),
326 (void **)&pInterface
, pCppI
->getOid().pData
,
327 (typelib_InterfaceTypeDescription
*)pTD
);
332 reinterpret_cast< uno_Any
* >( pCallStack
[1] ),
333 &pInterface
, pTD
, cpp_acquire
);
334 pInterface
->release();
335 TYPELIB_DANGER_RELEASE( pTD
);
336 *static_cast< void ** >(pReturnValue
) = pCallStack
[1];
339 TYPELIB_DANGER_RELEASE( pTD
);
341 } // else perform queryInterface()
344 pCppI
, aMemberDescr
.get(),
345 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
346 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
347 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
348 pCallStack
, pReturnValue
);
354 throw RuntimeException(
355 rtl::OUString::createFromAscii("no member description found!"),
356 (XInterface
*)pThis
);
361 //==================================================================================================
362 extern "C" void privateSnippetExecutorGeneral();
363 extern "C" void privateSnippetExecutorVoid();
364 extern "C" void privateSnippetExecutorHyper();
365 extern "C" void privateSnippetExecutorFloat();
366 extern "C" void privateSnippetExecutorDouble();
367 extern "C" void privateSnippetExecutorClass();
368 extern "C" typedef void (*PrivateSnippetExecutor
)();
370 int const codeSnippetSize
= 16;
372 unsigned char * codeSnippet(
373 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
374 typelib_TypeDescriptionReference
* returnType
)
376 typelib_TypeDescription
* returnTypeDescr
= 0;
378 TYPELIB_DANGER_GET( &returnTypeDescr
, returnType
);
380 typelib_TypeClass returnTypeClass
= returnType
? returnType
->eTypeClass
: typelib_TypeClass_VOID
;
381 if (!bridges::cpp_uno::shared::isSimpleType(returnTypeClass
) &&
382 !bridges::cpp_uno::shared::isSmallStruct(returnTypeDescr
)) {
383 functionIndex
|= 0x80000000;
385 PrivateSnippetExecutor exec
;
386 switch (returnTypeClass
) {
387 case typelib_TypeClass_VOID
:
388 exec
= privateSnippetExecutorVoid
;
390 case typelib_TypeClass_HYPER
:
391 case typelib_TypeClass_UNSIGNED_HYPER
:
392 exec
= privateSnippetExecutorHyper
;
394 case typelib_TypeClass_FLOAT
:
395 exec
= privateSnippetExecutorFloat
;
397 case typelib_TypeClass_DOUBLE
:
398 exec
= privateSnippetExecutorDouble
;
400 case typelib_TypeClass_STRUCT
:
401 if (bridges::cpp_uno::shared::isSmallStruct(returnTypeDescr
)) {
402 if (returnType
->pType
->nSize
<= 4) {
403 exec
= privateSnippetExecutorGeneral
;
405 else if (returnType
->pType
->nSize
<= 8) {
406 exec
= privateSnippetExecutorHyper
;
410 exec
= privateSnippetExecutorClass
;
413 case typelib_TypeClass_STRING
:
414 case typelib_TypeClass_TYPE
:
415 case typelib_TypeClass_ANY
:
416 case typelib_TypeClass_SEQUENCE
:
417 case typelib_TypeClass_INTERFACE
:
418 exec
= privateSnippetExecutorClass
;
421 exec
= privateSnippetExecutorGeneral
;
425 TYPELIB_DANGER_RELEASE( returnTypeDescr
);
426 unsigned char * p
= code
;
427 OSL_ASSERT(sizeof (sal_Int32
) == 4);
428 // mov function_index, %eax:
430 *reinterpret_cast< sal_Int32
* >(p
) = functionIndex
;
431 p
+= sizeof (sal_Int32
);
432 // mov vtable_offset, %edx:
434 *reinterpret_cast< sal_Int32
* >(p
) = vtableOffset
;
435 p
+= sizeof (sal_Int32
);
436 // jmp privateSnippetExecutor:
438 *reinterpret_cast< sal_Int32
* >(p
)
439 = ((unsigned char *) exec
) - p
- sizeof (sal_Int32
);
440 p
+= sizeof (sal_Int32
);
441 OSL_ASSERT(p
- code
<= codeSnippetSize
);
442 return code
+ codeSnippetSize
;
447 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
449 bridges::cpp_uno::shared::VtableFactory::Slot
*
450 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
452 return static_cast< Slot
* >(block
) + 2;
455 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
458 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
461 bridges::cpp_uno::shared::VtableFactory::Slot
*
462 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
463 void * block
, sal_Int32 slotCount
)
465 Slot
* slots
= mapBlockToVtable(block
);
468 return slots
+ slotCount
;
471 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
472 Slot
** slots
, unsigned char * code
,
473 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
474 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
476 (*slots
) -= functionCount
;
478 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
479 typelib_TypeDescription
* member
= 0;
480 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
481 OSL_ASSERT(member
!= 0);
482 switch (member
->eTypeClass
) {
483 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
487 code
, functionOffset
++, vtableOffset
,
488 reinterpret_cast< typelib_InterfaceAttributeTypeDescription
* >(
489 member
)->pAttributeTypeRef
);
491 if (!reinterpret_cast<
492 typelib_InterfaceAttributeTypeDescription
* >(
497 code
, functionOffset
++, vtableOffset
,
502 case typelib_TypeClass_INTERFACE_METHOD
:
505 code
, functionOffset
++, vtableOffset
,
506 reinterpret_cast< typelib_InterfaceMethodTypeDescription
* >(
507 member
)->pReturnTypeRef
);
514 TYPELIB_DANGER_RELEASE(member
);
519 void bridges::cpp_uno::shared::VtableFactory::flushCode(
520 unsigned char const *, unsigned char const *)