LCOV - code coverage report
Current view: top level - idlc/source - astinterface.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 174 201 86.6 %
Date: 2015-06-13 12:38:46 Functions: 14 14 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11