// See the file "COPYING" in the main distribution directory for copyright. #pragma once #include #include #include #include "Obj.h" #include "BroList.h" #include "TraverseTypes.h" template class IntrusivePtr; class ID; class BroType; class ListVal; class Scope : public BroObj { public: explicit Scope(ID* id, attr_list* al); ~Scope() override; template ID* Lookup(N&& name) const { const auto& entry = local.find(std::forward(name)); if ( entry != local.end() ) return entry->second; return nullptr; } template void Insert(N&& name, ID* id) { auto [it, inserted] = local.emplace(std::forward(name), id); if ( ! inserted ) { Unref(it->second); it->second = id; } } template ID* Remove(N&& name) { const auto& entry = local.find(std::forward(name)); if ( entry != local.end() ) { ID* id = entry->second; local.erase(entry); return id; } return nullptr; } ID* ScopeID() const { return scope_id; } attr_list* Attrs() const { return attrs; } BroType* ReturnType() const { return return_type; } size_t Length() const { return local.size(); } const std::map& Vars() { return local; } ID* GenerateTemporary(const char* name); // Returns the list of variables needing initialization, and // removes it from this Scope. id_list* GetInits(); // Adds a variable to the list. void AddInit(ID* id) { inits->push_back(id); } void Describe(ODesc* d) const override; TraversalCode Traverse(TraversalCallback* cb) const; protected: ID* scope_id; attr_list* attrs; BroType* return_type; std::map local; id_list* inits; }; extern bool in_debug; // If no_global is true, don't search in the default "global" namespace. extern IntrusivePtr lookup_ID(const char* name, const char* module, bool no_global = false, bool same_module_only = false, bool check_export = true); extern IntrusivePtr install_ID(const char* name, const char* module_name, bool is_global, bool is_export); extern void push_scope(ID* id, attr_list* attrs); extern void push_existing_scope(Scope* scope); // Returns the one popped off. extern IntrusivePtr pop_scope(); extern Scope* current_scope(); extern Scope* global_scope(); // Current module (identified by its name). extern std::string current_module;