1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <com/sun/star/uno/genfunc.hxx>
31 #include "com/sun/star/uno/RuntimeException.hpp"
32 #include "com/sun/star/uno/Sequence.hxx"
34 #include <typelib/typedescription.hxx>
36 #include "bridges/cpp_uno/shared/bridge.hxx"
37 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
38 #include "bridges/cpp_uno/shared/types.hxx"
39 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
43 #include "boost/static_assert.hpp"
46 using namespace ::com::sun::star::uno
;
51 //==================================================================================================
53 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
54 const typelib_TypeDescription
* pMemberTypeDescr
,
55 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
56 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
60 // pCallStack: ret, [return ptr], this, params
61 char * pCppStack
= (char *)(pCallStack
+1);
64 typelib_TypeDescription
* pReturnTypeDescr
= 0;
66 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
68 void * pUnoReturn
= 0;
69 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
73 // xxx todo: test PolyStructy<STRUCT<long>> foo()
74 if (CPPU_CURRENT_NAMESPACE::isSimpleReturnType( pReturnTypeDescr
))
76 pUnoReturn
= pReturnValue
; // direct way for simple types
78 else // complex return via ptr (pCppReturn)
80 pCppReturn
= *(void **)pCppStack
;
81 pCppStack
+= sizeof(void *);
82 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
84 ? alloca( pReturnTypeDescr
->nSize
)
85 : pCppReturn
); // direct way
89 pCppStack
+= sizeof( void* );
92 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
94 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
95 void ** pCppArgs
= pUnoArgs
+ nParams
;
96 // indizes of values this have to be converted (interface conversion cpp<=>uno)
97 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
98 // type descriptions for reconversions
99 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
101 sal_Int32 nTempIndizes
= 0;
103 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
105 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
106 typelib_TypeDescription
* pParamTypeDescr
= 0;
107 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
110 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
113 pCppArgs
[nPos
] = pCppStack
;
114 pUnoArgs
[nPos
] = pCppStack
;
115 switch (pParamTypeDescr
->eTypeClass
)
117 case typelib_TypeClass_HYPER
:
118 case typelib_TypeClass_UNSIGNED_HYPER
:
119 case typelib_TypeClass_DOUBLE
:
120 pCppStack
+= sizeof(sal_Int32
); // extra long
125 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
127 else // ptr to complex value | ref
129 pCppArgs
[nPos
] = *(void **)pCppStack
;
131 if (! rParam
.bIn
) // is pure out
133 // uno out is unconstructed mem!
134 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
135 pTempIndizes
[nTempIndizes
] = nPos
;
136 // will be released at reconversion
137 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
140 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
143 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
144 *(void **)pCppStack
, pParamTypeDescr
,
145 pThis
->getBridge()->getCpp2Uno() );
146 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
147 // will be released at reconversion
148 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
152 pUnoArgs
[nPos
] = *(void **)pCppStack
;
154 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
157 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
161 uno_Any aUnoExc
; // Any will be constructed by callee
162 uno_Any
* pUnoExc
= &aUnoExc
;
164 // invoke uno dispatch call
165 (*pThis
->getUnoI()->pDispatcher
)(
166 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
168 // in case an exception occurred...
171 // destruct temporary in/inout params
172 for ( ; nTempIndizes
--; )
174 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
176 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
177 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
178 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
180 if (pReturnTypeDescr
)
181 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
183 CPPU_CURRENT_NAMESPACE::raiseException(
184 &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
185 // has to destruct the any
187 else // else no exception occurred...
190 for ( ; nTempIndizes
--; )
192 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
193 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
195 if (pParams
[nIndex
].bOut
) // inout/out
197 // convert and assign
198 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
199 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
200 pThis
->getBridge()->getUno2Cpp() );
202 // destroy temp uno param
203 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
205 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
208 if (pCppReturn
) // has complex return
210 if (pUnoReturn
!= pCppReturn
) // needs reconversion
212 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
213 pThis
->getBridge()->getUno2Cpp() );
214 // destroy temp uno return
215 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
217 if (pReturnValue
!= pCppReturn
) {
218 // complex return ptr is set to eax if return value
219 // is not transferred via eax[/edx]:
220 *static_cast< void ** >(pReturnValue
) = pCppReturn
;
223 if (pReturnTypeDescr
)
225 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
231 //==================================================================================================
232 extern "C" void cpp_vtable_call(
233 int nFunctionIndex
, int nVtableOffset
, void** pCallStack
,
234 void * pReturnValue
)
236 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
238 // pCallStack: ret adr, [ret *], this, params
240 if( nFunctionIndex
& 0x80000000 )
242 nFunctionIndex
&= 0x7fffffff;
243 pThis
= pCallStack
[2];
247 pThis
= pCallStack
[1];
249 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
250 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
251 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
253 #if OSL_DEBUG_LEVEL > 1
254 fprintf( stderr
, "%p %p %p pThis=%p, pCppI=%p, function index=%d, vtable offset=%d\n", pCallStack
[0], pCallStack
[1], pCallStack
[2], pThis
, pCppI
, nFunctionIndex
, nVtableOffset
);
256 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
257 #if OSL_DEBUG_LEVEL > 1
258 fprintf( stderr
, "name=%s\n", rtl::OUStringToOString(pTypeDescr
->aBase
.pTypeName
, RTL_TEXTENCODING_UTF8
).getStr() );
260 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
261 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
263 throw RuntimeException(
264 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )),
265 (XInterface
*)pThis
);
268 // determine called method
269 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
270 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
272 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
273 #if OSL_DEBUG_LEVEL > 1
274 fprintf(stderr
, "calling %s\n", rtl::OUStringToOString(aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_UTF8
).getStr());
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( RTL_CONSTASCII_USTRINGPARAM( "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
* pReturnTypeRef
)
376 PrivateSnippetExecutor exec
;
377 if (pReturnTypeRef
== 0) {
378 exec
= privateSnippetExecutorVoid
;
381 switch (pReturnTypeRef
->eTypeClass
) {
382 case typelib_TypeClass_VOID
:
383 exec
= privateSnippetExecutorVoid
;
385 case typelib_TypeClass_HYPER
:
386 case typelib_TypeClass_UNSIGNED_HYPER
:
387 exec
= privateSnippetExecutorHyper
;
389 case typelib_TypeClass_FLOAT
:
390 exec
= privateSnippetExecutorFloat
;
392 case typelib_TypeClass_DOUBLE
:
393 exec
= privateSnippetExecutorDouble
;
395 case typelib_TypeClass_STRUCT
:
396 case typelib_TypeClass_EXCEPTION
: {
397 typelib_TypeDescription
* pReturnTypeDescr
= 0;
398 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
399 bool const bSimpleReturnStruct
=
400 CPPU_CURRENT_NAMESPACE::isSimpleReturnType(pReturnTypeDescr
);
401 sal_Int32
const nRetSize
= pReturnTypeDescr
->nSize
;
402 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
403 if (bSimpleReturnStruct
&& nRetSize
<= 8) {
404 exec
= privateSnippetExecutorGeneral
; // fills eax
406 exec
= privateSnippetExecutorHyper
; // fills eax/edx
410 case typelib_TypeClass_STRING
:
411 case typelib_TypeClass_TYPE
:
412 case typelib_TypeClass_SEQUENCE
:
413 case typelib_TypeClass_INTERFACE
:
414 case typelib_TypeClass_ANY
:
415 functionIndex
|= 0x80000000;
416 exec
= privateSnippetExecutorClass
;
419 exec
= privateSnippetExecutorGeneral
;
423 unsigned char * p
= code
;
424 OSL_ASSERT(sizeof (sal_Int32
) == 4);
425 // mov function_index, %eax:
427 *reinterpret_cast< sal_Int32
* >(p
) = functionIndex
;
428 p
+= sizeof (sal_Int32
);
429 // mov vtable_offset, %edx:
431 *reinterpret_cast< sal_Int32
* >(p
) = vtableOffset
;
432 p
+= sizeof (sal_Int32
);
433 // jmp privateSnippetExecutor:
435 *reinterpret_cast< sal_Int32
* >(p
)
436 = ((unsigned char *) exec
) - p
- sizeof (sal_Int32
);
437 p
+= sizeof (sal_Int32
);
438 OSL_ASSERT(p
- code
<= codeSnippetSize
);
439 #if OSL_DEBUG_LEVEL > 1
441 "==> codeSnippet to %s, functionIndex=%d%s, vtableOffset=%d\n",
442 (exec
== privateSnippetExecutorGeneral
? "General" :
443 (exec
== privateSnippetExecutorVoid
? "Void" :
444 (exec
== privateSnippetExecutorHyper
? "Hyper" :
445 (exec
== privateSnippetExecutorFloat
? "Float" :
446 (exec
== privateSnippetExecutorDouble
? "Double" :
447 (exec
== privateSnippetExecutorClass
? "Class" :
449 (functionIndex
& ~0x80000000), (functionIndex
& 0x80000000) ? "|0x80000000":"", vtableOffset
);
451 return code
+ codeSnippetSize
;
456 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
458 bridges::cpp_uno::shared::VtableFactory::Slot
*
459 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
461 return static_cast< Slot
* >(block
) + 2;
464 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
467 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
470 bridges::cpp_uno::shared::VtableFactory::Slot
*
471 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
472 void * block
, sal_Int32 slotCount
)
474 Slot
* slots
= mapBlockToVtable(block
);
477 return slots
+ slotCount
;
480 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
481 Slot
** slots
, unsigned char * code
,
482 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
483 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
485 (*slots
) -= functionCount
;
487 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
488 typelib_TypeDescription
* member
= 0;
489 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
490 OSL_ASSERT(member
!= 0);
491 switch (member
->eTypeClass
) {
492 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
496 code
, functionOffset
++, vtableOffset
,
497 reinterpret_cast< typelib_InterfaceAttributeTypeDescription
* >(
498 member
)->pAttributeTypeRef
);
500 if (!reinterpret_cast<
501 typelib_InterfaceAttributeTypeDescription
* >(
506 code
, functionOffset
++, vtableOffset
,
507 0 /* indicates VOID */);
511 case typelib_TypeClass_INTERFACE_METHOD
:
514 code
, functionOffset
++, vtableOffset
,
515 reinterpret_cast< typelib_InterfaceMethodTypeDescription
* >(
516 member
)->pReturnTypeRef
);
523 TYPELIB_DANGER_RELEASE(member
);
528 void bridges::cpp_uno::shared::VtableFactory::flushCode(
529 unsigned char const *, unsigned char const *)
532 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */