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: dependency.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 #include <osl/interlck.h>
32 #include <rtl/alloc.h>
33 #include <codemaker/dependency.hxx>
37 TypeDependency::TypeDependency()
39 m_pImpl
= new TypeDependencyImpl();
43 TypeDependency::~TypeDependency()
48 void TypeDependency::acquire()
50 osl_incrementInterlockedCount(&m_pImpl
->m_refCount
);
53 void TypeDependency::release()
55 if (0 == osl_decrementInterlockedCount(&m_pImpl
->m_refCount
))
61 sal_Bool
TypeDependency::insert(const OString
& type
, const OString
& depend
, sal_uInt16 use
)
63 sal_Bool ret
= sal_False
;
65 if (type
.getLength() > 0 && depend
.getLength() > 0)
67 if (m_pImpl
->m_dependencies
.count(type
) > 0)
69 TypeUsing
typeUsing(depend
, use
);
70 TypeUsingSet::iterator iter
;
71 if ((iter
= m_pImpl
->m_dependencies
[type
].find(typeUsing
)) != m_pImpl
->m_dependencies
[type
].end())
73 (((TypeUsing
*) &(*iter
))->m_use
) = (*iter
).m_use
| use
;
76 m_pImpl
->m_dependencies
[type
].insert(typeUsing
);
80 TypeUsing
typeUsing(depend
, use
);
82 tmpSet
.insert(typeUsing
);
83 m_pImpl
->m_dependencies
[type
]=tmpSet
;
90 TypeUsingSet
TypeDependency::getDependencies(const OString
& type
)
92 if (type
.getLength() > 0)
94 if (m_pImpl
->m_dependencies
.count(type
) > 0)
96 return m_pImpl
->m_dependencies
[type
];
100 return TypeUsingSet();
103 sal_Bool
TypeDependency::hasDependencies(const OString
& type
)
105 if (type
.getLength() > 0)
107 if (m_pImpl
->m_dependencies
.count(type
) > 0)
116 void TypeDependency::setGenerated(const OString
& type
, sal_uInt16 genFlag
)
118 // m_pImpl->m_generatedTypes.insert(type);
119 if (m_pImpl
->m_generatedTypes
.count(type
) > 0)
120 m_pImpl
->m_generatedTypes
[type
]= m_pImpl
->m_generatedTypes
[type
] | genFlag
;
122 m_pImpl
->m_generatedTypes
[type
]=genFlag
;
125 sal_Bool
TypeDependency::isGenerated(const OString
& type
, sal_uInt16 genFlag
)
128 if (m_pImpl->m_generatedTypes.count(type) > 0)
133 if (m_pImpl
->m_generatedTypes
.count(type
) > 0 &&
134 m_pImpl
->m_generatedTypes
[type
] & genFlag
)
142 static sal_Bool
checkFieldDependencies(TypeManager
& typeMgr
, TypeDependency
& dependencies
,
143 TypeReader
& reader
, const OString
& type
)
145 sal_uInt32 count
= reader
.getFieldCount();
147 if (count
== 0 || reader
.getTypeClass() == RT_TYPE_ENUM
)
151 for (sal_uInt16 i
=0; i
< count
; i
++)
153 fieldType
= reader
.getFieldType(i
);
155 if (fieldType
.getLength() > 0)
157 dependencies
.insert(type
, fieldType
, TYPEUSE_MEMBER
);
158 checkTypeDependencies(typeMgr
, dependencies
, fieldType
);
165 static sal_Bool
checkMethodDependencies(TypeManager
& typeMgr
, TypeDependency
& dependencies
,
166 TypeReader
& reader
, const OString
& type
)
168 sal_uInt32 count
= reader
.getMethodCount();
173 OString returnType
, paramType
, excType
;
174 sal_uInt32 paramCount
= 0;
175 sal_uInt32 excCount
= 0;
176 RTParamMode paramMode
= RT_PARAM_INVALID
;
177 for (sal_uInt16 i
=0; i
< count
; i
++)
179 returnType
= reader
.getMethodReturnType(i
);
181 dependencies
.insert(type
, returnType
, TYPEUSE_RETURN
);
182 checkTypeDependencies(typeMgr
, dependencies
, returnType
);
184 paramCount
= reader
.getMethodParamCount(i
);
185 excCount
= reader
.getMethodExcCount(i
);
188 for (j
=0; j
< paramCount
; j
++)
190 paramType
= reader
.getMethodParamType(i
, j
);
191 paramMode
= reader
.getMethodParamMode(i
, j
);
196 dependencies
.insert(type
, paramType
, TYPEUSE_INPARAM
);
199 dependencies
.insert(type
, paramType
, TYPEUSE_OUTPARAM
);
202 dependencies
.insert(type
, paramType
, TYPEUSE_INOUTPARAM
);
208 checkTypeDependencies(typeMgr
, dependencies
, paramType
);
211 for (j
=0; j
< excCount
; j
++)
213 excType
= reader
.getMethodExcType(i
, j
);
214 dependencies
.insert(type
, excType
, TYPEUSE_EXCEPTION
);
215 checkTypeDependencies(typeMgr
, dependencies
, excType
);
223 static sal_Bool
checkReferenceDependencies(TypeManager
& typeMgr
, TypeDependency
& dependencies
,
224 TypeReader
& reader
, const OString
& type
)
226 sal_uInt32 count
= reader
.getReferenceCount();
231 OString referenceName
;
232 for (sal_uInt16 i
=0; i
< count
; i
++)
234 referenceName
= reader
.getReferenceName(i
);
236 dependencies
.insert(type
, referenceName
, TYPEUSE_NORMAL
);
237 checkTypeDependencies(typeMgr
, dependencies
, referenceName
);
243 sal_Bool
checkTypeDependencies(TypeManager
& typeMgr
, TypeDependency
& dependencies
, const OString
& type
, sal_Bool bDepend
)
245 if (!typeMgr
.isValidType(type
))
248 if (dependencies
.hasDependencies(type
))
251 TypeReader reader
= typeMgr
.getTypeReader(type
);
253 if ( !reader
.isValid() )
255 if (type
.equals("/"))
261 if ( bDepend
&& reader
.getTypeClass() == RT_TYPE_MODULE
)
263 checkFieldDependencies(typeMgr
, dependencies
, reader
, type
);
267 for (sal_uInt16 i
= 0; i
< reader
.getSuperTypeCount(); ++i
) {
268 OString
superType(reader
.getSuperTypeName(i
));
269 dependencies
.insert(type
, superType
, TYPEUSE_SUPER
);
270 checkTypeDependencies(typeMgr
, dependencies
, superType
);
273 if (reader
.getTypeClass() == RT_TYPE_INTERFACE
)
275 dependencies
.insert(type
, "com/sun/star/uno/RuntimeException", TYPEUSE_EXCEPTION
);
276 dependencies
.insert(type
, "com/sun/star/uno/TypeClass", TYPEUSE_NORMAL
);
277 checkTypeDependencies(typeMgr
, dependencies
, "com/sun/star/uno/RuntimeException", bDepend
);
280 checkFieldDependencies(typeMgr
, dependencies
, reader
, type
);
281 checkMethodDependencies(typeMgr
, dependencies
, reader
, type
);
282 checkReferenceDependencies(typeMgr
, dependencies
, reader
, type
);
284 // make the scope modules as dependencies
285 sal_Int32 nPos
= type
.lastIndexOf( '/' );
289 OString
aScope( type
.copy( 0, nPos
) );
290 OStringBuffer
tmpBuf(aScope
.getLength());
295 tmpBuf
.append(aScope
.getToken(0, '/', nPos
));
296 dependencies
.insert(type
, tmpBuf
.getStr(), TYPEUSE_SCOPE
);
298 } while( nPos
!= -1 );