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

Expr.h

Go to the documentation of this file.
00001 // $Id: Expr.h,v 1.8 2005/01/03 07:19:06 vern Exp $
00002 //
00003 // Copyright (c) 1995, 1996, 1997, 1998, 1999, 2001, 2002
00004 //      The Regents of the University of California.  All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that: (1) source code distributions
00008 // retain the above copyright notice and this paragraph in its entirety, (2)
00009 // distributions including binary code include the above copyright notice and
00010 // this paragraph in its entirety in the documentation or other materials
00011 // provided with the distribution, and (3) all advertising materials mentioning
00012 // features or use of this software display the following acknowledgement:
00013 // ``This product includes software developed by the University of California,
00014 // Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
00015 // the University nor the names of its contributors may be used to endorse
00016 // or promote products derived from this software without specific prior
00017 // written permission.
00018 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00019 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022 #ifndef expr_h
00023 #define expr_h
00024 
00025 // BRO expressions.
00026 
00027 #include "BroList.h"
00028 #include "ID.h"
00029 #include "Timer.h"
00030 #include "Val.h"
00031 #include "Debug.h"
00032 #include "EventHandler.h"
00033 #include "TraverseTypes.h"
00034 
00035 typedef enum {
00036         EXPR_ANY = -1,
00037         EXPR_NAME, EXPR_CONST,
00038         EXPR_INCR, EXPR_DECR, EXPR_NOT, EXPR_POSITIVE, EXPR_NEGATE,
00039         EXPR_ADD, EXPR_SUB, EXPR_TIMES, EXPR_DIVIDE, EXPR_MOD,
00040         EXPR_AND, EXPR_OR,
00041         EXPR_LT, EXPR_LE, EXPR_EQ, EXPR_NE, EXPR_GE, EXPR_GT,
00042         EXPR_COND,
00043         EXPR_REF,
00044         EXPR_ASSIGN,
00045         EXPR_MATCH,
00046         EXPR_INDEX,
00047         EXPR_FIELD, EXPR_HAS_FIELD,
00048         EXPR_RECORD_CONSTRUCTOR, EXPR_FIELD_ASSIGN,
00049         EXPR_IN,
00050         EXPR_LIST,
00051         EXPR_CALL,
00052         EXPR_EVENT,
00053         EXPR_SCHEDULE,
00054         EXPR_ARITH_COERCE, EXPR_RECORD_COERCE,
00055         EXPR_FLATTEN,
00056 #define NUM_EXPRS (int(EXPR_FLATTEN) + 1)
00057 } BroExprTag;
00058 
00059 typedef enum {
00060         SIMPLIFY_GENERAL,       // regular simplification
00061         SIMPLIFY_LHS,           // simplify as the LHS of an assignment
00062 } SimplifyType;
00063 
00064 class Stmt;
00065 class Frame;
00066 class ListExpr;
00067 class CallExpr;
00068 class EventExpr;
00069 
00070 
00071 class Expr : public BroObj {
00072 public:
00073         BroType* Type() const           { return type; }
00074         const BroExprTag Tag() const    { return tag; }
00075 
00076         virtual ~Expr();
00077 
00078         Expr* Ref()                     { ::Ref(this); return this; }
00079 
00080         // Returns a fully simplified version of the expression (this
00081         // may be the same expression, or a newly created one).  simp_type
00082         // gives the context of the simplification.
00083         virtual Expr* Simplify(SimplifyType simp_type) = 0;
00084 
00085         // Evaluates the expression and returns a corresponding Val*,
00086         // or nil if the expression's value isn't fixed.
00087         virtual Val* Eval(Frame* f) const = 0;
00088 
00089         // Assign to the given value, if appropriate.
00090         virtual void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00091 
00092         // Returns the type corresponding to this expression interpreted
00093         // as an initialization.  The type should be Unref()'d when done
00094         // using it.  Returns nil if the initialization is illegal.
00095         virtual BroType* InitType() const;
00096 
00097         // Returns true if this expression, interpreted as an initialization,
00098         // constitutes a record element, false otherwise.  If the TypeDecl*
00099         // is non-nil and the expression is a record element, fills in the
00100         // TypeDecl with a description of the element.
00101         virtual int IsRecordElement(TypeDecl* td) const;
00102 
00103         // Returns a value corresponding to this expression interpreted
00104         // as an initialization, or nil if the expression is inconsistent
00105         // with the given type.  If "aggr" is non-nil, then this expression
00106         // is an element of the given aggregate, and it is added to it
00107         // accordingly.
00108         virtual Val* InitVal(const BroType* t, Val* aggr) const;
00109 
00110         // True if the expression has no side effects, false otherwise.
00111         virtual int IsPure() const;
00112 
00113         // True if the expression is a constant, false otherwise.
00114         int IsConst() const     { return tag == EXPR_CONST; }
00115 
00116         // True if the expression is in error (to alleviate error propagation).
00117         int IsError() const     { return type && type->Tag() == TYPE_ERROR; }
00118 
00119         // Mark expression as in error.
00120         void SetError()         { SetType(error_type()); }
00121         void SetError(const char* msg);
00122 
00123         // Returns the expression's constant value, or complains
00124         // if it's not a constant.
00125         inline Val* ExprVal() const;
00126 
00127         // True if the expression is a constant zero, false otherwise.
00128         int IsZero() const
00129                 {
00130                 return IsConst() && ExprVal()->IsZero();
00131                 }
00132 
00133         // True if the expression is a constant one, false otherwise.
00134         int IsOne() const
00135                 {
00136                 return IsConst() && ExprVal()->IsOne();
00137                 }
00138 
00139         // True if the expression supports the "add" or "delete" operations,
00140         // false otherwise.
00141         virtual int CanAdd() const;
00142         virtual int CanDel() const;
00143 
00144         virtual void Add(Frame* f);     // perform add operation
00145         virtual void Delete(Frame* f);  // perform delete operation
00146 
00147         // Return the expression converted to L-value form.  If expr
00148         // cannot be used as an L-value, reports an error and returns
00149         // the current value of expr (this is the default method).
00150         virtual Expr* MakeLvalue();
00151 
00152         // Marks the expression as one requiring (or at least appearing
00153         // with) parentheses.  Used for pretty-printing.
00154         void MarkParen()                { paren = 1; }
00155         int IsParen() const             { return paren; }
00156 
00157         const ListExpr* AsListExpr() const
00158                 {
00159                 if ( tag != EXPR_LIST )
00160                         BadTag("ExprVal::AsListExpr");
00161                 return (const ListExpr*) this;
00162                 }
00163         ListExpr* AsListExpr()
00164                 {
00165                 if ( tag != EXPR_LIST )
00166                         BadTag("ExprVal::AsListExpr");
00167                 return (ListExpr*) this;
00168                 }
00169 
00170         void Describe(ODesc* d) const;
00171 
00172         bool Serialize(SerialInfo* info) const;
00173         static Expr* Unserialize(UnserialInfo* info, BroExprTag want = EXPR_ANY);
00174 
00175         virtual TraversalCode Traverse(TraversalCallback* cb) const = 0;
00176 
00177 protected:
00178         Expr()  { type = 0; }
00179         Expr(BroExprTag arg_tag);
00180 
00181         virtual void ExprDescribe(ODesc* d) const = 0;
00182         void AddTag(ODesc* d) const;
00183 
00184         // Puts the expression in canonical form.
00185         virtual void Canonicize();
00186 
00187         void SetType(BroType* t);
00188 
00189         // Reports the given error and sets the expression's type to
00190         // TYPE_ERROR.
00191         void ExprError(const char msg[]);
00192 
00193         DECLARE_ABSTRACT_SERIAL(Expr);
00194 
00195         BroExprTag tag;
00196         BroType* type;
00197 
00198         int paren;
00199 };
00200 
00201 class NameExpr : public Expr {
00202 public:
00203         NameExpr(ID* id);
00204         ~NameExpr();
00205 
00206         ID* Id() const          { return id; }
00207 
00208         Expr* Simplify(SimplifyType simp_type);
00209         Val* Eval(Frame* f) const;
00210         void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00211         Expr* MakeLvalue();
00212         int IsPure() const;
00213 
00214         TraversalCode Traverse(TraversalCallback* cb) const;
00215 
00216 protected:
00217         friend class Expr;
00218         NameExpr()      { id = 0; }
00219 
00220         void ReferenceID();
00221         void ExprDescribe(ODesc* d) const;
00222 
00223         DECLARE_SERIAL(NameExpr);
00224 
00225         ID* id;
00226 };
00227 
00228 class ConstExpr : public Expr {
00229 public:
00230         ConstExpr(Val* val);
00231         ~ConstExpr();
00232 
00233         Val* Value() const      { return val; }
00234 
00235         Expr* Simplify(SimplifyType simp_type);
00236         Val* Eval(Frame* f) const;
00237 
00238         TraversalCode Traverse(TraversalCallback* cb) const;
00239 
00240 protected:
00241         friend class Expr;
00242         ConstExpr()     { val = 0; }
00243 
00244         void ExprDescribe(ODesc* d) const;
00245         DECLARE_SERIAL(ConstExpr);
00246 
00247         Val* val;
00248 };
00249 
00250 class UnaryExpr : public Expr {
00251 public:
00252         Expr* Op() const        { return op; }
00253 
00254         // Simplifies the operand and calls DoSimplify().
00255         virtual Expr* Simplify(SimplifyType simp_type);
00256 
00257         // UnaryExpr::Eval correctly handles vector types.  Any child
00258         // class that overrides Eval() should be modified to handle
00259         // vectors correctly as necessary.
00260         Val* Eval(Frame* f) const;
00261 
00262         int IsPure() const;
00263 
00264         TraversalCode Traverse(TraversalCallback* cb) const;
00265 
00266 protected:
00267         friend class Expr;
00268         UnaryExpr()     { op = 0; }
00269 
00270         UnaryExpr(BroExprTag arg_tag, Expr* arg_op);
00271         virtual ~UnaryExpr();
00272 
00273         void ExprDescribe(ODesc* d) const;
00274 
00275         // Can be overridden by subclasses that want to take advantage
00276         // of UnaryExpr's Simplify() method.
00277         virtual Expr* DoSimplify();
00278 
00279         // Returns the expression folded using the given constant.
00280         virtual Val* Fold(Val* v) const;
00281 
00282         DECLARE_SERIAL(UnaryExpr);
00283 
00284         Expr* op;
00285 };
00286 
00287 class BinaryExpr : public Expr {
00288 public:
00289         Expr* Op1() const       { return op1; }
00290         Expr* Op2() const       { return op2; }
00291 
00292         // Simplifies both operands, folds them if constant,
00293         // otherwise calls DoSimplify().
00294         virtual Expr* Simplify(SimplifyType simp_type);
00295         int IsPure() const;
00296 
00297         // BinaryExpr::Eval correctly handles vector types.  Any child
00298         // class that overrides Eval() should be modified to handle
00299         // vectors correctly as necessary.
00300         Val* Eval(Frame* f) const;
00301 
00302         TraversalCode Traverse(TraversalCallback* cb) const;
00303 
00304 protected:
00305         friend class Expr;
00306         BinaryExpr()    { op1 = op2 = 0; }
00307 
00308         BinaryExpr(BroExprTag arg_tag, Expr* arg_op1, Expr* arg_op2)
00309                 : Expr(arg_tag)
00310                 {
00311                 if ( ! (arg_op1 && arg_op2) )
00312                         return;
00313                 op1 = arg_op1;
00314                 op2 = arg_op2;
00315                 if ( op1->IsError() || op2->IsError() )
00316                         SetError();
00317                 }
00318         virtual ~BinaryExpr();
00319 
00320         // Can be overridden by subclasses that want to take advantage
00321         // of BinaryExpr's Simplify() method.
00322         virtual Expr* DoSimplify();
00323 
00324         // Returns the expression folded using the given constants.
00325         virtual Val* Fold(Val* v1, Val* v2) const;
00326 
00327         // Same for when the constants are strings.
00328         virtual Val* StringFold(Val* v1, Val* v2) const;
00329 
00330         // Same for when the constants are addresses or subnets.
00331         virtual Val* AddrFold(Val* v1, Val* v2) const;
00332         virtual Val* SubNetFold(Val* v1, Val* v2) const;
00333 
00334         int BothConst() const   { return op1->IsConst() && op2->IsConst(); }
00335 
00336         // Simplify both operands and canonicize.
00337         void SimplifyOps();
00338 
00339         // Exchange op1 and op2.
00340         void SwapOps();
00341 
00342         // Promote the operands to the given type tag, if necessary.
00343         void PromoteOps(TypeTag t);
00344 
00345         // Promote the expression to the given type tag (i.e., promote
00346         // operands and also set expression's type).
00347         void PromoteType(TypeTag t, bool is_vector);
00348 
00349         void ExprDescribe(ODesc* d) const;
00350 
00351         DECLARE_SERIAL(BinaryExpr);
00352 
00353         Expr* op1;
00354         Expr* op2;
00355 };
00356 
00357 class IncrExpr : public UnaryExpr {
00358 public:
00359         IncrExpr(BroExprTag tag, Expr* op);
00360 
00361         Val* Eval(Frame* f) const;
00362         Val* DoSingleEval(Frame* f, Val* v) const;
00363         int IsPure() const;
00364 
00365 protected:
00366         friend class Expr;
00367         IncrExpr()      { }
00368 
00369         DECLARE_SERIAL(IncrExpr);
00370 };
00371 
00372 class NotExpr : public UnaryExpr {
00373 public:
00374         NotExpr(Expr* op);
00375 
00376 protected:
00377         friend class Expr;
00378         NotExpr()       { }
00379 
00380         Expr* DoSimplify();
00381         Val* Fold(Val* v) const;
00382 
00383         DECLARE_SERIAL(NotExpr);
00384 };
00385 
00386 class PosExpr : public UnaryExpr {
00387 public:
00388         PosExpr(Expr* op);
00389 
00390 protected:
00391         friend class Expr;
00392         PosExpr()       { }
00393 
00394         Expr* DoSimplify();
00395         Val* Fold(Val* v) const;
00396 
00397         DECLARE_SERIAL(PosExpr);
00398 };
00399 
00400 class NegExpr : public UnaryExpr {
00401 public:
00402         NegExpr(Expr* op);
00403 
00404 protected:
00405         friend class Expr;
00406         NegExpr()       { }
00407 
00408         Expr* DoSimplify();
00409         Val* Fold(Val* v) const;
00410 
00411         DECLARE_SERIAL(NegExpr);
00412 };
00413 
00414 class AddExpr : public BinaryExpr {
00415 public:
00416         AddExpr(Expr* op1, Expr* op2);
00417         void Canonicize();
00418 
00419 protected:
00420         friend class Expr;
00421         AddExpr()       { }
00422 
00423         Expr* DoSimplify();
00424 
00425         DECLARE_SERIAL(AddExpr);
00426 
00427 };
00428 
00429 class SubExpr : public BinaryExpr {
00430 public:
00431         SubExpr(Expr* op1, Expr* op2);
00432 
00433 protected:
00434         friend class Expr;
00435         SubExpr()       { }
00436 
00437         Expr* DoSimplify();
00438 
00439         DECLARE_SERIAL(SubExpr);
00440 
00441 };
00442 
00443 class TimesExpr : public BinaryExpr {
00444 public:
00445         TimesExpr(Expr* op1, Expr* op2);
00446         void Canonicize();
00447 
00448 protected:
00449         friend class Expr;
00450         TimesExpr()     { }
00451 
00452         Expr* DoSimplify();
00453 
00454         DECLARE_SERIAL(TimesExpr);
00455 
00456 };
00457 
00458 class DivideExpr : public BinaryExpr {
00459 public:
00460         DivideExpr(Expr* op1, Expr* op2);
00461 
00462 protected:
00463         friend class Expr;
00464         DivideExpr()    { }
00465 
00466         Val* AddrFold(Val* v1, Val* v2) const;
00467         Expr* DoSimplify();
00468 
00469         DECLARE_SERIAL(DivideExpr);
00470 
00471 };
00472 
00473 class ModExpr : public BinaryExpr {
00474 public:
00475         ModExpr(Expr* op1, Expr* op2);
00476 
00477 protected:
00478         friend class Expr;
00479         ModExpr()       { }
00480 
00481         Expr* DoSimplify();
00482 
00483         DECLARE_SERIAL(ModExpr);
00484 };
00485 
00486 class BoolExpr : public BinaryExpr {
00487 public:
00488         BoolExpr(BroExprTag tag, Expr* op1, Expr* op2);
00489 
00490         Val* Eval(Frame* f) const;
00491         Val* DoSingleEval(Frame* f, Val* v1, Expr* op2) const;
00492 
00493 protected:
00494         friend class Expr;
00495         BoolExpr()      { }
00496 
00497         Expr* DoSimplify();
00498 
00499         DECLARE_SERIAL(BoolExpr);
00500 };
00501 
00502 class EqExpr : public BinaryExpr {
00503 public:
00504         EqExpr(BroExprTag tag, Expr* op1, Expr* op2);
00505         void Canonicize();
00506 
00507 protected:
00508         friend class Expr;
00509         EqExpr()        { }
00510 
00511         Expr* DoSimplify();
00512 
00513         Val* Fold(Val* v1, Val* v2) const;
00514 
00515         DECLARE_SERIAL(EqExpr);
00516 };
00517 
00518 class RelExpr : public BinaryExpr {
00519 public:
00520         RelExpr(BroExprTag tag, Expr* op1, Expr* op2);
00521         void Canonicize();
00522 
00523 protected:
00524         friend class Expr;
00525         RelExpr()       { }
00526 
00527         Expr* DoSimplify();
00528 
00529         DECLARE_SERIAL(RelExpr);
00530 };
00531 
00532 class CondExpr : public Expr {
00533 public:
00534         CondExpr(Expr* op1, Expr* op2, Expr* op3);
00535         ~CondExpr();
00536 
00537         Expr* Simplify(SimplifyType simp_type);
00538         Val* Eval(Frame* f) const;
00539         int IsPure() const;
00540 
00541         TraversalCode Traverse(TraversalCallback* cb) const;
00542 
00543 protected:
00544         friend class Expr;
00545         CondExpr()      { op1 = op2 = op3 = 0; }
00546 
00547         void ExprDescribe(ODesc* d) const;
00548 
00549         DECLARE_SERIAL(CondExpr);
00550 
00551         Expr* op1;
00552         Expr* op2;
00553         Expr* op3;
00554 };
00555 
00556 class RefExpr : public UnaryExpr {
00557 public:
00558         RefExpr(Expr* op);
00559 
00560         void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00561         Expr* MakeLvalue();
00562 
00563         // Only overridden to avoid special vector handling which doesn't apply
00564         // for this class.
00565         Val* Eval(Val* v) const;
00566 
00567 protected:
00568         friend class Expr;
00569         RefExpr()       { }
00570 
00571         Val* Fold(Val* v) const;
00572 
00573         DECLARE_SERIAL(RefExpr);
00574 };
00575 
00576 class AssignExpr : public BinaryExpr {
00577 public:
00578         AssignExpr(Expr* op1, Expr* op2, int is_init);
00579 
00580         Expr* Simplify(SimplifyType simp_type);
00581         Val* Eval(Frame* f) const;
00582         BroType* InitType() const;
00583         int IsRecordElement(TypeDecl* td) const;
00584         Val* InitVal(const BroType* t, Val* aggr) const;
00585         int IsPure() const;
00586 
00587 protected:
00588         friend class Expr;
00589         AssignExpr()    { }
00590 
00591         DECLARE_SERIAL(AssignExpr);
00592 
00593         int is_init;
00594 };
00595 
00596 class IndexExpr : public BinaryExpr {
00597 public:
00598         IndexExpr(Expr* op1, ListExpr* op2);
00599 
00600         int CanAdd() const;
00601         int CanDel() const;
00602 
00603         void Add(Frame* f);
00604         void Delete(Frame* f);
00605 
00606         Expr* Simplify(SimplifyType simp_type);
00607         void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00608         Expr* MakeLvalue();
00609 
00610         // Need to override Eval since it can take a vector arg but does
00611         // not necessarily return a vector.
00612         Val* Eval(Frame* f) const;
00613 
00614 protected:
00615         friend class Expr;
00616         IndexExpr()     { }
00617 
00618         Val* Fold(Val* v1, Val* v2) const;
00619 
00620         void ExprDescribe(ODesc* d) const;
00621 
00622         DECLARE_SERIAL(IndexExpr);
00623 };
00624 
00625 class FieldExpr : public UnaryExpr {
00626 public:
00627         FieldExpr(Expr* op, const char* field_name);
00628         ~FieldExpr();
00629 
00630         int Field() const       { return field; }
00631 
00632         Expr* Simplify(SimplifyType simp_type);
00633         void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00634 
00635         Expr* MakeLvalue();
00636 
00637 protected:
00638         friend class Expr;
00639         FieldExpr()     { field_name = 0; td = 0; }
00640 
00641         Val* Fold(Val* v) const;
00642 
00643         void ExprDescribe(ODesc* d) const;
00644 
00645         DECLARE_SERIAL(FieldExpr);
00646 
00647         const char* field_name;
00648         const TypeDecl* td;
00649         int field; // -1 = attributes
00650 };
00651 
00652 // "rec?$fieldname" is true if the value of $fieldname in rec is not nil.
00653 // "rec?$$attrname" is true if the attribute attrname is not nil.
00654 class HasFieldExpr : public UnaryExpr {
00655 public:
00656         HasFieldExpr(Expr* op, const char* field_name, bool is_attr);
00657         ~HasFieldExpr();
00658 
00659 protected:
00660         friend class Expr;
00661         HasFieldExpr()  { field_name = 0; }
00662 
00663         Val* Fold(Val* v) const;
00664 
00665         void ExprDescribe(ODesc* d) const;
00666 
00667         DECLARE_SERIAL(HasFieldExpr);
00668 
00669         bool is_attr;
00670         const char* field_name;
00671         int field;
00672 };
00673 
00674 class RecordConstructorExpr : public UnaryExpr {
00675 public:
00676         RecordConstructorExpr(ListExpr* constructor_list);
00677 
00678 protected:
00679         friend class Expr;
00680         RecordConstructorExpr() { }
00681 
00682         Val* InitVal(const BroType* t, Val* aggr) const;
00683         Val* Fold(Val* v) const;
00684 
00685         void ExprDescribe(ODesc* d) const;
00686 
00687         DECLARE_SERIAL(RecordConstructorExpr);
00688 };
00689 
00690 class FieldAssignExpr : public UnaryExpr {
00691 public:
00692         FieldAssignExpr(const char* field_name, Expr* value);
00693 
00694         const char* FieldName() const   { return field_name.c_str(); }
00695 
00696 protected:
00697         friend class Expr;
00698         FieldAssignExpr()       { }
00699 
00700         Val* Fold(Val* v) const;
00701         void ExprDescribe(ODesc* d) const;
00702 
00703         DECLARE_SERIAL(FieldAssignExpr);
00704 
00705         string field_name;
00706 };
00707 
00708 class RecordMatchExpr : public BinaryExpr {
00709 public:
00710         RecordMatchExpr(Expr* op1 /* record to match */, 
00711                         Expr* op2 /* cases to match against */);        
00712 
00713 protected:
00714         friend class Expr;
00715         RecordMatchExpr()
00716                 {
00717                 pred_field_index = result_field_index =
00718                         priority_field_index = 0;
00719                 }
00720 
00721         virtual Val* Fold(Val* v1, Val* v2) const;
00722         void ExprDescribe(ODesc*) const;
00723 
00724         DECLARE_SERIAL(RecordMatchExpr);
00725 
00726         // The following are used to hold the field offset of
00727         // $pred, $result, $priority, so the names only need to
00728         // be looked up at compile-time.
00729         int pred_field_index;
00730         int result_field_index;
00731         int priority_field_index;
00732 };
00733 
00734 class ArithCoerceExpr : public UnaryExpr {
00735 public:
00736         ArithCoerceExpr(Expr* op, TypeTag t);
00737 
00738 protected:
00739         friend class Expr;
00740         ArithCoerceExpr()       { }
00741 
00742         Expr* DoSimplify();
00743 
00744         Val* FoldSingleVal(Val* v, InternalTypeTag t) const;
00745         Val* Fold(Val* v) const;
00746 
00747         DECLARE_SERIAL(ArithCoerceExpr);
00748 };
00749 
00750 class RecordCoerceExpr : public UnaryExpr {
00751 public:
00752         RecordCoerceExpr(Expr* op, RecordType* r);
00753         ~RecordCoerceExpr();
00754 
00755 protected:
00756         friend class Expr;
00757         RecordCoerceExpr()      { map = 0; }
00758 
00759         Val* Fold(Val* v) const;
00760 
00761         DECLARE_SERIAL(RecordCoerceExpr);
00762 
00763         // For each super-record slot, gives subrecord slot with which to
00764         // fill it.
00765         int* map;
00766         int map_size;   // equivalent to Type()->AsRecordType()->NumFields()
00767 };
00768 
00769 // An internal operator for flattening array indices that are records
00770 // into a list of individual values.
00771 class FlattenExpr : public UnaryExpr {
00772 public:
00773         FlattenExpr(Expr* op);
00774 
00775 protected:
00776         friend class Expr;
00777         FlattenExpr()   { }
00778 
00779         Val* Fold(Val* v) const;
00780 
00781         DECLARE_SERIAL(FlattenExpr);
00782 
00783         int num_fields;
00784 };
00785 
00786 class EventHandler;
00787 
00788 class ScheduleTimer : public Timer {
00789 public:
00790         ScheduleTimer(EventHandlerPtr event, val_list* args, double t);
00791         ~ScheduleTimer();
00792 
00793         void Dispatch(double t, int is_expire);
00794 
00795 protected:
00796         EventHandlerPtr event;
00797         val_list* args;
00798 };
00799 
00800 class ScheduleExpr : public Expr {
00801 public:
00802         ScheduleExpr(Expr* when, EventExpr* event);
00803         ~ScheduleExpr();
00804 
00805         int IsPure() const;
00806 
00807         Expr* Simplify(SimplifyType simp_type);
00808 
00809         Val* Eval(Frame* f) const;
00810 
00811         Expr* When() const      { return when; }
00812         EventExpr* Event() const        { return event; }
00813 
00814         TraversalCode Traverse(TraversalCallback* cb) const;
00815 
00816 protected:
00817         friend class Expr;
00818         ScheduleExpr()  { when = 0; event = 0; }
00819 
00820         void ExprDescribe(ODesc* d) const;
00821 
00822         DECLARE_SERIAL(ScheduleExpr);
00823 
00824         Expr* when;
00825         EventExpr* event;
00826 };
00827 
00828 class InExpr : public BinaryExpr {
00829 public:
00830         InExpr(Expr* op1, Expr* op2);
00831 
00832 protected:
00833         friend class Expr;
00834         InExpr()        { }
00835 
00836         Val* Fold(Val* v1, Val* v2) const;
00837 
00838         DECLARE_SERIAL(InExpr);
00839 
00840 };
00841 
00842 class CallExpr : public Expr {
00843 public:
00844         CallExpr(Expr* func, ListExpr* args);
00845         ~CallExpr();
00846 
00847         Expr* Func() const      { return func; }
00848         ListExpr* Args() const  { return args; }
00849 
00850         int IsPure() const;
00851 
00852         Expr* Simplify(SimplifyType simp_type);
00853         Val* Eval(Frame* f) const;
00854 
00855         TraversalCode Traverse(TraversalCallback* cb) const;
00856 
00857 protected:
00858         friend class Expr;
00859         CallExpr()      { func = 0; args = 0; }
00860 
00861         void ExprDescribe(ODesc* d) const;
00862 
00863         DECLARE_SERIAL(CallExpr);
00864 
00865         Expr* func;
00866         ListExpr* args;
00867 };
00868 
00869 class EventExpr : public Expr {
00870 public:
00871         EventExpr(const char* name, ListExpr* args);
00872         ~EventExpr();
00873 
00874         const char* Name() const        { return name.c_str(); }
00875         ListExpr* Args() const          { return args; }
00876         EventHandlerPtr Handler()  const        { return handler; }
00877 
00878         Expr* Simplify(SimplifyType simp_type);
00879         Val* Eval(Frame* f) const;
00880 
00881         TraversalCode Traverse(TraversalCallback* cb) const;
00882 
00883 protected:
00884         friend class Expr;
00885         EventExpr()     { args = 0; }
00886 
00887         void ExprDescribe(ODesc* d) const;
00888 
00889         DECLARE_SERIAL(EventExpr);
00890 
00891         string name;
00892         EventHandlerPtr handler;
00893         ListExpr* args;
00894 };
00895 
00896 class ListExpr : public Expr {
00897 public:
00898         ListExpr();
00899         ListExpr(Expr* e);
00900         ~ListExpr();
00901 
00902         void Append(Expr* e);
00903 
00904         const expr_list& Exprs() const  { return exprs; }
00905         expr_list& Exprs()              { return exprs; }
00906 
00907         // True if the entire list represents pure values.
00908         int IsPure() const;
00909 
00910         // True if the entire list represents constant values.
00911         int AllConst() const;
00912 
00913         Expr* Simplify(SimplifyType simp_type);
00914         Val* Eval(Frame* f) const;
00915         BroType* InitType() const;
00916         Val* InitVal(const BroType* t, Val* aggr) const;
00917         Expr* MakeLvalue();
00918         void Assign(Frame* f, Val* v, Opcode op = OP_ASSIGN);
00919 
00920         TraversalCode Traverse(TraversalCallback* cb) const;
00921 
00922 protected:
00923         Val* AddSetInit(const BroType* t, Val* aggr) const;
00924 
00925         void ExprDescribe(ODesc* d) const;
00926 
00927         DECLARE_SERIAL(ListExpr);
00928 
00929         expr_list exprs;
00930 };
00931 
00932 
00933 class RecordAssignExpr : public ListExpr {
00934 public:
00935         RecordAssignExpr(Expr* record, Expr* init_list, int is_init);
00936 
00937         Val* Eval(Frame* f) const       { return ListExpr::Eval(f); }
00938 
00939 protected:
00940         friend class Expr;
00941         RecordAssignExpr()      { }
00942 
00943         DECLARE_SERIAL(RecordAssignExpr);
00944 };
00945 
00946 inline Val* Expr::ExprVal() const
00947         {
00948         if ( ! IsConst() )
00949                 BadTag("ExprVal::Val");
00950         return ((ConstExpr*) this)->Value();
00951         }
00952 
00953 // Decides whether to return an AssignExpr or a RecordAssignExpr.
00954 Expr* get_assign_expr(Expr* op1, Expr* op2, int is_init);
00955 
00956 // Type-check the given expression(s) against the given type(s).  Complain
00957 // if the expression cannot match the given type, returning 0.  If it can
00958 // match, promote it as necessary (modifying the ref parameter accordingly)
00959 // and return 1.
00960 extern int check_and_promote_expr(Expr*& e, BroType* t);
00961 extern int check_and_promote_exprs(ListExpr*& elements, TypeList* types);
00962 
00963 // Returns a fully simplified form of the expression.  Note that passed
00964 // expression and its subexpressions may be modified, Unref()'d, etc.
00965 Expr* simplify_expr(Expr* e, SimplifyType simp_type);
00966 
00967 // Returns a simplified ListExpr - guaranteed to still be a ListExpr,
00968 // even if it only contains one expr.
00969 ListExpr* simplify_expr_list(ListExpr* l, SimplifyType simp_type);
00970 
00971 // Returns a ListExpr simplified down to a list a values, or a nil
00972 // pointer if they couldn't all be reduced.
00973 val_list* eval_list(Frame* f, const ListExpr* l);
00974 
00975 // Returns true if two expressions are identical.
00976 extern int same_expr(const Expr* e1, const Expr* e2);
00977 
00978 // Returns true if e1 is "greater" than e2 - here "greater" is just
00979 // a heuristic, used with commutative operators to put them into
00980 // a canonical form.
00981 extern int expr_greater(const Expr* e1, const Expr* e2);
00982 
00983 // Return constants of the given type.
00984 Expr* make_zero(BroType* t);
00985 Expr* make_one(BroType* t);
00986 
00987 // True if the given Val* has a vector type
00988 inline bool is_vector(Expr* e)  { return e->Type()->Tag() == TYPE_VECTOR; }
00989 
00990 #endif

Generated on Wed Sep 14 02:56:09 2005 for bro_docs by doxygen 1.3.5