diff --git a/NEWS b/NEWS index 9c420cf241..fd24ba04a2 100644 --- a/NEWS +++ b/NEWS @@ -134,6 +134,9 @@ Deprecated Functionality - Marked the Continuation.h and PacketDumper.h files as deprecated. The code contained within them is unused by Zeek. +- ``Type::GetAliases()`` and ``Type::AddAlias()`` are deprecated, use + ``Type::Aliases()`` and ``Type::RegisterAlias()``. + Zeek 3.2.0 ========== diff --git a/src/Type.cc b/src/Type.cc index 30e05805e3..c4cc71d310 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1348,13 +1348,11 @@ void EnumType::CheckAndAddName(const string& module_name, const char* name, if ( vals.find(val) == vals.end() ) vals[val] = make_intrusive(IntrusivePtr{NewRef{}, this}, val); - set types = Type::GetAliases(GetName()); - set::const_iterator it; + const auto& types = Type::Aliases(GetName()); - for ( it = types.begin(); it != types.end(); ++it ) - if ( *it != this ) - (*it)->AsEnumType()->AddNameInternal(module_name, name, val, - is_export); + for ( const auto& t : types ) + if ( t.get() != this ) + t->AsEnumType()->AddNameInternal(module_name, name, val, is_export); } void EnumType::AddNameInternal(const string& module_name, const char* name, diff --git a/src/Type.h b/src/Type.h index 52dc2264a5..fc909de25d 100644 --- a/src/Type.h +++ b/src/Type.h @@ -270,13 +270,64 @@ public: void SetName(const std::string& arg_name) { name = arg_name; } const std::string& GetName() const { return name; } - typedef std::map > TypeAliasMap; + struct TypePtrComparer { + bool operator()(const TypePtr& a, const TypePtr& b) const + { return a.get() < b.get(); } + }; + using TypePtrSet = std::set; + using TypeAliasMap = std::map>; + /** + * Returns a mapping of type-name to all other type names declared as + * an alias to it. + */ + static const TypeAliasMap& GetAliasMap() + { return type_aliases; } + + /** + * Returns true if the given type name has any declared aliases + */ + static bool HasAliases(std::string_view type_name) + { return Type::type_aliases.find(type_name) != Type::type_aliases.end(); } + + /** + * Returns the set of all type names declared as an aliases to the given + * type name. A static empty set is returned if there are no aliases. + */ + static const TypePtrSet& Aliases(std::string_view type_name) + { + static TypePtrSet empty; + auto it = Type::type_aliases.find(type_name); + return it == Type::type_aliases.end() ? empty : it->second; + } + + [[deprecated("Remove in v4.1. Use zeek::Type::Aliases() instead.")]] static std::set GetAliases(const std::string& type_name) - { return Type::type_aliases[type_name]; } + { + std::set rval; + for ( const auto& t : Type::type_aliases[type_name] ) + rval.emplace(t.get()); + return rval; + } + /** + * Registers a new type alias. + * @param type_name the name of the type to register a new alias for. + * @param type the associated alias type of *type_name*. + * @return true if the alias is now registered or false if the alias was + * already previously registered. + */ + static bool RegisterAlias(std::string_view type_name, TypePtr type) + { + auto it = Type::type_aliases.find(type_name); + if ( it == Type::type_aliases.end() ) + it = Type::type_aliases.emplace(std::string{type_name}, TypePtrSet{}).first; + return it->second.emplace(std::move(type)).second; + } + + [[deprecated("Remove in v4.1. Use zeek::Type::RegisterAlias().")]] static void AddAlias(const std::string &type_name, Type* type) - { Type::type_aliases[type_name].insert(type); } + { Type::type_aliases[type_name].insert({NewRef{}, type}); } protected: Type() = default; diff --git a/src/Var.cc b/src/Var.cc index a0adcb8f4f..10fe916b72 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -382,10 +382,10 @@ void add_type(ID* id, TypePtr t, std::unique_ptr> attr) // Clone the type to preserve type name aliasing. tnew = t->ShallowClone(); - Type::AddAlias(new_type_name, tnew.get()); + Type::RegisterAlias(new_type_name, tnew); if ( new_type_name != old_type_name && ! old_type_name.empty() ) - Type::AddAlias(old_type_name, tnew.get()); + Type::RegisterAlias(old_type_name, tnew); tnew->SetName(id->Name()); diff --git a/src/parse.y b/src/parse.y index 83e57e8aa3..9d9076312c 100644 --- a/src/parse.y +++ b/src/parse.y @@ -171,7 +171,7 @@ static void parser_redef_enum (zeek::detail::ID *id) static void extend_record(zeek::detail::ID* id, std::unique_ptr fields, std::unique_ptr> attrs) { - std::set types = zeek::Type::GetAliases(id->Name()); + const auto& types = zeek::Type::Aliases(id->Name()); if ( types.empty() ) { @@ -189,7 +189,7 @@ static void extend_record(zeek::detail::ID* id, std::unique_ptrAsRecordType()->AddFields(*fields, add_log_attr);