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

ArithCoerceExpr Class Reference

#include <Expr.h>

Inheritance diagram for ArithCoerceExpr:

Inheritance graph
[legend]
Collaboration diagram for ArithCoerceExpr:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ArithCoerceExpr (Expr *op, TypeTag t)

Protected Member Functions

 ArithCoerceExpr ()
ExprDoSimplify ()
ValFoldSingleVal (Val *v, InternalTypeTag t) const
ValFold (Val *v) const
 DECLARE_SERIAL (ArithCoerceExpr)

Friends

class Expr

Constructor & Destructor Documentation

ArithCoerceExpr::ArithCoerceExpr Expr op,
TypeTag  t
 

Definition at line 3193 of file Expr.cc.

References BroType::AsVectorType(), base_type(), EXPR_ARITH_COERCE, Expr::ExprError(), IsArithmetic, IsBool, Expr::IsError(), IsVector, Expr::SetType(), BroType::Tag(), Expr::Type(), TYPE_ENUM, TYPE_INTERVAL, TYPE_TIME, TypeTag, and VectorType::YieldType().

03194 : UnaryExpr(EXPR_ARITH_COERCE, arg_op)
03195         {
03196         if ( IsError() )
03197                 return;
03198 
03199         TypeTag bt = op->Type()->Tag();
03200         TypeTag vbt = bt;
03201 
03202         if ( IsVector(bt) )
03203                 {
03204                 SetType(new VectorType(base_type(t)));
03205                 vbt = op->Type()->AsVectorType()->YieldType()->Tag();
03206                 }
03207         else
03208                 SetType(base_type(t));
03209 
03210         if ( (bt == TYPE_ENUM) != (t == TYPE_ENUM) )
03211                 ExprError("can't convert to/from enumerated type");
03212 
03213         else if ( ! IsArithmetic(t) && ! IsBool(t) &&
03214                   t != TYPE_TIME && t != TYPE_INTERVAL )
03215                 ExprError("bad coercion");
03216 
03217         else if ( ! IsArithmetic(bt) && ! IsBool(bt) &&
03218                   ! IsArithmetic(vbt) && ! IsBool(vbt) )
03219                 ExprError("bad coercion value");
03220         }

ArithCoerceExpr::ArithCoerceExpr  )  [inline, protected]
 

Definition at line 740 of file Expr.h.

00740 { }


Member Function Documentation

ArithCoerceExpr::DECLARE_SERIAL ArithCoerceExpr   )  [protected]
 

Expr * ArithCoerceExpr::DoSimplify  )  [protected, virtual]
 

Reimplemented from UnaryExpr.

Definition at line 3222 of file Expr.cc.

References bro_int_t, bro_uint_t, double, Expr::ExprVal(), int, BroObj::Internal(), Val::InternalDouble(), Val::InternalInt(), BroType::InternalType(), InternalTypeTag, Val::InternalUnsigned(), is_vector(), Expr::IsConst(), Expr::Ref(), Expr::Type(), TYPE_INTERNAL_DOUBLE, TYPE_INTERNAL_INT, TYPE_INTERNAL_UNSIGNED, Val, and BroObj::Warn().

03223         {
03224         if ( is_vector(op) )
03225                 return this;
03226 
03227         InternalTypeTag my_int = type->InternalType();
03228         InternalTypeTag op_int = op->Type()->InternalType();
03229 
03230         if ( my_int == TYPE_INTERNAL_UNSIGNED )
03231                 my_int = TYPE_INTERNAL_INT;
03232         if ( op_int == TYPE_INTERNAL_UNSIGNED )
03233                 op_int = TYPE_INTERNAL_INT;
03234 
03235         if ( my_int == op_int )
03236                 return op->Ref();
03237 
03238         if ( op->IsConst() )
03239                 {
03240                 if ( my_int == TYPE_INTERNAL_INT )
03241                         {
03242                         if ( op_int != TYPE_INTERNAL_DOUBLE )
03243                                 Internal("bad coercion in CoerceExpr::DoSimplify");
03244                         double d = op->ExprVal()->InternalDouble();
03245                         bro_int_t i = int(d);
03246 
03247                         if ( i < 0 &&
03248                              type->InternalType() == TYPE_INTERNAL_UNSIGNED )
03249                                 Warn("coercion produces negative count value");
03250 
03251                         if ( d != double(i) )
03252                                 Warn("coercion loses precision");
03253 
03254                         return new ConstExpr(new Val(i, type->Tag()));
03255                         }
03256 
03257                 if ( my_int == TYPE_INTERNAL_DOUBLE )
03258                         {
03259                         if ( op_int == TYPE_INTERNAL_INT )
03260                                 {
03261                                 bro_int_t i = op->ExprVal()->InternalInt();
03262                                 double d = double(i);
03263 
03264                                 if ( i != bro_int_t(d) )
03265                                         Warn("coercion loses precision");
03266 
03267                                 return new ConstExpr(new Val(d, type->Tag()));
03268                                 }
03269 
03270                         if ( op_int == TYPE_INTERNAL_UNSIGNED )
03271                                 {
03272                                 bro_uint_t u = op->ExprVal()->InternalUnsigned();
03273                                 double d = double(u);
03274 
03275                                 if ( u != (bro_uint_t) (d) )
03276                                         Warn("coercion loses precision");
03277 
03278                                 return new ConstExpr(new Val(d, type->Tag()));
03279                                 }
03280 
03281                         }
03282 
03283                 Internal("bad coercion in CoerceExpr::DoSimplify");
03284                 }
03285 
03286         return this;
03287         }

Val * ArithCoerceExpr::Fold Val v  )  const [protected, virtual]
 

Reimplemented from UnaryExpr.

Definition at line 3307 of file Expr.cc.

References VectorVal::Assign(), BroType::AsVectorType(), FoldSingleVal(), BroType::InternalType(), InternalTypeTag, is_vector(), VectorVal::Lookup(), VectorVal::Size(), Expr::Type(), TYPE_VECTOR, VECTOR_MIN, and VectorType::YieldType().

03308         {
03309         InternalTypeTag t = type->InternalType();
03310 
03311         if ( ! is_vector(v) )
03312                 {
03313                 // Our result type might be vector, in which case this
03314                 // invocation is being done per-element rather than on
03315                 // the whole vector.  Correct the type tag if necessary.
03316                 if ( type->Tag() == TYPE_VECTOR )
03317                         t = Type()->AsVectorType()->YieldType()->InternalType();
03318                 return FoldSingleVal(v, t);
03319                 }
03320 
03321         t = Type()->AsVectorType()->YieldType()->InternalType();
03322 
03323         VectorVal* vv = v->AsVectorVal();
03324         VectorVal* result = new VectorVal(Type()->AsVectorType());
03325         for ( unsigned int i = VECTOR_MIN; i < vv->Size() + VECTOR_MIN; ++i )
03326                 {
03327                 Val* elt = vv->Lookup(i);
03328                 if ( elt )
03329                         result->Assign(i, FoldSingleVal(elt, t), this);
03330                 else
03331                         result->Assign(i, 0, this);
03332                 }
03333 
03334         return result;
03335         }

Val * ArithCoerceExpr::FoldSingleVal Val v,
InternalTypeTag  t
const [protected]
 

Definition at line 3289 of file Expr.cc.

References Val::CoerceToDouble(), Val::CoerceToInt(), Val::CoerceToUnsigned(), BroObj::Internal(), TYPE_COUNT, TYPE_DOUBLE, TYPE_INT, TYPE_INTERNAL_DOUBLE, TYPE_INTERNAL_INT, TYPE_INTERNAL_UNSIGNED, and Val.

Referenced by Fold().

03290         {
03291         switch ( t ) {
03292         case TYPE_INTERNAL_DOUBLE:
03293                 return new Val(v->CoerceToDouble(), TYPE_DOUBLE);
03294 
03295         case TYPE_INTERNAL_INT:
03296                 return new Val(v->CoerceToInt(), TYPE_INT);
03297 
03298         case TYPE_INTERNAL_UNSIGNED:
03299                 return new Val(v->CoerceToUnsigned(), TYPE_COUNT);
03300 
03301         default:
03302                 Internal("bad type in CoerceExpr::Fold");
03303                 return 0;
03304         }
03305         }


Friends And Related Function Documentation

friend class Expr [friend]
 

Reimplemented from UnaryExpr.

Definition at line 739 of file Expr.h.


The documentation for this class was generated from the following files:
Generated on Wed Sep 14 03:07:38 2005 for bro_docs by doxygen 1.3.5