diff options
author | Roberto Raggi <roberto.raggi@nokia.com> | 2010-08-11 12:26:02 +0200 |
---|---|---|
committer | Roberto Raggi <roberto.raggi@nokia.com> | 2010-08-11 15:25:18 +0200 |
commit | 354b9712e4655040930a9f18de4e6b4c71dc42d9 (patch) | |
tree | 474bab43aa8a84893f38b8a0552f8071404e6a12 /src/shared/cplusplus/Scope.cpp | |
parent | 5accc9664ea247a5b9e1fa6097a04252fb57f01b (diff) | |
download | qt-creator-354b9712e4655040930a9f18de4e6b4c71dc42d9.tar.gz |
Merged ScopedSymbol and Scope.
Diffstat (limited to 'src/shared/cplusplus/Scope.cpp')
-rw-r--r-- | src/shared/cplusplus/Scope.cpp | 252 |
1 files changed, 119 insertions, 133 deletions
diff --git a/src/shared/cplusplus/Scope.cpp b/src/shared/cplusplus/Scope.cpp index a38dce4840..0d8f75eeb2 100644 --- a/src/shared/cplusplus/Scope.cpp +++ b/src/shared/cplusplus/Scope.cpp @@ -55,147 +55,85 @@ using namespace CPlusPlus; -Scope::Scope(ScopedSymbol *owner) - : _owner(owner), - _symbols(0), - _hash(0), - _allocatedSymbols(0), - _symbolCount(-1), - _hashSize(0), - _startOffset(0), - _endOffset(0) -{ } - -Scope::~Scope() +class CPlusPlus::SymbolTable { - if (_symbols) - free(_symbols); - if (_hash) - free(_hash); -} + SymbolTable(const SymbolTable &other); + void operator =(const SymbolTable &other); -ScopedSymbol *Scope::owner() const -{ return _owner; } +public: + typedef Symbol **iterator; -void Scope::setOwner(ScopedSymbol *owner) -{ _owner = owner; } +public: + /// Constructs an empty Scope. + SymbolTable(Scope *owner = 0); -Scope *Scope::enclosingScope() const -{ - if (! _owner) - return 0; + /// Destroy this scope. + ~SymbolTable(); - return _owner->scope(); -} + /// Returns this scope's owner Symbol. + Scope *owner() const; -Scope *Scope::enclosingNamespaceScope() const -{ - Scope *scope = enclosingScope(); - for (; scope; scope = scope->enclosingScope()) { - if (scope->owner()->isNamespace()) - break; - } - return scope; -} + /// Sets this scope's owner Symbol. + void setOwner(Scope *owner); // ### remove me -Scope *Scope::enclosingClassScope() const -{ - Scope *scope = enclosingScope(); - for (; scope; scope = scope->enclosingScope()) { - if (scope->owner()->isClass()) - break; - } - return scope; -} + /// Adds a Symbol to this Scope. + void enterSymbol(Symbol *symbol); -Scope *Scope::enclosingEnumScope() const -{ - Scope *scope = enclosingScope(); - for (; scope; scope = scope->enclosingScope()) { - if (scope->owner()->isEnum()) - break; - } - return scope; -} + /// Returns true if this Scope is empty; otherwise returns false. + bool isEmpty() const; -Scope *Scope::enclosingPrototypeScope() const -{ - Scope *scope = enclosingScope(); - for (; scope; scope = scope->enclosingScope()) { - if (scope->owner()->isFunction()) - break; - } - return scope; -} + /// Returns the number of symbols is in the scope. + unsigned symbolCount() const; -Scope *Scope::enclosingBlockScope() const -{ - Scope *scope = enclosingScope(); - for (; scope; scope = scope->enclosingScope()) { - if (scope->owner()->isBlock()) - break; - } - return scope; -} + /// Returns the Symbol at the given position. + Symbol *symbolAt(unsigned index) const; -bool Scope::isNamespaceScope() const -{ - if (_owner) - return _owner->isNamespace(); - return false; -} + /// Returns the first Symbol in the scope. + iterator firstSymbol() const; -bool Scope::isClassScope() const -{ - if (_owner) - return _owner->isClass(); - return false; -} + /// Returns the last Symbol in the scope. + iterator lastSymbol() const; -bool Scope::isEnumScope() const -{ - if (_owner) - return _owner->isEnum(); - return false; -} + Symbol *lookat(const Name *name) const; + Symbol *lookat(const Identifier *id) const; + Symbol *lookat(int operatorId) const; -bool Scope::isBlockScope() const -{ - if (_owner) - return _owner->isBlock(); - return false; -} +private: + /// Returns the hash value for the given Symbol. + unsigned hashValue(Symbol *symbol) const; -bool Scope::isObjCClassScope() const -{ - if (_owner) - return _owner->isObjCClass(); - return false; -} + /// Updates the hash table. + void rehash(); -bool Scope::isObjCProtocolScope() const -{ - if (_owner) - return _owner->isObjCProtocol(); - return false; -} +private: + enum { DefaultInitialSize = 11 }; -bool Scope::isPrototypeScope() const -{ - if (_owner) - return _owner->isFunction(); - return false; -} + Scope *_owner; + Symbol **_symbols; + Symbol **_hash; + int _allocatedSymbols; + int _symbolCount; + int _hashSize; +}; + +SymbolTable::SymbolTable(Scope *owner) + : _owner(owner), + _symbols(0), + _hash(0), + _allocatedSymbols(0), + _symbolCount(-1), + _hashSize(0) +{ } -bool Scope::isObjCMethodScope() const +SymbolTable::~SymbolTable() { - ObjCMethod *m = 0; - if (_owner && 0 != (m = _owner->asObjCMethod())) - return m->arguments() != this; - return false; + if (_symbols) + free(_symbols); + if (_hash) + free(_hash); } -void Scope::enterSymbol(Symbol *symbol) +void SymbolTable::enterSymbol(Symbol *symbol) { if (++_symbolCount == _allocatedSymbols) { _allocatedSymbols <<= 1; @@ -207,7 +145,7 @@ void Scope::enterSymbol(Symbol *symbol) assert(! symbol->_scope || symbol->scope() == this); symbol->_index = _symbolCount; - symbol->_scope = this; + symbol->_scope = _owner; _symbols[_symbolCount] = symbol; if (_symbolCount >= _hashSize * 0.6) @@ -219,7 +157,7 @@ void Scope::enterSymbol(Symbol *symbol) } } -Symbol *Scope::lookat(const Name *name) const +Symbol *SymbolTable::lookat(const Name *name) const { if (! name) return 0; @@ -234,7 +172,7 @@ Symbol *Scope::lookat(const Name *name) const return 0; } -Symbol *Scope::lookat(const Identifier *id) const +Symbol *SymbolTable::lookat(const Identifier *id) const { if (! _hash || ! id) return 0; @@ -264,7 +202,7 @@ Symbol *Scope::lookat(const Identifier *id) const return symbol; } -Symbol *Scope::lookat(int operatorId) const +Symbol *SymbolTable::lookat(int operatorId) const { if (! _hash) return 0; @@ -281,7 +219,7 @@ Symbol *Scope::lookat(int operatorId) const return symbol; } -void Scope::rehash() +void SymbolTable::rehash() { _hashSize <<= 1; @@ -299,7 +237,7 @@ void Scope::rehash() } } -unsigned Scope::hashValue(Symbol *symbol) const +unsigned SymbolTable::hashValue(Symbol *symbol) const { if (! symbol) return 0; @@ -307,35 +245,83 @@ unsigned Scope::hashValue(Symbol *symbol) const return symbol->hashCode() % _hashSize; } -bool Scope::isEmpty() const +bool SymbolTable::isEmpty() const { return _symbolCount == -1; } -unsigned Scope::symbolCount() const +unsigned SymbolTable::symbolCount() const { return _symbolCount + 1; } -Symbol *Scope::symbolAt(unsigned index) const +Symbol *SymbolTable::symbolAt(unsigned index) const { if (! _symbols) return 0; return _symbols[index]; } -Scope::iterator Scope::firstSymbol() const +SymbolTable::iterator SymbolTable::firstSymbol() const { return _symbols; } -Scope::iterator Scope::lastSymbol() const +SymbolTable::iterator SymbolTable::lastSymbol() const { return _symbols + _symbolCount + 1; } +Scope::Scope(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name) + : Symbol(translationUnit, sourceLocation, name), + _members(0), + _startOffset(0), + _endOffset(0) +{ } + +Scope::~Scope() +{ delete _members; } + +/// Adds a Symbol to this Scope. +void Scope::addMember(Symbol *symbol) +{ + if (! _members) + _members = new SymbolTable(this); + + _members->enterSymbol(symbol); +} + +/// Returns true if this Scope is empty; otherwise returns false. +bool Scope::isEmpty() const +{ return _members ? _members->isEmpty() : true; } + +/// Returns the number of symbols is in the scope. +unsigned Scope::memberCount() const +{ return _members ? _members->symbolCount() : 0; } + +/// Returns the Symbol at the given position. +Symbol *Scope::memberAt(unsigned index) const +{ return _members ? _members->symbolAt(index) : 0; } + +/// Returns the first Symbol in the scope. +Scope::iterator Scope::firstMember() const +{ return _members ? _members->firstSymbol() : 0; } + +/// Returns the last Symbol in the scope. +Scope::iterator Scope::lastMember() const +{ return _members ? _members->lastSymbol() : 0; } + +Symbol *Scope::find(const Name *name) const +{ return _members ? _members->lookat(name) : 0; } + +Symbol *Scope::find(const Identifier *id) const +{ return _members ? _members->lookat(id) : 0; } + +Symbol *Scope::find(int operatorId) const +{ return _members ? _members->lookat(operatorId) : 0; } + +/// Set the start offset of the scope unsigned Scope::startOffset() const { return _startOffset; } void Scope::setStartOffset(unsigned offset) { _startOffset = offset; } +/// Set the end offset of the scope unsigned Scope::endOffset() const { return _endOffset; } void Scope::setEndOffset(unsigned offset) { _endOffset = offset; } - - |