1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <cassert>
23 #include <cstddef>
24 #include <limits>
25 
26 #ifdef SAL_UNX
27 #include <sal/alloca.h>
28 #endif
29 #include <o3tl/any.hxx>
30 #include <typelib/typedescription.hxx>
31 #include <uno/data.h>
32 
33 #include "base.hxx"
34 
35 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
36 #include <com/sun/star/reflection/XIdlField2.hpp>
37 #include <com/sun/star/uno/RuntimeException.hpp>
38 #include <cppuhelper/queryinterface.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <cppuhelper/typeprovider.hxx>
41 
42 using namespace css::lang;
43 using namespace css::reflection;
44 using namespace css::uno;
45 
46 namespace {
47 
multipleOf16(std::size_t n)48 std::size_t multipleOf16(std::size_t n) {
49     assert(n <= std::numeric_limits<std::size_t>::max() - 15);
50     return (n + 15) & ~std::size_t(15);
51 }
52 
53 }
54 
55 namespace stoc_corefl
56 {
57 
58 
59 class IdlAttributeFieldImpl
60     : public IdlMemberImpl
61     , public XIdlField
62     , public XIdlField2
63 {
64 public:
getAttributeTypeDescr() const65     typelib_InterfaceAttributeTypeDescription * getAttributeTypeDescr() const
66         { return reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(getTypeDescr()); }
67 
IdlAttributeFieldImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeDescription * pTypeDescr,typelib_TypeDescription * pDeclTypeDescr)68     IdlAttributeFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
69                            typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
70         : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
71         {}
72 
73     // XInterface
74     virtual Any SAL_CALL queryInterface( const Type & rType ) override;
75     virtual void SAL_CALL acquire() throw() override;
76     virtual void SAL_CALL release() throw() override;
77 
78     // XTypeProvider
79     virtual Sequence< Type > SAL_CALL getTypes() override;
80     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
81 
82     // XIdlMember
83     virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
84     virtual OUString SAL_CALL getName() override;
85     // XIdlField
86     virtual Reference< XIdlClass > SAL_CALL getType() override;
87     virtual FieldAccessMode SAL_CALL getAccessMode() override;
88     virtual Any SAL_CALL get( const Any & rObj ) override;
89     virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
90     // XIdlField2: getType, getAccessMode and get are equal to XIdlField
91     virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
92 
93 private:
94     void checkException(
95         uno_Any * exception, Reference< XInterface > const & context) const;
96 };
97 
98 // XInterface
99 
queryInterface(const Type & rType)100 Any IdlAttributeFieldImpl::queryInterface( const Type & rType )
101 {
102     Any aRet( ::cppu::queryInterface( rType,
103                                       static_cast< XIdlField * >( this ),
104                                       static_cast< XIdlField2 * >( this ) ) );
105     return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
106 }
107 
acquire()108 void IdlAttributeFieldImpl::acquire() throw()
109 {
110     IdlMemberImpl::acquire();
111 }
112 
release()113 void IdlAttributeFieldImpl::release() throw()
114 {
115     IdlMemberImpl::release();
116 }
117 
118 // XTypeProvider
119 
getTypes()120 Sequence< Type > IdlAttributeFieldImpl::getTypes()
121 {
122     static cppu::OTypeCollection s_aTypes(
123         cppu::UnoType<XIdlField2>::get(),
124         cppu::UnoType<XIdlField>::get(),
125         IdlMemberImpl::getTypes() );
126 
127     return s_aTypes.getTypes();
128 }
129 
getImplementationId()130 Sequence< sal_Int8 > IdlAttributeFieldImpl::getImplementationId()
131 {
132     return css::uno::Sequence<sal_Int8>();
133 }
134 
135 // XIdlMember
136 
getDeclaringClass()137 Reference< XIdlClass > IdlAttributeFieldImpl::getDeclaringClass()
138 {
139     if (! _xDeclClass.is())
140     {
141         ::osl::MutexGuard aGuard( getMutexAccess() );
142         if (! _xDeclClass.is())
143         {
144             OUString aName(getAttributeTypeDescr()->aBase.aBase.pTypeName);
145             sal_Int32 i = aName.indexOf(':');
146             OSL_ASSERT(i >= 0);
147             _xDeclClass = getReflection()->forName(aName.copy(0, i));
148         }
149     }
150     return _xDeclClass;
151 }
152 
getName()153 OUString IdlAttributeFieldImpl::getName()
154 {
155     return IdlMemberImpl::getName();
156 }
157 
158 // XIdlField
159 
getType()160 Reference< XIdlClass > IdlAttributeFieldImpl::getType()
161 {
162     return getReflection()->forType(
163         getAttributeTypeDescr()->pAttributeTypeRef );
164 }
165 
getAccessMode()166 FieldAccessMode IdlAttributeFieldImpl::getAccessMode()
167 {
168     return (getAttributeTypeDescr()->bReadOnly
169             ? FieldAccessMode_READONLY : FieldAccessMode_READWRITE);
170 }
171 
get(const Any & rObj)172 Any IdlAttributeFieldImpl::get( const Any & rObj )
173 {
174     uno_Interface * pUnoI = getReflection()->mapToUno(
175         rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
176     OSL_ENSURE( pUnoI, "### illegal destination object given!" );
177     if (pUnoI)
178     {
179         TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
180         typelib_TypeDescription * pTD = aTD.get();
181 
182         uno_Any aExc;
183         uno_Any * pExc = &aExc;
184         void * pReturn = alloca( pTD->nSize );
185 
186         (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), pReturn, nullptr, &pExc );
187         (*pUnoI->release)( pUnoI );
188 
189         checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
190         Any aRet;
191         uno_any_destruct(
192             &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
193         uno_any_constructAndConvert( &aRet, pReturn, pTD, getReflection()->getUno2Cpp().get() );
194         uno_destructData( pReturn, pTD, nullptr );
195         return aRet;
196     }
197     throw IllegalArgumentException(
198         "illegal object given!",
199         static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
200 }
201 
set(Any & rObj,const Any & rValue)202 void IdlAttributeFieldImpl::set( Any & rObj, const Any & rValue )
203 {
204     if (getAttributeTypeDescr()->bReadOnly)
205     {
206         throw IllegalAccessException(
207             "cannot set readonly attribute!",
208             static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
209     }
210 
211     uno_Interface * pUnoI = getReflection()->mapToUno(
212         rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
213     OSL_ENSURE( pUnoI, "### illegal destination object given!" );
214     if (pUnoI)
215     {
216         TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
217         typelib_TypeDescription * pTD = aTD.get();
218 
219         // construct uno value to be set
220         void * pArgs[1];
221         void * pArg = pArgs[0] = alloca( pTD->nSize );
222 
223         bool bAssign;
224         if (pTD->eTypeClass == typelib_TypeClass_ANY)
225         {
226             uno_copyAndConvertData( pArg, const_cast< Any * >(&rValue),
227                                     pTD, getReflection()->getCpp2Uno().get() );
228             bAssign = true;
229         }
230         else if (typelib_typedescriptionreference_equals( rValue.getValueTypeRef(), pTD->pWeakRef ))
231         {
232             uno_copyAndConvertData( pArg, const_cast< void * >(rValue.getValue()),
233                                     pTD, getReflection()->getCpp2Uno().get() );
234             bAssign = true;
235         }
236         else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
237         {
238             Reference< XInterface > xObj;
239             bAssign = extract(
240                 rValue, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xObj,
241                 getReflection() );
242             if (bAssign)
243             {
244                 *static_cast<void **>(pArg) = getReflection()->getCpp2Uno().mapInterface(
245                     xObj.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
246             }
247         }
248         else
249         {
250             typelib_TypeDescription * pValueTD = nullptr;
251             TYPELIB_DANGER_GET( &pValueTD, rValue.getValueTypeRef() );
252             // construct temp uno val to do proper assignment: todo opt
253             void * pTemp = alloca( pValueTD->nSize );
254             uno_copyAndConvertData(
255                 pTemp, const_cast<void *>(rValue.getValue()), pValueTD, getReflection()->getCpp2Uno().get() );
256             uno_constructData(
257                 pArg, pTD );
258             // assignment does simple conversion
259             bAssign = uno_assignData(
260                 pArg, pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
261             uno_destructData(
262                 pTemp, pValueTD, nullptr );
263             TYPELIB_DANGER_RELEASE( pValueTD );
264         }
265 
266         if (bAssign)
267         {
268             uno_Any aExc;
269             uno_Any * pExc = &aExc;
270             (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), nullptr, pArgs, &pExc );
271             (*pUnoI->release)( pUnoI );
272 
273             uno_destructData( pArg, pTD, nullptr );
274             checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
275             return;
276         }
277         (*pUnoI->release)( pUnoI );
278 
279         throw IllegalArgumentException(
280             "illegal value given!",
281             *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
282     }
283     throw IllegalArgumentException(
284         "illegal destination object given!",
285         static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
286 }
287 
set(const Any & rObj,const Any & rValue)288 void IdlAttributeFieldImpl::set( const Any & rObj, const Any & rValue )
289 {
290     IdlAttributeFieldImpl::set( const_cast< Any & >( rObj ), rValue );
291 }
292 
checkException(uno_Any * exception,Reference<XInterface> const & context) const293 void IdlAttributeFieldImpl::checkException(
294     uno_Any * exception, Reference< XInterface > const & context) const
295 {
296     if (exception == nullptr)
297         return;
298 
299     Any e;
300     uno_any_destruct(&e, reinterpret_cast< uno_ReleaseFunc >(cpp_release));
301     uno_type_any_constructAndConvert(
302         &e, exception->pData, exception->pType,
303         getReflection()->getUno2Cpp().get());
304     uno_any_destruct(exception, nullptr);
305     if (!e.isExtractableTo(
306             cppu::UnoType<RuntimeException>::get()))
307     {
308         throw WrappedTargetRuntimeException(
309             "non-RuntimeException occurred when accessing an"
310             " interface type attribute",
311             context, e);
312     }
313     cppu::throwException(e);
314 }
315 
316 
317 class IdlInterfaceMethodImpl
318     : public IdlMemberImpl
319     , public XIdlMethod
320 {
321     std::unique_ptr<Sequence< Reference< XIdlClass > >> _pExceptionTypes;
322     std::unique_ptr<Sequence< Reference< XIdlClass > >> _pParamTypes;
323     std::unique_ptr<Sequence< ParamInfo >>              _pParamInfos;
324 
325 public:
getMethodTypeDescr() const326     typelib_InterfaceMethodTypeDescription * getMethodTypeDescr() const
327         { return reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(getTypeDescr()); }
328 
IdlInterfaceMethodImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeDescription * pTypeDescr,typelib_TypeDescription * pDeclTypeDescr)329     IdlInterfaceMethodImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
330                             typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
331         : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
332         {}
333 
334     // XInterface
335     virtual Any SAL_CALL queryInterface( const Type & rType ) override;
336     virtual void SAL_CALL acquire() throw() override;
337     virtual void SAL_CALL release() throw() override;
338 
339     // XTypeProvider
340     virtual Sequence< Type > SAL_CALL getTypes() override;
341     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
342 
343     // XIdlMember
344     virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
345     virtual OUString SAL_CALL getName() override;
346     // XIdlMethod
347     virtual Reference< XIdlClass > SAL_CALL getReturnType() override;
348     virtual Sequence< Reference< XIdlClass > > SAL_CALL getParameterTypes() override;
349     virtual Sequence< ParamInfo > SAL_CALL getParameterInfos() override;
350     virtual Sequence< Reference< XIdlClass > > SAL_CALL getExceptionTypes() override;
351     virtual MethodMode SAL_CALL getMode() override;
352     virtual Any SAL_CALL invoke( const Any & rObj, Sequence< Any > & rArgs ) override;
353 };
354 
355 // XInterface
356 
queryInterface(const Type & rType)357 Any IdlInterfaceMethodImpl::queryInterface( const Type & rType )
358 {
359     Any aRet( ::cppu::queryInterface( rType, static_cast< XIdlMethod * >( this ) ) );
360     return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
361 }
362 
acquire()363 void IdlInterfaceMethodImpl::acquire() throw()
364 {
365     IdlMemberImpl::acquire();
366 }
367 
release()368 void IdlInterfaceMethodImpl::release() throw()
369 {
370     IdlMemberImpl::release();
371 }
372 
373 // XTypeProvider
374 
getTypes()375 Sequence< Type > IdlInterfaceMethodImpl::getTypes()
376 {
377     static cppu::OTypeCollection s_aTypes(
378         cppu::UnoType<XIdlMethod>::get(),
379         IdlMemberImpl::getTypes() );
380 
381     return s_aTypes.getTypes();
382 }
383 
getImplementationId()384 Sequence< sal_Int8 > IdlInterfaceMethodImpl::getImplementationId()
385 {
386     return css::uno::Sequence<sal_Int8>();
387 }
388 
389 // XIdlMember
390 
getDeclaringClass()391 Reference< XIdlClass > IdlInterfaceMethodImpl::getDeclaringClass()
392 {
393     if (! _xDeclClass.is())
394     {
395         ::osl::MutexGuard aGuard( getMutexAccess() );
396         if (! _xDeclClass.is())
397         {
398             OUString aName(getMethodTypeDescr()->aBase.aBase.pTypeName);
399             sal_Int32 i = aName.indexOf(':');
400             OSL_ASSERT(i >= 0);
401             _xDeclClass = getReflection()->forName(aName.copy(0, i));
402         }
403     }
404     return _xDeclClass;
405 }
406 
getName()407 OUString IdlInterfaceMethodImpl::getName()
408 {
409     return IdlMemberImpl::getName();
410 }
411 
412 // XIdlMethod
413 
getReturnType()414 Reference< XIdlClass > SAL_CALL IdlInterfaceMethodImpl::getReturnType()
415 {
416     return getReflection()->forType( getMethodTypeDescr()->pReturnTypeRef );
417 }
418 
getExceptionTypes()419 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getExceptionTypes()
420 {
421     if (! _pExceptionTypes)
422     {
423         ::osl::MutexGuard aGuard( getMutexAccess() );
424         if (! _pExceptionTypes)
425         {
426             sal_Int32 nExc = getMethodTypeDescr()->nExceptions;
427             std::unique_ptr<Sequence< Reference< XIdlClass > >> pTempExceptionTypes(
428                 new Sequence< Reference< XIdlClass > >( nExc ));
429             Reference< XIdlClass > * pExceptionTypes = pTempExceptionTypes->getArray();
430 
431             typelib_TypeDescriptionReference ** ppExc =
432                 getMethodTypeDescr()->ppExceptions;
433             IdlReflectionServiceImpl * pRefl = getReflection();
434 
435             while (nExc--)
436                 pExceptionTypes[nExc] = pRefl->forType( ppExc[nExc] );
437 
438             _pExceptionTypes = std::move(pTempExceptionTypes);
439         }
440     }
441     return *_pExceptionTypes;
442 }
443 
getParameterTypes()444 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getParameterTypes()
445 {
446     if (! _pParamTypes)
447     {
448         ::osl::MutexGuard aGuard( getMutexAccess() );
449         if (! _pParamTypes)
450         {
451             sal_Int32 nParams = getMethodTypeDescr()->nParams;
452             std::unique_ptr<Sequence< Reference< XIdlClass > > > pTempParamTypes(
453                 new Sequence< Reference< XIdlClass > >( nParams ));
454             Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
455 
456             typelib_MethodParameter * pTypelibParams =
457                 getMethodTypeDescr()->pParams;
458             IdlReflectionServiceImpl * pRefl = getReflection();
459 
460             while (nParams--)
461                 pParamTypes[nParams] = pRefl->forType( pTypelibParams[nParams].pTypeRef );
462 
463             _pParamTypes = std::move(pTempParamTypes);
464         }
465     }
466     return *_pParamTypes;
467 }
468 
getParameterInfos()469 Sequence< ParamInfo > IdlInterfaceMethodImpl::getParameterInfos()
470 {
471     if (! _pParamInfos)
472     {
473         ::osl::MutexGuard aGuard( getMutexAccess() );
474         if (! _pParamInfos)
475         {
476             sal_Int32 nParams = getMethodTypeDescr()->nParams;
477             std::unique_ptr<Sequence< ParamInfo > > pTempParamInfos( new Sequence< ParamInfo >( nParams ) );
478             ParamInfo * pParamInfos = pTempParamInfos->getArray();
479 
480             typelib_MethodParameter * pTypelibParams =
481                 getMethodTypeDescr()->pParams;
482 
483             if (_pParamTypes) // use param types
484             {
485                 const Reference< XIdlClass > * pParamTypes = _pParamTypes->getConstArray();
486 
487                 while (nParams--)
488                 {
489                     const typelib_MethodParameter & rParam = pTypelibParams[nParams];
490                     ParamInfo & rInfo = pParamInfos[nParams];
491                     rInfo.aName = rParam.pName;
492                     if (rParam.bIn)
493                         rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
494                     else
495                         rInfo.aMode = ParamMode_OUT;
496                     rInfo.aType = pParamTypes[nParams];
497                 }
498             }
499             else // make also param types sequence if not already initialized
500             {
501                 std::unique_ptr<Sequence< Reference< XIdlClass > > > pTempParamTypes(
502                     new Sequence< Reference< XIdlClass > >( nParams ));
503                 Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
504 
505                 IdlReflectionServiceImpl * pRefl = getReflection();
506 
507                 while (nParams--)
508                 {
509                     const typelib_MethodParameter & rParam = pTypelibParams[nParams];
510                     ParamInfo & rInfo = pParamInfos[nParams];
511                     rInfo.aName = rParam.pName;
512                     if (rParam.bIn)
513                         rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
514                     else
515                         rInfo.aMode = ParamMode_OUT;
516                     rInfo.aType = pParamTypes[nParams] = pRefl->forType( rParam.pTypeRef );
517                 }
518 
519                 _pParamTypes = std::move(pTempParamTypes);
520             }
521 
522             _pParamInfos = std::move(pTempParamInfos);
523         }
524     }
525     return *_pParamInfos;
526 }
527 
getMode()528 MethodMode SAL_CALL IdlInterfaceMethodImpl::getMode()
529 {
530     return
531         getMethodTypeDescr()->bOneWay ? MethodMode_ONEWAY : MethodMode_TWOWAY;
532 }
533 
invoke(const Any & rObj,Sequence<Any> & rArgs)534 Any SAL_CALL IdlInterfaceMethodImpl::invoke( const Any & rObj, Sequence< Any > & rArgs )
535 {
536     if (auto ifc = o3tl::tryAccess<css::uno::Reference<css::uno::XInterface>>(
537             rObj))
538     {
539         // acquire()/ release()
540         if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
541                                     "com.sun.star.uno.XInterface::acquire" ) == 0)
542         {
543             (*ifc)->acquire();
544             return Any();
545         }
546         else if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
547                                          "com.sun.star.uno.XInterface::release" ) == 0)
548         {
549             (*ifc)->release();
550             return Any();
551         }
552     }
553 
554     uno_Interface * pUnoI = getReflection()->mapToUno(
555         rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
556     OSL_ENSURE( pUnoI, "### illegal destination object given!" );
557     if (pUnoI)
558     {
559         sal_Int32 nParams = getMethodTypeDescr()->nParams;
560         if (rArgs.getLength() != nParams)
561         {
562             (*pUnoI->release)( pUnoI );
563             throw IllegalArgumentException(
564                 "expected " + OUString::number(nParams) +
565                  " arguments, got " + OUString::number(rArgs.getLength()),
566                 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
567         }
568 
569         Any * pCppArgs = rArgs.getArray();
570         typelib_MethodParameter * pParams = getMethodTypeDescr()->pParams;
571         typelib_TypeDescription * pReturnType = nullptr;
572         TYPELIB_DANGER_GET(
573             &pReturnType, getMethodTypeDescr()->pReturnTypeRef );
574 
575         // C/C++ ABIs typically assume that structs are padded at the end, and
576         // that those padding bytes may be written to (e.g., to write into the
577         // end of a "short" struct by writing the full contents of a "long"
578         // register); so create enough space here (assuming that no ABI requires
579         // padding larger than 16 byte boundaries):
580         void * pUnoReturn = (pReturnType->nSize == 0) ? nullptr : alloca( multipleOf16(pReturnType->nSize) );
581         void ** ppUnoArgs = static_cast<void **>(alloca( sizeof(void *) * nParams *2 ));
582         typelib_TypeDescription ** ppParamTypes = reinterpret_cast<typelib_TypeDescription **>(ppUnoArgs + nParams);
583 
584         // convert arguments
585         for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
586         {
587             ppParamTypes[nPos] = nullptr;
588             TYPELIB_DANGER_GET( ppParamTypes + nPos, pParams[nPos].pTypeRef );
589             typelib_TypeDescription * pTD = ppParamTypes[nPos];
590 
591             ppUnoArgs[nPos] = alloca( pTD->nSize );
592             if (pParams[nPos].bIn)
593             {
594                 bool bAssign;
595                 if (typelib_typedescriptionreference_equals(
596                         pCppArgs[nPos].getValueTypeRef(), pTD->pWeakRef ))
597                 {
598                     uno_type_copyAndConvertData(
599                         ppUnoArgs[nPos], const_cast<void *>(pCppArgs[nPos].getValue()),
600                         pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
601                     bAssign = true;
602                 }
603                 else if (pTD->eTypeClass == typelib_TypeClass_ANY)
604                 {
605                     uno_type_any_constructAndConvert(
606                         static_cast<uno_Any *>(ppUnoArgs[nPos]), const_cast<void *>(pCppArgs[nPos].getValue()),
607                         pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
608                     bAssign = true;
609                 }
610                 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
611                 {
612                     Reference< XInterface > xDest;
613                     bAssign = extract(
614                         pCppArgs[nPos], reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD),
615                         xDest, getReflection() );
616                     if (bAssign)
617                     {
618                         *static_cast<void **>(ppUnoArgs[nPos]) = getReflection()->getCpp2Uno().mapInterface(
619                             xDest.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
620                     }
621                 }
622                 else
623                 {
624                     typelib_TypeDescription * pValueTD = nullptr;
625                     TYPELIB_DANGER_GET( &pValueTD, pCppArgs[nPos].getValueTypeRef() );
626                     // construct temp uno val to do proper assignment: todo opt
627                     void * pTemp = alloca( pValueTD->nSize );
628                     uno_copyAndConvertData(
629                         pTemp, const_cast<void *>(pCppArgs[nPos].getValue()), pValueTD,
630                         getReflection()->getCpp2Uno().get() );
631                     uno_constructData(
632                         ppUnoArgs[nPos], pTD );
633                     // assignment does simple conversion
634                     bAssign = uno_assignData(
635                         ppUnoArgs[nPos], pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
636                     uno_destructData(
637                         pTemp, pValueTD, nullptr );
638                     TYPELIB_DANGER_RELEASE( pValueTD );
639                 }
640 
641                 if (! bAssign)
642                 {
643                     IllegalArgumentException aExc(
644                         "cannot coerce argument type during corereflection call:"
645                         "\narg no.: " + OUString::number(nPos)
646                         + " expected: \"" + OUString::unacquired(&pTD->pTypeName)
647                         + "\" actual: \"" + OUString::unacquired(&pCppArgs[nPos].getValueTypeRef()->pTypeName)
648                         + "\"",
649                         *o3tl::doAccess<Reference<XInterface>>(rObj), static_cast<sal_Int16>(nPos) );
650 
651                     // cleanup
652                     while (nPos--)
653                     {
654                         if (pParams[nPos].bIn)
655                             uno_destructData( ppUnoArgs[nPos], ppParamTypes[nPos], nullptr );
656                         TYPELIB_DANGER_RELEASE( ppParamTypes[nPos] );
657                     }
658                     TYPELIB_DANGER_RELEASE( pReturnType );
659                     (*pUnoI->release)( pUnoI );
660 
661                     throw aExc;
662                 }
663             }
664         }
665 
666         uno_Any aUnoExc;
667         uno_Any * pUnoExc = &aUnoExc;
668 
669         (*pUnoI->pDispatcher)(
670             pUnoI, getTypeDescr(), pUnoReturn, ppUnoArgs, &pUnoExc );
671         (*pUnoI->release)( pUnoI );
672 
673         Any aRet;
674         if (pUnoExc)
675         {
676             // cleanup
677             while (nParams--)
678             {
679                 if (pParams[nParams].bIn)
680                     uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
681                 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
682             }
683             TYPELIB_DANGER_RELEASE( pReturnType );
684 
685             InvocationTargetException aExc;
686             aExc.Context = *o3tl::doAccess<Reference<XInterface>>(rObj);
687             aExc.Message = "exception occurred during invocation!";
688             uno_any_destruct(
689                 &aExc.TargetException,
690                 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
691             uno_type_copyAndConvertData(
692                 &aExc.TargetException, pUnoExc, cppu::UnoType<Any>::get().getTypeLibType(),
693                 getReflection()->getUno2Cpp().get() );
694             uno_any_destruct( pUnoExc, nullptr );
695             throw aExc;
696         }
697         else
698         {
699             // reconvert arguments and cleanup
700             while (nParams--)
701             {
702                 if (pParams[nParams].bOut) // write back
703                 {
704                     uno_any_destruct(
705                         &pCppArgs[nParams],
706                         reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
707                     uno_any_constructAndConvert(
708                         &pCppArgs[nParams], ppUnoArgs[nParams], ppParamTypes[nParams],
709                         getReflection()->getUno2Cpp().get() );
710                 }
711                 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
712                 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
713             }
714             uno_any_destruct(
715                 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
716             uno_any_constructAndConvert(
717                 &aRet, pUnoReturn, pReturnType,
718                 getReflection()->getUno2Cpp().get() );
719             uno_destructData( pUnoReturn, pReturnType, nullptr );
720             TYPELIB_DANGER_RELEASE( pReturnType );
721         }
722         return aRet;
723     }
724     throw IllegalArgumentException(
725         "illegal destination object given!",
726         static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
727 }
728 
729 
~InterfaceIdlClassImpl()730 InterfaceIdlClassImpl::~InterfaceIdlClassImpl()
731 {
732     for ( sal_Int32 nPos = _nMethods + _nAttributes; nPos--; )
733         typelib_typedescription_release( _pSortedMemberInit[nPos].second );
734 }
735 
736 
getSuperclasses()737 Sequence< Reference< XIdlClass > > InterfaceIdlClassImpl::getSuperclasses()
738 {
739     ::osl::MutexGuard aGuard(getMutexAccess());
740     if (!_xSuperClasses.hasElements()) {
741         typelib_InterfaceTypeDescription * pType = getTypeDescr();
742         _xSuperClasses.realloc(pType->nBaseTypes);
743         for (sal_Int32 i = 0; i < pType->nBaseTypes; ++i) {
744             _xSuperClasses[i] = getReflection()->forType(
745                 &pType->ppBaseTypes[i]->aBase);
746             OSL_ASSERT(_xSuperClasses[i].is());
747         }
748     }
749     return _xSuperClasses;
750 }
751 
initMembers()752 void InterfaceIdlClassImpl::initMembers()
753 {
754     sal_Int32 nAll = getTypeDescr()->nAllMembers;
755     std::unique_ptr<MemberInit[]> pSortedMemberInit(new MemberInit[nAll]);
756     typelib_TypeDescriptionReference ** ppAllMembers = getTypeDescr()->ppAllMembers;
757 
758     for ( sal_Int32 nPos = 0; nPos < nAll; ++nPos )
759     {
760         sal_Int32 nIndex;
761         if (ppAllMembers[nPos]->eTypeClass == typelib_TypeClass_INTERFACE_METHOD)
762         {
763             // methods to front
764             nIndex = _nMethods;
765             ++_nMethods;
766         }
767         else
768         {
769             ++_nAttributes;
770             nIndex = (nAll - _nAttributes);
771             // attributes at the back
772         }
773 
774         typelib_TypeDescription * pTD = nullptr;
775         typelib_typedescriptionreference_getDescription( &pTD, ppAllMembers[nPos] );
776         assert(pTD && "### cannot get type description!");
777         pSortedMemberInit[nIndex].first = reinterpret_cast<typelib_InterfaceMemberTypeDescription *>(pTD)->pMemberName;
778         pSortedMemberInit[nIndex].second = pTD;
779     }
780 
781     _pSortedMemberInit = std::move(pSortedMemberInit);
782 }
783 
isAssignableFrom(const Reference<XIdlClass> & xType)784 sal_Bool InterfaceIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
785 {
786     if (xType.is() && xType->getTypeClass() == TypeClass_INTERFACE)
787     {
788         if (equals( xType ))
789             return true;
790         else
791         {
792             const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
793             if (std::any_of(rSeq.begin(), rSeq.end(),
794                     [this](const Reference<XIdlClass>& rType){ return isAssignableFrom(rType); }))
795                 return true;
796         }
797     }
798     return false;
799 }
800 
getUik()801 Uik InterfaceIdlClassImpl::getUik()
802 {
803     return Uik(0, 0, 0, 0, 0);
804         // Uiks are deprecated and this function must not be called
805 }
806 
getMethods()807 Sequence< Reference< XIdlMethod > > InterfaceIdlClassImpl::getMethods()
808 {
809     ::osl::MutexGuard aGuard( getMutexAccess() );
810     if (! _pSortedMemberInit)
811         initMembers();
812 
813     // create methods sequence
814     Sequence< Reference< XIdlMethod > > aRet( _nMethods );
815     Reference< XIdlMethod > * pRet = aRet.getArray();
816     for ( sal_Int32 nPos = _nMethods; nPos--; )
817     {
818 
819         /*_aName2Method[_pSortedMemberInit[nPos].first] = */pRet[nPos] = new IdlInterfaceMethodImpl(
820             getReflection(), _pSortedMemberInit[nPos].first,
821             _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
822     }
823     return aRet;
824 }
825 
getFields()826 Sequence< Reference< XIdlField > > InterfaceIdlClassImpl::getFields()
827 {
828     ::osl::MutexGuard aGuard( getMutexAccess() );
829     if (! _pSortedMemberInit)
830         initMembers();
831 
832     // create fields sequence
833     Sequence< Reference< XIdlField > > aRet( _nAttributes );
834     Reference< XIdlField > * pRet = aRet.getArray();
835     for ( sal_Int32 nPos = _nAttributes; nPos--; )
836     {
837         /*_aName2Field[_pSortedMemberInit[_nMethods+nPos].first] = */pRet[_nAttributes-nPos-1] =
838             new IdlAttributeFieldImpl(
839                 getReflection(), _pSortedMemberInit[_nMethods+nPos].first,
840                 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
841     }
842     return aRet;
843 }
844 
getMethod(const OUString & rName)845 Reference< XIdlMethod > InterfaceIdlClassImpl::getMethod( const OUString & rName )
846 {
847     ::osl::MutexGuard aGuard( getMutexAccess() );
848     if (! _pSortedMemberInit)
849         initMembers();
850 
851     Reference< XIdlMethod > xRet;
852 
853     // try weak map
854     const OUString2Method::const_iterator iFind( _aName2Method.find( rName ) );
855     if (iFind != _aName2Method.end())
856         xRet = (*iFind).second; // harden ref
857 
858     if (! xRet.is())
859     {
860         for ( sal_Int32 nPos = _nMethods; nPos--; )
861         {
862             if (_pSortedMemberInit[nPos].first == rName)
863             {
864                 _aName2Method[rName] = xRet = new IdlInterfaceMethodImpl(
865                     getReflection(), rName,
866                     _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
867                 break;
868             }
869         }
870     }
871     return xRet;
872 }
873 
getField(const OUString & rName)874 Reference< XIdlField > InterfaceIdlClassImpl::getField( const OUString & rName )
875 {
876     ::osl::MutexGuard aGuard( getMutexAccess() );
877     if (! _pSortedMemberInit)
878         initMembers();
879 
880     Reference< XIdlField > xRet;
881 
882     // try weak map
883     const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
884     if (iFind != _aName2Field.end())
885         xRet = (*iFind).second; // harden ref
886 
887     if (! xRet.is())
888     {
889         for ( sal_Int32 nPos = _nAttributes; nPos--; )
890         {
891             if (_pSortedMemberInit[_nMethods+nPos].first == rName)
892             {
893                 _aName2Field[rName] = xRet = new IdlAttributeFieldImpl(
894                     getReflection(), rName,
895                     _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
896                 break;
897             }
898         }
899     }
900     return xRet;
901 }
902 
createObject(Any & rObj)903 void InterfaceIdlClassImpl::createObject( Any & rObj )
904 {
905     // interfaces cannot be constructed
906     rObj.clear();
907 }
908 
909 }
910 
911 
912 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
913