mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 08:08:19 +00:00
Reformat the world
This commit is contained in:
parent
194cb24547
commit
b2f171ec69
714 changed files with 35149 additions and 35203 deletions
158
src/Func.h
158
src/Func.h
|
@ -2,38 +2,42 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "zeek/ZeekList.h"
|
||||
#include "zeek/Stmt.h"
|
||||
#include "zeek/BifReturnVal.h"
|
||||
#include "zeek/Obj.h"
|
||||
#include "zeek/Scope.h"
|
||||
#include "zeek/Type.h" /* for function_flavor */
|
||||
#include "zeek/Stmt.h"
|
||||
#include "zeek/TraverseTypes.h"
|
||||
#include "zeek/Type.h" /* for function_flavor */
|
||||
#include "zeek/ZeekArgs.h"
|
||||
#include "zeek/BifReturnVal.h"
|
||||
#include "zeek/ZeekList.h"
|
||||
|
||||
namespace caf {
|
||||
namespace caf
|
||||
{
|
||||
template <class> class expected;
|
||||
}
|
||||
}
|
||||
|
||||
namespace broker {
|
||||
namespace broker
|
||||
{
|
||||
class data;
|
||||
using vector = std::vector<data>;
|
||||
using caf::expected;
|
||||
}
|
||||
}
|
||||
|
||||
namespace zeek {
|
||||
namespace zeek
|
||||
{
|
||||
|
||||
class Val;
|
||||
class FuncType;
|
||||
|
||||
namespace detail {
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class Scope;
|
||||
class Stmt;
|
||||
|
@ -46,34 +50,41 @@ using StmtPtr = IntrusivePtr<Stmt>;
|
|||
|
||||
class ScriptFunc;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace detail
|
||||
|
||||
class Func;
|
||||
using FuncPtr = IntrusivePtr<Func>;
|
||||
|
||||
class Func : public Obj {
|
||||
class Func : public Obj
|
||||
{
|
||||
public:
|
||||
|
||||
static inline const FuncPtr nil;
|
||||
|
||||
enum Kind { SCRIPT_FUNC, BUILTIN_FUNC };
|
||||
enum Kind
|
||||
{
|
||||
SCRIPT_FUNC,
|
||||
BUILTIN_FUNC
|
||||
};
|
||||
|
||||
explicit Func(Kind arg_kind);
|
||||
|
||||
~Func() override;
|
||||
|
||||
virtual bool IsPure() const = 0;
|
||||
FunctionFlavor Flavor() const { return GetType()->Flavor(); }
|
||||
FunctionFlavor Flavor() const { return GetType()->Flavor(); }
|
||||
|
||||
struct Body {
|
||||
struct Body
|
||||
{
|
||||
detail::StmtPtr stmts;
|
||||
int priority;
|
||||
bool operator<(const Body& other) const
|
||||
{ return priority > other.priority; } // reverse sort
|
||||
};
|
||||
{
|
||||
return priority > other.priority;
|
||||
} // reverse sort
|
||||
};
|
||||
|
||||
const std::vector<Body>& GetBodies() const { return bodies; }
|
||||
bool HasBodies() const { return bodies.size(); }
|
||||
const std::vector<Body>& GetBodies() const { return bodies; }
|
||||
bool HasBodies() const { return bodies.size(); }
|
||||
|
||||
/**
|
||||
* Calls a Zeek function.
|
||||
|
@ -81,16 +92,14 @@ public:
|
|||
* @param parent the frame from which the function is being called.
|
||||
* @return the return value of the function call.
|
||||
*/
|
||||
virtual ValPtr Invoke(
|
||||
zeek::Args* args, detail::Frame* parent = nullptr) const = 0;
|
||||
virtual ValPtr Invoke(zeek::Args* args, detail::Frame* parent = nullptr) const = 0;
|
||||
|
||||
/**
|
||||
* A version of Invoke() taking a variable number of individual arguments.
|
||||
*/
|
||||
template <class... Args>
|
||||
std::enable_if_t<
|
||||
std::is_convertible_v<std::tuple_element_t<0, std::tuple<Args...>>, ValPtr>,
|
||||
ValPtr>
|
||||
std::enable_if_t<std::is_convertible_v<std::tuple_element_t<0, std::tuple<Args...>>, ValPtr>,
|
||||
ValPtr>
|
||||
Invoke(Args&&... args) const
|
||||
{
|
||||
auto zargs = zeek::Args{std::forward<Args>(args)...};
|
||||
|
@ -98,20 +107,18 @@ public:
|
|||
}
|
||||
|
||||
// Add a new event handler to an existing function (event).
|
||||
virtual void AddBody(detail::StmtPtr new_body,
|
||||
const std::vector<detail::IDPtr>& new_inits,
|
||||
virtual void AddBody(detail::StmtPtr new_body, const std::vector<detail::IDPtr>& new_inits,
|
||||
size_t new_frame_size, int priority = 0);
|
||||
|
||||
virtual void SetScope(detail::ScopePtr newscope);
|
||||
virtual detail::ScopePtr GetScope() const { return scope; }
|
||||
virtual detail::ScopePtr GetScope() const { return scope; }
|
||||
|
||||
const FuncTypePtr& GetType() const
|
||||
{ return type; }
|
||||
const FuncTypePtr& GetType() const { return type; }
|
||||
|
||||
Kind GetKind() const { return kind; }
|
||||
Kind GetKind() const { return kind; }
|
||||
|
||||
const char* Name() const { return name.c_str(); }
|
||||
void SetName(const char* arg_name) { name = arg_name; }
|
||||
void SetName(const char* arg_name) { name = arg_name; }
|
||||
|
||||
void Describe(ODesc* d) const override = 0;
|
||||
virtual void DescribeDebug(ODesc* d, const zeek::Args* args) const;
|
||||
|
@ -122,7 +129,9 @@ public:
|
|||
|
||||
uint32_t GetUniqueFuncID() const { return unique_id; }
|
||||
static const FuncPtr& GetFuncPtrByID(uint32_t id)
|
||||
{ return id >= unique_ids.size() ? Func::nil : unique_ids[id]; }
|
||||
{
|
||||
return id >= unique_ids.size() ? Func::nil : unique_ids[id];
|
||||
}
|
||||
|
||||
protected:
|
||||
Func();
|
||||
|
@ -131,8 +140,7 @@ protected:
|
|||
void CopyStateInto(Func* other) const;
|
||||
|
||||
// Helper function for checking result of plugin hook.
|
||||
void CheckPluginResult(bool handled, const ValPtr& hook_result,
|
||||
FunctionFlavor flavor) const;
|
||||
void CheckPluginResult(bool handled, const ValPtr& hook_result, FunctionFlavor flavor) const;
|
||||
|
||||
std::vector<Body> bodies;
|
||||
detail::ScopePtr scope;
|
||||
|
@ -141,19 +149,20 @@ protected:
|
|||
FuncTypePtr type;
|
||||
std::string name;
|
||||
static inline std::vector<FuncPtr> unique_ids;
|
||||
};
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class ScriptFunc : public Func {
|
||||
class ScriptFunc : public Func
|
||||
{
|
||||
public:
|
||||
ScriptFunc(const IDPtr& id, StmtPtr body,
|
||||
const std::vector<IDPtr>& inits,
|
||||
size_t frame_size, int priority);
|
||||
ScriptFunc(const IDPtr& id, StmtPtr body, const std::vector<IDPtr>& inits, size_t frame_size,
|
||||
int priority);
|
||||
|
||||
// For compiled scripts.
|
||||
ScriptFunc(std::string name, FuncTypePtr ft,
|
||||
std::vector<StmtPtr> bodies, std::vector<int> priorities);
|
||||
ScriptFunc(std::string name, FuncTypePtr ft, std::vector<StmtPtr> bodies,
|
||||
std::vector<int> priorities);
|
||||
|
||||
~ScriptFunc() override;
|
||||
|
||||
|
@ -176,7 +185,7 @@ public:
|
|||
*
|
||||
* @return internal frame kept by the function for persisting captures
|
||||
*/
|
||||
Frame* GetCapturesFrame() const { return captures_frame; }
|
||||
Frame* GetCapturesFrame() const { return captures_frame; }
|
||||
|
||||
// Same definition as in Frame.h.
|
||||
using OffsetMap = std::unordered_map<std::string, int>;
|
||||
|
@ -186,8 +195,7 @@ public:
|
|||
*
|
||||
* @return pointer to mapping of captures to slots
|
||||
*/
|
||||
const OffsetMap* GetCapturesOffsetMap() const
|
||||
{ return captures_offset_mapping; }
|
||||
const OffsetMap* GetCapturesOffsetMap() const { return captures_offset_mapping; }
|
||||
|
||||
// The following "Closure" methods implement the deprecated
|
||||
// capture-by-reference functionality.
|
||||
|
@ -233,9 +241,8 @@ public:
|
|||
*/
|
||||
bool DeserializeCaptures(const broker::vector& data);
|
||||
|
||||
void AddBody(StmtPtr new_body,
|
||||
const std::vector<IDPtr>& new_inits,
|
||||
size_t new_frame_size, int priority) override;
|
||||
void AddBody(StmtPtr new_body, const std::vector<IDPtr>& new_inits, size_t new_frame_size,
|
||||
int priority) override;
|
||||
|
||||
/**
|
||||
* Replaces the given current instance of a function body with
|
||||
|
@ -245,17 +252,16 @@ public:
|
|||
* @param old_body Body to replace.
|
||||
* @param new_body New body to use; can be nil.
|
||||
*/
|
||||
void ReplaceBody(const detail::StmtPtr& old_body,
|
||||
detail::StmtPtr new_body);
|
||||
void ReplaceBody(const detail::StmtPtr& old_body, detail::StmtPtr new_body);
|
||||
|
||||
StmtPtr CurrentBody() const { return current_body; }
|
||||
int CurrentPriority() const { return current_priority; }
|
||||
StmtPtr CurrentBody() const { return current_body; }
|
||||
int CurrentPriority() const { return current_priority; }
|
||||
|
||||
/**
|
||||
* Returns the function's frame size.
|
||||
* @return The number of ValPtr slots in the function's frame.
|
||||
*/
|
||||
int FrameSize() const { return frame_size; }
|
||||
int FrameSize() const { return frame_size; }
|
||||
|
||||
/**
|
||||
* Changes the function's frame size to a new size - used for
|
||||
|
@ -263,20 +269,17 @@ public:
|
|||
*
|
||||
* @param new_size The frame size the function should use.
|
||||
*/
|
||||
void SetFrameSize(int new_size) { frame_size = new_size; }
|
||||
void SetFrameSize(int new_size) { frame_size = new_size; }
|
||||
|
||||
/** Sets this function's outer_id list. */
|
||||
void SetOuterIDs(IDPList ids)
|
||||
{ outer_ids = std::move(ids); }
|
||||
void SetOuterIDs(IDPList ids) { outer_ids = std::move(ids); }
|
||||
|
||||
void Describe(ODesc* d) const override;
|
||||
|
||||
protected:
|
||||
ScriptFunc() : Func(SCRIPT_FUNC) {}
|
||||
ScriptFunc() : Func(SCRIPT_FUNC) { }
|
||||
|
||||
StmtPtr AddInits(
|
||||
StmtPtr body,
|
||||
const std::vector<IDPtr>& inits);
|
||||
StmtPtr AddInits(StmtPtr body, const std::vector<IDPtr>& inits);
|
||||
|
||||
/**
|
||||
* Clones this function along with its closures.
|
||||
|
@ -323,39 +326,46 @@ private:
|
|||
|
||||
// ... and its priority.
|
||||
int current_priority = 0;
|
||||
};
|
||||
};
|
||||
|
||||
using built_in_func = BifReturnVal (*)(Frame* frame, const Args* args);
|
||||
|
||||
class BuiltinFunc final : public Func {
|
||||
class BuiltinFunc final : public Func
|
||||
{
|
||||
public:
|
||||
BuiltinFunc(built_in_func func, const char* name, bool is_pure);
|
||||
~BuiltinFunc() override;
|
||||
|
||||
bool IsPure() const override;
|
||||
ValPtr Invoke(zeek::Args* args, Frame* parent) const override;
|
||||
built_in_func TheFunc() const { return func; }
|
||||
built_in_func TheFunc() const { return func; }
|
||||
|
||||
void Describe(ODesc* d) const override;
|
||||
|
||||
protected:
|
||||
BuiltinFunc() { func = nullptr; is_pure = 0; }
|
||||
BuiltinFunc()
|
||||
{
|
||||
func = nullptr;
|
||||
is_pure = 0;
|
||||
}
|
||||
|
||||
built_in_func func;
|
||||
bool is_pure;
|
||||
};
|
||||
};
|
||||
|
||||
extern bool check_built_in_call(BuiltinFunc* f, CallExpr* call);
|
||||
|
||||
struct CallInfo {
|
||||
struct CallInfo
|
||||
{
|
||||
const CallExpr* call;
|
||||
const Func* func;
|
||||
const zeek::Args& args;
|
||||
};
|
||||
};
|
||||
|
||||
// Struct that collects all the specifics defining a Func. Used for ScriptFuncs
|
||||
// with closures.
|
||||
struct function_ingredients {
|
||||
struct function_ingredients
|
||||
{
|
||||
|
||||
// Gathers all of the information from a scope and a function body needed
|
||||
// to build a function.
|
||||
|
@ -367,7 +377,7 @@ struct function_ingredients {
|
|||
int frame_size;
|
||||
int priority;
|
||||
ScopePtr scope;
|
||||
};
|
||||
};
|
||||
|
||||
extern std::vector<CallInfo> call_stack;
|
||||
|
||||
|
@ -388,7 +398,7 @@ extern void emit_builtin_exception(const char* msg);
|
|||
extern void emit_builtin_exception(const char* msg, const ValPtr& arg);
|
||||
extern void emit_builtin_exception(const char* msg, Obj* arg);
|
||||
|
||||
} // namespace detail
|
||||
} // namespace detail
|
||||
|
||||
extern std::string render_call_stack();
|
||||
|
||||
|
@ -397,4 +407,4 @@ extern void emit_builtin_error(const char* msg);
|
|||
extern void emit_builtin_error(const char* msg, const ValPtr&);
|
||||
extern void emit_builtin_error(const char* msg, Obj* arg);
|
||||
|
||||
} // namespace zeek
|
||||
} // namespace zeek
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue