Branch data Line data Source code
1 : : // $Id: Func.h 6916 2009-09-24 20:48:36Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef func_h
6 : : #define func_h
7 : :
8 : : #include "BroList.h"
9 : : #include "Obj.h"
10 : : #include "Debug.h"
11 : :
12 : : class Val;
13 : : class ListExpr;
14 : : class FuncType;
15 : : class Stmt;
16 : : class Frame;
17 : : class ID;
18 : :
19 : : class Func : public BroObj {
20 : : public:
21 : :
22 : : enum Kind { BRO_FUNC, BUILTIN_FUNC };
23 : :
24 : 1686 : Func(Kind arg_kind)
25 : 1686 : { scope = 0; kind = arg_kind; id = 0; return_value = 0; }
26 : :
27 : : virtual ~Func();
28 : :
29 : : virtual int IsPure() const = 0;
30 : 354 : int IsEvent() const { return FType()->IsEvent(); }
31 : :
32 : 249 : struct Body {
33 : : Stmt* stmts;
34 : : int priority;
35 : 2634 : bool operator<(const Body& other) const
36 : 2634 : { return priority > other.priority; } // reverse sort
37 : : };
38 : :
39 : 0 : virtual const vector<Body>& GetBodies() const { return bodies; }
40 : :
41 : : // virtual Val* Call(ListExpr* args) const = 0;
42 : : virtual Val* Call(val_list* args, Frame* parent = 0) const = 0;
43 : :
44 : : // Add a new event handler to an existing function (event).
45 : : virtual void AddBody(Stmt* new_body, id_list* new_inits,
46 : : int new_frame_size, int priority = 0);
47 : :
48 : 1125 : virtual void SetScope(Scope* newscope) { scope = newscope; }
49 : 0 : virtual Scope* GetScope() const { return scope; }
50 : :
51 : 168208 : virtual FuncType* FType() const
52 : : {
53 : 168208 : return (FuncType*) id->Type()->AsFuncType();
54 : : }
55 : :
56 : 1714 : Kind GetKind() const { return kind; }
57 : :
58 : 0 : const ID* GetID() const { return id; }
59 : : void SetID(ID *arg_id);
60 : :
61 : : virtual void Describe(ODesc* d) const = 0;
62 : : virtual void DescribeDebug(ODesc* d, const val_list* args) const;
63 : :
64 : : // This (un-)serializes only a single body (as given in SerialInfo).
65 : : bool Serialize(SerialInfo* info) const;
66 : : static Func* Unserialize(UnserialInfo* info);
67 : :
68 : : ID* GetReturnValueID() const;
69 : : virtual TraversalCode Traverse(TraversalCallback* cb) const;
70 : :
71 : : protected:
72 : 0 : Func() { scope = 0; id = 0; return_value = 0; }
73 : :
74 : : DECLARE_ABSTRACT_SERIAL(Func);
75 : :
76 : : vector<Body> bodies;
77 : : Scope* scope;
78 : : Kind kind;
79 : : ID* id;
80 : : ID* return_value;
81 : : };
82 : :
83 : :
84 : : class BroFunc : public Func {
85 : : public:
86 : : BroFunc(ID* id, Stmt* body, id_list* inits, int frame_size);
87 : : ~BroFunc();
88 : :
89 : : int IsPure() const;
90 : : Val* Call(val_list* args, Frame* parent) const;
91 : :
92 : : void AddBody(Stmt* new_body, id_list* new_inits, int new_frame_size,
93 : : int priority);
94 : :
95 : : int FrameSize() const { return frame_size; }
96 : :
97 : : void Describe(ODesc* d) const;
98 : :
99 : : protected:
100 : 0 : BroFunc() : Func(BRO_FUNC) {}
101 : : Stmt* AddInits(Stmt* body, id_list* inits);
102 : :
103 : 0 : DECLARE_SERIAL(BroFunc);
104 : :
105 : : int frame_size;
106 : : };
107 : :
108 : : typedef Val* (*built_in_func)(Frame* frame, val_list* args);
109 : :
110 : : class BuiltinFunc : public Func {
111 : : public:
112 : : BuiltinFunc(built_in_func func, const char* name, int is_pure);
113 : : ~BuiltinFunc();
114 : :
115 : : int IsPure() const;
116 : : Val* Call(val_list* args, Frame* parent) const;
117 : 0 : const char* Name() const { return name; }
118 : 892 : built_in_func TheFunc() const { return func; }
119 : :
120 : : void Describe(ODesc* d) const;
121 : :
122 : : protected:
123 : 0 : BuiltinFunc() { func = 0; name = 0; is_pure = 0; }
124 : :
125 : 0 : DECLARE_SERIAL(BuiltinFunc);
126 : :
127 : : built_in_func func;
128 : : const char* name;
129 : : int is_pure;
130 : : };
131 : :
132 : :
133 : : extern void builtin_run_time(const char* msg, BroObj* arg = 0);
134 : : extern void init_builtin_funcs();
135 : :
136 : : extern bool check_built_in_call(BuiltinFunc* f, CallExpr* call);
137 : :
138 : : // This global is set prior to the interpreter making a function call.
139 : : // It's there so that built-in functions can access the location information
140 : : // associated with a call when reporting error messages.
141 : : extern const Expr* calling_expr;
142 : :
143 : : // This is set to true after the built-in functions have been initialized.
144 : : extern bool did_builtin_init;
145 : :
146 : : #endif
|