Change base_type() to return const-ref, deprecate base_type_no_ref()

This commit is contained in:
Jon Siwek 2020-05-07 16:16:18 -07:00
parent 2aa84eb86e
commit bb25f5d568
6 changed files with 34 additions and 39 deletions

View file

@ -141,7 +141,7 @@ BroType* OpaqueVal::UnserializeType(const broker::data& data)
if ( ! tag ) if ( ! tag )
return nullptr; return nullptr;
return base_type(static_cast<TypeTag>(*tag)).release(); return base_type(static_cast<TypeTag>(*tag))->Ref();
} }
IntrusivePtr<Val> OpaqueVal::DoClone(CloneState* state) IntrusivePtr<Val> OpaqueVal::DoClone(CloneState* state)

View file

@ -1365,7 +1365,7 @@ BroType* VectorType::YieldType()
// return any as that's what other code historically expects for type // return any as that's what other code historically expects for type
// comparisions. // comparisions.
if ( IsUnspecifiedVector() ) if ( IsUnspecifiedVector() )
return base_type_no_ref(TYPE_ANY); return ::base_type(TYPE_ANY).get();
return yield_type.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())); d->Add(fmt(":zeek:type:`%s`", yield_type->GetName().c_str()));
} }
BroType* base_type_no_ref(TypeTag tag) const IntrusivePtr<BroType>& base_type(TypeTag tag)
{ {
static BroType* base_types[NUM_TYPES]; static IntrusivePtr<BroType> base_types[NUM_TYPES];
// We could check here that "tag" actually corresponds to a BRO // We could check here that "tag" actually corresponds to a basic type.
// basic type. if ( ! base_types[tag] )
int t = int(tag);
if ( ! base_types[t] )
{ {
base_types[t] = new BroType(tag, true); base_types[tag] = make_intrusive<BroType>(tag, true);
// Give the base types a pseudo-location for easier identification. // Give the base types a pseudo-location for easier identification.
Location l(type_name(tag), 0, 0, 0, 0); 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 // 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), // 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 // false otherwise. Assumes that t1's tag is different from t2's. Note

View file

@ -757,16 +757,16 @@ extern OpaqueType* ocsp_resp_opaque_type;
extern OpaqueType* paraglob_type; extern OpaqueType* paraglob_type;
// Returns the basic (non-parameterized) type with the given type. // Returns the basic (non-parameterized) type with the given type.
// The reference count of the type is not increased. const IntrusivePtr<BroType>& base_type(TypeTag tag);
BroType* base_type_no_ref(TypeTag tag);
// Returns the basic (non-parameterized) type with the given type. // Returns the basic (non-parameterized) type with the given type.
// The caller assumes responsibility for a reference to the type. // The reference count of the type is not increased.
inline IntrusivePtr<BroType> base_type(TypeTag tag) [[deprecated("Remove in v4.1. Use ::base_type() instead")]]
{ return {NewRef{}, base_type_no_ref(tag)}; } inline BroType* base_type_no_ref(TypeTag tag)
{ return base_type(tag).get(); }
// Returns the basic error type. // Returns the basic error type.
inline IntrusivePtr<BroType> error_type() { return base_type(TYPE_ERROR); } inline const IntrusivePtr<BroType>& error_type() { return base_type(TYPE_ERROR); }
// True if the two types are equivalent. If is_init is true then the test is // 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 // done in the context of an initialization. If match_record_field_names is

View file

@ -1122,7 +1122,7 @@ IntrusivePtr<Val> StringVal::DoClone(CloneState* state)
} }
PatternVal::PatternVal(RE_Matcher* re) PatternVal::PatternVal(RE_Matcher* re)
: Val(base_type_no_ref(TYPE_PATTERN)) : Val(base_type(TYPE_PATTERN).get())
{ {
val.re_val = re; val.re_val = re;
} }
@ -1130,7 +1130,6 @@ PatternVal::PatternVal(RE_Matcher* re)
PatternVal::~PatternVal() PatternVal::~PatternVal()
{ {
delete AsPattern(); delete AsPattern();
Unref(type); // base_type() ref'd it, so did our base constructor
} }
bool PatternVal::AddTo(Val* v, bool /* is_first_init */) const bool PatternVal::AddTo(Val* v, bool /* is_first_init */) const

View file

@ -126,7 +126,7 @@ union BroValUnion {
class Val : public BroObj { class Val : public BroObj {
public: public:
Val(double d, TypeTag t) 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()
: 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<typename V> template<typename V>
Val(V &&v, TypeTag t) noexcept Val(V &&v, TypeTag t) noexcept
: val(std::forward<V>(v)), type(base_type(t).release()) : val(std::forward<V>(v)), type(base_type(t)->Ref())
{ {
} }

View file

@ -843,72 +843,72 @@ enum_body_elem:
type: type:
TOK_BOOL { TOK_BOOL {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_BOOL).release(); $$ = base_type(TYPE_BOOL)->Ref();
} }
| TOK_INT { | TOK_INT {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_INT).release(); $$ = base_type(TYPE_INT)->Ref();
} }
| TOK_COUNT { | TOK_COUNT {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_COUNT).release(); $$ = base_type(TYPE_COUNT)->Ref();
} }
| TOK_COUNTER { | TOK_COUNTER {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_COUNTER).release(); $$ = base_type(TYPE_COUNTER)->Ref();
} }
| TOK_DOUBLE { | TOK_DOUBLE {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_DOUBLE).release(); $$ = base_type(TYPE_DOUBLE)->Ref();
} }
| TOK_TIME { | TOK_TIME {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_TIME).release(); $$ = base_type(TYPE_TIME)->Ref();
} }
| TOK_INTERVAL { | TOK_INTERVAL {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_INTERVAL).release(); $$ = base_type(TYPE_INTERVAL)->Ref();
} }
| TOK_STRING { | TOK_STRING {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_STRING).release(); $$ = base_type(TYPE_STRING)->Ref();
} }
| TOK_PATTERN { | TOK_PATTERN {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_PATTERN).release(); $$ = base_type(TYPE_PATTERN)->Ref();
} }
| TOK_TIMER { | TOK_TIMER {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_TIMER).release(); $$ = base_type(TYPE_TIMER)->Ref();
} }
| TOK_PORT { | TOK_PORT {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_PORT).release(); $$ = base_type(TYPE_PORT)->Ref();
} }
| TOK_ADDR { | TOK_ADDR {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_ADDR).release(); $$ = base_type(TYPE_ADDR)->Ref();
} }
| TOK_SUBNET { | TOK_SUBNET {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_SUBNET).release(); $$ = base_type(TYPE_SUBNET)->Ref();
} }
| TOK_ANY { | TOK_ANY {
set_location(@1); set_location(@1);
$$ = base_type(TYPE_ANY).release(); $$ = base_type(TYPE_ANY)->Ref();
} }
| TOK_TABLE '[' type_list ']' TOK_OF type | TOK_TABLE '[' type_list ']' TOK_OF type
@ -1012,7 +1012,7 @@ type:
NullStmt here; NullStmt here;
if ( $1 ) if ( $1 )
$1->Error("not a Zeek type", &here); $1->Error("not a Zeek type", &here);
$$ = error_type().release(); $$ = error_type()->Ref();
} }
else else
{ {