Revert Attributes::Attrs back to return an attr_list and mark it deprecated

This commit is contained in:
Tim Wojtulewicz 2020-06-29 17:47:45 -07:00
parent e1338cc379
commit 5b8aaf6497
4 changed files with 36 additions and 13 deletions

3
NEWS
View file

@ -105,9 +105,6 @@ Changed Functionality
- ``AsVector()`` has changed to return ``std::vector<IntrusivePtr<Val>>*``. - ``AsVector()`` has changed to return ``std::vector<IntrusivePtr<Val>>*``.
- ``Attributes::Attrs()`` now returns ``const std::vector<IntrusivePtr<Attr>>&``
instead of ``attr_list*``
- Moved a large number of classes from the global namespace into either the - Moved a large number of classes from the global namespace into either the
``zeek`` or ``zeek::detail`` namespace. See https://github.com/zeek/zeek/issues/266 ``zeek`` or ``zeek::detail`` namespace. See https://github.com/zeek/zeek/issues/266
for the rationale behind these changes. Most types that were moved and functions for the rationale behind these changes. Most types that were moved and functions

View file

@ -48,8 +48,6 @@ Attr::Attr(::attr_tag t) : Attr(static_cast<attr_tag>(t))
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
Attr::~Attr() = default;
void Attr::SetAttrExpr(IntrusivePtr<zeek::detail::Expr> e) void Attr::SetAttrExpr(IntrusivePtr<zeek::detail::Expr> e)
{ expr = std::move(e); } { expr = std::move(e); }
@ -192,10 +190,17 @@ Attributes::Attributes(std::vector<IntrusivePtr<Attr>> a,
AddAttr(std::move(attr)); AddAttr(std::move(attr));
} }
Attributes::~Attributes()
{
attrs_list.clear();
attrs.clear();
}
void Attributes::AddAttr(IntrusivePtr<Attr> attr) void Attributes::AddAttr(IntrusivePtr<Attr> attr)
{ {
// We overwrite old attributes by deleting them first. // We overwrite old attributes by deleting them first.
RemoveAttr(attr->Tag()); RemoveAttr(attr->Tag());
attrs_list.push_back(attr.get());
attrs.emplace_back(attr); attrs.emplace_back(attr);
// We only check the attribute after we've added it, to facilitate // We only check the attribute after we've added it, to facilitate
@ -206,23 +211,31 @@ void Attributes::AddAttr(IntrusivePtr<Attr> attr)
// those attributes only have meaning for a redefinable value. // those attributes only have meaning for a redefinable value.
if ( (attr->Tag() == ATTR_ADD_FUNC || attr->Tag() == ATTR_DEL_FUNC) && if ( (attr->Tag() == ATTR_ADD_FUNC || attr->Tag() == ATTR_DEL_FUNC) &&
! Find(ATTR_REDEF) ) ! Find(ATTR_REDEF) )
attrs.emplace_back(make_intrusive<Attr>(ATTR_REDEF)); {
auto a = make_intrusive<Attr>(ATTR_REDEF);
attrs_list.push_back(a.get());
attrs.emplace_back(a);
}
// For DEFAULT, add an implicit OPTIONAL if it's not a global. // For DEFAULT, add an implicit OPTIONAL if it's not a global.
if ( ! global_var && attr->Tag() == ATTR_DEFAULT && if ( ! global_var && attr->Tag() == ATTR_DEFAULT &&
! Find(ATTR_OPTIONAL) ) ! Find(ATTR_OPTIONAL) )
attrs.emplace_back(make_intrusive<Attr>(ATTR_OPTIONAL)); {
auto a = make_intrusive<Attr>(ATTR_OPTIONAL);
attrs_list.push_back(a.get());
attrs.emplace_back(a);
}
} }
void Attributes::AddAttrs(const IntrusivePtr<Attributes>& a) void Attributes::AddAttrs(const IntrusivePtr<Attributes>& a)
{ {
for ( const auto& attr : a->Attrs() ) for ( const auto& attr : a->GetAttrs() )
AddAttr(attr); AddAttr(attr);
} }
void Attributes::AddAttrs(Attributes* a) void Attributes::AddAttrs(Attributes* a)
{ {
for ( const auto& attr : a->Attrs() ) for ( const auto& attr : a->GetAttrs() )
AddAttr(attr); AddAttr(attr);
Unref(a); Unref(a);
@ -248,6 +261,10 @@ const IntrusivePtr<Attr>& Attributes::Find(attr_tag t) const
void Attributes::RemoveAttr(attr_tag t) void Attributes::RemoveAttr(attr_tag t)
{ {
for ( int i = 0; i < attrs_list.length(); i++ )
if ( attrs_list[i]->Tag() == t )
attrs_list.remove_nth(i--);
for ( auto it = attrs.begin(); it != attrs.end(); ) for ( auto it = attrs.begin(); it != attrs.end(); )
{ {
if ( (*it)->Tag() == t ) if ( (*it)->Tag() == t )

View file

@ -76,7 +76,7 @@ public:
explicit Attr(::attr_tag t); explicit Attr(::attr_tag t);
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
~Attr() override; ~Attr() override = default;
attr_tag Tag() const { return tag; } attr_tag Tag() const { return tag; }
@ -122,6 +122,8 @@ public:
bool in_record, bool is_global); bool in_record, bool is_global);
Attributes(IntrusivePtr<Type> t, bool in_record, bool is_global); Attributes(IntrusivePtr<Type> t, bool in_record, bool is_global);
~Attributes() override;
void AddAttr(IntrusivePtr<Attr> a); void AddAttr(IntrusivePtr<Attr> a);
void AddAttrs(const IntrusivePtr<Attributes>& a); void AddAttrs(const IntrusivePtr<Attributes>& a);
@ -147,7 +149,11 @@ public:
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
void DescribeReST(ODesc* d, bool shorten = false) const; void DescribeReST(ODesc* d, bool shorten = false) const;
const std::vector<IntrusivePtr<Attr>>& Attrs() const [[deprecated("Remove in v4.1. Use GetAttrs().")]]
const attr_list* Attrs() const
{ return &attrs_list; }
const std::vector<IntrusivePtr<Attr>>& GetAttrs() const
{ return attrs; } { return attrs; }
bool operator==(const Attributes& other) const; bool operator==(const Attributes& other) const;
@ -157,6 +163,9 @@ protected:
IntrusivePtr<Type> type; IntrusivePtr<Type> type;
std::vector<IntrusivePtr<Attr>> attrs; std::vector<IntrusivePtr<Attr>> attrs;
// Remove in v4.1. This is used by Attrs(), which is deprecated.
attr_list attrs_list;
bool in_record; bool in_record;
bool global_var; bool global_var;
}; };

View file

@ -2076,7 +2076,7 @@ bool AssignExpr::TypeCheck(const IntrusivePtr<Attributes>& attrs)
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attr_copy; std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attr_copy;
if ( attrs ) if ( attrs )
attr_copy = std::make_unique<std::vector<IntrusivePtr<Attr>>>(attrs->Attrs()); attr_copy = std::make_unique<std::vector<IntrusivePtr<Attr>>>(attrs->GetAttrs());
bool empty_list_assignment = (op2->AsListExpr()->Exprs().empty()); bool empty_list_assignment = (op2->AsListExpr()->Exprs().empty());
@ -2172,7 +2172,7 @@ bool AssignExpr::TypeCheck(const IntrusivePtr<Attributes>& attrs)
if ( sce->GetAttrs() ) if ( sce->GetAttrs() )
{ {
const auto& a = sce->GetAttrs()->Attrs(); const auto& a = sce->GetAttrs()->GetAttrs();
attr_copy = std::make_unique<std::vector<IntrusivePtr<Attr>>>(a); attr_copy = std::make_unique<std::vector<IntrusivePtr<Attr>>>(a);
} }