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()
76 !arm::return_in_hidden_param(pReturnTypeRef
)
78 CPPU_CURRENT_NAMESPACE::isSimpleReturnType( pReturnTypeDescr
)
82 pUnoReturn
= pReturnValue
; // direct way for simple types
84 else // complex return via ptr (pCppReturn)
86 pCppReturn
= *(void **)pCppStack
;
87 pCppStack
+= sizeof(void *);
88 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
90 ? alloca( pReturnTypeDescr
->nSize
)
91 : pCppReturn
); // direct way
95 pCppStack
+= sizeof( void* );
98 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
100 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
101 void ** pCppArgs
= pUnoArgs
+ nParams
;
102 // indizes of values this have to be converted (interface conversion cpp<=>uno)
103 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
104 // type descriptions for reconversions
105 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
107 sal_Int32 nTempIndizes
= 0;
109 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
111 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
112 typelib_TypeDescription
* pParamTypeDescr
= 0;
113 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
116 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
119 pCppArgs
[nPos
] = pCppStack
;
120 pUnoArgs
[nPos
] = pCppStack
;
121 switch (pParamTypeDescr
->eTypeClass
)
123 case typelib_TypeClass_HYPER
:
124 case typelib_TypeClass_UNSIGNED_HYPER
:
125 case typelib_TypeClass_DOUBLE
:
126 pCppStack
+= sizeof(sal_Int32
); // extra long
131 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
133 else // ptr to complex value | ref
135 pCppArgs
[nPos
] = *(void **)pCppStack
;
137 if (! rParam
.bIn
) // is pure out
139 // uno out is unconstructed mem!
140 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
141 pTempIndizes
[nTempIndizes
] = nPos
;
142 // will be released at reconversion
143 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
146 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
149 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
150 *(void **)pCppStack
, pParamTypeDescr
,
151 pThis
->getBridge()->getCpp2Uno() );
152 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
153 // will be released at reconversion
154 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
158 pUnoArgs
[nPos
] = *(void **)pCppStack
;
160 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
163 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
167 uno_Any aUnoExc
; // Any will be constructed by callee
168 uno_Any
* pUnoExc
= &aUnoExc
;
170 // invoke uno dispatch call
171 (*pThis
->getUnoI()->pDispatcher
)(
172 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
174 // in case an exception occurred...
177 // destruct temporary in/inout params
178 for ( ; nTempIndizes
--; )
180 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
182 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
183 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
184 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
186 if (pReturnTypeDescr
)
187 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
189 CPPU_CURRENT_NAMESPACE::raiseException(
190 &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
191 // has to destruct the any
193 else // else no exception occurred...
196 for ( ; nTempIndizes
--; )
198 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
199 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
201 if (pParams
[nIndex
].bOut
) // inout/out
203 // convert and assign
204 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
205 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
206 pThis
->getBridge()->getUno2Cpp() );
208 // destroy temp uno param
209 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
211 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
214 if (pCppReturn
) // has complex return
216 if (pUnoReturn
!= pCppReturn
) // needs reconversion
218 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
219 pThis
->getBridge()->getUno2Cpp() );
220 // destroy temp uno return
221 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
223 if (pReturnValue
!= pCppReturn
) {
224 // complex return ptr is set to eax if return value
225 // is not transferred via eax[/edx]:
226 *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(
259 #if OSL_DEBUG_LEVEL > 1
260 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
);
262 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
263 #if OSL_DEBUG_LEVEL > 1
264 fprintf( stderr
, "name=%s\n", rtl::OUStringToOString(pTypeDescr
->aBase
.pTypeName
, RTL_TEXTENCODING_UTF8
).getStr() );
266 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
267 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
269 throw RuntimeException(
270 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )),
271 (XInterface
*)pThis
);
274 // determine called method
275 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
276 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
278 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
279 #if OSL_DEBUG_LEVEL > 1
280 fprintf(stderr
, "calling %s\n", rtl::OUStringToOString(aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_UTF8
).getStr());
282 switch (aMemberDescr
.get()->eTypeClass
)
284 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
286 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
290 pCppI
, aMemberDescr
.get(),
291 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
293 pCallStack
, pReturnValue
);
298 typelib_MethodParameter aParam
;
300 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
301 aParam
.bIn
= sal_True
;
302 aParam
.bOut
= sal_False
;
305 pCppI
, aMemberDescr
.get(),
306 0, // indicates void return
308 pCallStack
, pReturnValue
);
312 case typelib_TypeClass_INTERFACE_METHOD
:
315 switch (nFunctionIndex
)
318 pCppI
->acquireProxy(); // non virtual call!
321 pCppI
->releaseProxy(); // non virtual call!
323 case 0: // queryInterface() opt
325 typelib_TypeDescription
* pTD
= 0;
326 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[3] )->getTypeLibType() );
329 XInterface
* pInterface
= 0;
330 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
331 pCppI
->getBridge()->getCppEnv(),
332 (void **)&pInterface
, pCppI
->getOid().pData
,
333 (typelib_InterfaceTypeDescription
*)pTD
);
338 reinterpret_cast< uno_Any
* >( pCallStack
[1] ),
339 &pInterface
, pTD
, cpp_acquire
);
340 pInterface
->release();
341 TYPELIB_DANGER_RELEASE( pTD
);
342 *static_cast< void ** >(pReturnValue
) = pCallStack
[1];
345 TYPELIB_DANGER_RELEASE( pTD
);
347 } // else perform queryInterface()
350 pCppI
, aMemberDescr
.get(),
351 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
352 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
353 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
354 pCallStack
, pReturnValue
);
360 throw RuntimeException(
361 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )),
362 (XInterface
*)pThis
);
367 //==================================================================================================
369 extern int nFunIndexes
, nVtableOffsets
;
370 extern int codeSnippets
[];
373 unsigned char * codeSnippet(
374 sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
378 typelib_TypeDescriptionReference
* pReturnTypeRef
382 OSL_ASSERT(functionIndex
< nFunIndexes
);
383 if (!(functionIndex
< nFunIndexes
))
386 OSL_ASSERT(vtableOffset
< nVtableOffsets
);
387 if (!(vtableOffset
< nVtableOffsets
))
391 return ((unsigned char *) &codeSnippets
) + codeSnippets
[functionIndex
*nVtableOffsets
*2 + vtableOffset
*2 + bHasHiddenParam
];
393 enum { General
, Void
, Hyper
, Float
, Double
, Class
} exec
;
394 bool bHasHiddenParam
= false;
395 if (pReturnTypeRef
== 0) {
399 switch (pReturnTypeRef
->eTypeClass
) {
400 case typelib_TypeClass_VOID
:
403 case typelib_TypeClass_HYPER
:
404 case typelib_TypeClass_UNSIGNED_HYPER
:
407 case typelib_TypeClass_FLOAT
:
410 case typelib_TypeClass_DOUBLE
:
413 case typelib_TypeClass_STRUCT
:
414 case typelib_TypeClass_EXCEPTION
: {
415 typelib_TypeDescription
* pReturnTypeDescr
= 0;
416 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
417 bool const bSimpleReturnStruct
=
418 CPPU_CURRENT_NAMESPACE::isSimpleReturnType(pReturnTypeDescr
);
419 sal_Int32
const nRetSize
= pReturnTypeDescr
->nSize
;
420 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
421 if (bSimpleReturnStruct
&& nRetSize
<= 8) {
422 exec
= General
; // fills eax
424 exec
= Hyper
; // fills eax/edx
428 case typelib_TypeClass_STRING
:
429 case typelib_TypeClass_TYPE
:
430 case typelib_TypeClass_SEQUENCE
:
431 case typelib_TypeClass_INTERFACE
:
432 case typelib_TypeClass_ANY
:
442 // The codeSnippets table is indexed by functionIndex, vtableOffset, exec and flag
444 fprintf(stderr
, "Indexing codeSnippets with %ld [%ld,%ld,%d,%d]\n",
445 functionIndex
*nVtableOffsets
*6*2 + vtableOffset
*6*2 + exec
*2 + bHasHiddenParam
,
446 functionIndex
, vtableOffset
, (int) exec
, bHasHiddenParam
);
447 return ((unsigned char *) &codeSnippets
) + codeSnippets
[functionIndex
*nVtableOffsets
*6*2 + vtableOffset
*6*2 + exec
*2 + bHasHiddenParam
];
453 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
455 bridges::cpp_uno::shared::VtableFactory::Slot
*
456 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
458 return static_cast< Slot
* >(block
) + 2;
461 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
467 return (slotCount
+ 2) * sizeof (Slot
);
471 bridges::cpp_uno::shared::VtableFactory::Slot
*
472 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
473 void * block
, sal_Int32 slotCount
)
475 Slot
* slots
= mapBlockToVtable(block
);
478 return slots
+ slotCount
;
481 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
482 Slot
** slots
, unsigned char * code
,
483 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
484 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
486 (*slots
) -= functionCount
;
488 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
489 typelib_TypeDescription
* member
= 0;
490 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
491 OSL_ASSERT(member
!= 0);
492 switch (member
->eTypeClass
) {
493 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
495 typelib_InterfaceAttributeTypeDescription
*pAttrTD
=
496 reinterpret_cast<typelib_InterfaceAttributeTypeDescription
*>( member
);
499 (s
++)->fn
= codeSnippet(
500 functionOffset
++, vtableOffset
,
502 arm::return_in_hidden_param( pAttrTD
->pAttributeTypeRef
)
504 reinterpret_cast< typelib_InterfaceAttributeTypeDescription
* >(
505 member
)->pAttributeTypeRef
509 if (!reinterpret_cast<
510 typelib_InterfaceAttributeTypeDescription
* >(
513 (s
++)->fn
= codeSnippet(
514 functionOffset
++, vtableOffset
,
518 0 /* indicates VOID */
524 case typelib_TypeClass_INTERFACE_METHOD
:
526 typelib_InterfaceMethodTypeDescription
*pMethodTD
=
528 typelib_InterfaceMethodTypeDescription
* >(member
);
530 (s
++)->fn
= codeSnippet(
531 functionOffset
++, vtableOffset
,
533 arm::return_in_hidden_param(pMethodTD
->pReturnTypeRef
)
535 reinterpret_cast< typelib_InterfaceMethodTypeDescription
* >(
536 member
)->pReturnTypeRef
545 TYPELIB_DANGER_RELEASE(member
);
550 void bridges::cpp_uno::shared::VtableFactory::flushCode(
551 unsigned char const *, unsigned char const *)
555 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */