Scope: use class IntrusivePtr

This commit is contained in:
Max Kellermann 2020-03-05 19:49:21 +01:00
parent 36a26a7b43
commit dc518c0fb4
3 changed files with 14 additions and 20 deletions

View file

@ -16,9 +16,9 @@ static scope_list scopes;
static Scope* top_scope; static Scope* top_scope;
Scope::Scope(ID* id, attr_list* al) Scope::Scope(IntrusivePtr<ID> id, attr_list* al)
:scope_id(std::move(id))
{ {
scope_id = id;
attrs = al; attrs = al;
return_type = 0; return_type = 0;
@ -26,19 +26,15 @@ Scope::Scope(ID* id, attr_list* al)
if ( id ) if ( id )
{ {
BroType* id_type = id->Type(); BroType* id_type = scope_id->Type();
if ( id_type->Tag() == TYPE_ERROR ) if ( id_type->Tag() == TYPE_ERROR )
return; return;
else if ( id_type->Tag() != TYPE_FUNC ) else if ( id_type->Tag() != TYPE_FUNC )
reporter->InternalError("bad scope id"); reporter->InternalError("bad scope id");
Ref(id);
FuncType* ft = id->Type()->AsFuncType(); FuncType* ft = id->Type()->AsFuncType();
return_type = ft->YieldType(); return_type = {NewRef{}, ft->YieldType()};
if ( return_type )
Ref(return_type);
} }
} }
@ -55,9 +51,6 @@ Scope::~Scope()
delete attrs; delete attrs;
} }
Unref(scope_id);
Unref(return_type);
if ( inits ) if ( inits )
{ {
for ( const auto& i : *inits ) for ( const auto& i : *inits )
@ -198,9 +191,9 @@ void push_existing_scope(Scope* scope)
scopes.push_back(scope); scopes.push_back(scope);
} }
void push_scope(ID* id, attr_list* attrs) void push_scope(IntrusivePtr<ID> id, attr_list* attrs)
{ {
top_scope = new Scope(id, attrs); top_scope = new Scope(std::move(id), attrs);
scopes.push_back(top_scope); scopes.push_back(top_scope);
} }

View file

@ -8,6 +8,7 @@
#include "Obj.h" #include "Obj.h"
#include "BroList.h" #include "BroList.h"
#include "IntrusivePtr.h"
#include "TraverseTypes.h" #include "TraverseTypes.h"
template <class T> class IntrusivePtr; template <class T> class IntrusivePtr;
@ -17,7 +18,7 @@ class ListVal;
class Scope : public BroObj { class Scope : public BroObj {
public: public:
explicit Scope(ID* id, attr_list* al); explicit Scope(IntrusivePtr<ID> id, attr_list* al);
~Scope() override; ~Scope() override;
template<typename N> template<typename N>
@ -58,9 +59,9 @@ public:
return nullptr; return nullptr;
} }
ID* ScopeID() const { return scope_id; } ID* ScopeID() const { return scope_id.get(); }
attr_list* Attrs() const { return attrs; } attr_list* Attrs() const { return attrs; }
BroType* ReturnType() const { return return_type; } BroType* ReturnType() const { return return_type.get(); }
size_t Length() const { return local.size(); } size_t Length() const { return local.size(); }
const std::map<std::string, ID*>& Vars() { return local; } const std::map<std::string, ID*>& Vars() { return local; }
@ -79,9 +80,9 @@ public:
TraversalCode Traverse(TraversalCallback* cb) const; TraversalCode Traverse(TraversalCallback* cb) const;
protected: protected:
ID* scope_id; IntrusivePtr<ID> scope_id;
attr_list* attrs; attr_list* attrs;
BroType* return_type; IntrusivePtr<BroType> return_type;
std::map<std::string, ID*> local; std::map<std::string, ID*> local;
id_list* inits; id_list* inits;
}; };
@ -98,7 +99,7 @@ extern IntrusivePtr<ID> lookup_ID(const char* name, const char* module,
extern IntrusivePtr<ID> install_ID(const char* name, const char* module_name, extern IntrusivePtr<ID> install_ID(const char* name, const char* module_name,
bool is_global, bool is_export); bool is_global, bool is_export);
extern void push_scope(ID* id, attr_list* attrs); extern void push_scope(IntrusivePtr<ID> id, attr_list* attrs);
extern void push_existing_scope(Scope* scope); extern void push_existing_scope(Scope* scope);
// Returns the one popped off. // Returns the one popped off.

View file

@ -394,7 +394,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor,
else else
id->SetType(t); id->SetType(t);
push_scope(id, attrs); push_scope({NewRef{}, id}, attrs);
RecordType* args = t->Args(); RecordType* args = t->Args();
int num_args = args->NumFields(); int num_args = args->NumFields();