tools/icheck/parser/src/shared/cplusplus/Symbol.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 "Symbol.h"
       
    62 #include "Symbols.h"
       
    63 #include "Control.h"
       
    64 #include "Names.h"
       
    65 #include "TranslationUnit.h"
       
    66 #include "Literals.h"
       
    67 #include "MemoryPool.h"
       
    68 #include "SymbolVisitor.h"
       
    69 #include "NameVisitor.h"
       
    70 #include "Scope.h"
       
    71 #include <cassert>
       
    72 
       
    73 using namespace CPlusPlus;
       
    74 
       
    75 class Symbol::HashCode: protected NameVisitor
       
    76 {
       
    77 public:
       
    78     HashCode()
       
    79         : _value(0)
       
    80     { }
       
    81 
       
    82     virtual ~HashCode()
       
    83     { }
       
    84 
       
    85     unsigned operator()(const Name *name)
       
    86     {
       
    87         unsigned previousValue = switchValue(0);
       
    88         accept(name);
       
    89         return switchValue(previousValue);
       
    90     }
       
    91 
       
    92 protected:
       
    93     unsigned switchValue(unsigned value)
       
    94     {
       
    95         unsigned previousValue = _value;
       
    96         _value = value;
       
    97         return previousValue;
       
    98     }
       
    99 
       
   100     virtual void visit(const NameId *name)
       
   101     { _value = name->identifier()->hashCode(); }
       
   102 
       
   103     virtual void visit(const TemplateNameId *name)
       
   104     { _value = name->identifier()->hashCode(); }
       
   105 
       
   106     virtual void visit(const DestructorNameId *name)
       
   107     { _value = name->identifier()->hashCode(); }
       
   108 
       
   109     virtual void visit(const OperatorNameId *name)
       
   110     { _value = unsigned(name->kind()); }
       
   111 
       
   112     virtual void visit(const ConversionNameId *)
       
   113     { _value = 0; } // ### TODO: implement me
       
   114 
       
   115     virtual void visit(const QualifiedNameId *name)
       
   116     { _value = operator()(name->unqualifiedNameId()); }
       
   117 
       
   118     virtual void visit(const SelectorNameId *name)
       
   119     { _value = name->identifier()->hashCode(); }
       
   120 
       
   121 private:
       
   122     unsigned _value;
       
   123 };
       
   124 
       
   125 class Symbol::IdentityForName: protected NameVisitor
       
   126 {
       
   127 public:
       
   128     IdentityForName()
       
   129         : _identity(0)
       
   130     { }
       
   131 
       
   132     virtual ~IdentityForName()
       
   133     { }
       
   134 
       
   135     const Name *operator()(const Name *name)
       
   136     {
       
   137         const Name *previousIdentity = switchIdentity(0);
       
   138         accept(name);
       
   139         return switchIdentity(previousIdentity);
       
   140     }
       
   141 
       
   142 protected:
       
   143     const Name *switchIdentity(const Name *identity)
       
   144     {
       
   145         const Name *previousIdentity = _identity;
       
   146         _identity = identity;
       
   147         return previousIdentity;
       
   148     }
       
   149 
       
   150     virtual void visit(const NameId *name)
       
   151     { _identity = name; }
       
   152 
       
   153     virtual void visit(const TemplateNameId *name)
       
   154     { _identity = name; }
       
   155 
       
   156     virtual void visit(const DestructorNameId *name)
       
   157     { _identity = name; }
       
   158 
       
   159     virtual void visit(const OperatorNameId *name)
       
   160     { _identity = name; }
       
   161 
       
   162     virtual void visit(const ConversionNameId *name)
       
   163     { _identity = name; }
       
   164 
       
   165     virtual void visit(const QualifiedNameId *name)
       
   166     { _identity = name->unqualifiedNameId(); }
       
   167 
       
   168     virtual void visit(const SelectorNameId *name)
       
   169     { _identity = name; }
       
   170 
       
   171 private:
       
   172     const Name *_identity;
       
   173 };
       
   174 
       
   175 Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
       
   176     : _control(translationUnit->control()),
       
   177       _sourceLocation(sourceLocation),
       
   178       _sourceOffset(0),
       
   179       _startOffset(0),
       
   180       _endOffset(0),
       
   181       _name(0),
       
   182       _hashCode(0),
       
   183       _storage(Symbol::NoStorage),
       
   184       _visibility(Symbol::Public),
       
   185       _scope(0),
       
   186       _index(0),
       
   187       _next(0),
       
   188       _isGenerated(false)
       
   189 {
       
   190     setSourceLocation(sourceLocation);
       
   191     setName(name);
       
   192 }
       
   193 
       
   194 Symbol::~Symbol()
       
   195 { }
       
   196 
       
   197 Control *Symbol::control() const
       
   198 { return _control; }
       
   199 
       
   200 TranslationUnit *Symbol::translationUnit() const
       
   201 { return _control->translationUnit(); }
       
   202 
       
   203 void Symbol::visitSymbol(SymbolVisitor *visitor)
       
   204 {
       
   205     if (visitor->preVisit(this))
       
   206         visitSymbol0(visitor);
       
   207     visitor->postVisit(this);
       
   208 }
       
   209 
       
   210 void Symbol::visitSymbol(Symbol *symbol, SymbolVisitor *visitor)
       
   211 {
       
   212     if (! symbol)
       
   213         return;
       
   214 
       
   215     symbol->visitSymbol(visitor);
       
   216 }
       
   217 
       
   218 unsigned Symbol::sourceLocation() const
       
   219 { return _sourceLocation; }
       
   220 
       
   221 unsigned Symbol::sourceOffset() const
       
   222 { return _sourceOffset; }
       
   223 
       
   224 bool Symbol::isGenerated() const
       
   225 { return _isGenerated; }
       
   226 
       
   227 void Symbol::setSourceLocation(unsigned sourceLocation)
       
   228 {
       
   229     _sourceLocation = sourceLocation;
       
   230 
       
   231     if (! _sourceLocation) {
       
   232         _isGenerated = false;
       
   233         _sourceOffset = 0;
       
   234     } else {
       
   235         TranslationUnit *unit = translationUnit();
       
   236 
       
   237         const Token &tk = unit->tokenAt(sourceLocation);
       
   238 
       
   239         _isGenerated = tk.f.generated;
       
   240         _sourceOffset = tk.offset;
       
   241     }
       
   242 }
       
   243 
       
   244 unsigned Symbol::line() const
       
   245 {
       
   246     unsigned line = 0, column = 0;
       
   247     const StringLiteral *fileId = 0;
       
   248     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
       
   249     return line;
       
   250 }
       
   251 
       
   252 unsigned Symbol::column() const
       
   253 {
       
   254     unsigned line = 0, column = 0;
       
   255     const StringLiteral *fileId = 0;
       
   256     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
       
   257     return column;
       
   258 }
       
   259 
       
   260 const StringLiteral *Symbol::fileId() const
       
   261 {
       
   262     unsigned line = 0, column = 0;
       
   263     const StringLiteral *fileId = 0;
       
   264     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
       
   265     return fileId;
       
   266 }
       
   267 
       
   268 void Symbol::getPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
       
   269 { translationUnit()->getPosition(_sourceOffset, line, column, fileId); }
       
   270 
       
   271 void Symbol::getStartPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
       
   272 { translationUnit()->getPosition(_startOffset, line, column, fileId); }
       
   273 
       
   274 void Symbol::getEndPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
       
   275 { translationUnit()->getPosition(_endOffset, line, column, fileId); }
       
   276 
       
   277 const char *Symbol::fileName() const
       
   278 { return fileId()->chars(); }
       
   279 
       
   280 unsigned Symbol::fileNameLength() const
       
   281 { return fileId()->size(); }
       
   282 
       
   283 unsigned Symbol::startOffset() const
       
   284 { return _startOffset; }
       
   285 
       
   286 void Symbol::setStartOffset(unsigned offset)
       
   287 { _startOffset = offset; }
       
   288 
       
   289 unsigned Symbol::endOffset() const
       
   290 { return _endOffset; }
       
   291 
       
   292 void Symbol::setEndOffset(unsigned offset)
       
   293 { _endOffset = offset; }
       
   294 
       
   295 const Name *Symbol::identity() const
       
   296 {
       
   297     IdentityForName id;
       
   298     return id(_name);
       
   299 }
       
   300 
       
   301 const Name *Symbol::name() const
       
   302 { return _name; }
       
   303 
       
   304 void Symbol::setName(const Name *name)
       
   305 {
       
   306     _name = name;
       
   307 
       
   308     if (! _name)
       
   309         _hashCode = 0;
       
   310     else {
       
   311         IdentityForName identityForName;
       
   312         HashCode hh;
       
   313         _hashCode = hh(identityForName(_name));
       
   314     }
       
   315 }
       
   316 
       
   317 const Identifier *Symbol::identifier() const
       
   318 {
       
   319     if (_name)
       
   320         return _name->identifier();
       
   321 
       
   322     return 0;
       
   323 }
       
   324 
       
   325 Scope *Symbol::scope() const
       
   326 { return _scope; }
       
   327 
       
   328 void Symbol::setScope(Scope *scope)
       
   329 {
       
   330     assert(! _scope);
       
   331     _scope = scope;
       
   332 }
       
   333 
       
   334 Symbol *Symbol::enclosingSymbol() const
       
   335 {
       
   336     if (! _scope)
       
   337         return 0;
       
   338 
       
   339     return _scope->owner();
       
   340 }
       
   341 
       
   342 Scope *Symbol::enclosingNamespaceScope() const
       
   343 {
       
   344     if (! _scope)
       
   345         return 0;
       
   346 
       
   347     else if (_scope->isNamespaceScope())
       
   348         return _scope;
       
   349 
       
   350     return _scope->enclosingNamespaceScope();
       
   351 }
       
   352 
       
   353 Scope *Symbol::enclosingClassScope() const
       
   354 {
       
   355     if (! _scope)
       
   356         return 0;
       
   357 
       
   358     else if (_scope->isClassScope())
       
   359         return _scope;
       
   360 
       
   361     return _scope->enclosingClassScope();
       
   362 }
       
   363 
       
   364 Scope *Symbol::enclosingEnumScope() const
       
   365 {
       
   366     if (! _scope)
       
   367         return 0;
       
   368 
       
   369     else if (_scope->isEnumScope())
       
   370         return _scope;
       
   371 
       
   372     return _scope->enclosingEnumScope();
       
   373 }
       
   374 
       
   375 Scope *Symbol::enclosingFunctionScope() const
       
   376 {
       
   377     if (! _scope)
       
   378         return 0;
       
   379 
       
   380     else if (_scope->isFunctionScope())
       
   381         return _scope;
       
   382 
       
   383     return _scope->enclosingFunctionScope();
       
   384 }
       
   385 
       
   386 Scope *Symbol::enclosingBlockScope() const
       
   387 {
       
   388     if (! _scope)
       
   389         return 0;
       
   390 
       
   391     else if (_scope->isBlockScope())
       
   392         return _scope;
       
   393 
       
   394     return _scope->enclosingBlockScope();
       
   395 }
       
   396 
       
   397 unsigned Symbol::index() const
       
   398 { return _index; }
       
   399 
       
   400 Symbol *Symbol::next() const
       
   401 { return _next; }
       
   402 
       
   403 unsigned Symbol::hashCode() const
       
   404 { return _hashCode; }
       
   405 
       
   406 int Symbol::storage() const
       
   407 { return _storage; }
       
   408 
       
   409 void Symbol::setStorage(int storage)
       
   410 { _storage = storage; }
       
   411 
       
   412 int Symbol::visibility() const
       
   413 { return _visibility; }
       
   414 
       
   415 void Symbol::setVisibility(int visibility)
       
   416 { _visibility = visibility; }
       
   417 
       
   418 bool Symbol::isFriend() const
       
   419 { return _storage == Friend; }
       
   420 
       
   421 bool Symbol::isRegister() const
       
   422 { return _storage == Register; }
       
   423 
       
   424 bool Symbol::isStatic() const
       
   425 { return _storage == Static; }
       
   426 
       
   427 bool Symbol::isExtern() const
       
   428 { return _storage == Extern; }
       
   429 
       
   430 bool Symbol::isMutable() const
       
   431 { return _storage == Mutable; }
       
   432 
       
   433 bool Symbol::isTypedef() const
       
   434 { return _storage == Typedef; }
       
   435 
       
   436 bool Symbol::isPublic() const
       
   437 { return _visibility == Public; }
       
   438 
       
   439 bool Symbol::isProtected() const
       
   440 { return _visibility == Protected; }
       
   441 
       
   442 bool Symbol::isPrivate() const
       
   443 { return _visibility == Private; }
       
   444 
       
   445 bool Symbol::isScopedSymbol() const
       
   446 { return asScopedSymbol() != 0; }
       
   447 
       
   448 bool Symbol::isEnum() const
       
   449 { return asEnum()  != 0; }
       
   450 
       
   451 bool Symbol::isFunction() const
       
   452 { return asFunction() != 0; }
       
   453 
       
   454 bool Symbol::isNamespace() const
       
   455 { return asNamespace() != 0; }
       
   456 
       
   457 bool Symbol::isClass() const
       
   458 { return asClass() != 0; }
       
   459 
       
   460 bool Symbol::isForwardClassDeclaration() const
       
   461 { return asForwardClassDeclaration() != 0; }
       
   462 
       
   463 bool Symbol::isBlock() const
       
   464 { return asBlock() != 0; }
       
   465 
       
   466 bool Symbol::isUsingNamespaceDirective() const
       
   467 { return asUsingNamespaceDirective() != 0; }
       
   468 
       
   469 bool Symbol::isUsingDeclaration() const
       
   470 { return asUsingDeclaration() != 0; }
       
   471 
       
   472 bool Symbol::isDeclaration() const
       
   473 { return asDeclaration() != 0; }
       
   474 
       
   475 bool Symbol::isArgument() const
       
   476 { return asArgument() != 0; }
       
   477 
       
   478 bool Symbol::isTypenameArgument() const
       
   479 { return asTypenameArgument() != 0; }
       
   480 
       
   481 bool Symbol::isBaseClass() const
       
   482 { return asBaseClass() != 0; }
       
   483 
       
   484 bool Symbol::isObjCBaseClass() const
       
   485 { return asObjCBaseClass() != 0; }
       
   486 
       
   487 bool Symbol::isObjCBaseProtocol() const
       
   488 { return asObjCBaseProtocol() != 0; }
       
   489 
       
   490 bool Symbol::isObjCClass() const
       
   491 { return asObjCClass() != 0; }
       
   492 
       
   493 bool Symbol::isObjCForwardClassDeclaration() const
       
   494 { return asObjCForwardClassDeclaration() != 0; }
       
   495 
       
   496 bool Symbol::isObjCProtocol() const
       
   497 { return asObjCProtocol() != 0; }
       
   498 
       
   499 bool Symbol::isObjCForwardProtocolDeclaration() const
       
   500 { return asObjCForwardProtocolDeclaration() != 0; }
       
   501 
       
   502 bool Symbol::isObjCMethod() const
       
   503 { return asObjCMethod() != 0; }
       
   504 
       
   505 bool Symbol::isObjCPropertyDeclaration() const
       
   506 { return asObjCPropertyDeclaration() != 0; }