diff -r 000000000000 -r 876b1a06bc25 tools/icheck/parser/src/shared/cplusplus/Symbol.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/icheck/parser/src/shared/cplusplus/Symbol.cpp Wed Aug 25 15:49:42 2010 +0300 @@ -0,0 +1,506 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +// Copyright (c) 2008 Roberto Raggi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "Symbol.h" +#include "Symbols.h" +#include "Control.h" +#include "Names.h" +#include "TranslationUnit.h" +#include "Literals.h" +#include "MemoryPool.h" +#include "SymbolVisitor.h" +#include "NameVisitor.h" +#include "Scope.h" +#include + +using namespace CPlusPlus; + +class Symbol::HashCode: protected NameVisitor +{ +public: + HashCode() + : _value(0) + { } + + virtual ~HashCode() + { } + + unsigned operator()(const Name *name) + { + unsigned previousValue = switchValue(0); + accept(name); + return switchValue(previousValue); + } + +protected: + unsigned switchValue(unsigned value) + { + unsigned previousValue = _value; + _value = value; + return previousValue; + } + + virtual void visit(const NameId *name) + { _value = name->identifier()->hashCode(); } + + virtual void visit(const TemplateNameId *name) + { _value = name->identifier()->hashCode(); } + + virtual void visit(const DestructorNameId *name) + { _value = name->identifier()->hashCode(); } + + virtual void visit(const OperatorNameId *name) + { _value = unsigned(name->kind()); } + + virtual void visit(const ConversionNameId *) + { _value = 0; } // ### TODO: implement me + + virtual void visit(const QualifiedNameId *name) + { _value = operator()(name->unqualifiedNameId()); } + + virtual void visit(const SelectorNameId *name) + { _value = name->identifier()->hashCode(); } + +private: + unsigned _value; +}; + +class Symbol::IdentityForName: protected NameVisitor +{ +public: + IdentityForName() + : _identity(0) + { } + + virtual ~IdentityForName() + { } + + const Name *operator()(const Name *name) + { + const Name *previousIdentity = switchIdentity(0); + accept(name); + return switchIdentity(previousIdentity); + } + +protected: + const Name *switchIdentity(const Name *identity) + { + const Name *previousIdentity = _identity; + _identity = identity; + return previousIdentity; + } + + virtual void visit(const NameId *name) + { _identity = name; } + + virtual void visit(const TemplateNameId *name) + { _identity = name; } + + virtual void visit(const DestructorNameId *name) + { _identity = name; } + + virtual void visit(const OperatorNameId *name) + { _identity = name; } + + virtual void visit(const ConversionNameId *name) + { _identity = name; } + + virtual void visit(const QualifiedNameId *name) + { _identity = name->unqualifiedNameId(); } + + virtual void visit(const SelectorNameId *name) + { _identity = name; } + +private: + const Name *_identity; +}; + +Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name) + : _control(translationUnit->control()), + _sourceLocation(sourceLocation), + _sourceOffset(0), + _startOffset(0), + _endOffset(0), + _name(0), + _hashCode(0), + _storage(Symbol::NoStorage), + _visibility(Symbol::Public), + _scope(0), + _index(0), + _next(0), + _isGenerated(false) +{ + setSourceLocation(sourceLocation); + setName(name); +} + +Symbol::~Symbol() +{ } + +Control *Symbol::control() const +{ return _control; } + +TranslationUnit *Symbol::translationUnit() const +{ return _control->translationUnit(); } + +void Symbol::visitSymbol(SymbolVisitor *visitor) +{ + if (visitor->preVisit(this)) + visitSymbol0(visitor); + visitor->postVisit(this); +} + +void Symbol::visitSymbol(Symbol *symbol, SymbolVisitor *visitor) +{ + if (! symbol) + return; + + symbol->visitSymbol(visitor); +} + +unsigned Symbol::sourceLocation() const +{ return _sourceLocation; } + +unsigned Symbol::sourceOffset() const +{ return _sourceOffset; } + +bool Symbol::isGenerated() const +{ return _isGenerated; } + +void Symbol::setSourceLocation(unsigned sourceLocation) +{ + _sourceLocation = sourceLocation; + + if (! _sourceLocation) { + _isGenerated = false; + _sourceOffset = 0; + } else { + TranslationUnit *unit = translationUnit(); + + const Token &tk = unit->tokenAt(sourceLocation); + + _isGenerated = tk.f.generated; + _sourceOffset = tk.offset; + } +} + +unsigned Symbol::line() const +{ + unsigned line = 0, column = 0; + const StringLiteral *fileId = 0; + translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId); + return line; +} + +unsigned Symbol::column() const +{ + unsigned line = 0, column = 0; + const StringLiteral *fileId = 0; + translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId); + return column; +} + +const StringLiteral *Symbol::fileId() const +{ + unsigned line = 0, column = 0; + const StringLiteral *fileId = 0; + translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId); + return fileId; +} + +void Symbol::getPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const +{ translationUnit()->getPosition(_sourceOffset, line, column, fileId); } + +void Symbol::getStartPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const +{ translationUnit()->getPosition(_startOffset, line, column, fileId); } + +void Symbol::getEndPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const +{ translationUnit()->getPosition(_endOffset, line, column, fileId); } + +const char *Symbol::fileName() const +{ return fileId()->chars(); } + +unsigned Symbol::fileNameLength() const +{ return fileId()->size(); } + +unsigned Symbol::startOffset() const +{ return _startOffset; } + +void Symbol::setStartOffset(unsigned offset) +{ _startOffset = offset; } + +unsigned Symbol::endOffset() const +{ return _endOffset; } + +void Symbol::setEndOffset(unsigned offset) +{ _endOffset = offset; } + +const Name *Symbol::identity() const +{ + IdentityForName id; + return id(_name); +} + +const Name *Symbol::name() const +{ return _name; } + +void Symbol::setName(const Name *name) +{ + _name = name; + + if (! _name) + _hashCode = 0; + else { + IdentityForName identityForName; + HashCode hh; + _hashCode = hh(identityForName(_name)); + } +} + +const Identifier *Symbol::identifier() const +{ + if (_name) + return _name->identifier(); + + return 0; +} + +Scope *Symbol::scope() const +{ return _scope; } + +void Symbol::setScope(Scope *scope) +{ + assert(! _scope); + _scope = scope; +} + +Symbol *Symbol::enclosingSymbol() const +{ + if (! _scope) + return 0; + + return _scope->owner(); +} + +Scope *Symbol::enclosingNamespaceScope() const +{ + if (! _scope) + return 0; + + else if (_scope->isNamespaceScope()) + return _scope; + + return _scope->enclosingNamespaceScope(); +} + +Scope *Symbol::enclosingClassScope() const +{ + if (! _scope) + return 0; + + else if (_scope->isClassScope()) + return _scope; + + return _scope->enclosingClassScope(); +} + +Scope *Symbol::enclosingEnumScope() const +{ + if (! _scope) + return 0; + + else if (_scope->isEnumScope()) + return _scope; + + return _scope->enclosingEnumScope(); +} + +Scope *Symbol::enclosingFunctionScope() const +{ + if (! _scope) + return 0; + + else if (_scope->isFunctionScope()) + return _scope; + + return _scope->enclosingFunctionScope(); +} + +Scope *Symbol::enclosingBlockScope() const +{ + if (! _scope) + return 0; + + else if (_scope->isBlockScope()) + return _scope; + + return _scope->enclosingBlockScope(); +} + +unsigned Symbol::index() const +{ return _index; } + +Symbol *Symbol::next() const +{ return _next; } + +unsigned Symbol::hashCode() const +{ return _hashCode; } + +int Symbol::storage() const +{ return _storage; } + +void Symbol::setStorage(int storage) +{ _storage = storage; } + +int Symbol::visibility() const +{ return _visibility; } + +void Symbol::setVisibility(int visibility) +{ _visibility = visibility; } + +bool Symbol::isFriend() const +{ return _storage == Friend; } + +bool Symbol::isRegister() const +{ return _storage == Register; } + +bool Symbol::isStatic() const +{ return _storage == Static; } + +bool Symbol::isExtern() const +{ return _storage == Extern; } + +bool Symbol::isMutable() const +{ return _storage == Mutable; } + +bool Symbol::isTypedef() const +{ return _storage == Typedef; } + +bool Symbol::isPublic() const +{ return _visibility == Public; } + +bool Symbol::isProtected() const +{ return _visibility == Protected; } + +bool Symbol::isPrivate() const +{ return _visibility == Private; } + +bool Symbol::isScopedSymbol() const +{ return asScopedSymbol() != 0; } + +bool Symbol::isEnum() const +{ return asEnum() != 0; } + +bool Symbol::isFunction() const +{ return asFunction() != 0; } + +bool Symbol::isNamespace() const +{ return asNamespace() != 0; } + +bool Symbol::isClass() const +{ return asClass() != 0; } + +bool Symbol::isForwardClassDeclaration() const +{ return asForwardClassDeclaration() != 0; } + +bool Symbol::isBlock() const +{ return asBlock() != 0; } + +bool Symbol::isUsingNamespaceDirective() const +{ return asUsingNamespaceDirective() != 0; } + +bool Symbol::isUsingDeclaration() const +{ return asUsingDeclaration() != 0; } + +bool Symbol::isDeclaration() const +{ return asDeclaration() != 0; } + +bool Symbol::isArgument() const +{ return asArgument() != 0; } + +bool Symbol::isTypenameArgument() const +{ return asTypenameArgument() != 0; } + +bool Symbol::isBaseClass() const +{ return asBaseClass() != 0; } + +bool Symbol::isObjCBaseClass() const +{ return asObjCBaseClass() != 0; } + +bool Symbol::isObjCBaseProtocol() const +{ return asObjCBaseProtocol() != 0; } + +bool Symbol::isObjCClass() const +{ return asObjCClass() != 0; } + +bool Symbol::isObjCForwardClassDeclaration() const +{ return asObjCForwardClassDeclaration() != 0; } + +bool Symbol::isObjCProtocol() const +{ return asObjCProtocol() != 0; } + +bool Symbol::isObjCForwardProtocolDeclaration() const +{ return asObjCForwardProtocolDeclaration() != 0; } + +bool Symbol::isObjCMethod() const +{ return asObjCMethod() != 0; } + +bool Symbol::isObjCPropertyDeclaration() const +{ return asObjCPropertyDeclaration() != 0; }