fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / idlc / source / astinterface.cxx
blob97d8f020170c673fe0965bdfdc9e26993cf81a02
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <idlc/astinterface.hxx>
21 #include <idlc/astattribute.hxx>
22 #include <idlc/astoperation.hxx>
23 #include "idlc/idlc.hxx"
25 #include <registry/version.h>
26 #include <registry/writer.hxx>
28 #include <osl/diagnose.h>
30 AstInterface::AstInterface(const OString& name,
31 AstInterface const * pInherits,
32 AstScope* pScope)
33 : AstType(NT_interface, name, pScope)
34 , AstScope(NT_interface)
35 , m_mandatoryInterfaces(0)
36 , m_bIsDefined(false)
37 , m_bForwarded(false)
38 , m_bForwardedInSameFile(false)
39 , m_bSingleInheritance(pInherits != 0)
41 if (pInherits != 0) {
42 addInheritedInterface(pInherits, false, OUString());
46 AstInterface::~AstInterface()
50 AstInterface::DoubleDeclarations AstInterface::checkInheritedInterfaceClashes(
51 AstInterface const * ifc, bool optional) const
53 DoubleDeclarations doubleDecls;
54 std::set< OString > seen;
55 checkInheritedInterfaceClashes(
56 doubleDecls, seen, ifc, true, optional, optional);
57 return doubleDecls;
60 void AstInterface::addInheritedInterface(
61 AstType const * ifc, bool optional, OUString const & documentation)
63 m_inheritedInterfaces.push_back(
64 InheritedInterface(ifc, optional, documentation));
65 if (!optional) {
66 ++m_mandatoryInterfaces;
68 AstInterface const * resolved = resolveInterfaceTypedefs(ifc);
69 addVisibleInterface(resolved, true, optional);
70 if (optional) {
71 addOptionalVisibleMembers(resolved);
75 AstInterface::DoubleMemberDeclarations AstInterface::checkMemberClashes(
76 AstDeclaration const * member) const
78 DoubleMemberDeclarations doubleMembers;
79 checkMemberClashes(doubleMembers, member, true);
80 return doubleMembers;
83 void AstInterface::addMember(AstDeclaration /*TODO: const*/ * member) {
84 addDeclaration(member);
85 m_visibleMembers.insert(
86 VisibleMembers::value_type(
87 member->getLocalName(), VisibleMember(member)));
90 void AstInterface::forwardDefined(AstInterface const & def)
92 setImported(def.isImported());
93 setInMainfile(def.isInMainfile());
94 setLineNumber(def.getLineNumber());
95 setFileName(def.getFileName());
96 setDocumentation(def.getDocumentation());
97 m_inheritedInterfaces = def.m_inheritedInterfaces;
98 m_mandatoryInterfaces = def.m_mandatoryInterfaces;
99 m_bIsDefined = true;
102 bool AstInterface::dump(RegistryKey& rKey)
104 if ( !isDefined() )
105 return true;
107 RegistryKey localKey;
108 if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey) != RegError::NO_ERROR)
110 fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
111 idlc()->getOptions()->getProgramName().getStr(),
112 getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
113 return false;
116 if (m_mandatoryInterfaces > SAL_MAX_UINT16
117 || m_inheritedInterfaces.size() - m_mandatoryInterfaces
118 > SAL_MAX_UINT16)
120 fprintf(
121 stderr, "%s: interface %s has too many direct base interfaces\n",
122 idlc()->getOptions()->getProgramName().getStr(),
123 getScopedName().getStr());
124 return false;
126 sal_uInt16 nBaseTypes = static_cast< sal_uInt16 >(m_mandatoryInterfaces);
127 sal_uInt16 nAttributes = 0;
128 sal_uInt16 nMethods = 0;
129 sal_uInt16 nReferences = static_cast< sal_uInt16 >(
130 m_inheritedInterfaces.size() - m_mandatoryInterfaces);
131 typereg_Version version
132 = (nBaseTypes <= 1 && nReferences == 0 && !m_bPublished
133 ? TYPEREG_VERSION_0 : TYPEREG_VERSION_1);
134 for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
135 ++i)
137 switch ((*i)->getNodeType()) {
138 case NT_attribute:
140 if (!increment(&nAttributes, "attributes")) {
141 return false;
143 AstAttribute * attr = static_cast<AstAttribute *>(*i);
144 if (attr->isBound()) {
145 version = TYPEREG_VERSION_1;
147 DeclList::size_type getCount = attr->getGetExceptionCount();
148 if (getCount > SAL_MAX_UINT16) {
149 fprintf(
150 stderr,
151 ("%s: raises clause of getter for attribute %s of"
152 " interface %s is too long\n"),
153 idlc()->getOptions()->getProgramName().getStr(),
154 (*i)->getLocalName().getStr(),
155 getScopedName().getStr());
156 return false;
158 if (getCount > 0) {
159 version = TYPEREG_VERSION_1;
160 if (!increment(&nMethods, "attributes")) {
161 return false;
164 DeclList::size_type setCount = attr->getSetExceptionCount();
165 if (setCount > SAL_MAX_UINT16) {
166 fprintf(
167 stderr,
168 ("%s: raises clause of setter for attribute %s of"
169 " interface %s is too long\n"),
170 idlc()->getOptions()->getProgramName().getStr(),
171 (*i)->getLocalName().getStr(),
172 getScopedName().getStr());
173 return false;
175 if (setCount > 0) {
176 version = TYPEREG_VERSION_1;
177 if (!increment(&nMethods, "attributes")) {
178 return false;
181 break;
184 case NT_operation:
185 if (!increment(&nMethods, "methods")) {
186 return false;
188 break;
190 default:
191 OSL_ASSERT(false);
192 break;
196 OUString emptyStr;
197 typereg::Writer aBlob(
198 version, getDocumentation(), emptyStr, RT_TYPE_INTERFACE, m_bPublished,
199 OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), nBaseTypes,
200 nAttributes, nMethods, nReferences);
202 sal_uInt16 superTypeIndex = 0;
203 sal_uInt16 referenceIndex = 0;
204 for (InheritedInterfaces::iterator i = m_inheritedInterfaces.begin();
205 i != m_inheritedInterfaces.end(); ++i)
207 if (i->isOptional()) {
208 aBlob.setReferenceData(
209 referenceIndex++, i->getDocumentation(), RTReferenceType::SUPPORTS,
210 RTFieldAccess::OPTIONAL,
211 OStringToOUString(
212 i->getInterface()->getRelativName(),
213 RTL_TEXTENCODING_UTF8));
214 } else {
215 aBlob.setSuperTypeName(
216 superTypeIndex++,
217 OStringToOUString(
218 i->getInterface()->getRelativName(),
219 RTL_TEXTENCODING_UTF8));
223 sal_uInt16 attributeIndex = 0;
224 sal_uInt16 methodIndex = 0;
225 for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
226 ++i)
228 switch ((*i)->getNodeType()) {
229 case NT_attribute:
230 static_cast<AstAttribute *>(*i)->dumpBlob(
231 aBlob, attributeIndex++, &methodIndex);
232 break;
234 case NT_operation:
235 static_cast<AstOperation *>(*i)->dumpBlob(aBlob, methodIndex++);
236 break;
238 default:
239 OSL_ASSERT(false);
240 break;
244 sal_uInt32 aBlobSize;
245 void const * pBlob = aBlob.getBlob(&aBlobSize);
247 if (localKey.setValue(emptyStr, RegValueType::BINARY, const_cast<RegValue>(pBlob), aBlobSize) != RegError::NO_ERROR)
249 fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
250 idlc()->getOptions()->getProgramName().getStr(),
251 getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
252 return false;
255 return true;
258 void AstInterface::checkInheritedInterfaceClashes(
259 DoubleDeclarations & doubleDeclarations,
260 std::set< OString > & seenInterfaces, AstInterface const * ifc,
261 bool direct, bool optional, bool mainOptional) const
263 if (direct || optional
264 || seenInterfaces.insert(ifc->getScopedName()).second)
266 VisibleInterfaces::const_iterator visible(
267 m_visibleInterfaces.find(ifc->getScopedName()));
268 if (visible != m_visibleInterfaces.end()) {
269 switch (visible->second) {
270 case INTERFACE_INDIRECT_OPTIONAL:
271 if (direct && optional) {
272 doubleDeclarations.interfaces.push_back(ifc);
273 return;
275 break;
277 case INTERFACE_DIRECT_OPTIONAL:
278 if (direct || !mainOptional) {
279 doubleDeclarations.interfaces.push_back(ifc);
281 return;
283 case INTERFACE_INDIRECT_MANDATORY:
284 if (direct) {
285 doubleDeclarations.interfaces.push_back(ifc);
287 return;
289 case INTERFACE_DIRECT_MANDATORY:
290 if (direct || (!optional && !mainOptional)) {
291 doubleDeclarations.interfaces.push_back(ifc);
293 return;
296 if (direct || !optional) {
297 for (DeclList::const_iterator i(ifc->getIteratorBegin());
298 i != ifc->getIteratorEnd(); ++i)
300 checkMemberClashes(
301 doubleDeclarations.members, *i, !mainOptional);
303 for (InheritedInterfaces::const_iterator i(
304 ifc->m_inheritedInterfaces.begin());
305 i != ifc->m_inheritedInterfaces.end(); ++i)
307 checkInheritedInterfaceClashes(
308 doubleDeclarations, seenInterfaces, i->getResolved(),
309 false, i->isOptional(), mainOptional);
315 void AstInterface::checkMemberClashes(
316 DoubleMemberDeclarations & doubleMembers, AstDeclaration const * member,
317 bool checkOptional) const
319 VisibleMembers::const_iterator i(
320 m_visibleMembers.find(member->getLocalName()));
321 if (i != m_visibleMembers.end()) {
322 if (i->second.mandatory != 0) {
323 if (i->second.mandatory->getScopedName() != member->getScopedName())
325 DoubleMemberDeclaration d;
326 d.first = i->second.mandatory;
327 d.second = member;
328 doubleMembers.push_back(d);
330 } else if (checkOptional) {
331 for (VisibleMember::Optionals::const_iterator j(
332 i->second.optionals.begin());
333 j != i->second.optionals.end(); ++j)
335 if (j->second->getScopedName() != member->getScopedName()) {
336 DoubleMemberDeclaration d;
337 d.first = j->second;
338 d.second = member;
339 doubleMembers.push_back(d);
346 void AstInterface::addVisibleInterface(
347 AstInterface const * ifc, bool direct, bool optional)
349 InterfaceKind kind = optional
350 ? direct ? INTERFACE_DIRECT_OPTIONAL : INTERFACE_INDIRECT_OPTIONAL
351 : direct ? INTERFACE_DIRECT_MANDATORY : INTERFACE_INDIRECT_MANDATORY;
352 std::pair< VisibleInterfaces::iterator, bool > result(
353 m_visibleInterfaces.insert(
354 VisibleInterfaces::value_type(ifc->getScopedName(), kind)));
355 bool seen = !result.second
356 && result.first->second >= INTERFACE_INDIRECT_MANDATORY;
357 if (!result.second && kind > result.first->second) {
358 result.first->second = kind;
360 if (!optional && !seen) {
361 for (DeclList::const_iterator i(ifc->getIteratorBegin());
362 i != ifc->getIteratorEnd(); ++i)
364 m_visibleMembers.insert(
365 VisibleMembers::value_type(
366 (*i)->getLocalName(), VisibleMember(*i)));
368 for (InheritedInterfaces::const_iterator i(
369 ifc->m_inheritedInterfaces.begin());
370 i != ifc->m_inheritedInterfaces.end(); ++i)
372 addVisibleInterface(i->getResolved(), false, i->isOptional());
377 void AstInterface::addOptionalVisibleMembers(AstInterface const * ifc) {
378 for (DeclList::const_iterator i(ifc->getIteratorBegin());
379 i != ifc->getIteratorEnd(); ++i)
381 VisibleMembers::iterator visible(
382 m_visibleMembers.find((*i)->getLocalName()));
383 if (visible == m_visibleMembers.end()) {
384 visible = m_visibleMembers.insert(
385 VisibleMembers::value_type(
386 (*i)->getLocalName(), VisibleMember())).first;
388 if (visible->second.mandatory == 0) {
389 visible->second.optionals.insert(
390 VisibleMember::Optionals::value_type(ifc->getScopedName(), *i));
393 for (InheritedInterfaces::const_iterator i(
394 ifc->m_inheritedInterfaces.begin());
395 i != ifc->m_inheritedInterfaces.end(); ++i)
397 if (!i->isOptional()) {
398 addOptionalVisibleMembers(i->getResolved());
403 bool AstInterface::increment(sal_uInt16 * counter, char const * sort) const {
404 if (*counter == SAL_MAX_UINT16) {
405 fprintf(
406 stderr, "%s: interface %s has too many direct %s\n",
407 idlc()->getOptions()->getProgramName().getStr(),
408 getScopedName().getStr(), sort);
409 return false;
411 ++*counter;
412 return true;
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */