LCOV - code coverage report
Current view: top level - idlc/source - astscope.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 127 175 72.6 %
Date: 2014-04-11 Functions: 9 11 81.8 %
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/astscope.hxx>
      21             : #include <idlc/astbasetype.hxx>
      22             : #include <idlc/astinterface.hxx>
      23             : #include <idlc/errorhandler.hxx>
      24             : 
      25             : 
      26             : using namespace ::rtl;
      27             : 
      28        1623 : bool isGlobal(const OString& scopedName)
      29             : {
      30        1623 :     return scopedName.isEmpty() || scopedName.startsWith(":");
      31             : }
      32             : 
      33        4126 : AstScope::AstScope(NodeType nodeType)
      34        4126 :     : m_nodeType(nodeType)
      35             : {
      36             : 
      37        4126 : }
      38             : 
      39         442 : AstScope::~AstScope()
      40             : {
      41             : 
      42         442 : }
      43             : 
      44       13959 : AstDeclaration* AstScope::addDeclaration(AstDeclaration* pDecl)
      45             : {
      46       13959 :     AstDeclaration* pDeclaration = NULL;
      47             : 
      48       13959 :     if ((pDeclaration = lookupForAdd(pDecl)) != NULL)
      49             :     {
      50           4 :         if ( pDecl->hasAncestor(pDeclaration) )
      51             :         {
      52           0 :             idlc()->error()->error2(EIDL_REDEF_SCOPE, pDecl, pDeclaration);
      53           0 :             return NULL;
      54             :         }
      55           7 :         if ( (pDecl->getNodeType() == pDeclaration->getNodeType()) &&
      56           3 :              (pDecl->getNodeType() == NT_sequence
      57           3 :               || pDecl->getNodeType() == NT_instantiated_struct) )
      58             :         {
      59           0 :             return pDeclaration;
      60             :         }
      61           8 :         if ( (pDeclaration->getNodeType() == NT_interface)
      62           0 :              && (pDecl->getNodeType() == NT_interface)
      63           4 :              && !((AstInterface*)pDeclaration)->isDefined() )
      64             :         {
      65           0 :             m_declarations.push_back(pDecl);
      66           0 :             return pDecl;
      67             :         }
      68           5 :         if ( (NT_service == m_nodeType) &&
      69           1 :              ( ((pDecl->getNodeType() == NT_interface_member)
      70           1 :                 && (pDeclaration->getNodeType() == NT_interface)) ||
      71           1 :                ((pDecl->getNodeType() == NT_service_member)
      72           0 :                 && (pDeclaration->getNodeType() == NT_service)) )
      73             :             )
      74             :         {
      75           0 :             m_declarations.push_back(pDecl);
      76           0 :             return pDecl;
      77             :         }
      78             : 
      79           4 :         idlc()->error()->error2(EIDL_REDEF_SCOPE, scopeAsDecl(this), pDecl);
      80           4 :         return NULL;
      81             :     }
      82             : 
      83       13955 :     m_declarations.push_back(pDecl);
      84       13955 :     return pDecl;
      85             : }
      86             : 
      87         384 : sal_uInt16 AstScope::getNodeCount(NodeType nodeType)
      88             : {
      89         384 :     DeclList::const_iterator iter = getIteratorBegin();
      90         384 :     DeclList::const_iterator end = getIteratorEnd();
      91         384 :     AstDeclaration* pDecl = NULL;
      92         384 :     sal_uInt16 count = 0;
      93             : 
      94        3663 :     while ( iter != end )
      95             :     {
      96        2895 :         pDecl = *iter;
      97        2895 :         if ( pDecl->getNodeType() == nodeType )
      98         171 :             count++;
      99        2895 :         ++iter;
     100             :     }
     101         384 :     return count;
     102             : }
     103             : 
     104        1623 : AstDeclaration* AstScope::lookupByName(const OString& scopedName)
     105             : {
     106        1623 :     AstDeclaration* pDecl = NULL;
     107        1623 :     AstScope*       pScope = NULL;
     108        1623 :     if (scopedName.isEmpty())
     109           0 :         return NULL;
     110             : 
     111             :     // If name starts with "::" start look up in global scope
     112        1623 :     if ( isGlobal(scopedName) )
     113             :     {
     114         454 :         pDecl = scopeAsDecl(this);
     115         454 :         if ( !pDecl )
     116           0 :             return NULL;
     117             : 
     118         454 :         pScope = pDecl->getScope();
     119             :         // If this is the global scope ...
     120         454 :         if ( !pScope )
     121             :         {
     122             :             // look up the scopedName part after "::"
     123         193 :             OString subName = scopedName.copy(2);
     124         193 :             pDecl = lookupByName(subName);
     125         193 :             return pDecl;
     126             :             //return pScope->lookupByName();
     127             :         }
     128             :         // OK, not global scope yet, so simply iterate with parent scope
     129         261 :         pDecl = pScope->lookupByName(scopedName);
     130         261 :         return pDecl;
     131             :     }
     132             : 
     133             :     // The name does not start with "::"
     134             :     // Look up in the local scope and start with the first scope
     135        1169 :     sal_Int32 nIndex = scopedName.indexOf(':');
     136        1169 :     OString firstScope =  nIndex > 0 ? scopedName.copy(0, nIndex) : scopedName;
     137        1169 :     bool    bFindFirstScope = true;
     138        1169 :     pDecl = lookupByNameLocal(firstScope);
     139        1169 :     if ( !pDecl )
     140             :     {
     141         596 :         bFindFirstScope = false;
     142             : 
     143             :         // OK, not found. Go down parent scope chain
     144         596 :         pDecl = scopeAsDecl(this);
     145         596 :         if ( pDecl )
     146             :         {
     147         596 :             pScope = pDecl->getScope();
     148         596 :             if ( pScope )
     149         369 :                    pDecl = pScope->lookupByName(scopedName);
     150             :             else
     151         227 :                 pDecl = NULL;
     152             : 
     153             :              // Special case for scope which is an interface. We
     154             :              // have to look in the inherited interfaces as well.
     155         596 :             if ( !pDecl )
     156             :             {
     157         299 :                 if (m_nodeType == NT_interface)
     158           0 :                     pDecl = lookupInInherited(scopedName);
     159             :             }
     160             :        }
     161             :     }
     162             : 
     163        1169 :     if ( bFindFirstScope && (firstScope != scopedName) )
     164             :     {
     165         224 :         sal_Int32 i = 0;
     166         224 :         sal_Int32 nOffset = 2;
     167         867 :         do
     168             :         {
     169         884 :             pScope = declAsScope(pDecl);
     170         884 :             if( pScope )
     171             :             {
     172         884 :                 pDecl = pScope->lookupByNameLocal(scopedName.getToken(nOffset, ':', i ));
     173         884 :                 nOffset = 1;
     174             :             }
     175         884 :             if( !pDecl )
     176          17 :                 break;
     177         867 :         } while( i != -1 );
     178             : 
     179         224 :         if ( !pDecl )
     180             :         {
     181             :             // last try if is not the global scope and the scopeName isn't specify global too
     182          17 :             pDecl = scopeAsDecl(this);
     183          17 :             if ( pDecl && (pDecl->getLocalName() != "") )
     184             :             {
     185           0 :                 pScope = pDecl->getScope();
     186           0 :                 if ( pScope )
     187           0 :                     pDecl = pScope->lookupByName(scopedName);
     188             :             } else
     189             :             {
     190          17 :                 pDecl = NULL;
     191             :             }
     192             :         }
     193             : 
     194             :     }
     195             : 
     196        1169 :     return pDecl;
     197             : }
     198             : 
     199       17693 : AstDeclaration* AstScope::lookupByNameLocal(const OString& name) const
     200             : {
     201       17693 :     DeclList::const_iterator iter(m_declarations.begin());
     202       17693 :     DeclList::const_iterator end(m_declarations.end());
     203       17693 :     AstDeclaration* pDecl = NULL;
     204             : 
     205      153652 :     while ( iter != end )
     206             :     {
     207      121317 :         pDecl = *iter;
     208      121317 :         if ( pDecl->getLocalName() == name )
     209        3051 :             return pDecl;
     210      118266 :         ++iter;
     211             :     }
     212       14642 :     return NULL;
     213             : }
     214             : 
     215           0 : AstDeclaration* AstScope::lookupInInherited(const OString& scopedName) const
     216             : {
     217           0 :     AstInterface* pInterface = (AstInterface*)this;
     218             : 
     219           0 :     if ( !pInterface )
     220           0 :         return NULL;
     221             : 
     222             :     // Can't look in an interface which was not yet defined
     223           0 :     if ( !pInterface->getScope() )
     224             :     {
     225           0 :         idlc()->error()->forwardLookupError(pInterface, scopedName);
     226             :     }
     227             : 
     228             :     // OK, loop through inherited interfaces. Stop when you find it
     229             :     AstInterface::InheritedInterfaces::const_iterator iter(
     230           0 :         pInterface->getAllInheritedInterfaces().begin());
     231             :     AstInterface::InheritedInterfaces::const_iterator end(
     232           0 :         pInterface->getAllInheritedInterfaces().end());
     233           0 :     while ( iter != end )
     234             :     {
     235           0 :         AstInterface const * resolved = iter->getResolved();
     236           0 :         AstDeclaration* pDecl = resolved->lookupByNameLocal(scopedName);
     237           0 :         if ( pDecl )
     238           0 :             return pDecl;
     239           0 :         pDecl = resolved->lookupInInherited(scopedName);
     240           0 :         if ( pDecl )
     241           0 :             return pDecl;
     242           0 :         ++iter;
     243             :     }
     244             :     // Not found
     245           0 :     return NULL;
     246             : }
     247             : 
     248        1500 : AstDeclaration* AstScope::lookupPrimitiveType(ExprType type)
     249             : {
     250        1500 :     AstDeclaration* pDecl = NULL;
     251        1500 :     AstScope*       pScope = NULL;
     252        1500 :     OString         typeName;
     253        1500 :     pDecl = scopeAsDecl(this);
     254        1500 :     if ( !pDecl )
     255           0 :         return NULL;
     256        1500 :     pScope = pDecl->getScope();
     257        1500 :     if ( pScope)
     258           0 :         return pScope->lookupPrimitiveType(type);
     259             : 
     260        1500 :     switch (type)
     261             :     {
     262             :         case ET_none:
     263             :             OSL_ASSERT(false);
     264           0 :             break;
     265             :         case ET_short:
     266           3 :             typeName = OString("short");
     267           3 :             break;
     268             :         case ET_ushort:
     269           5 :             typeName = OString("unsigned short");
     270           5 :             break;
     271             :         case ET_long:
     272         120 :             typeName = OString("long");
     273         120 :             break;
     274             :         case ET_ulong:
     275           1 :             typeName = OString("unsigned long");
     276           1 :             break;
     277             :         case ET_hyper:
     278           0 :             typeName = OString("hyper");
     279           0 :             break;
     280             :         case ET_uhyper:
     281           1 :             typeName = OString("unsigned hyper");
     282           1 :             break;
     283             :         case ET_float:
     284           0 :             typeName = OString("float");
     285           0 :             break;
     286             :         case ET_double:
     287           0 :             typeName = OString("double");
     288           0 :             break;
     289             :         case ET_char:
     290           1 :             typeName = OString("char");
     291           1 :             break;
     292             :         case ET_byte:
     293           0 :             typeName = OString("byte");
     294           0 :             break;
     295             :         case ET_boolean:
     296           0 :             typeName = OString("boolean");
     297           0 :             break;
     298             :         case ET_any:
     299         341 :             typeName = OString("any");
     300         341 :             break;
     301             :         case ET_void:
     302         698 :             typeName = OString("void");
     303         698 :             break;
     304             :         case ET_type:
     305         329 :             typeName = OString("type");
     306         329 :             break;
     307             :         case ET_string:
     308           1 :             typeName = OString("string");
     309           1 :             break;
     310             :     }
     311             : 
     312        1500 :     pDecl = lookupByNameLocal(typeName);
     313             : 
     314        1500 :     if ( pDecl && (pDecl->getNodeType() == NT_predefined) )
     315             :     {
     316        1500 :         AstBaseType* pBaseType = (AstBaseType*)pDecl;
     317             : 
     318        1500 :         if ( pBaseType->getExprType() == type )
     319        1500 :             return pDecl;
     320             :     }
     321             : 
     322           0 :     return NULL;
     323             : }
     324             : 
     325       14140 : AstDeclaration* AstScope::lookupForAdd(AstDeclaration* pDecl)
     326             : {
     327       14140 :     if ( !pDecl )
     328           0 :         return NULL;
     329             : 
     330       14140 :     AstDeclaration* pRetDecl = lookupByNameLocal(pDecl->getLocalName());
     331             : 
     332       14140 :    return pRetDecl;
     333             : }
     334             : 
     335             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10