From c4737c1fdf5ab3d950adaa5002367797c993ed37 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Tue, 1 Dec 2009 14:38:42 +0100 Subject: [PATCH] Store the names in a set. --- src/shared/cplusplus/CheckDeclaration.cpp | 5 +- src/shared/cplusplus/Control.cpp | 245 +++++++++------------- src/shared/cplusplus/Names.cpp | 22 +- src/shared/cplusplus/Names.h | 33 ++- 4 files changed, 125 insertions(+), 180 deletions(-) diff --git a/src/shared/cplusplus/CheckDeclaration.cpp b/src/shared/cplusplus/CheckDeclaration.cpp index bd6be26743f..426a7d9de2b 100644 --- a/src/shared/cplusplus/CheckDeclaration.cpp +++ b/src/shared/cplusplus/CheckDeclaration.cpp @@ -378,8 +378,9 @@ bool CheckDeclaration::visit(LinkageSpecificationAST *ast) bool CheckDeclaration::visit(NamespaceAST *ast) { - const Identifier *id = identifier(ast->identifier_token); - const Name *namespaceName = control()->nameId(id); + const Name *namespaceName = 0; + if (const Identifier *id = identifier(ast->identifier_token)) + namespaceName = control()->nameId(id); unsigned sourceLocation = ast->firstToken(); diff --git a/src/shared/cplusplus/Control.cpp b/src/shared/cplusplus/Control.cpp index ec14aa796b3..f03384274a9 100644 --- a/src/shared/cplusplus/Control.cpp +++ b/src/shared/cplusplus/Control.cpp @@ -129,6 +129,76 @@ template <> struct Compare } }; +template <> struct Compare +{ + bool operator()(const NameId &name, const NameId &otherName) const + { + return name.identifier() < otherName.identifier(); + } +}; + +template <> struct Compare +{ + bool operator()(const DestructorNameId &name, const DestructorNameId &otherName) const + { + return name.identifier() < otherName.identifier(); + } +}; + +template <> struct Compare +{ + bool operator()(const OperatorNameId &name, const OperatorNameId &otherName) const + { + return name.kind() < otherName.kind(); + } +}; + +template <> struct Compare +{ + bool operator()(const ConversionNameId &name, const ConversionNameId &otherName) const + { + return name.type() < otherName.type(); + } +}; +template <> struct Compare +{ + bool operator()(const TemplateNameId &name, const TemplateNameId &otherName) const + { + const Identifier *id = name.identifier(); + const Identifier *otherId = otherName.identifier(); + + if (id == otherId) + return std::lexicographical_compare(name.firstTemplateArgument(), name.lastTemplateArgument(), + otherName.firstTemplateArgument(), otherName.lastTemplateArgument()); + + return id < otherId; + } +}; +template <> struct Compare +{ + bool operator()(const QualifiedNameId &name, const QualifiedNameId &otherName) const + { + if (name.isGlobal() == otherName.isGlobal()) + return std::lexicographical_compare(name.firstName(), name.lastName(), + otherName.firstName(), otherName.lastName()); + + return name.isGlobal() < otherName.isGlobal(); + } +}; + +template <> struct Compare +{ + bool operator()(const SelectorNameId &name, const SelectorNameId &otherName) const + { + if (name.hasArguments() == otherName.hasArguments()) + return std::lexicographical_compare(name.firstName(), name.lastName(), + otherName.firstName(), otherName.lastName()); + + return name.hasArguments() < otherName.hasArguments(); + } +}; + + template class Table: public std::set<_Tp, Compare<_Tp> > { @@ -139,13 +209,6 @@ public: } // end of anonymous namespace -template -static void delete_map_entries(_Iterator first, _Iterator last) -{ - for (; first != last; ++first) - delete first->second; -} - template static void delete_array_entries(_Iterator first, _Iterator last) { @@ -153,10 +216,6 @@ static void delete_array_entries(_Iterator first, _Iterator last) delete *first; } -template -static void delete_map_entries(const _Map &m) -{ delete_map_entries(m.begin(), m.end()); } - template static void delete_array_entries(const _Array &a) { delete_array_entries(a.begin(), a.end()); } @@ -172,14 +231,6 @@ public: ~Data() { - // names - delete_map_entries(nameIds); - delete_map_entries(destructorNameIds); - delete_map_entries(operatorNameIds); - delete_map_entries(conversionNameIds); - delete_map_entries(qualifiedNameIds); - delete_map_entries(templateNameIds); - // symbols delete_array_entries(symbols); } @@ -188,77 +239,41 @@ public: { if (! id) return 0; - std::map::iterator it = nameIds.lower_bound(id); - if (it == nameIds.end() || it->first != id) - it = nameIds.insert(it, std::make_pair(id, new NameId(id))); - return it->second; + + return nameIds.intern(NameId(id)); } - const TemplateNameId *findOrInsertTemplateNameId(const Identifier *id, - const std::vector &templateArguments) + template + const TemplateNameId *findOrInsertTemplateNameId(const Identifier *id, _Iterator first, _Iterator last) { - if (! id) - return 0; - const TemplateNameIdKey key(id, templateArguments); - std::map::iterator it = - templateNameIds.lower_bound(key); - if (it == templateNameIds.end() || it->first != key) { - const FullySpecifiedType *args = 0; - if (templateArguments.size()) - args = &templateArguments[0]; - const TemplateNameId *templ = new TemplateNameId(id, args, templateArguments.size()); - it = templateNameIds.insert(it, std::make_pair(key, templ)); - } - return it->second; + return templateNameIds.intern(TemplateNameId(id, first, last)); } const DestructorNameId *findOrInsertDestructorNameId(const Identifier *id) { - if (! id) - return 0; - std::map::iterator it = destructorNameIds.lower_bound(id); - if (it == destructorNameIds.end() || it->first != id) - it = destructorNameIds.insert(it, std::make_pair(id, new DestructorNameId(id))); - return it->second; + return destructorNameIds.intern(DestructorNameId(id)); } const OperatorNameId *findOrInsertOperatorNameId(int kind) { - const int key(kind); - std::map::iterator it = operatorNameIds.lower_bound(key); - if (it == operatorNameIds.end() || it->first != key) - it = operatorNameIds.insert(it, std::make_pair(key, new OperatorNameId(kind))); - return it->second; + return operatorNameIds.intern(OperatorNameId(kind)); } const ConversionNameId *findOrInsertConversionNameId(const FullySpecifiedType &type) { - std::map::iterator it = - conversionNameIds.lower_bound(type); - if (it == conversionNameIds.end() || it->first != type) - it = conversionNameIds.insert(it, std::make_pair(type, new ConversionNameId(type))); - return it->second; + return conversionNameIds.intern(ConversionNameId(type)); } - const QualifiedNameId *findOrInsertQualifiedNameId(const std::vector &names, bool isGlobal) + template + const QualifiedNameId *findOrInsertQualifiedNameId(_Iterator first, _Iterator last, bool isGlobal) { - const QualifiedNameIdKey key(names, isGlobal); - std::map::iterator it = - qualifiedNameIds.lower_bound(key); - if (it == qualifiedNameIds.end() || it->first != key) { - const QualifiedNameId *name = new QualifiedNameId(&names[0], names.size(), isGlobal); - it = qualifiedNameIds.insert(it, std::make_pair(key, name)); - } - return it->second; + return qualifiedNameIds.intern(QualifiedNameId(first, last, isGlobal)); } - const SelectorNameId *findOrInsertSelectorNameId(const std::vector &names, bool hasArguments) + template + const SelectorNameId *findOrInsertSelectorNameId(_Iterator first, _Iterator last, bool hasArguments) { - const SelectorNameIdKey key(names, hasArguments); - std::map::iterator it = selectorNameIds.lower_bound(key); - if (it == selectorNameIds.end() || it->first != key) - it = selectorNameIds.insert(it, std::make_pair(key, new SelectorNameId(&names[0], names.size(), hasArguments))); - return it->second; + return selectorNameIds.intern(SelectorNameId(first, last, hasArguments)); } IntegerType *findOrInsertIntegerType(int kind) @@ -439,75 +454,6 @@ public: return u; } - struct TemplateNameIdKey { - const Identifier *id; - std::vector templateArguments; - - TemplateNameIdKey(const Identifier *id, const std::vector &templateArguments) - : id(id), templateArguments(templateArguments) - { } - - bool operator == (const TemplateNameIdKey &other) const - { return id == other.id && templateArguments == other.templateArguments; } - - bool operator != (const TemplateNameIdKey &other) const - { return ! operator==(other); } - - bool operator < (const TemplateNameIdKey &other) const - { - if (id == other.id) - return std::lexicographical_compare(templateArguments.begin(), - templateArguments.end(), - other.templateArguments.begin(), - other.templateArguments.end()); - return id < other.id; - } - }; - - struct QualifiedNameIdKey { - std::vector names; - bool isGlobal; - - QualifiedNameIdKey(const std::vector &names, bool isGlobal) : - names(names), isGlobal(isGlobal) - { } - - bool operator == (const QualifiedNameIdKey &other) const - { return isGlobal == other.isGlobal && names == other.names; } - - bool operator != (const QualifiedNameIdKey &other) const - { return ! operator==(other); } - - bool operator < (const QualifiedNameIdKey &other) const - { - if (isGlobal == other.isGlobal) - return std::lexicographical_compare(names.begin(), names.end(), - other.names.begin(), other.names.end()); - return isGlobal < other.isGlobal; - } - }; - - struct SelectorNameIdKey { - std::vector _names; - bool _hasArguments; - - SelectorNameIdKey(const std::vector &names, bool hasArguments): _names(names), _hasArguments(hasArguments) {} - - bool operator==(const SelectorNameIdKey &other) const - { return _names == other._names && _hasArguments == other._hasArguments; } - - bool operator!=(const SelectorNameIdKey &other) const - { return !operator==(other); } - - bool operator<(const SelectorNameIdKey &other) const - { - if (_hasArguments == other._hasArguments) - return std::lexicographical_compare(_names.begin(), _names.end(), other._names.begin(), other._names.end()); - else - return _hasArguments < other._hasArguments; - } - }; - Control *control; TranslationUnit *translationUnit; DiagnosticClient *diagnosticClient; @@ -521,13 +467,13 @@ public: // ### replace std::map with lookup tables. ASAP! // names - std::map nameIds; - std::map destructorNameIds; - std::map operatorNameIds; - std::map conversionNameIds; - std::map templateNameIds; - std::map qualifiedNameIds; - std::map selectorNameIds; + Table nameIds; + Table destructorNameIds; + Table operatorNameIds; + Table conversionNameIds; + Table templateNameIds; + Table qualifiedNameIds; + Table selectorNameIds; // types VoidType voidType; @@ -641,8 +587,7 @@ const TemplateNameId *Control::templateNameId(const Identifier *id, const FullySpecifiedType *const args, unsigned argv) { - std::vector templateArguments(args, args + argv); - return d->findOrInsertTemplateNameId(id, templateArguments); + return d->findOrInsertTemplateNameId(id, args, args + argv); } const DestructorNameId *Control::destructorNameId(const Identifier *id) @@ -658,16 +603,14 @@ const QualifiedNameId *Control::qualifiedNameId(const Name *const *names, unsigned nameCount, bool isGlobal) { - std::vector classOrNamespaceNames(names, names + nameCount); - return d->findOrInsertQualifiedNameId(classOrNamespaceNames, isGlobal); + return d->findOrInsertQualifiedNameId(names, names + nameCount, isGlobal); } const SelectorNameId *Control::selectorNameId(const Name *const *names, unsigned nameCount, bool hasArguments) { - std::vector selectorNames(names, names + nameCount); - return d->findOrInsertSelectorNameId(selectorNames, hasArguments); + return d->findOrInsertSelectorNameId(names, names + nameCount, hasArguments); } diff --git a/src/shared/cplusplus/Names.cpp b/src/shared/cplusplus/Names.cpp index 4bc64f497d7..79196da173f 100644 --- a/src/shared/cplusplus/Names.cpp +++ b/src/shared/cplusplus/Names.cpp @@ -50,17 +50,11 @@ #include "NameVisitor.h" #include "Literals.h" #include +#include #include using namespace CPlusPlus; -QualifiedNameId::QualifiedNameId(const Name *const *names, - unsigned nameCount, - bool isGlobal) - : _names(names, names + nameCount), - _isGlobal(isGlobal) -{ } - QualifiedNameId::~QualifiedNameId() { } @@ -159,13 +153,6 @@ bool DestructorNameId::isEqualTo(const Name *other) const return l->isEqualTo(r); } -TemplateNameId::TemplateNameId(const Identifier *identifier, - const FullySpecifiedType templateArguments[], - unsigned templateArgumentCount) - : _identifier(identifier), - _templateArguments(templateArguments, templateArguments + templateArgumentCount) -{ } - TemplateNameId::~TemplateNameId() { } @@ -249,13 +236,6 @@ bool ConversionNameId::isEqualTo(const Name *other) const return _type.isEqualTo(c->type()); } -SelectorNameId::SelectorNameId(const Name *const *names, - unsigned nameCount, - bool hasArguments) - : _names(names, names + nameCount), - _hasArguments(hasArguments) -{ } - SelectorNameId::~SelectorNameId() { } diff --git a/src/shared/cplusplus/Names.h b/src/shared/cplusplus/Names.h index 9bae0e07fb5..9ce31a9b60e 100644 --- a/src/shared/cplusplus/Names.h +++ b/src/shared/cplusplus/Names.h @@ -59,7 +59,10 @@ namespace CPlusPlus { class CPLUSPLUS_EXPORT QualifiedNameId: public Name { public: - QualifiedNameId(const Name *const *names, unsigned nameCount, bool isGlobal = false); + template + QualifiedNameId(_Iterator first, _Iterator last, bool isGlobal = false) + : _names(first, last), _isGlobal(isGlobal) {} + virtual ~QualifiedNameId(); virtual const Identifier *identifier() const; @@ -75,6 +78,11 @@ public: virtual const QualifiedNameId *asQualifiedNameId() const { return this; } + typedef std::vector::const_iterator NameIterator; + + NameIterator firstName() const { return _names.begin(); } + NameIterator lastName() const { return _names.end(); } + protected: virtual void accept0(NameVisitor *visitor) const; @@ -126,9 +134,10 @@ private: class CPLUSPLUS_EXPORT TemplateNameId: public Name { public: - TemplateNameId(const Identifier *identifier, - const FullySpecifiedType templateArguments[], - unsigned templateArgumentCount); + template + TemplateNameId(const Identifier *identifier, _Iterator first, _Iterator last) + : _identifier(identifier), _templateArguments(first, last) {} + virtual ~TemplateNameId(); virtual const Identifier *identifier() const; @@ -142,6 +151,11 @@ public: virtual const TemplateNameId *asTemplateNameId() const { return this; } + typedef std::vector::const_iterator TemplateArgumentIterator; + + TemplateArgumentIterator firstTemplateArgument() const { return _templateArguments.begin(); } + TemplateArgumentIterator lastTemplateArgument() const { return _templateArguments.end(); } + protected: virtual void accept0(NameVisitor *visitor) const; @@ -250,7 +264,10 @@ private: class CPLUSPLUS_EXPORT SelectorNameId: public Name { public: - SelectorNameId(const Name *const *names, unsigned nameCount, bool hasArguments); + template + SelectorNameId(_Iterator first, _Iterator last, bool hasArguments) + : _names(first, last), _hasArguments(hasArguments) {} + virtual ~SelectorNameId(); virtual const Identifier *identifier() const; @@ -264,6 +281,11 @@ public: virtual const SelectorNameId *asSelectorNameId() const { return this; } + typedef std::vector::const_iterator NameIterator; + + NameIterator firstName() const { return _names.begin(); } + NameIterator lastName() const { return _names.end(); } + protected: virtual void accept0(NameVisitor *visitor) const; @@ -274,5 +296,4 @@ private: } // end of namespace CPlusPlus - #endif // CPLUSPLUS_NAMES_H