Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Var.cc File Reference

#include "config.h"
#include "Var.h"
#include "Func.h"
#include "Stmt.h"
#include "Scope.h"
#include "Serializer.h"
#include "RemoteSerializer.h"
#include "EventRegistry.h"

Include dependency graph for Var.cc:

Include dependency graph

Go to the source code of this file.

Functions

BroTypereduce_type (BroType *t)
BroTypeinit_type (Expr *init)
Valinit_val (Expr *init, const BroType *t, Val *aggr)
void make_var (ID *id, BroType *t, init_class c, Expr *init, attr_list *attr, decl_type dt, int do_init)
void add_global (ID *id, BroType *t, init_class c, Expr *init, attr_list *attr, decl_type dt)
Stmtadd_local (ID *id, BroType *t, init_class c, Expr *init, attr_list *attr, decl_type dt)
void add_type (ID *id, BroType *t, attr_list *attr, int)
void begin_func (ID *id, const char *module_name, function_flavor flavor, int is_redef, FuncType *t)
void end_func (Stmt *body)
Valinternal_val (const char *name)
Valopt_internal_val (const char *name)
double opt_internal_double (const char *name)
bro_int_t opt_internal_int (const char *name)
StringValopt_internal_string (const char *name)
TableValopt_internal_table (const char *name)
ListValinternal_list_val (const char *name)
BroTypeinternal_type (const char *name)
Funcinternal_func (const char *name)
EventHandlerPtr internal_handler (const char *name)


Function Documentation

void add_global ID id,
BroType t,
init_class  c,
Expr init,
attr_list *  attr,
decl_type  dt
 

Definition at line 292 of file Var.cc.

References id, and make_var().

Referenced by yyparse().

00294         {
00295         make_var(id, t, c, init, attr, dt, 1);
00296         }

Stmt* add_local ID id,
BroType t,
init_class  c,
Expr init,
attr_list *  attr,
decl_type  dt
 

Definition at line 298 of file Var.cc.

References Scope::AddInit(), current_scope(), BroObj::GetLocationInfo(), id, INIT_FULL, make_var(), Ref(), Stmt::SetLocationInfo(), BroType::Tag(), TYPE_RECORD, TYPE_TABLE, and TYPE_VECTOR.

Referenced by ForStmt::ForStmt(), and yyparse().

00300         {
00301         make_var(id, t, c, init, attr, dt, 0);
00302 
00303         if ( init )
00304                 {
00305                 if ( c != INIT_FULL )
00306                         id->Error("can't use += / -= for initializations of local variables");
00307 
00308                 Ref(id);
00309 
00310                 Stmt* stmt =
00311                         new ExprStmt(new AssignExpr(new NameExpr(id), init, 0));
00312                 stmt->SetLocationInfo(init->GetLocationInfo());
00313 
00314                 return stmt;
00315                 }
00316 
00317         else
00318                 {
00319                 if ( t->Tag() == TYPE_RECORD || t->Tag() == TYPE_TABLE ||
00320                      t->Tag() == TYPE_VECTOR )
00321                         current_scope()->AddInit(id);
00322 
00323                 return new NullStmt;
00324                 }
00325         }

void add_type ID id,
BroType t,
attr_list *  attr,
int 
 

Definition at line 327 of file Var.cc.

References id.

Referenced by yyparse().

00328         {
00329         id->SetType(t);
00330         id->MakeType();
00331 
00332         if ( attr )
00333                 id->SetAttrs(new Attributes(attr, t));
00334         }

void begin_func ID id,
const char *  module_name,
function_flavor  flavor,
int  is_redef,
FuncType t
 

Definition at line 336 of file Var.cc.

References args, FuncType::Args(), FuncType::ClearYieldType(), copy_string(), ID::Error(), FUNC_FLAVOR_EVENT, id, TypeDecl::id, install_ID(), ID::IsGlobal(), lookup_ID(), push_scope(), same_type(), ID::SetType(), BroType::Tag(), TypeDecl::type, TYPE_VOID, and FuncType::YieldType().

Referenced by yyparse().

00338         {
00339         if ( flavor == FUNC_FLAVOR_EVENT )
00340                 {
00341                 const BroType* yt = t->YieldType();
00342 
00343                 if ( yt && yt->Tag() != TYPE_VOID )
00344                         id->Error("event cannot yield a value", t);
00345 
00346                 t->ClearYieldType();
00347                 }
00348 
00349         if ( id->Type() )
00350                 {
00351                 if ( ! same_type(id->Type(), t) )
00352                         id->Type()->Error("incompatible types", t);
00353                 }
00354 
00355         else if ( is_redef )
00356                 id->Error("redef of not-previously-declared value");
00357 
00358         if ( id->HasVal() )
00359                 {
00360                 int id_is_event = id->ID_Val()->AsFunc()->IsEvent();
00361 
00362                 if ( id_is_event != (flavor == FUNC_FLAVOR_EVENT) )
00363                         id->Error("inconsistency between event and function", t);
00364                 if ( id_is_event )
00365                         {
00366                         if ( is_redef )
00367                                 // Clear out value so it will be replaced.
00368                                 id->SetVal(0);
00369                         }
00370                 else
00371                         {
00372                         if ( ! id->IsRedefinable() )
00373                                 id->Error("already defined");
00374                         }
00375                 }
00376         else
00377                 id->SetType(t);
00378 
00379         push_scope(id);
00380 
00381         RecordType* args = t->Args();
00382         int num_args = args->NumFields();
00383         for ( int i = 0; i < num_args; ++i )
00384                 {
00385                 TypeDecl* arg_i = args->FieldDecl(i);
00386                 ID* arg_id = lookup_ID(arg_i->id, module_name);
00387 
00388                 if ( arg_id && ! arg_id->IsGlobal() )
00389                         arg_id->Error("argument name used twice");
00390 
00391                 arg_id = install_ID(copy_string(arg_i->id), module_name,
00392                                         false, false);
00393                 arg_id->SetType(arg_i->type->Ref());
00394                 }
00395         }

void end_func Stmt body  ) 
 

Definition at line 397 of file Var.cc.

References current_scope(), Scope::GetInits(), id, Scope::Length(), pop_scope(), Scope::ScopeID(), and Val.

Referenced by yyparse().

00398         {
00399         int frame_size = current_scope()->Length();
00400         id_list* inits = current_scope()->GetInits();
00401 
00402         Scope* scope = pop_scope();
00403         ID* id = scope->ScopeID();
00404 
00405         if ( id->HasVal() )
00406                 id->ID_Val()->AsFunc()->AddBody(body, inits, frame_size);
00407         else
00408                 {
00409                 Func* f = new BroFunc(id, body, inits, frame_size);
00410                 id->SetVal(new Val(f));
00411                 id->SetConst();
00412                 }
00413 
00414         id->ID_Val()->AsFunc()->SetScope(scope);
00415         }

BroType* init_type Expr init  )  [static]
 

Definition at line 51 of file Var.cc.

References TypeList::Append(), Expr::AsListExpr(), BroType::AsTableType(), BroType::AsTypeList(), BroObj::Error(), EXPR_LIST, ListExpr::Exprs(), ListExpr::InitType(), Expr::InitType(), Expr::IsRecordElement(), BroType::IsSet(), merge_types(), reduce_type(), same_type(), BroType::Tag(), Expr::Tag(), TYPE_LIST, TYPE_TABLE, TypeList::Types(), and Unref().

Referenced by make_var().

00052         {
00053         if ( init->Tag() != EXPR_LIST )
00054                 {
00055                 BroType* t = init->InitType();
00056                 if ( ! t )
00057                         return 0;
00058 
00059                 if ( t->Tag() == TYPE_LIST &&
00060                      t->AsTypeList()->Types()->length() != 1 )
00061                         {
00062                         init->Error("list used in scalar initialization");
00063                         Unref(t);
00064                         return 0;
00065                         }
00066 
00067                 return t;
00068                 }
00069 
00070         ListExpr* init_list = init->AsListExpr();
00071         const expr_list& el = init_list->Exprs();
00072 
00073         if ( el.length() == 0 )
00074                 {
00075                 init->Error("empty list in untyped initialization");
00076                 return 0;
00077                 }
00078 
00079         // Could be a record, a set, or a list of table elements.
00080         Expr* e0 = el[0];
00081         if ( e0->IsRecordElement(0) )
00082                 // ListExpr's know how to build a record from their
00083                 // components.
00084                 return init_list->InitType();
00085 
00086         BroType* t = e0->InitType();
00087         if ( t )
00088                 t = reduce_type(t);
00089         if ( ! t )
00090                 return 0;
00091 
00092         for ( int i = 1; t && i < el.length(); ++i )
00093                 {
00094                 BroType* el_t = el[i]->InitType();
00095                 BroType* ti = el_t ? reduce_type(el_t) : 0;
00096                 if ( ! ti )
00097                         {
00098                         Unref(t);
00099                         return 0;
00100                         }
00101 
00102                 if ( same_type(t, ti) )
00103                         {
00104                         Unref(ti);
00105                         continue;
00106                         }
00107 
00108                 BroType* t_merge = merge_types(t, ti);
00109                 Unref(t);
00110                 Unref(ti);
00111                 t = t_merge;
00112                 }
00113 
00114         if ( ! t )
00115                 return 0;
00116 
00117         if ( t->Tag() == TYPE_TABLE && ! t->AsTableType()->IsSet() )
00118                 // A list of table elements.
00119                 return t;
00120 
00121         // A set.  If the index type isn't yet a type list, make
00122         // it one, as that's what's required for creating a set type.
00123         if ( t->Tag() != TYPE_LIST )
00124                 {
00125                 TypeList* tl = new TypeList(t);
00126                 tl->Append(t);
00127                 t = tl;
00128                 }
00129 
00130         return new SetType(t->AsTypeList(), 0);
00131         }

Val* init_val Expr init,
const BroType t,
Val aggr
[static]
 

Definition at line 133 of file Var.cc.

References Expr::InitVal().

00134         {
00135         return init->InitVal(t, aggr);
00136         }

Func* internal_func const char *  name  ) 
 

Definition at line 491 of file Var.cc.

References internal_val(), and name.

Referenced by Discarder::Discarder(), and main().

00492         {
00493         Val* v = internal_val(name);
00494         if ( v )
00495                 return v->AsFunc();
00496         else
00497                 return 0;
00498         }

EventHandlerPtr internal_handler const char *  name  ) 
 

Definition at line 500 of file Var.cc.

References event_registry, EventRegistry::Lookup(), name, EventRegistry::Register(), and EventHandler::SetUsed().

Referenced by DNS_Mgr::Init(), main(), and terminate_bro().

00501         {
00502         // If there already is an entry in the registry, we have a
00503         // local handler on the script layer.
00504         EventHandler* h = event_registry->Lookup(name);
00505         if ( h )
00506                 {
00507                 h->SetUsed();
00508                 return h;
00509                 }
00510 
00511         h = new EventHandler(name);
00512         event_registry->Register(h);
00513 
00514         h->SetUsed();
00515 
00516         return h;
00517         }

ListVal* internal_list_val const char *  name  ) 
 

Definition at line 456 of file Var.cc.

References TableVal::ConvertToPureList(), GLOBAL_MODULE_NAME, id, internal_error(), BroType::IsSet(), lookup_ID(), name, BroType::Tag(), Val::Type(), and TYPE_LIST.

Referenced by init_net_var().

00457         {
00458         ID* id = lookup_ID(name, GLOBAL_MODULE_NAME);
00459         if ( ! id )
00460                 return 0;
00461 
00462         Val* v = id->ID_Val();
00463         if ( v )
00464                 {
00465                 if ( v->Type()->Tag() == TYPE_LIST )
00466                         return (ListVal*) v;
00467 
00468                 else if ( v->Type()->IsSet() )
00469                         {
00470                         TableVal* tv = v->AsTableVal();
00471                         ListVal* lv = tv->ConvertToPureList();
00472                         return lv;
00473                         }
00474 
00475                 else
00476                         internal_error("internal variable %s is not a list", name);
00477                 }
00478 
00479         return 0;
00480         }

BroType* internal_type const char *  name  ) 
 

Definition at line 482 of file Var.cc.

References GLOBAL_MODULE_NAME, id, internal_error(), lookup_ID(), and name.

Referenced by NetSessions::BuildHeader(), Discarder::Discarder(), DNS_Mgr::Init(), init_builtin_funcs(), init_general_global_var(), and init_net_var().

00483         {
00484         ID* id = lookup_ID(name, GLOBAL_MODULE_NAME);
00485         if ( ! id )
00486                 internal_error("internal type %s missing", name);
00487 
00488         return id->Type();
00489         }

Val* internal_val const char *  name  ) 
 

Definition at line 417 of file Var.cc.

References GLOBAL_MODULE_NAME, id, internal_error(), lookup_ID(), and name.

Referenced by init_general_global_var(), init_net_var(), internal_func(), main(), OSFingerprint::OSFingerprint(), and SecondaryPath::SecondaryPath().

00418         {
00419         ID* id = lookup_ID(name, GLOBAL_MODULE_NAME);
00420         if ( ! id )
00421                 internal_error("internal variable %s missing", name);
00422 
00423         return id->ID_Val();
00424         }

void make_var ID id,
BroType t,
init_class  c,
Expr init,
attr_list *  attr,
decl_type  dt,
int  do_init
[static]
 

Definition at line 138 of file Var.cc.

References BroType::AsRecordType(), BroType::AsSetType(), BroType::AsTableType(), BroType::AsVectorType(), ATTR_ADD_FUNC, ATTR_DEL_FUNC, ATTR_PERSISTENT, ATTR_SYNCHRONIZED, error_type(), id, INIT_EXTRA, INIT_REMOVE, init_type(), init_val(), BroType::IsSet(), optimize, Ref(), same_type(), SetType::SetElements(), Expr::Simplify(), SIMPLIFY_GENERAL, BroType::Tag(), TYPE_ERROR, TYPE_RECORD, TYPE_TABLE, TYPE_VECTOR, VAR_CONST, and VAR_REDEF.

Referenced by add_global(), and add_local().

00140         {
00141         if ( id->Type() )
00142                 {
00143                 if ( id->IsRedefinable() || (! init && attr) )
00144                         {
00145                         BroObj* redef_obj = init ? (BroObj*) init : (BroObj*) t;
00146                         if ( dt != VAR_REDEF )
00147                                 id->Warn("redefinition requires \"redef\"", redef_obj, 1);
00148                         }
00149 
00150                 else if ( dt != VAR_REDEF || init || ! attr )
00151                         {
00152                         id->Error("already defined", init);
00153                         return;
00154                         }
00155                 }
00156 
00157         if ( dt == VAR_REDEF )
00158                 {
00159                 if ( ! id->Type() )
00160                         {
00161                         id->Error("\"redef\" used but not previously defined");
00162                         return;
00163                         }
00164 
00165                 if ( ! t )
00166                         t = id->Type();
00167                 }
00168 
00169         if ( id->Type() && id->Type()->Tag() != TYPE_ERROR )
00170                 {
00171                 if ( dt != VAR_REDEF &&
00172                      (! init || ! do_init || (! t && ! (t = init_type(init)))) )
00173                         {
00174                         id->Error("already defined", init);
00175                         return;
00176                         }
00177 
00178                 // Allow redeclaration in order to initialize.
00179                 if ( ! same_type(t, id->Type()) )
00180                         {
00181                         id->Error("redefinition changes type", init);
00182                         return;
00183                         }
00184                 }
00185 
00186         if ( init && optimize )
00187                 init = init->Simplify(SIMPLIFY_GENERAL);
00188 
00189         if ( t && t->IsSet() )
00190                 { // Check for set with explicit elements.
00191                 SetType* st = t->AsTableType()->AsSetType();
00192                 ListExpr* elements = st->SetElements();
00193 
00194                 if ( elements )
00195                         {
00196                         if ( init )
00197                                 {
00198                                 id->Error("double initialization", init);
00199                                 return;
00200                                 }
00201 
00202                         Ref(elements);
00203                         init = elements;
00204                         }
00205                 }
00206 
00207         if ( ! t )
00208                 { // Take type from initialization.
00209                 if ( ! init )
00210                         {
00211                         id->Error("no type given");
00212                         return;
00213                         }
00214 
00215                 t = init_type(init);
00216                 if ( ! t )
00217                         {
00218                         id->SetType(error_type());
00219                         return;
00220                         }
00221                 }
00222         else
00223                 Ref(t);
00224 
00225         id->SetType(t);
00226 
00227         if ( attr )
00228                 id->AddAttrs(new Attributes(attr, t));
00229 
00230         if ( id->FindAttr(ATTR_PERSISTENT) || id->FindAttr(ATTR_SYNCHRONIZED) )
00231                 {
00232                 if ( dt == VAR_CONST )
00233                         {
00234                         id->Error("&persistant/synchronized with constant");
00235                         return;
00236                         }
00237 
00238                 if ( ! id->IsGlobal() )
00239                         {
00240                         id->Error("&persistant/synchronized with non-global");
00241                         return;
00242                         }
00243                 }
00244 
00245         if ( do_init )
00246                 {
00247                 if ( (c == INIT_EXTRA && id->FindAttr(ATTR_ADD_FUNC)) ||
00248                      (c == INIT_REMOVE && id->FindAttr(ATTR_DEL_FUNC)) )
00249                         // Just apply the function.
00250                         id->SetVal(init, c);
00251 
00252                 else if ( dt != VAR_REDEF || init || ! attr )
00253                         {
00254                         Val* aggr;
00255                         if ( t->Tag() == TYPE_RECORD )
00256                                 aggr = new RecordVal(t->AsRecordType());
00257 
00258                         else if ( t->Tag() == TYPE_TABLE )
00259                                 aggr = new TableVal(t->AsTableType(), id->Attrs());
00260 
00261                         else if ( t->Tag() == TYPE_VECTOR )
00262                                 aggr = new VectorVal(t->AsVectorType());
00263 
00264                         else
00265                                 aggr = 0;
00266 
00267                         Val* v = 0;
00268                         if ( init )
00269                                 {
00270                                 v = init_val(init, t, aggr);
00271                                 if ( ! v )
00272                                         return;
00273                                 }
00274 
00275                         if ( aggr )
00276                                 id->SetVal(aggr, c);
00277                         else if ( v )
00278                                 id->SetVal(v, c);
00279                         }
00280                 }
00281 
00282         if ( dt == VAR_CONST )
00283                 {
00284                 if ( ! init && ! id->IsRedefinable() )
00285                         id->Error("const variable must be initialized");
00286 
00287                 id->SetConst();
00288                 }
00289         }

double opt_internal_double const char *  name  ) 
 

Definition at line 432 of file Var.cc.

References Val::InternalDouble(), name, and opt_internal_val().

Referenced by init_general_global_var(), and init_net_var().

00433         {
00434         Val* v = opt_internal_val(name);
00435         return v ? v->InternalDouble() : 0.0;
00436         }

bro_int_t opt_internal_int const char *  name  ) 
 

Definition at line 438 of file Var.cc.

References bro_int_t, Val::InternalInt(), name, and opt_internal_val().

Referenced by Discarder::Discarder(), init_general_global_var(), and init_net_var().

00439         {
00440         Val* v = opt_internal_val(name);
00441         return v ? v->InternalInt() : 0;
00442         }

StringVal* opt_internal_string const char *  name  ) 
 

Definition at line 444 of file Var.cc.

References name, and opt_internal_val().

Referenced by init_general_global_var(), and init_net_var().

00445         {
00446         Val* v = opt_internal_val(name);
00447         return v ? v->AsStringVal() : 0;
00448         }

TableVal* opt_internal_table const char *  name  ) 
 

Definition at line 450 of file Var.cc.

References name, and opt_internal_val().

Referenced by init_net_var().

00451         {
00452         Val* v = opt_internal_val(name);
00453         return v ? v->AsTableVal() : 0;
00454         }

Val* opt_internal_val const char *  name  ) 
 

Definition at line 426 of file Var.cc.

References GLOBAL_MODULE_NAME, id, lookup_ID(), and name.

Referenced by init_net_var(), opt_internal_double(), opt_internal_int(), opt_internal_string(), and opt_internal_table().

00427         {
00428         ID* id = lookup_ID(name, GLOBAL_MODULE_NAME);
00429         return id ? id->ID_Val() : 0;
00430         }

BroType* reduce_type BroType t  )  [static]
 

Definition at line 33 of file Var.cc.

References BroType::AsTableType(), flatten_type(), IndexType::Indices(), BroType::IsSet(), BroType::Tag(), TYPE_LIST, and TypeList::Types().

Referenced by init_type().

00034         {
00035         if ( t->Tag() == TYPE_LIST )
00036                 return flatten_type(t);
00037 
00038         else if ( t->IsSet() )
00039                 {
00040                 TypeList* tl = t->AsTableType()->Indices();
00041                 if ( tl->Types()->length() == 1 )
00042                         return (*tl->Types())[0];
00043                 else
00044                         return tl;
00045                 }
00046 
00047         else
00048                 return t;
00049         }


Generated on Wed Sep 14 03:07:16 2005 for bro_docs by doxygen 1.3.5