1*61dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*61dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*61dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*61dff127SAndrew Rist  * distributed with this work for additional information
6*61dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*61dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*61dff127SAndrew Rist  * "License"); you may not use this file except in compliance
9*61dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*61dff127SAndrew Rist  *
11*61dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*61dff127SAndrew Rist  *
13*61dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*61dff127SAndrew Rist  * software distributed under the License is distributed on an
15*61dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*61dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
17*61dff127SAndrew Rist  * specific language governing permissions and limitations
18*61dff127SAndrew Rist  * under the License.
19*61dff127SAndrew Rist  *
20*61dff127SAndrew Rist  *************************************************************/
21*61dff127SAndrew Rist 
22*61dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtables.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "osl/diagnose.h"
30cdf0e10cSrcweir #include "sal/types.h"
31cdf0e10cSrcweir #include "typelib/typedescription.h"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <algorithm>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir  *  Calculates the number of vtables associated with an interface type.
40cdf0e10cSrcweir  *
41cdf0e10cSrcweir  * <p>Multiple-inheritance C++ classes have more than one vtable.</p>
42cdf0e10cSrcweir  *
43cdf0e10cSrcweir  * @param type a non-null pointer to an interface type description
44cdf0e10cSrcweir  * @return the number of vtables associated with the given interface type
45cdf0e10cSrcweir  */
getVtableCount(typelib_InterfaceTypeDescription const * type)46cdf0e10cSrcweir sal_Int32 getVtableCount(typelib_InterfaceTypeDescription const * type) {
47cdf0e10cSrcweir     sal_Int32 n = 0;
48cdf0e10cSrcweir     for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
49cdf0e10cSrcweir         n += getVtableCount(type->ppBaseTypes[i]);
50cdf0e10cSrcweir     }
51cdf0e10cSrcweir     return std::max< sal_Int32 >(n, 1);
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir  * Maps a local member index to a local function index.
56cdf0e10cSrcweir  *
57cdf0e10cSrcweir  * <p><em>Local</em> members/functions are those not inherited from any base
58cdf0e10cSrcweir  * types.  The number of <em>functions</em> is potentially larger than the
59cdf0e10cSrcweir  * number of <em>members</em>, as each read&ndash;write attribute member counts
60cdf0e10cSrcweir  * as two functions.</p>
61cdf0e10cSrcweir  *
62cdf0e10cSrcweir  * @param type a non-null pointer to an interface type description
63cdf0e10cSrcweir  * @param localMember a local member index, relative to the given interface type
64cdf0e10cSrcweir  * @return the local function index corresponding to the given local member
65cdf0e10cSrcweir  *     index, relative to the given interface type
66cdf0e10cSrcweir  */
mapLocalMemberToLocalFunction(typelib_InterfaceTypeDescription * type,sal_Int32 localMember)67cdf0e10cSrcweir sal_Int32 mapLocalMemberToLocalFunction(
68cdf0e10cSrcweir     typelib_InterfaceTypeDescription * type, sal_Int32 localMember)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     typelib_typedescription_complete(
71cdf0e10cSrcweir         reinterpret_cast< typelib_TypeDescription ** >(&type));
72cdf0e10cSrcweir     sal_Int32 localMemberOffset = type->nAllMembers - type->nMembers;
73cdf0e10cSrcweir     sal_Int32 localFunctionOffset = type->nMapFunctionIndexToMemberIndex
74cdf0e10cSrcweir         - bridges::cpp_uno::shared::getLocalFunctions(type);
75cdf0e10cSrcweir     return type->pMapMemberIndexToFunctionIndex[localMemberOffset + localMember]
76cdf0e10cSrcweir         - localFunctionOffset;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir // Since on Solaris we compile with --instances=static, getVtableSlot cannot be
80cdf0e10cSrcweir // a template function, with explicit instantiates for
81cdf0e10cSrcweir // T = typelib_InterfaceAttributeTypeDescription and
82cdf0e10cSrcweir // T = typelib_InterfaceMethodTypeDescription in this file; hence, there are two
83cdf0e10cSrcweir // overloaded versions of getVtableSlot that both delegate to this template
84cdf0e10cSrcweir // function:
doGetVtableSlot(T const * ifcMember)85cdf0e10cSrcweir template< typename T > bridges::cpp_uno::shared::VtableSlot doGetVtableSlot(
86cdf0e10cSrcweir     T const * ifcMember)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     bridges::cpp_uno::shared::VtableSlot slot;
89cdf0e10cSrcweir     slot.offset = 0;
90cdf0e10cSrcweir     T * member = const_cast< T * >(ifcMember);
91cdf0e10cSrcweir     while (member->pBaseRef != 0) {
92cdf0e10cSrcweir         OSL_ASSERT(member->nIndex < member->pInterface->nBaseTypes);
93cdf0e10cSrcweir         for (sal_Int32 i = 0; i < member->nIndex; ++i) {
94cdf0e10cSrcweir             slot.offset += getVtableCount(member->pInterface->ppBaseTypes[i]);
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         typelib_TypeDescription * desc = 0;
97cdf0e10cSrcweir         typelib_typedescriptionreference_getDescription(
98cdf0e10cSrcweir             &desc, member->pBaseRef);
99cdf0e10cSrcweir         OSL_ASSERT(
100cdf0e10cSrcweir             desc != 0 && desc->eTypeClass == member->aBase.aBase.eTypeClass);
101cdf0e10cSrcweir         if (member != ifcMember) {
102cdf0e10cSrcweir             typelib_typedescription_release(&member->aBase.aBase);
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         member = reinterpret_cast< T * >(desc);
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir     slot.index
107cdf0e10cSrcweir         = bridges::cpp_uno::shared::getPrimaryFunctions(
108cdf0e10cSrcweir             member->pInterface->pBaseTypeDescription)
109cdf0e10cSrcweir         + mapLocalMemberToLocalFunction(member->pInterface, member->nIndex);
110cdf0e10cSrcweir     if (member != ifcMember) {
111cdf0e10cSrcweir         typelib_typedescription_release(&member->aBase.aBase);
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir     return slot;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared {
119cdf0e10cSrcweir 
getLocalFunctions(typelib_InterfaceTypeDescription const * type)120cdf0e10cSrcweir sal_Int32 getLocalFunctions(typelib_InterfaceTypeDescription const * type) {
121cdf0e10cSrcweir     return type->nMembers == 0
122cdf0e10cSrcweir         ? 0
123cdf0e10cSrcweir         : (type->nMapFunctionIndexToMemberIndex
124cdf0e10cSrcweir            - type->pMapMemberIndexToFunctionIndex[
125cdf0e10cSrcweir                type->nAllMembers - type->nMembers]);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
getPrimaryFunctions(typelib_InterfaceTypeDescription * type)128cdf0e10cSrcweir sal_Int32 getPrimaryFunctions(typelib_InterfaceTypeDescription * type) {
129cdf0e10cSrcweir     sal_Int32 n = 0;
130cdf0e10cSrcweir     for (; type != 0; type = type->pBaseTypeDescription) {
131cdf0e10cSrcweir         typelib_typedescription_complete(
132cdf0e10cSrcweir             reinterpret_cast< typelib_TypeDescription ** >(&type));
133cdf0e10cSrcweir         n += getLocalFunctions(type);
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir     return n;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
getVtableSlot(typelib_InterfaceAttributeTypeDescription const * ifcMember)138cdf0e10cSrcweir VtableSlot getVtableSlot(
139cdf0e10cSrcweir     typelib_InterfaceAttributeTypeDescription const * ifcMember)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     return doGetVtableSlot(ifcMember);
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
getVtableSlot(typelib_InterfaceMethodTypeDescription const * ifcMember)144cdf0e10cSrcweir VtableSlot getVtableSlot(
145cdf0e10cSrcweir     typelib_InterfaceMethodTypeDescription const * ifcMember)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir     return doGetVtableSlot(ifcMember);
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir } } }
151