LCOV - code coverage report
Current view: top level - libreoffice/idlc/source - astinterface.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 148 196 75.5 %
Date: 2012-12-27 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             : using namespace ::rtl;
      29             : 
      30       35298 : AstInterface::AstInterface(const ::rtl::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(sal_False)
      38             :     , m_bForwardedInSameFile(sal_False)
      39       35298 :     , m_bSingleInheritance(pInherits != 0)
      40             : {
      41       35298 :     if (pInherits != 0) {
      42       21440 :         addInheritedInterface(pInherits, false, rtl::OUString());
      43             :     }
      44       35298 : }
      45             : 
      46        3906 : AstInterface::~AstInterface()
      47             : {
      48        3906 : }
      49             : 
      50        2094 : AstInterface::DoubleDeclarations AstInterface::checkInheritedInterfaceClashes(
      51             :     AstInterface const * ifc, bool optional) const
      52             : {
      53        2094 :     DoubleDeclarations doubleDecls;
      54        2094 :     std::set< rtl::OString > seen;
      55             :     checkInheritedInterfaceClashes(
      56        2094 :         doubleDecls, seen, ifc, true, optional, optional);
      57        2094 :     return doubleDecls;
      58             : }
      59             : 
      60       23534 : void AstInterface::addInheritedInterface(
      61             :     AstType const * ifc, bool optional, rtl::OUString const & documentation)
      62             : {
      63             :     m_inheritedInterfaces.push_back(
      64       23534 :         InheritedInterface(ifc, optional, documentation));
      65       23534 :     if (!optional) {
      66       23514 :         ++m_mandatoryInterfaces;
      67             :     }
      68       23534 :     AstInterface const * resolved = resolveInterfaceTypedefs(ifc);
      69       23534 :     addVisibleInterface(resolved, true, optional);
      70       23534 :     if (optional) {
      71          20 :         addOptionalVisibleMembers(resolved);
      72             :     }
      73       23534 : }
      74             : 
      75      107282 : AstInterface::DoubleMemberDeclarations AstInterface::checkMemberClashes(
      76             :     AstDeclaration const * member) const
      77             : {
      78      107282 :     DoubleMemberDeclarations doubleMembers;
      79      107282 :     checkMemberClashes(doubleMembers, member, true);
      80      107282 :     return doubleMembers;
      81             : }
      82             : 
      83      107696 : void AstInterface::addMember(AstDeclaration /*TODO: const*/ * member) {
      84      107696 :     addDeclaration(member);
      85             :     m_visibleMembers.insert(
      86             :         VisibleMembers::value_type(
      87      107696 :             member->getLocalName(), VisibleMember(member)));
      88      107696 : }
      89             : 
      90         880 : void AstInterface::forwardDefined(AstInterface const & def)
      91             : {
      92         880 :     setImported(def.isImported());
      93         880 :     setInMainfile(def.isInMainfile());
      94         880 :     setLineNumber(def.getLineNumber());
      95         880 :     setFileName(def.getFileName());
      96         880 :     setDocumentation(def.getDocumentation());
      97         880 :     m_inheritedInterfaces = def.m_inheritedInterfaces;
      98         880 :     m_mandatoryInterfaces = def.m_mandatoryInterfaces;
      99         880 :     m_bIsDefined = true;
     100         880 : }
     101             : 
     102        2522 : sal_Bool AstInterface::dump(RegistryKey& rKey)
     103             : {
     104        2522 :     if ( !isDefined() )
     105         657 :         return sal_True;
     106             : 
     107        1865 :     RegistryKey localKey;
     108        1865 :     if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey))
     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 sal_False;
     114             :     }
     115             : 
     116        3730 :     if (m_mandatoryInterfaces > SAL_MAX_UINT16
     117        1865 :         || 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        1865 :     sal_uInt16 nBaseTypes = static_cast< sal_uInt16 >(m_mandatoryInterfaces);
     127        1865 :     sal_uInt16 nAttributes = 0;
     128        1865 :     sal_uInt16 nMethods = 0;
     129             :     sal_uInt16 nReferences = static_cast< sal_uInt16 >(
     130        1865 :         m_inheritedInterfaces.size() - m_mandatoryInterfaces);
     131             :     typereg_Version version
     132             :         = (nBaseTypes <= 1 && nReferences == 0 && !m_bPublished
     133        1865 :            ? TYPEREG_VERSION_0 : TYPEREG_VERSION_1);
     134        9253 :     for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
     135             :           ++i)
     136             :     {
     137        7388 :         switch ((*i)->getNodeType()) {
     138             :         case NT_attribute:
     139             :             {
     140        1104 :                 if (!increment(&nAttributes, "attributes")) {
     141           0 :                     return false;
     142             :                 }
     143        1104 :                 AstAttribute * attr = (AstAttribute *)(*i);
     144        1104 :                 if (attr->isBound()) {
     145         153 :                     version = TYPEREG_VERSION_1;
     146             :                 }
     147        1104 :                 DeclList::size_type getCount = attr->getGetExceptionCount();
     148        1104 :                 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        1104 :                 if (getCount > 0) {
     159          86 :                     version = TYPEREG_VERSION_1;
     160          86 :                     if (!increment(&nMethods, "attributes")) {
     161           0 :                         return false;
     162             :                     }
     163             :                 }
     164        1104 :                 DeclList::size_type setCount = attr->getSetExceptionCount();
     165        1104 :                 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        1104 :                 if (setCount > 0) {
     176         109 :                     version = TYPEREG_VERSION_1;
     177         109 :                     if (!increment(&nMethods, "attributes")) {
     178           0 :                         return false;
     179             :                     }
     180             :                 }
     181        1104 :                 break;
     182             :             }
     183             : 
     184             :         case NT_operation:
     185        6284 :             if (!increment(&nMethods, "methods")) {
     186           0 :                 return false;
     187             :             }
     188        6284 :             break;
     189             : 
     190             :         default:
     191             :             OSL_ASSERT(false);
     192           0 :             break;
     193             :         }
     194             :     }
     195             : 
     196        1865 :     OUString emptyStr;
     197             :     typereg::Writer aBlob(
     198        1865 :         version, getDocumentation(), emptyStr, RT_TYPE_INTERFACE, m_bPublished,
     199        1865 :         OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), nBaseTypes,
     200        5595 :         nAttributes, nMethods, nReferences);
     201             : 
     202        1865 :     sal_uInt16 superTypeIndex = 0;
     203        1865 :     sal_uInt16 referenceIndex = 0;
     204       11796 :     for (InheritedInterfaces::iterator i = m_inheritedInterfaces.begin();
     205        7864 :           i != m_inheritedInterfaces.end(); ++i)
     206             :     {
     207        2067 :         if (i->isOptional()) {
     208             :             aBlob.setReferenceData(
     209             :                 referenceIndex++, i->getDocumentation(), RT_REF_SUPPORTS,
     210             :                 RT_ACCESS_OPTIONAL,
     211             :                 OStringToOUString(
     212           6 :                     i->getInterface()->getRelativName(),
     213           6 :                     RTL_TEXTENCODING_UTF8));
     214             :         } else {
     215             :             aBlob.setSuperTypeName(
     216             :                 superTypeIndex++,
     217             :                 OStringToOUString(
     218        2061 :                     i->getInterface()->getRelativName(),
     219        2061 :                     RTL_TEXTENCODING_UTF8));
     220             :         }
     221             :     }
     222             : 
     223        1865 :     sal_uInt16 attributeIndex = 0;
     224        1865 :     sal_uInt16 methodIndex = 0;
     225        9253 :     for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
     226             :           ++i)
     227             :     {
     228        7388 :         switch ((*i)->getNodeType()) {
     229             :         case NT_attribute:
     230             : 
     231        1104 :             ((AstAttribute *)(*i))->dumpBlob(
     232        2208 :                 aBlob, attributeIndex++, &methodIndex);
     233        1104 :             break;
     234             : 
     235             :         case NT_operation:
     236        6284 :             ((AstOperation *)(*i))->dumpBlob(aBlob, methodIndex++);
     237        6284 :             break;
     238             : 
     239             :         default:
     240             :             OSL_ASSERT(false);
     241           0 :             break;
     242             :         }
     243             :     }
     244             : 
     245             :     sal_uInt32 aBlobSize;
     246        1865 :     void const * pBlob = aBlob.getBlob(&aBlobSize);
     247             : 
     248        1865 :     if (localKey.setValue(emptyStr, RG_VALUETYPE_BINARY, (RegValue)pBlob, aBlobSize))
     249             :     {
     250             :         fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
     251           0 :                 idlc()->getOptions()->getProgramName().getStr(),
     252           0 :                 getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
     253           0 :         return sal_False;
     254             :     }
     255             : 
     256        1865 :     return true;
     257             : }
     258             : 
     259        5164 : void AstInterface::checkInheritedInterfaceClashes(
     260             :     DoubleDeclarations & doubleDeclarations,
     261             :     std::set< rtl::OString > & seenInterfaces, AstInterface const * ifc,
     262             :     bool direct, bool optional, bool mainOptional) const
     263             : {
     264        8234 :     if (direct || optional
     265        3070 :         || seenInterfaces.insert(ifc->getScopedName()).second)
     266             :     {
     267             :         VisibleInterfaces::const_iterator visible(
     268        4901 :             m_visibleInterfaces.find(ifc->getScopedName()));
     269        4901 :         if (visible != m_visibleInterfaces.end()) {
     270         854 :             switch (visible->second) {
     271             :             case INTERFACE_INDIRECT_OPTIONAL:
     272           0 :                 if (direct && optional) {
     273           0 :                     doubleDeclarations.interfaces.push_back(ifc);
     274             :                     return;
     275             :                 }
     276           0 :                 break;
     277             : 
     278             :             case INTERFACE_DIRECT_OPTIONAL:
     279           0 :                 if (direct || !mainOptional) {
     280           0 :                     doubleDeclarations.interfaces.push_back(ifc);
     281             :                 }
     282             :                 return;
     283             : 
     284             :             case INTERFACE_INDIRECT_MANDATORY:
     285         854 :                 if (direct) {
     286           0 :                     doubleDeclarations.interfaces.push_back(ifc);
     287             :                 }
     288             :                 return;
     289             : 
     290             :             case INTERFACE_DIRECT_MANDATORY:
     291           0 :                 if (direct || (!optional && !mainOptional)) {
     292           0 :                     doubleDeclarations.interfaces.push_back(ifc);
     293             :                 }
     294             :                 return;
     295             :             }
     296             :         }
     297        4047 :         if (direct || !optional) {
     298       53688 :             for (DeclList::const_iterator i(ifc->getIteratorBegin());
     299       35792 :                   i != ifc->getIteratorEnd(); ++i)
     300             :             {
     301             :                 checkMemberClashes(
     302       13849 :                     doubleDeclarations.members, *i, !mainOptional);
     303             :             }
     304       21351 :             for (InheritedInterfaces::const_iterator i(
     305        4047 :                       ifc->m_inheritedInterfaces.begin());
     306       14234 :                   i != ifc->m_inheritedInterfaces.end(); ++i)
     307             :             {
     308             :                 checkInheritedInterfaceClashes(
     309             :                     doubleDeclarations, seenInterfaces, i->getResolved(),
     310        3070 :                     false, i->isOptional(), mainOptional);
     311             :             }
     312             :         }
     313             :     }
     314             : }
     315             : 
     316      121131 : void AstInterface::checkMemberClashes(
     317             :     DoubleMemberDeclarations & doubleMembers, AstDeclaration const * member,
     318             :     bool checkOptional) const
     319             : {
     320             :     VisibleMembers::const_iterator i(
     321      121131 :         m_visibleMembers.find(member->getLocalName()));
     322      121131 :     if (i != m_visibleMembers.end()) {
     323           0 :         if (i->second.mandatory != 0) {
     324           0 :             if (i->second.mandatory->getScopedName() != member->getScopedName())
     325             :             {
     326             :                 DoubleMemberDeclaration d;
     327           0 :                 d.first = i->second.mandatory;
     328           0 :                 d.second = member;
     329           0 :                 doubleMembers.push_back(d);
     330             :             }
     331           0 :         } else if (checkOptional) {
     332           0 :             for (VisibleMember::Optionals::const_iterator j(
     333           0 :                      i->second.optionals.begin());
     334           0 :                  j != i->second.optionals.end(); ++j)
     335             :             {
     336           0 :                 if (j->second->getScopedName() != member->getScopedName()) {
     337             :                     DoubleMemberDeclaration d;
     338           0 :                     d.first = j->second;
     339           0 :                     d.second = member;
     340           0 :                     doubleMembers.push_back(d);
     341             :                 }
     342             :             }
     343             :         }
     344             :     }
     345      121131 : }
     346             : 
     347       36109 : void AstInterface::addVisibleInterface(
     348             :     AstInterface const * ifc, bool direct, bool optional)
     349             : {
     350             :     InterfaceKind kind = optional
     351             :         ? direct ? INTERFACE_DIRECT_OPTIONAL : INTERFACE_INDIRECT_OPTIONAL
     352       36109 :         : direct ? INTERFACE_DIRECT_MANDATORY : INTERFACE_INDIRECT_MANDATORY;
     353             :     std::pair< VisibleInterfaces::iterator, bool > result(
     354             :         m_visibleInterfaces.insert(
     355       36109 :             VisibleInterfaces::value_type(ifc->getScopedName(), kind)));
     356       36109 :     bool seen = !result.second
     357       36109 :         && result.first->second >= INTERFACE_INDIRECT_MANDATORY;
     358       36109 :     if (!result.second && kind > result.first->second) {
     359           0 :         result.first->second = kind;
     360             :     }
     361       36109 :     if (!optional && !seen) {
     362      406245 :         for (DeclList::const_iterator i(ifc->getIteratorBegin());
     363      270830 :               i != ifc->getIteratorEnd(); ++i)
     364             :         {
     365             :             m_visibleMembers.insert(
     366             :                 VisibleMembers::value_type(
     367      100432 :                     (*i)->getLocalName(), VisibleMember(*i)));
     368             :         }
     369      142674 :         for (InheritedInterfaces::const_iterator i(
     370       34983 :                   ifc->m_inheritedInterfaces.begin());
     371       95116 :               i != ifc->m_inheritedInterfaces.end(); ++i)
     372             :         {
     373       12575 :             addVisibleInterface(i->getResolved(), false, i->isOptional());
     374             :         }
     375             :     }
     376       36109 : }
     377             : 
     378          40 : void AstInterface::addOptionalVisibleMembers(AstInterface const * ifc) {
     379         594 :     for (DeclList::const_iterator i(ifc->getIteratorBegin());
     380         396 :           i != ifc->getIteratorEnd(); ++i)
     381             :     {
     382             :         VisibleMembers::iterator visible(
     383         158 :             m_visibleMembers.find((*i)->getLocalName()));
     384         158 :         if (visible == m_visibleMembers.end()) {
     385             :             visible = m_visibleMembers.insert(
     386             :                 VisibleMembers::value_type(
     387          98 :                     (*i)->getLocalName(), VisibleMember())).first;
     388             :         }
     389         158 :         if (visible->second.mandatory == 0) {
     390          98 :             visible->second.optionals.insert(
     391         196 :                 VisibleMember::Optionals::value_type(ifc->getScopedName(), *i));
     392             :         }
     393             :     }
     394         180 :     for (InheritedInterfaces::const_iterator i(
     395          40 :               ifc->m_inheritedInterfaces.begin());
     396         120 :           i != ifc->m_inheritedInterfaces.end(); ++i)
     397             :     {
     398          20 :         if (!i->isOptional()) {
     399          20 :             addOptionalVisibleMembers(i->getResolved());
     400             :         }
     401             :     }
     402          40 : }
     403             : 
     404        7583 : bool AstInterface::increment(sal_uInt16 * counter, char const * sort) const {
     405        7583 :     if (*counter == SAL_MAX_UINT16) {
     406             :         fprintf(
     407             :             stderr, "%s: interface %s has too many direct %s\n",
     408           0 :             idlc()->getOptions()->getProgramName().getStr(),
     409           0 :             getScopedName().getStr(), sort);
     410           0 :         return false;
     411             :     }
     412        7583 :     ++*counter;
     413        7583 :     return true;
     414             : }
     415             : 
     416             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10