diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index 3970e20ab5..2ae0b06455 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -141,7 +141,7 @@ BroType* OpaqueVal::UnserializeType(const broker::data& data) if ( ! tag ) return nullptr; - return base_type(static_cast(*tag)).release(); + return base_type(static_cast(*tag))->Ref(); } IntrusivePtr OpaqueVal::DoClone(CloneState* state) diff --git a/src/Type.cc b/src/Type.cc index 131346ceb1..d4cbfd0f15 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1365,7 +1365,7 @@ BroType* VectorType::YieldType() // return any as that's what other code historically expects for type // comparisions. if ( IsUnspecifiedVector() ) - return base_type_no_ref(TYPE_ANY); + return ::base_type(TYPE_ANY).get(); return yield_type.get(); } @@ -1431,26 +1431,22 @@ void VectorType::DescribeReST(ODesc* d, bool roles_only) const d->Add(fmt(":zeek:type:`%s`", yield_type->GetName().c_str())); } -BroType* base_type_no_ref(TypeTag tag) +const IntrusivePtr& base_type(TypeTag tag) { - static BroType* base_types[NUM_TYPES]; + static IntrusivePtr base_types[NUM_TYPES]; - // We could check here that "tag" actually corresponds to a BRO - // basic type. - - int t = int(tag); - if ( ! base_types[t] ) + // We could check here that "tag" actually corresponds to a basic type. + if ( ! base_types[tag] ) { - base_types[t] = new BroType(tag, true); + base_types[tag] = make_intrusive(tag, true); // Give the base types a pseudo-location for easier identification. Location l(type_name(tag), 0, 0, 0, 0); - base_types[t]->SetLocationInfo(&l); + base_types[tag]->SetLocationInfo(&l); } - return base_types[t]; + return base_types[tag]; } - // Returns true if t1 is initialization-compatible with t2 (i.e., if an // initializer with type t1 can be used to initialize a value with type t2), // false otherwise. Assumes that t1's tag is different from t2's. Note diff --git a/src/Type.h b/src/Type.h index 29f7e8383f..d8c24e365a 100644 --- a/src/Type.h +++ b/src/Type.h @@ -757,16 +757,16 @@ extern OpaqueType* ocsp_resp_opaque_type; extern OpaqueType* paraglob_type; // Returns the basic (non-parameterized) type with the given type. -// The reference count of the type is not increased. -BroType* base_type_no_ref(TypeTag tag); +const IntrusivePtr& base_type(TypeTag tag); // Returns the basic (non-parameterized) type with the given type. -// The caller assumes responsibility for a reference to the type. -inline IntrusivePtr base_type(TypeTag tag) - { return {NewRef{}, base_type_no_ref(tag)}; } +// The reference count of the type is not increased. +[[deprecated("Remove in v4.1. Use ::base_type() instead")]] +inline BroType* base_type_no_ref(TypeTag tag) + { return base_type(tag).get(); } // Returns the basic error type. -inline IntrusivePtr error_type() { return base_type(TYPE_ERROR); } +inline const IntrusivePtr& error_type() { return base_type(TYPE_ERROR); } // True if the two types are equivalent. If is_init is true then the test is // done in the context of an initialization. If match_record_field_names is diff --git a/src/Val.cc b/src/Val.cc index 622510d203..8d1a1707e7 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1122,7 +1122,7 @@ IntrusivePtr StringVal::DoClone(CloneState* state) } PatternVal::PatternVal(RE_Matcher* re) - : Val(base_type_no_ref(TYPE_PATTERN)) + : Val(base_type(TYPE_PATTERN).get()) { val.re_val = re; } @@ -1130,7 +1130,6 @@ PatternVal::PatternVal(RE_Matcher* re) PatternVal::~PatternVal() { delete AsPattern(); - Unref(type); // base_type() ref'd it, so did our base constructor } bool PatternVal::AddTo(Val* v, bool /* is_first_init */) const diff --git a/src/Val.h b/src/Val.h index 95a687709c..455ae7b19c 100644 --- a/src/Val.h +++ b/src/Val.h @@ -126,7 +126,7 @@ union BroValUnion { class Val : public BroObj { public: Val(double d, TypeTag t) - : val(d), type(base_type(t).release()) + : val(d), type(base_type(t)->Ref()) { } @@ -143,7 +143,7 @@ public: } Val() - : val(bro_int_t(0)), type(base_type(TYPE_ERROR).release()) + : val(bro_int_t(0)), type(base_type(TYPE_ERROR)->Ref()) { } @@ -341,7 +341,7 @@ protected: template Val(V &&v, TypeTag t) noexcept - : val(std::forward(v)), type(base_type(t).release()) + : val(std::forward(v)), type(base_type(t)->Ref()) { } diff --git a/src/parse.y b/src/parse.y index 928340f20b..00f2e65874 100644 --- a/src/parse.y +++ b/src/parse.y @@ -843,72 +843,72 @@ enum_body_elem: type: TOK_BOOL { set_location(@1); - $$ = base_type(TYPE_BOOL).release(); + $$ = base_type(TYPE_BOOL)->Ref(); } | TOK_INT { set_location(@1); - $$ = base_type(TYPE_INT).release(); + $$ = base_type(TYPE_INT)->Ref(); } | TOK_COUNT { set_location(@1); - $$ = base_type(TYPE_COUNT).release(); + $$ = base_type(TYPE_COUNT)->Ref(); } | TOK_COUNTER { set_location(@1); - $$ = base_type(TYPE_COUNTER).release(); + $$ = base_type(TYPE_COUNTER)->Ref(); } | TOK_DOUBLE { set_location(@1); - $$ = base_type(TYPE_DOUBLE).release(); + $$ = base_type(TYPE_DOUBLE)->Ref(); } | TOK_TIME { set_location(@1); - $$ = base_type(TYPE_TIME).release(); + $$ = base_type(TYPE_TIME)->Ref(); } | TOK_INTERVAL { set_location(@1); - $$ = base_type(TYPE_INTERVAL).release(); + $$ = base_type(TYPE_INTERVAL)->Ref(); } | TOK_STRING { set_location(@1); - $$ = base_type(TYPE_STRING).release(); + $$ = base_type(TYPE_STRING)->Ref(); } | TOK_PATTERN { set_location(@1); - $$ = base_type(TYPE_PATTERN).release(); + $$ = base_type(TYPE_PATTERN)->Ref(); } | TOK_TIMER { set_location(@1); - $$ = base_type(TYPE_TIMER).release(); + $$ = base_type(TYPE_TIMER)->Ref(); } | TOK_PORT { set_location(@1); - $$ = base_type(TYPE_PORT).release(); + $$ = base_type(TYPE_PORT)->Ref(); } | TOK_ADDR { set_location(@1); - $$ = base_type(TYPE_ADDR).release(); + $$ = base_type(TYPE_ADDR)->Ref(); } | TOK_SUBNET { set_location(@1); - $$ = base_type(TYPE_SUBNET).release(); + $$ = base_type(TYPE_SUBNET)->Ref(); } | TOK_ANY { set_location(@1); - $$ = base_type(TYPE_ANY).release(); + $$ = base_type(TYPE_ANY)->Ref(); } | TOK_TABLE '[' type_list ']' TOK_OF type @@ -1012,7 +1012,7 @@ type: NullStmt here; if ( $1 ) $1->Error("not a Zeek type", &here); - $$ = error_type().release(); + $$ = error_type()->Ref(); } else {