Line data Source code
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 <idlc/astinterface.hxx>
21 : #include <idlc/astattribute.hxx>
22 : #include <idlc/astoperation.hxx>
23 : #include "idlc/idlc.hxx"
24 :
25 : #include <registry/version.h>
26 : #include <registry/writer.hxx>
27 :
28 1152 : AstInterface::AstInterface(const OString& name,
29 : AstInterface const * pInherits,
30 : AstScope* pScope)
31 : : AstType(NT_interface, name, pScope)
32 : , AstScope(NT_interface)
33 : , m_mandatoryInterfaces(0)
34 : , m_bIsDefined(false)
35 : , m_bForwarded(false)
36 : , m_bForwardedInSameFile(false)
37 1152 : , m_bSingleInheritance(pInherits != 0)
38 : {
39 1152 : if (pInherits != 0) {
40 42 : addInheritedInterface(pInherits, false, OUString());
41 : }
42 1152 : }
43 :
44 24 : AstInterface::~AstInterface()
45 : {
46 24 : }
47 :
48 538 : AstInterface::DoubleDeclarations AstInterface::checkInheritedInterfaceClashes(
49 : AstInterface const * ifc, bool optional) const
50 : {
51 538 : DoubleDeclarations doubleDecls;
52 1076 : std::set< OString > seen;
53 : checkInheritedInterfaceClashes(
54 538 : doubleDecls, seen, ifc, true, optional, optional);
55 1076 : return doubleDecls;
56 : }
57 :
58 542 : void AstInterface::addInheritedInterface(
59 : AstType const * ifc, bool optional, OUString const & documentation)
60 : {
61 : m_inheritedInterfaces.push_back(
62 542 : InheritedInterface(ifc, optional, documentation));
63 542 : if (!optional) {
64 476 : ++m_mandatoryInterfaces;
65 : }
66 542 : AstInterface const * resolved = resolveInterfaceTypedefs(ifc);
67 542 : addVisibleInterface(resolved, true, optional);
68 542 : if (optional) {
69 66 : addOptionalVisibleMembers(resolved);
70 : }
71 542 : }
72 :
73 174 : AstInterface::DoubleMemberDeclarations AstInterface::checkMemberClashes(
74 : AstDeclaration const * member) const
75 : {
76 174 : DoubleMemberDeclarations doubleMembers;
77 174 : checkMemberClashes(doubleMembers, member, true);
78 174 : return doubleMembers;
79 : }
80 :
81 2144 : void AstInterface::addMember(AstDeclaration /*TODO: const*/ * member) {
82 2144 : addDeclaration(member);
83 : m_visibleMembers.insert(
84 : VisibleMembers::value_type(
85 2144 : member->getLocalName(), VisibleMember(member)));
86 2144 : }
87 :
88 6 : void AstInterface::forwardDefined(AstInterface const & def)
89 : {
90 6 : setImported(def.isImported());
91 6 : setInMainfile(def.isInMainfile());
92 6 : setLineNumber(def.getLineNumber());
93 6 : setFileName(def.getFileName());
94 6 : setDocumentation(def.getDocumentation());
95 6 : m_inheritedInterfaces = def.m_inheritedInterfaces;
96 6 : m_mandatoryInterfaces = def.m_mandatoryInterfaces;
97 6 : m_bIsDefined = true;
98 6 : }
99 :
100 256 : bool AstInterface::dump(RegistryKey& rKey)
101 : {
102 256 : if ( !isDefined() )
103 0 : return true;
104 :
105 256 : RegistryKey localKey;
106 256 : if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey))
107 : {
108 : fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
109 0 : idlc()->getOptions()->getProgramName().getStr(),
110 0 : getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
111 0 : return false;
112 : }
113 :
114 512 : if (m_mandatoryInterfaces > SAL_MAX_UINT16
115 256 : || m_inheritedInterfaces.size() - m_mandatoryInterfaces
116 : > SAL_MAX_UINT16)
117 : {
118 : fprintf(
119 : stderr, "%s: interface %s has too many direct base interfaces\n",
120 0 : idlc()->getOptions()->getProgramName().getStr(),
121 0 : getScopedName().getStr());
122 0 : return false;
123 : }
124 256 : sal_uInt16 nBaseTypes = static_cast< sal_uInt16 >(m_mandatoryInterfaces);
125 256 : sal_uInt16 nAttributes = 0;
126 256 : sal_uInt16 nMethods = 0;
127 : sal_uInt16 nReferences = static_cast< sal_uInt16 >(
128 256 : m_inheritedInterfaces.size() - m_mandatoryInterfaces);
129 : typereg_Version version
130 250 : = (nBaseTypes <= 1 && nReferences == 0 && !m_bPublished
131 468 : ? TYPEREG_VERSION_0 : TYPEREG_VERSION_1);
132 340 : for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
133 : ++i)
134 : {
135 84 : switch ((*i)->getNodeType()) {
136 : case NT_attribute:
137 : {
138 46 : if (!increment(&nAttributes, "attributes")) {
139 0 : return false;
140 : }
141 46 : AstAttribute * attr = static_cast<AstAttribute *>(*i);
142 46 : if (attr->isBound()) {
143 8 : version = TYPEREG_VERSION_1;
144 : }
145 46 : DeclList::size_type getCount = attr->getGetExceptionCount();
146 46 : if (getCount > SAL_MAX_UINT16) {
147 : fprintf(
148 : stderr,
149 : ("%s: raises clause of getter for attribute %s of"
150 : " interface %s is too long\n"),
151 0 : idlc()->getOptions()->getProgramName().getStr(),
152 0 : (*i)->getLocalName().getStr(),
153 0 : getScopedName().getStr());
154 0 : return false;
155 : }
156 46 : if (getCount > 0) {
157 18 : version = TYPEREG_VERSION_1;
158 18 : if (!increment(&nMethods, "attributes")) {
159 0 : return false;
160 : }
161 : }
162 46 : DeclList::size_type setCount = attr->getSetExceptionCount();
163 46 : if (setCount > SAL_MAX_UINT16) {
164 : fprintf(
165 : stderr,
166 : ("%s: raises clause of setter for attribute %s of"
167 : " interface %s is too long\n"),
168 0 : idlc()->getOptions()->getProgramName().getStr(),
169 0 : (*i)->getLocalName().getStr(),
170 0 : getScopedName().getStr());
171 0 : return false;
172 : }
173 46 : if (setCount > 0) {
174 8 : version = TYPEREG_VERSION_1;
175 8 : if (!increment(&nMethods, "attributes")) {
176 0 : return false;
177 : }
178 : }
179 46 : break;
180 : }
181 :
182 : case NT_operation:
183 38 : if (!increment(&nMethods, "methods")) {
184 0 : return false;
185 : }
186 38 : break;
187 :
188 : default:
189 : OSL_ASSERT(false);
190 0 : break;
191 : }
192 : }
193 :
194 256 : OUString emptyStr;
195 : typereg::Writer aBlob(
196 256 : version, getDocumentation(), emptyStr, RT_TYPE_INTERFACE, m_bPublished,
197 512 : OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), nBaseTypes,
198 1024 : nAttributes, nMethods, nReferences);
199 :
200 256 : sal_uInt16 superTypeIndex = 0;
201 256 : sal_uInt16 referenceIndex = 0;
202 1686 : for (InheritedInterfaces::iterator i = m_inheritedInterfaces.begin();
203 1124 : i != m_inheritedInterfaces.end(); ++i)
204 : {
205 306 : if (i->isOptional()) {
206 : aBlob.setReferenceData(
207 : referenceIndex++, i->getDocumentation(), RT_REF_SUPPORTS,
208 : RT_ACCESS_OPTIONAL,
209 : OStringToOUString(
210 50 : i->getInterface()->getRelativName(),
211 50 : RTL_TEXTENCODING_UTF8));
212 : } else {
213 : aBlob.setSuperTypeName(
214 : superTypeIndex++,
215 : OStringToOUString(
216 256 : i->getInterface()->getRelativName(),
217 256 : RTL_TEXTENCODING_UTF8));
218 : }
219 : }
220 :
221 256 : sal_uInt16 attributeIndex = 0;
222 256 : sal_uInt16 methodIndex = 0;
223 340 : for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
224 : ++i)
225 : {
226 84 : switch ((*i)->getNodeType()) {
227 : case NT_attribute:
228 46 : static_cast<AstAttribute *>(*i)->dumpBlob(
229 92 : aBlob, attributeIndex++, &methodIndex);
230 46 : break;
231 :
232 : case NT_operation:
233 38 : static_cast<AstOperation *>(*i)->dumpBlob(aBlob, methodIndex++);
234 38 : break;
235 :
236 : default:
237 : OSL_ASSERT(false);
238 0 : break;
239 : }
240 : }
241 :
242 : sal_uInt32 aBlobSize;
243 256 : void const * pBlob = aBlob.getBlob(&aBlobSize);
244 :
245 256 : if (localKey.setValue(emptyStr, RG_VALUETYPE_BINARY, (RegValue)pBlob, aBlobSize))
246 : {
247 : fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
248 0 : idlc()->getOptions()->getProgramName().getStr(),
249 0 : getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
250 0 : return false;
251 : }
252 :
253 512 : return true;
254 : }
255 :
256 712 : void AstInterface::checkInheritedInterfaceClashes(
257 : DoubleDeclarations & doubleDeclarations,
258 : std::set< OString > & seenInterfaces, AstInterface const * ifc,
259 : bool direct, bool optional, bool mainOptional) const
260 : {
261 886 : if (direct || optional
262 870 : || seenInterfaces.insert(ifc->getScopedName()).second)
263 : {
264 : VisibleInterfaces::const_iterator visible(
265 712 : m_visibleInterfaces.find(ifc->getScopedName()));
266 712 : if (visible != m_visibleInterfaces.end()) {
267 52 : switch (visible->second) {
268 : case INTERFACE_INDIRECT_OPTIONAL:
269 4 : if (direct && optional) {
270 2 : doubleDeclarations.interfaces.push_back(ifc);
271 52 : return;
272 : }
273 2 : break;
274 :
275 : case INTERFACE_DIRECT_OPTIONAL:
276 12 : if (direct || !mainOptional) {
277 8 : doubleDeclarations.interfaces.push_back(ifc);
278 : }
279 12 : return;
280 :
281 : case INTERFACE_INDIRECT_MANDATORY:
282 20 : if (direct) {
283 6 : doubleDeclarations.interfaces.push_back(ifc);
284 : }
285 20 : return;
286 :
287 : case INTERFACE_DIRECT_MANDATORY:
288 16 : if (direct || (!optional && !mainOptional)) {
289 10 : doubleDeclarations.interfaces.push_back(ifc);
290 : }
291 16 : return;
292 : }
293 : }
294 662 : if (direct || !optional) {
295 6504 : for (DeclList::const_iterator i(ifc->getIteratorBegin());
296 4336 : i != ifc->getIteratorEnd(); ++i)
297 : {
298 : checkMemberClashes(
299 1514 : doubleDeclarations.members, *i, !mainOptional);
300 : }
301 2484 : for (InheritedInterfaces::const_iterator i(
302 654 : ifc->m_inheritedInterfaces.begin());
303 1656 : i != ifc->m_inheritedInterfaces.end(); ++i)
304 : {
305 : checkInheritedInterfaceClashes(
306 : doubleDeclarations, seenInterfaces, i->getResolved(),
307 174 : false, i->isOptional(), mainOptional);
308 : }
309 : }
310 : }
311 : }
312 :
313 1688 : void AstInterface::checkMemberClashes(
314 : DoubleMemberDeclarations & doubleMembers, AstDeclaration const * member,
315 : bool checkOptional) const
316 : {
317 : VisibleMembers::const_iterator i(
318 1688 : m_visibleMembers.find(member->getLocalName()));
319 1688 : if (i != m_visibleMembers.end()) {
320 210 : if (i->second.mandatory != 0) {
321 18 : if (i->second.mandatory->getScopedName() != member->getScopedName())
322 : {
323 : DoubleMemberDeclaration d;
324 12 : d.first = i->second.mandatory;
325 12 : d.second = member;
326 12 : doubleMembers.push_back(d);
327 : }
328 192 : } else if (checkOptional) {
329 960 : for (VisibleMember::Optionals::const_iterator j(
330 160 : i->second.optionals.begin());
331 640 : j != i->second.optionals.end(); ++j)
332 : {
333 160 : if (j->second->getScopedName() != member->getScopedName()) {
334 : DoubleMemberDeclaration d;
335 4 : d.first = j->second;
336 4 : d.second = member;
337 4 : doubleMembers.push_back(d);
338 : }
339 : }
340 : }
341 : }
342 1688 : }
343 :
344 654 : void AstInterface::addVisibleInterface(
345 : AstInterface const * ifc, bool direct, bool optional)
346 : {
347 : InterfaceKind kind = optional
348 : ? direct ? INTERFACE_DIRECT_OPTIONAL : INTERFACE_INDIRECT_OPTIONAL
349 654 : : direct ? INTERFACE_DIRECT_MANDATORY : INTERFACE_INDIRECT_MANDATORY;
350 : std::pair< VisibleInterfaces::iterator, bool > result(
351 : m_visibleInterfaces.insert(
352 654 : VisibleInterfaces::value_type(ifc->getScopedName(), kind)));
353 654 : bool seen = !result.second
354 654 : && result.first->second >= INTERFACE_INDIRECT_MANDATORY;
355 654 : if (!result.second && kind > result.first->second) {
356 2 : result.first->second = kind;
357 : }
358 654 : if (!optional && !seen) {
359 5982 : for (DeclList::const_iterator i(ifc->getIteratorBegin());
360 3988 : i != ifc->getIteratorEnd(); ++i)
361 : {
362 : m_visibleMembers.insert(
363 : VisibleMembers::value_type(
364 1418 : (*i)->getLocalName(), VisibleMember(*i)));
365 : }
366 2064 : for (InheritedInterfaces::const_iterator i(
367 576 : ifc->m_inheritedInterfaces.begin());
368 1376 : i != ifc->m_inheritedInterfaces.end(); ++i)
369 : {
370 112 : addVisibleInterface(i->getResolved(), false, i->isOptional());
371 : }
372 : }
373 654 : }
374 :
375 148 : void AstInterface::addOptionalVisibleMembers(AstInterface const * ifc) {
376 1062 : for (DeclList::const_iterator i(ifc->getIteratorBegin());
377 708 : i != ifc->getIteratorEnd(); ++i)
378 : {
379 : VisibleMembers::iterator visible(
380 206 : m_visibleMembers.find((*i)->getLocalName()));
381 206 : if (visible == m_visibleMembers.end()) {
382 : visible = m_visibleMembers.insert(
383 : VisibleMembers::value_type(
384 150 : (*i)->getLocalName(), VisibleMember())).first;
385 : }
386 206 : if (visible->second.mandatory == 0) {
387 188 : visible->second.optionals.insert(
388 376 : VisibleMember::Optionals::value_type(ifc->getScopedName(), *i));
389 : }
390 : }
391 714 : for (InheritedInterfaces::const_iterator i(
392 148 : ifc->m_inheritedInterfaces.begin());
393 476 : i != ifc->m_inheritedInterfaces.end(); ++i)
394 : {
395 90 : if (!i->isOptional()) {
396 82 : addOptionalVisibleMembers(i->getResolved());
397 : }
398 : }
399 148 : }
400 :
401 110 : bool AstInterface::increment(sal_uInt16 * counter, char const * sort) const {
402 110 : if (*counter == SAL_MAX_UINT16) {
403 : fprintf(
404 : stderr, "%s: interface %s has too many direct %s\n",
405 0 : idlc()->getOptions()->getProgramName().getStr(),
406 0 : getScopedName().getStr(), sort);
407 0 : return false;
408 : }
409 110 : ++*counter;
410 110 : return true;
411 : }
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|