tools/icheck/parser/src/shared/cplusplus/Names.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
       
    42 //
       
    43 // Permission is hereby granted, free of charge, to any person obtaining a copy
       
    44 // of this software and associated documentation files (the "Software"), to deal
       
    45 // in the Software without restriction, including without limitation the rights
       
    46 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    47 // copies of the Software, and to permit persons to whom the Software is
       
    48 // furnished to do so, subject to the following conditions:
       
    49 //
       
    50 // The above copyright notice and this permission notice shall be included in
       
    51 // all copies or substantial portions of the Software.
       
    52 //
       
    53 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    54 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    55 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    56 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    57 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    58 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    59 // THE SOFTWARE.
       
    60 
       
    61 #include "Names.h"
       
    62 #include "NameVisitor.h"
       
    63 #include "Literals.h"
       
    64 #include <cstring>
       
    65 #include <cassert>
       
    66 #include <algorithm>
       
    67 
       
    68 using namespace CPlusPlus;
       
    69 
       
    70 QualifiedNameId::~QualifiedNameId()
       
    71 { }
       
    72 
       
    73 void QualifiedNameId::accept0(NameVisitor *visitor) const
       
    74 { visitor->visit(this); }
       
    75 
       
    76 const Identifier *QualifiedNameId::identifier() const
       
    77 {
       
    78     if (const Name *u = unqualifiedNameId())
       
    79         return u->identifier();
       
    80 
       
    81     return 0;
       
    82 }
       
    83 
       
    84 unsigned QualifiedNameId::nameCount() const
       
    85 { return _names.size(); }
       
    86 
       
    87 const Name *QualifiedNameId::nameAt(unsigned index) const
       
    88 { return _names[index]; }
       
    89 
       
    90 bool QualifiedNameId::isGlobal() const
       
    91 { return _isGlobal; }
       
    92 
       
    93 const Name *QualifiedNameId::unqualifiedNameId() const
       
    94 {
       
    95     if (_names.empty())
       
    96         return 0;
       
    97 
       
    98     return _names.back();
       
    99 }
       
   100 
       
   101 bool QualifiedNameId::isEqualTo(const Name *other) const
       
   102 {
       
   103     const QualifiedNameId *q = other->asQualifiedNameId();
       
   104     if (! q)
       
   105         return false;
       
   106     else if (isGlobal() != q->isGlobal())
       
   107         return false;
       
   108     else {
       
   109         const unsigned count = nameCount();
       
   110         if (count != q->nameCount())
       
   111             return false;
       
   112         for (unsigned i = 0; i < count; ++i) {
       
   113             const Name *l = nameAt(i);
       
   114             const Name *r = q->nameAt(i);
       
   115             if (! l->isEqualTo(r))
       
   116                 return false;
       
   117         }
       
   118     }
       
   119     return true;
       
   120 }
       
   121 
       
   122 NameId::NameId(const Identifier *identifier)
       
   123     : _identifier(identifier)
       
   124 { }
       
   125 
       
   126 NameId::~NameId()
       
   127 { }
       
   128 
       
   129 void NameId::accept0(NameVisitor *visitor) const
       
   130 { visitor->visit(this); }
       
   131 
       
   132 const Identifier *NameId::identifier() const
       
   133 { return _identifier; }
       
   134 
       
   135 bool NameId::isEqualTo(const Name *other) const
       
   136 {
       
   137     const NameId *nameId = other->asNameId();
       
   138     if (! nameId)
       
   139         return false;
       
   140     const Identifier *l = identifier();
       
   141     const Identifier *r = nameId->identifier();
       
   142     return l->isEqualTo(r);
       
   143 }
       
   144 
       
   145 DestructorNameId::DestructorNameId(const Identifier *identifier)
       
   146     : _identifier(identifier)
       
   147 { }
       
   148 
       
   149 DestructorNameId::~DestructorNameId()
       
   150 { }
       
   151 
       
   152 void DestructorNameId::accept0(NameVisitor *visitor) const
       
   153 { visitor->visit(this); }
       
   154 
       
   155 const Identifier *DestructorNameId::identifier() const
       
   156 { return _identifier; }
       
   157 
       
   158 bool DestructorNameId::isEqualTo(const Name *other) const
       
   159 {
       
   160     const DestructorNameId *d = other->asDestructorNameId();
       
   161     if (! d)
       
   162         return false;
       
   163     const Identifier *l = identifier();
       
   164     const Identifier *r = d->identifier();
       
   165     return l->isEqualTo(r);
       
   166 }
       
   167 
       
   168 TemplateNameId::~TemplateNameId()
       
   169 { }
       
   170 
       
   171 void TemplateNameId::accept0(NameVisitor *visitor) const
       
   172 { visitor->visit(this); }
       
   173 
       
   174 const Identifier *TemplateNameId::identifier() const
       
   175 { return _identifier; }
       
   176 
       
   177 unsigned TemplateNameId::templateArgumentCount() const
       
   178 { return _templateArguments.size(); }
       
   179 
       
   180 const FullySpecifiedType &TemplateNameId::templateArgumentAt(unsigned index) const
       
   181 { return _templateArguments[index]; }
       
   182 
       
   183 bool TemplateNameId::isEqualTo(const Name *other) const
       
   184 {
       
   185     const TemplateNameId *t = other->asTemplateNameId();
       
   186     if (! t)
       
   187         return false;
       
   188     const Identifier *l = identifier();
       
   189     const Identifier *r = t->identifier();
       
   190     if (! l->isEqualTo(r))
       
   191         return false;
       
   192     if (templateArgumentCount() != t->templateArgumentCount())
       
   193         return false;
       
   194     for (unsigned i = 0; i < templateArgumentCount(); ++i) {
       
   195         const FullySpecifiedType &l = _templateArguments[i];
       
   196         const FullySpecifiedType &r = t->_templateArguments[i];
       
   197         if (! l.isEqualTo(r))
       
   198             return false;
       
   199     }
       
   200     return true;
       
   201 }
       
   202 
       
   203 OperatorNameId::OperatorNameId(int kind)
       
   204     : _kind(kind)
       
   205 { }
       
   206 
       
   207 OperatorNameId::~OperatorNameId()
       
   208 { }
       
   209 
       
   210 void OperatorNameId::accept0(NameVisitor *visitor) const
       
   211 { visitor->visit(this); }
       
   212 
       
   213 int OperatorNameId::kind() const
       
   214 { return _kind; }
       
   215 
       
   216 const Identifier *OperatorNameId::identifier() const
       
   217 { return 0; }
       
   218 
       
   219 bool OperatorNameId::isEqualTo(const Name *other) const
       
   220 {
       
   221     const OperatorNameId *o = other->asOperatorNameId();
       
   222     if (! o)
       
   223         return false;
       
   224     return _kind == o->kind();
       
   225 }
       
   226 
       
   227 ConversionNameId::ConversionNameId(const FullySpecifiedType &type)
       
   228     : _type(type)
       
   229 { }
       
   230 
       
   231 ConversionNameId::~ConversionNameId()
       
   232 { }
       
   233 
       
   234 void ConversionNameId::accept0(NameVisitor *visitor) const
       
   235 { visitor->visit(this); }
       
   236 
       
   237 FullySpecifiedType ConversionNameId::type() const
       
   238 { return _type; }
       
   239 
       
   240 const Identifier *ConversionNameId::identifier() const
       
   241 { return 0; }
       
   242 
       
   243 bool ConversionNameId::isEqualTo(const Name *other) const
       
   244 {
       
   245     const ConversionNameId *c = other->asConversionNameId();
       
   246     if (! c)
       
   247         return false;
       
   248     return _type.isEqualTo(c->type());
       
   249 }
       
   250 
       
   251 SelectorNameId::~SelectorNameId()
       
   252 { }
       
   253 
       
   254 void SelectorNameId::accept0(NameVisitor *visitor) const
       
   255 { visitor->visit(this); }
       
   256 
       
   257 const Identifier *SelectorNameId::identifier() const
       
   258 {
       
   259     if (_names.empty())
       
   260         return 0;
       
   261 
       
   262     return nameAt(0)->identifier();
       
   263 }
       
   264 
       
   265 unsigned SelectorNameId::nameCount() const
       
   266 { return _names.size(); }
       
   267 
       
   268 const Name *SelectorNameId::nameAt(unsigned index) const
       
   269 { return _names[index]; }
       
   270 
       
   271 bool SelectorNameId::hasArguments() const
       
   272 { return _hasArguments; }
       
   273 
       
   274 bool SelectorNameId::isEqualTo(const Name *other) const
       
   275 {
       
   276     const SelectorNameId *q = other->asSelectorNameId();
       
   277     if (! q)
       
   278         return false;
       
   279     else if (hasArguments() != q->hasArguments())
       
   280         return false;
       
   281     else {
       
   282         const unsigned count = nameCount();
       
   283         if (count != q->nameCount())
       
   284             return false;
       
   285         for (unsigned i = 0; i < count; ++i) {
       
   286             const Name *l = nameAt(i);
       
   287             const Name *r = q->nameAt(i);
       
   288             if (! l->isEqualTo(r))
       
   289                 return false;
       
   290         }
       
   291     }
       
   292     return true;
       
   293 }
       
   294 
       
   295