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

dtool/src/cppparser/cppExpression.cxx

Go to the documentation of this file.
00001 // Filename: cppExpression.cxx
00002 // Created by:  drose (25Oct99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "cppExpression.h"
00021 #include "cppToken.h"
00022 #include "cppIdentifier.h"
00023 #include "cppType.h"
00024 #include "cppSimpleType.h"
00025 #include "cppPointerType.h"
00026 #include "cppConstType.h"
00027 #include "cppArrayType.h"
00028 #include "cppPreprocessor.h"
00029 #include "cppInstance.h"
00030 #include "cppFunctionGroup.h"
00031 #include "cppFunctionType.h"
00032 #include "cppBison.h"
00033 
00034 #include <assert.h>
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: CPPExpresion::Result::Constructor
00038 //       Access: Public
00039 //  Description:
00040 ////////////////////////////////////////////////////////////////////
00041 CPPExpression::Result::
00042 Result() {
00043   _type = RT_error;
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: CPPExpresion::Result::Constructor
00048 //       Access: Public
00049 //  Description:
00050 ////////////////////////////////////////////////////////////////////
00051 CPPExpression::Result::
00052 Result(int value) {
00053   _type = RT_integer;
00054   _u._integer = value;
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: CPPExpresion::Result::Constructor
00059 //       Access: Public
00060 //  Description:
00061 ////////////////////////////////////////////////////////////////////
00062 CPPExpression::Result::
00063 Result(double value) {
00064   _type = RT_real;
00065   _u._real = value;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: CPPExpresion::Result::Constructor
00070 //       Access: Public
00071 //  Description:
00072 ////////////////////////////////////////////////////////////////////
00073 CPPExpression::Result::
00074 Result(void *value) {
00075   _type = RT_pointer;
00076   _u._pointer = value;
00077 }
00078 
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: CPPExpresion::Result::as_integer
00082 //       Access: Public
00083 //  Description:
00084 ////////////////////////////////////////////////////////////////////
00085 int CPPExpression::Result::
00086 as_integer() const {
00087   switch (_type) {
00088   case RT_integer:
00089     return _u._integer;
00090 
00091   case RT_real:
00092     return (int)_u._real;
00093 
00094   case RT_pointer:
00095     return (int)_u._pointer;
00096 
00097   default:
00098     cerr << "Invalid type\n";
00099     assert(false);
00100     return 0;
00101   }
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: CPPExpresion::Result::as_real
00106 //       Access: Public
00107 //  Description:
00108 ////////////////////////////////////////////////////////////////////
00109 double CPPExpression::Result::
00110 as_real() const {
00111   switch (_type) {
00112   case RT_integer:
00113     return (double)_u._integer;
00114 
00115   case RT_real:
00116     return _u._real;
00117 
00118   case RT_pointer:
00119     return (double)(int)_u._pointer;
00120 
00121   default:
00122     cerr << "Invalid type\n";
00123     assert(false);
00124     return 0.0;
00125   }
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: CPPExpresion::Result::as_pointer
00130 //       Access: Public
00131 //  Description:
00132 ////////////////////////////////////////////////////////////////////
00133 void *CPPExpression::Result::
00134 as_pointer() const {
00135   switch (_type) {
00136   case RT_integer:
00137     return (void *)_u._integer;
00138 
00139   case RT_real:
00140     return (void *)(int)_u._real;
00141 
00142   case RT_pointer:
00143     return _u._pointer;
00144 
00145   default:
00146     cerr << "Invalid type\n";
00147     assert(false);
00148     return (void *)NULL;
00149   }
00150 }
00151 
00152 ////////////////////////////////////////////////////////////////////
00153 //     Function: CPPExpresion::Result::output
00154 //       Access: Public
00155 //  Description:
00156 ////////////////////////////////////////////////////////////////////
00157 void CPPExpression::Result::
00158 output(ostream &out) const {
00159   switch (_type) {
00160   case RT_integer:
00161     out << _u._integer;
00162     break;
00163 
00164   case RT_real:
00165     out << _u._real;
00166     break;
00167 
00168   case RT_pointer:
00169     out << _u._pointer;
00170     break;
00171 
00172   case RT_error:
00173     out << "(error)";
00174     break;
00175 
00176   default:
00177     out << "(**invalid type**)\n";
00178   }
00179 }
00180 
00181 ////////////////////////////////////////////////////////////////////
00182 //     Function: CPPExpresion::Constructor
00183 //       Access: Public
00184 //  Description:
00185 ////////////////////////////////////////////////////////////////////
00186 CPPExpression::
00187 CPPExpression(int value) :
00188   CPPDeclaration(CPPFile())
00189 {
00190   _type = T_integer;
00191   _u._integer = value;
00192 }
00193 
00194 ////////////////////////////////////////////////////////////////////
00195 //     Function: CPPExpression::Constructor
00196 //       Access: Public
00197 //  Description:
00198 ////////////////////////////////////////////////////////////////////
00199 CPPExpression::
00200 CPPExpression(double value) :
00201   CPPDeclaration(CPPFile())
00202 {
00203   _type = T_real;
00204   _u._real = value;
00205 }
00206 
00207 ////////////////////////////////////////////////////////////////////
00208 //     Function: CPPExpression::Constructor
00209 //       Access: Public
00210 //  Description:
00211 ////////////////////////////////////////////////////////////////////
00212 CPPExpression::
00213 CPPExpression(const string &value) :
00214   CPPDeclaration(CPPFile())
00215 {
00216   _type = T_string;
00217   _str = value;
00218 }
00219 
00220 ////////////////////////////////////////////////////////////////////
00221 //     Function: CPPExpression::Constructor
00222 //       Access: Public
00223 //  Description:
00224 ////////////////////////////////////////////////////////////////////
00225 CPPExpression::
00226 CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
00227               CPPScope *global_scope, CPPPreprocessor *error_sink) :
00228   CPPDeclaration(CPPFile())
00229 {
00230   CPPDeclaration *decl =
00231     ident->find_symbol(current_scope, global_scope, error_sink);
00232 
00233   if (decl != NULL) {
00234     CPPInstance *inst = decl->as_instance();
00235     if (inst != NULL) {
00236       _type = T_variable;
00237       _u._variable = inst;
00238       return;
00239     }
00240     CPPFunctionGroup *fgroup = decl->as_function_group();
00241     if (fgroup != NULL) {
00242       _type = T_function;
00243       _u._fgroup = fgroup;
00244       return;
00245     }
00246   }
00247 
00248   _type = T_unknown_ident;
00249   _u._ident = ident;
00250 }
00251 
00252 ////////////////////////////////////////////////////////////////////
00253 //     Function: CPPExpression::Constructor
00254 //       Access: Public
00255 //  Description:
00256 ////////////////////////////////////////////////////////////////////
00257 CPPExpression::
00258 CPPExpression(int unary_operator, CPPExpression *op1) :
00259   CPPDeclaration(CPPFile())
00260 {
00261   _type = T_unary_operation;
00262   _u._op._operator = unary_operator;
00263   _u._op._op1 = op1;
00264   _u._op._op2 = NULL;
00265   _u._op._op3 = NULL;
00266 }
00267 
00268 ////////////////////////////////////////////////////////////////////
00269 //     Function: CPPExpression::Constructor
00270 //       Access: Public
00271 //  Description:
00272 ////////////////////////////////////////////////////////////////////
00273 CPPExpression::
00274 CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2) :
00275   CPPDeclaration(CPPFile())
00276 {
00277   _type = T_binary_operation;
00278   _u._op._operator = binary_operator;
00279   _u._op._op1 = op1;
00280   _u._op._op2 = op2;
00281   _u._op._op3 = NULL;
00282 }
00283 
00284 ////////////////////////////////////////////////////////////////////
00285 //     Function: CPPExpression::Constructor
00286 //       Access: Public
00287 //  Description:
00288 ////////////////////////////////////////////////////////////////////
00289 CPPExpression::
00290 CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2,
00291               CPPExpression *op3) :
00292   CPPDeclaration(CPPFile())
00293 {
00294   _type = T_trinary_operation;
00295   _u._op._operator = trinary_operator;
00296   _u._op._op1 = op1;
00297   _u._op._op2 = op2;
00298   _u._op._op3 = op3;
00299 }
00300 
00301 ////////////////////////////////////////////////////////////////////
00302 //     Function: CPPExpression::named typecast_op constructor
00303 //       Access: Public, Static
00304 //  Description: Creates an expression that represents a typecast
00305 //               operation.
00306 ////////////////////////////////////////////////////////////////////
00307 CPPExpression CPPExpression::
00308 typecast_op(CPPType *type, CPPExpression *op1) {
00309   CPPExpression expr(0);
00310   expr._type = T_typecast;
00311   expr._u._typecast._to = type;
00312   expr._u._typecast._op1 = op1;
00313   return expr;
00314 }
00315 
00316 ////////////////////////////////////////////////////////////////////
00317 //     Function: CPPExpression::named construct_op constructor
00318 //       Access: Public, Static
00319 //  Description: Creates an expression that represents a constructor
00320 //               call.
00321 ////////////////////////////////////////////////////////////////////
00322 CPPExpression CPPExpression::
00323 construct_op(CPPType *type, CPPExpression *op1) {
00324   CPPExpression expr(0);
00325   if (op1 == NULL) {
00326     // A default constructor call--no parameters.
00327     expr._type = T_default_construct;
00328     expr._u._typecast._to = type;
00329     expr._u._typecast._op1 = NULL;
00330   } else {
00331     // A normal constructor call, with parameters.
00332     expr._type = T_construct;
00333     expr._u._typecast._to = type;
00334     expr._u._typecast._op1 = op1;
00335   }
00336   return expr;
00337 }
00338 
00339 ////////////////////////////////////////////////////////////////////
00340 //     Function: CPPExpression::named new_op constructor
00341 //       Access: Public, Static
00342 //  Description: Creates an expression that represents a use of the
00343 //               new operator.
00344 ////////////////////////////////////////////////////////////////////
00345 CPPExpression CPPExpression::
00346 new_op(CPPType *type, CPPExpression *op1) {
00347   CPPExpression expr(0);
00348   if (op1 == NULL) {
00349     // A default new operation--no parameters.
00350     expr._type = T_default_new;
00351     expr._u._typecast._to = type;
00352     expr._u._typecast._op1 = NULL;
00353   } else {
00354     // A normal new operation, with parameters.
00355     expr._type = T_new;
00356     expr._u._typecast._to = type;
00357     expr._u._typecast._op1 = op1;
00358   }
00359   return expr;
00360 }
00361 
00362 ////////////////////////////////////////////////////////////////////
00363 //     Function: CPPExpression::named sizeof_func constructor
00364 //       Access: Public, Static
00365 //  Description:
00366 ////////////////////////////////////////////////////////////////////
00367 CPPExpression CPPExpression::
00368 sizeof_func(CPPType *type) {
00369   CPPExpression expr(0);
00370   expr._type = T_sizeof;
00371   expr._u._typecast._to = type;
00372   expr._u._typecast._op1 = NULL;
00373   return expr;
00374 }
00375 
00376 ////////////////////////////////////////////////////////////////////
00377 //     Function: CPPExpression::Destructor
00378 //       Access: Public
00379 //  Description:
00380 ////////////////////////////////////////////////////////////////////
00381 CPPExpression::
00382 ~CPPExpression() {
00383 }
00384 
00385 ////////////////////////////////////////////////////////////////////
00386 //     Function: CPPExpression::evaluate
00387 //       Access: Public
00388 //  Description:
00389 ////////////////////////////////////////////////////////////////////
00390 CPPExpression::Result CPPExpression::
00391 evaluate() const {
00392   Result r1, r2;
00393 
00394   switch (_type) {
00395   case T_integer:
00396     return Result(_u._integer);
00397 
00398   case T_real:
00399     return Result(_u._real);
00400 
00401   case T_string:
00402     return Result();
00403 
00404   case T_variable:
00405   case T_function:
00406     return Result();
00407 
00408   case T_unknown_ident:
00409     return Result();
00410 
00411   case T_typecast:
00412     assert(_u._typecast._op1 != NULL);
00413     r1 = _u._typecast._op1->evaluate();
00414     if (r1._type != RT_error) {
00415       CPPSimpleType *stype = _u._typecast._to->as_simple_type();
00416       if (stype != NULL) {
00417         if (stype->_type == CPPSimpleType::T_int) {
00418           return Result(r1.as_integer());
00419         } else if (stype->_type == CPPSimpleType::T_float ||
00420                    stype->_type == CPPSimpleType::T_double) {
00421           return Result(r1.as_real());
00422         }
00423       }
00424       if (_u._typecast._to->as_pointer_type()) {
00425         return Result(r1.as_pointer());
00426       }
00427     }
00428     return Result();
00429 
00430   case T_construct:
00431   case T_default_construct:
00432   case T_new:
00433   case T_default_new:
00434   case T_sizeof:
00435     return Result();
00436 
00437   case T_binary_operation:
00438     assert(_u._op._op2 != NULL);
00439     r2 = _u._op._op2->evaluate();
00440 
00441     // The operators && and || are special cases: these are
00442     // shirt-circuiting operators.  Thus, if we are using either of
00443     // these it might be acceptable for the second operand to be
00444     // invalid, since we might never evaluate it.
00445 
00446     // In all other cases, both operands must be valid in order for
00447     // the operation to be valid.
00448     if (r2._type == RT_error &&
00449         (_u._op._operator != OROR && _u._op._operator != ANDAND)) {
00450       return r2;
00451     }
00452     // Fall through
00453 
00454 
00455   case T_trinary_operation:
00456     // The trinary operator is also a short-circuiting operator: we
00457     // don't test the second or third operands until we need them.
00458     // The only critical one is the first operand.
00459 
00460     // Fall through
00461 
00462   case T_unary_operation:
00463     assert(_u._op._op1 != NULL);
00464     r1 = _u._op._op1->evaluate();
00465     if (r1._type == RT_error) {
00466       // Here's one more special case: if the first operand is
00467       // invalid, it really means we don't know how to evaluate it.
00468       // However, if the operator is ||, then it might not matter as
00469       // long as we can evaluate the second one *and* that comes out
00470       // to be true.
00471       if (_u._op._operator == OROR && r2._type == RT_integer &&
00472           r2.as_integer() != 0) {
00473         return r2;
00474       }
00475 
00476       // Ditto for the operator being && and the second one coming out
00477       // false.
00478       if (_u._op._operator == ANDAND && r2._type == RT_integer &&
00479           r2.as_integer() == 0) {
00480         return r2;
00481       }
00482 
00483       return r1;
00484     }
00485 
00486     switch (_u._op._operator) {
00487     case UNARY_NOT:
00488       return Result(!r1.as_integer());
00489 
00490     case UNARY_NEGATE:
00491       return Result(~r1.as_integer());
00492 
00493     case UNARY_MINUS:
00494       return (r1._type == RT_real) ? Result(-r1.as_real()) : Result(-r1.as_integer());
00495 
00496     case UNARY_STAR:
00497     case UNARY_REF:
00498       return Result();
00499 
00500     case '*':
00501       if (r1._type == RT_real || r2._type == RT_real) {
00502         return Result(r1.as_real() * r2.as_real());
00503       } else {
00504         return Result(r1.as_integer() * r2.as_integer());
00505       }
00506 
00507     case '/':
00508       if (r1._type == RT_real || r2._type == RT_real) {
00509         return Result(r1.as_real() / r2.as_real());
00510       } else {
00511         return Result(r1.as_integer() / r2.as_integer());
00512       }
00513 
00514     case '%':
00515       return Result(r1.as_integer() % r2.as_integer());
00516 
00517     case '+':
00518       if (r1._type == RT_real || r2._type == RT_real) {
00519         return Result(r1.as_real() + r2.as_real());
00520       } else {
00521         return Result(r1.as_integer() + r2.as_integer());
00522       }
00523 
00524     case '-':
00525       if (r1._type == RT_real || r2._type == RT_real) {
00526         return Result(r1.as_real() - r2.as_real());
00527       } else {
00528         return Result(r1.as_integer() - r2.as_integer());
00529       }
00530 
00531     case '|':
00532       return Result(r1.as_integer() | r2.as_integer());
00533 
00534     case '&':
00535       return Result(r1.as_integer() & r2.as_integer());
00536 
00537     case OROR:
00538       if (r1.as_integer()) {
00539         return r1;
00540       } else {
00541         return r2;
00542       }
00543 
00544     case ANDAND:
00545       if (r1.as_integer()) {
00546         return r2;
00547       } else {
00548         return r1;
00549       }
00550 
00551     case EQCOMPARE:
00552       if (r1._type == RT_real || r2._type == RT_real) {
00553         return Result(r1.as_real() == r2.as_real());
00554       } else {
00555         return Result(r1.as_integer() == r2.as_integer());
00556       }
00557 
00558     case NECOMPARE:
00559       if (r1._type == RT_real || r2._type == RT_real) {
00560         return Result(r1.as_real() != r2.as_real());
00561       } else {
00562         return Result(r1.as_integer() != r2.as_integer());
00563       }
00564 
00565     case LECOMPARE:
00566       if (r1._type == RT_real || r2._type == RT_real) {
00567         return Result(r1.as_real() <= r2.as_real());
00568       } else {
00569         return Result(r1.as_integer() <= r2.as_integer());
00570       }
00571 
00572     case GECOMPARE:
00573       if (r1._type == RT_real || r2._type == RT_real) {
00574         return Result(r1.as_real() >= r2.as_real());
00575       } else {
00576         return Result(r1.as_integer() >= r2.as_integer());
00577       }
00578 
00579     case '<':
00580       if (r1._type == RT_real || r2._type == RT_real) {
00581         return Result(r1.as_real() < r2.as_real());
00582       } else {
00583         return Result(r1.as_integer() < r2.as_integer());
00584       }
00585 
00586     case '>':
00587       if (r1._type == RT_real || r2._type == RT_real) {
00588         return Result(r1.as_real() > r2.as_real());
00589       } else {
00590         return Result(r1.as_integer() > r2.as_integer());
00591       }
00592 
00593     case LSHIFT:
00594       return Result(r1.as_integer() << r2.as_integer());
00595 
00596     case RSHIFT:
00597       return Result(r1.as_integer() >> r2.as_integer());
00598 
00599     case '?':
00600       return r1.as_integer() ?
00601         _u._op._op2->evaluate() : _u._op._op3->evaluate();
00602 
00603     case '.':
00604     case POINTSAT:
00605       return Result();
00606 
00607     case '[': // Array element reference
00608       return Result();
00609 
00610     case 'f': // Function evaluation
00611       return Result();
00612 
00613     case ',':
00614       return r2;
00615 
00616     default:
00617       cerr << "**unexpected operator**\n";
00618       abort();
00619     }
00620 
00621   default:
00622     cerr << "**invalid operand**\n";
00623     abort();
00624   }
00625 
00626   return Result();  // Compiler kludge; can't get here.
00627 }
00628 
00629 ////////////////////////////////////////////////////////////////////
00630 //     Function: CPPExpression::determine_type
00631 //       Access: Public
00632 //  Description: Returns the type of the expression, if it is known,
00633 //               or NULL if the type cannot be determined.
00634 ////////////////////////////////////////////////////////////////////
00635 CPPType *CPPExpression::
00636 determine_type() const {
00637   CPPType *t1 = (CPPType *)NULL;
00638   CPPType *t2 = (CPPType *)NULL;
00639 
00640   CPPType *int_type =
00641     CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
00642 
00643   CPPType *bool_type =
00644     CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
00645 
00646   CPPType *float_type =
00647     CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
00648 
00649   CPPType *char_type =
00650     CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
00651 
00652   CPPType *const_char_type =
00653     CPPType::new_type(new CPPConstType(char_type));
00654 
00655   CPPType *char_star_type =
00656     CPPType::new_type(new CPPPointerType(const_char_type));
00657 
00658   switch (_type) {
00659   case T_integer:
00660     return int_type;
00661 
00662   case T_real:
00663     return float_type;
00664 
00665   case T_string:
00666     return char_star_type;
00667 
00668   case T_variable:
00669     return _u._variable->_type;
00670 
00671   case T_function:
00672     if (_u._fgroup->get_return_type() == (CPPType *)NULL) {
00673       // There are multiple functions by this name that have different
00674       // return types.  We could attempt to differentiate them based
00675       // on the parameter list, but that's a lot of work.  Let's just
00676       // give up.
00677       return (CPPType *)NULL;
00678     }
00679     return _u._fgroup->_instances.front()->_type;
00680 
00681   case T_unknown_ident:
00682     return (CPPType *)NULL;
00683 
00684   case T_typecast:
00685   case T_construct:
00686   case T_default_construct:
00687     return _u._typecast._to;
00688 
00689   case T_new:
00690   case T_default_new:
00691     return CPPType::new_type(new CPPPointerType(_u._typecast._to));
00692 
00693   case T_sizeof:
00694     return int_type;
00695 
00696   case T_binary_operation:
00697   case T_trinary_operation:
00698     assert(_u._op._op2 != NULL);
00699     t2 = _u._op._op2->determine_type();
00700     // Fall through
00701 
00702   case T_unary_operation:
00703     assert(_u._op._op1 != NULL);
00704     t1 = _u._op._op1->determine_type();
00705 
00706     switch (_u._op._operator) {
00707     case UNARY_NOT:
00708       return bool_type;
00709 
00710     case UNARY_NEGATE:
00711       return int_type;
00712 
00713     case UNARY_MINUS:
00714       return t1;
00715 
00716     case UNARY_STAR:
00717     case '[': // Array element reference
00718       if (t1 != NULL) {
00719         if (t1->as_pointer_type()) {
00720           return t1->as_pointer_type()->_pointing_at;
00721         }
00722         if (t1->as_array_type()) {
00723           return t1->as_array_type()->_element_type;
00724         }
00725       }
00726       return NULL;
00727 
00728     case UNARY_REF:
00729       return t1;
00730 
00731     case '*':
00732     case '/':
00733     case '+':
00734     case '-':
00735       if (t1 == NULL) {
00736         return t2;
00737       } else if (t2 == NULL) {
00738         return t1;
00739       } else if (t1->as_pointer_type()) {
00740         if (t2->as_pointer_type()) {
00741           return int_type;
00742         }
00743         return t1;
00744       }
00745       return elevate_type(t1, t2);
00746 
00747     case '%':
00748     case '|':
00749     case '&':
00750     case LSHIFT:
00751     case RSHIFT:
00752       return int_type;
00753 
00754     case OROR:
00755     case ANDAND:
00756     case EQCOMPARE:
00757     case NECOMPARE:
00758     case LECOMPARE:
00759     case GECOMPARE:
00760     case '<':
00761     case '>':
00762       return bool_type;
00763 
00764     case '?':
00765       return t2;
00766 
00767     case '.':
00768     case POINTSAT:
00769       return NULL;
00770 
00771     case 'f': // Function evaluation
00772       if (t1 != NULL) {
00773         CPPFunctionType *ftype = t1->as_function_type();
00774         if (ftype != (CPPFunctionType *)NULL) {
00775           return ftype->_return_type;
00776         }
00777       }
00778       return NULL;
00779 
00780     case ',':
00781       return t2;
00782 
00783     default:
00784       cerr << "**unexpected operator**\n";
00785       abort();
00786     }
00787 
00788   default:
00789     cerr << "**invalid operand**\n";
00790     abort();
00791   }
00792 
00793   return NULL;  // Compiler kludge; can't get here.
00794 }
00795 
00796 ////////////////////////////////////////////////////////////////////
00797 //     Function: CPPExpression::substitute_decl
00798 //       Access: Public, Virtual
00799 //  Description:
00800 ////////////////////////////////////////////////////////////////////
00801 CPPDeclaration *CPPExpression::
00802 substitute_decl(CPPDeclaration::SubstDecl &subst,
00803                 CPPScope *current_scope, CPPScope *global_scope) {
00804   CPPDeclaration *top =
00805     CPPDeclaration::substitute_decl(subst, current_scope, global_scope);
00806   if (top != this) {
00807     return top;
00808   }
00809 
00810   CPPExpression *rep = new CPPExpression(*this);
00811   bool any_changed = false;
00812   CPPDeclaration *decl;
00813 
00814   switch (_type) {
00815   case T_variable:
00816     decl = _u._variable->substitute_decl(subst, current_scope, global_scope);
00817     if (decl != rep->_u._variable) {
00818       if (decl->as_instance()) {
00819         // Replacing the variable reference with another variable reference.
00820         rep->_u._variable = decl->as_instance();
00821         any_changed = true;
00822       } else if (decl->as_expression()) {
00823         // Replacing the variable reference with an expression.
00824         delete rep;
00825         rep = decl->as_expression();
00826         any_changed = true;
00827       }
00828     }
00829     break;
00830 
00831   case T_typecast:
00832   case T_construct:
00833   case T_new:
00834     rep->_u._typecast._op1 =
00835       _u._typecast._op1->substitute_decl(subst, current_scope, global_scope)
00836       ->as_expression();
00837     any_changed = any_changed || (rep->_u._typecast._op1 != _u._typecast._op1);
00838     // fall through
00839 
00840   case T_default_construct:
00841   case T_default_new:
00842   case T_sizeof:
00843     rep->_u._typecast._to =
00844       _u._typecast._to->substitute_decl(subst, current_scope, global_scope)
00845       ->as_type();
00846     any_changed = any_changed || (rep->_u._typecast._to != _u._typecast._to);
00847     break;
00848 
00849   case T_trinary_operation:
00850     rep->_u._op._op3 =
00851       _u._op._op3->substitute_decl(subst, current_scope, global_scope)
00852       ->as_expression();
00853     any_changed = any_changed || (rep->_u._op._op3 != _u._op._op3);
00854     // fall through
00855 
00856   case T_binary_operation:
00857     rep->_u._op._op2 =
00858       _u._op._op2->substitute_decl(subst, current_scope, global_scope)
00859       ->as_expression();
00860     any_changed = any_changed || (rep->_u._op._op2 != _u._op._op2);
00861     // fall through
00862 
00863   case T_unary_operation:
00864     rep->_u._op._op1 =
00865       _u._op._op1->substitute_decl(subst, current_scope, global_scope)
00866       ->as_expression();
00867     any_changed = any_changed || (rep->_u._op._op1 != _u._op._op1);
00868     break;
00869 
00870   default:
00871     break;
00872   }
00873 
00874   if (!any_changed) {
00875     delete rep;
00876     rep = this;
00877   }
00878   subst.insert(SubstDecl::value_type(this, rep));
00879   return rep;
00880 }
00881 
00882 
00883 ////////////////////////////////////////////////////////////////////
00884 //     Function: CPPExpression::is_tbd
00885 //       Access: Public
00886 //  Description: Returns true if any type within the expression list is
00887 //               a CPPTBDType and thus isn't fully determined right
00888 //               now.
00889 ////////////////////////////////////////////////////////////////////
00890 bool CPPExpression::
00891 is_tbd() const {
00892   switch (_type) {
00893   case T_variable:
00894     return true;
00895 
00896   case T_typecast:
00897   case T_construct:
00898   case T_new:
00899   case T_default_construct:
00900   case T_default_new:
00901   case T_sizeof:
00902     return _u._typecast._to->is_tbd();
00903 
00904   case T_trinary_operation:
00905     if (_u._op._op3->is_tbd()) {
00906       return true;
00907     }
00908     // fall through
00909 
00910   case T_binary_operation:
00911     if (_u._op._op2->is_tbd()) {
00912       return true;
00913     }
00914     // fall through
00915 
00916   case T_unary_operation:
00917     if (_u._op._op1->is_tbd()) {
00918       return true;
00919     }
00920     return false;
00921 
00922   default:
00923     return false;
00924   }
00925 }
00926 
00927 ////////////////////////////////////////////////////////////////////
00928 //     Function: CPPExpression::output
00929 //       Access: Public
00930 //  Description:
00931 ////////////////////////////////////////////////////////////////////
00932 void CPPExpression::
00933 output(ostream &out, int indent_level, CPPScope *scope, bool) const {
00934   switch (_type) {
00935   case T_integer:
00936     out << _u._integer;
00937     break;
00938 
00939   case T_real:
00940     out << _u._real;
00941     break;
00942 
00943   case T_string:
00944     out << '"';
00945     {
00946       string::const_iterator si;
00947       for (si = _str.begin(); si != _str.end(); ++si) {
00948         switch (*si) {
00949         case '\n':
00950           out << "\\n";
00951           break;
00952 
00953         case '\t':
00954           out << "\\t";
00955           break;
00956 
00957         case '\r':
00958           out << "\\r";
00959           break;
00960 
00961         case '\a':
00962           out << "\\a";
00963           break;
00964 
00965         case '"':
00966           out << "\\\"";
00967           break;
00968 
00969         default:
00970           if (isprint(*si)) {
00971             out << *si;
00972           } else {
00973             out << '\\' << oct << setw(3) << setfill('0') << (int)(*si)
00974                 << dec << setw(0);
00975           }
00976         }
00977       }
00978     }
00979     out << '"';
00980     break;
00981 
00982   case T_variable:
00983     _u._variable->_ident->output(out, scope);
00984     break;
00985 
00986   case T_function:
00987     out << _u._fgroup->_name;
00988     break;
00989 
00990   case T_unknown_ident:
00991     _u._ident->output(out, scope);
00992     break;
00993 
00994   case T_typecast:
00995     out << "(";
00996     _u._typecast._to->output(out, indent_level, scope, false);
00997     out << ")(";
00998     _u._typecast._op1->output(out, indent_level, scope, false);
00999     out << ")";
01000     break;
01001 
01002   case T_construct:
01003     out << "(" << _u._typecast._to->get_typedef_name(scope)
01004         << "(";
01005     _u._typecast._op1->output(out, indent_level, scope, false);
01006     out << "))";
01007     break;
01008 
01009   case T_default_construct:
01010     out << "(" << _u._typecast._to->get_typedef_name(scope) << "())";
01011     break;
01012 
01013   case T_new:
01014     out << "(new ";
01015     _u._typecast._to->output(out, indent_level, scope, false);
01016     out << "(";
01017     _u._typecast._op1->output(out, indent_level, scope, false);
01018     out << "))";
01019     break;
01020 
01021   case T_default_new:
01022     out << "(new ";
01023     _u._typecast._to->output(out, indent_level, scope, false);
01024     out << "())";
01025     break;
01026 
01027   case T_sizeof:
01028     out << "sizeof(";
01029     _u._typecast._to->output(out, indent_level, scope, false);
01030     out << ")";
01031     break;
01032 
01033   case T_unary_operation:
01034     switch (_u._op._operator) {
01035     case UNARY_NOT:
01036       out << "(! ";
01037       _u._op._op1->output(out, indent_level, scope, false);
01038       out << ")";
01039       break;
01040 
01041     case UNARY_NEGATE:
01042       out << "(~ ";
01043       _u._op._op1->output(out, indent_level, scope, false);
01044       out << ")";
01045       break;
01046 
01047     case UNARY_MINUS:
01048       out << "(- ";
01049       _u._op._op1->output(out, indent_level, scope, false);
01050       out << ")";
01051       break;
01052 
01053     case UNARY_STAR:
01054       out << "(* ";
01055       _u._op._op1->output(out, indent_level, scope, false);
01056       out << ")";
01057       break;
01058 
01059     case UNARY_REF:
01060       out << "(& ";
01061       _u._op._op1->output(out, indent_level, scope, false);
01062       out << ")";
01063       break;
01064 
01065     case 'f': // Function evaluation, no parameters.
01066       out << "(";
01067       _u._op._op1->output(out, indent_level, scope, false);
01068       out << "())";
01069       break;
01070 
01071     default:
01072       out << "(" << (char)_u._op._operator << " ";
01073       _u._op._op1->output(out, indent_level, scope, false);
01074       out << ")";
01075       break;
01076     }
01077     break;
01078 
01079   case T_binary_operation:
01080     switch (_u._op._operator) {
01081     case OROR:
01082       out << "(";
01083       _u._op._op1->output(out, indent_level, scope, false);
01084       out << " || ";
01085       _u._op._op2->output(out, indent_level, scope, false);
01086       out << ")";
01087       break;
01088 
01089     case ANDAND:
01090       out << "(";
01091       _u._op._op1->output(out, indent_level, scope, false);
01092       out << " && ";
01093       _u._op._op2->output(out, indent_level, scope, false);
01094       out << ")";
01095       break;
01096 
01097     case EQCOMPARE:
01098       out << "(";
01099       _u._op._op1->output(out, indent_level, scope, false);
01100       out << " == ";
01101       _u._op._op2->output(out, indent_level, scope, false);
01102       out << ")";
01103       break;
01104 
01105     case NECOMPARE:
01106       out << "(";
01107       _u._op._op1->output(out, indent_level, scope, false);
01108       out << " != ";
01109       _u._op._op2->output(out, indent_level, scope, false);
01110       out << ")";
01111       break;
01112 
01113     case LECOMPARE:
01114       out << "(";
01115       _u._op._op1->output(out, indent_level, scope, false);
01116       out << " <= ";
01117       _u._op._op2->output(out, indent_level, scope, false);
01118       out << ")";
01119       break;
01120 
01121     case GECOMPARE:
01122       out << "(";
01123       _u._op._op1->output(out, indent_level, scope, false);
01124       out << " >= ";
01125       _u._op._op2->output(out, indent_level, scope, false);
01126       out << ")";
01127       break;
01128 
01129     case LSHIFT:
01130       out << "(";
01131       _u._op._op1->output(out, indent_level, scope, false);
01132       out << " << ";
01133       _u._op._op2->output(out, indent_level, scope, false);
01134       out << ")";
01135       break;
01136 
01137     case RSHIFT:
01138       out << "(";
01139       _u._op._op1->output(out, indent_level, scope, false);
01140       out << " >> ";
01141       _u._op._op2->output(out, indent_level, scope, false);
01142       out << ")";
01143       break;
01144 
01145     case '.':
01146       out << "(";
01147       _u._op._op1->output(out, indent_level, scope, false);
01148       out << ".";
01149       _u._op._op2->output(out, indent_level, scope, false);
01150       out << ")";
01151       break;
01152 
01153     case POINTSAT:
01154       out << "(";
01155       _u._op._op1->output(out, indent_level, scope, false);
01156       out << "->";
01157       _u._op._op2->output(out, indent_level, scope, false);
01158       out << ")";
01159       break;
01160 
01161     case '[': // Array element reference
01162       out << "(";
01163       _u._op._op1->output(out, indent_level, scope, false);
01164       out << "[";
01165       _u._op._op2->output(out, indent_level, scope, false);
01166       out << "])";
01167       break;
01168 
01169     case 'f': // Function evaluation
01170       out << "(";
01171       _u._op._op1->output(out, indent_level, scope, false);
01172       out << "(";
01173       _u._op._op2->output(out, indent_level, scope, false);
01174       out << "))";
01175       break;
01176 
01177     case ',': // Comma, no parens are used
01178       _u._op._op1->output(out, indent_level, scope, false);
01179       out << ", ";
01180       _u._op._op2->output(out, indent_level, scope, false);
01181       break;
01182 
01183     default:
01184       out << "(";
01185       _u._op._op1->output(out, indent_level, scope, false);
01186       out << " " << (char)_u._op._operator << " ";
01187       _u._op._op2->output(out, indent_level, scope, false);
01188       out << ")";
01189     }
01190     break;
01191 
01192   case T_trinary_operation:
01193     out << "(";
01194     _u._op._op1->output(out, indent_level, scope, false);
01195     out << " ? ";
01196     _u._op._op2->output(out, indent_level, scope, false);
01197     out << " : ";
01198     _u._op._op3->output(out, indent_level, scope, false);
01199     out << ")";
01200     break;
01201 
01202   default:
01203     out << "(** invalid operand type " << (int)_type << " **)";
01204   }
01205 }
01206 
01207 ////////////////////////////////////////////////////////////////////
01208 //     Function: CPPExpression::get_subtype
01209 //       Access: Public, Virtual
01210 //  Description:
01211 ////////////////////////////////////////////////////////////////////
01212 CPPDeclaration::SubType CPPExpression::
01213 get_subtype() const {
01214   return ST_expression;
01215 }
01216 
01217 ////////////////////////////////////////////////////////////////////
01218 //     Function: CPPExpression::as_expression
01219 //       Access: Public, Virtual
01220 //  Description:
01221 ////////////////////////////////////////////////////////////////////
01222 CPPExpression *CPPExpression::
01223 as_expression() {
01224   return this;
01225 }
01226 
01227 ////////////////////////////////////////////////////////////////////
01228 //     Function: CPPExpression::elevate_type
01229 //       Access: Public, Static
01230 //  Description: Returns the most general of the two given types.
01231 ////////////////////////////////////////////////////////////////////
01232 CPPType *CPPExpression::
01233 elevate_type(CPPType *t1, CPPType *t2) {
01234   CPPSimpleType *st1 = t1->as_simple_type();
01235   CPPSimpleType *st2 = t2->as_simple_type();
01236 
01237   if (st1 == NULL || st2 == NULL) {
01238     // Nothing we can do about this.  Who knows?
01239     return NULL;
01240   }
01241 
01242   if (st1->_type == st2->_type) {
01243     // They have the same type, so return the one with the largest
01244     // flag bits.
01245     if (st1->_flags & CPPSimpleType::F_longlong) {
01246       return st1;
01247     } else if (st2->_flags & CPPSimpleType::F_longlong) {
01248       return st2;
01249     } else if (st1->_flags & CPPSimpleType::F_long) {
01250       return st1;
01251     } else if (st2->_flags & CPPSimpleType::F_long) {
01252       return st2;
01253     } else if (st1->_flags & CPPSimpleType::F_short) {
01254       return st2;
01255     } else if (st2->_flags & CPPSimpleType::F_short) {
01256       return st1;
01257     }
01258     return st1;
01259   }
01260 
01261   // They have different types.
01262   if (st1->_type == CPPSimpleType::T_float ||
01263       st1->_type == CPPSimpleType::T_double) {
01264     return st1;
01265   } else if (st2->_type == CPPSimpleType::T_float ||
01266              st2->_type == CPPSimpleType::T_double) {
01267     return st2;
01268   } else if (st1->_type == CPPSimpleType::T_int) {
01269     return st1;
01270   } else if (st2->_type == CPPSimpleType::T_int) {
01271     return st2;
01272   } else if (st1->_type == CPPSimpleType::T_bool) {
01273     return st1;
01274   } else if (st2->_type == CPPSimpleType::T_bool) {
01275     return st2;
01276   }
01277   return st1;
01278 }
01279 
01280 ////////////////////////////////////////////////////////////////////
01281 //     Function: CPPExpression::is_equal
01282 //       Access: Protected, Virtual
01283 //  Description: Called by CPPDeclaration to determine whether this
01284 //               expr is equivalent to another expr.
01285 ////////////////////////////////////////////////////////////////////
01286 bool CPPExpression::
01287 is_equal(const CPPDeclaration *other) const {
01288   const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
01289   assert(ot != NULL);
01290 
01291   if (_type != ot->_type) {
01292     return false;
01293   }
01294 
01295   switch (_type) {
01296   case T_integer:
01297     return _u._integer == ot->_u._integer;
01298 
01299   case T_real:
01300     return _u._real == ot->_u._real;
01301 
01302   case T_string:
01303     return _str == ot->_str;
01304 
01305   case T_variable:
01306     return _u._variable == ot->_u._variable;
01307 
01308   case T_function:
01309     return _u._fgroup == ot->_u._fgroup;
01310 
01311   case T_unknown_ident:
01312     return *_u._ident == *ot->_u._ident;
01313 
01314   case T_typecast:
01315   case T_construct:
01316   case T_new:
01317     return _u._typecast._to == ot->_u._typecast._to &&
01318       *_u._typecast._op1 == *ot->_u._typecast._op1;
01319 
01320   case T_default_construct:
01321   case T_default_new:
01322   case T_sizeof:
01323     return _u._typecast._to == ot->_u._typecast._to;
01324 
01325   case T_unary_operation:
01326     return *_u._op._op1 == *ot->_u._op._op1;
01327 
01328   case T_binary_operation:
01329     return *_u._op._op1 == *ot->_u._op._op1 &&
01330       *_u._op._op2 == *ot->_u._op._op2;
01331 
01332   case T_trinary_operation:
01333     return *_u._op._op1 == *ot->_u._op._op1 &&
01334       *_u._op._op2 == *ot->_u._op._op2;
01335 
01336   default:
01337     cerr << "(** invalid operand type " << (int)_type << " **)";
01338   }
01339 
01340   return true;
01341 }
01342 
01343 ////////////////////////////////////////////////////////////////////
01344 //     Function: CPPExpression::is_less
01345 //       Access: Protected, Virtual
01346 //  Description: Called by CPPDeclaration to determine whether this
01347 //               expr should be ordered before another expr of the
01348 //               same type, in an arbitrary but fixed ordering.
01349 ////////////////////////////////////////////////////////////////////
01350 bool CPPExpression::
01351 is_less(const CPPDeclaration *other) const {
01352   const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
01353   assert(ot != NULL);
01354 
01355   if (_type != ot->_type) {
01356     return (int)_type < (int)ot->_type;
01357   }
01358 
01359   switch (_type) {
01360   case T_integer:
01361     return _u._integer < ot->_u._integer;
01362 
01363   case T_real:
01364     return _u._real < ot->_u._real;
01365 
01366   case T_string:
01367     return _str < ot->_str;
01368 
01369   case T_variable:
01370     return _u._variable < ot->_u._variable;
01371 
01372   case T_function:
01373     return *_u._fgroup < *ot->_u._fgroup;
01374 
01375   case T_unknown_ident:
01376     return *_u._ident < *ot->_u._ident;
01377 
01378   case T_typecast:
01379   case T_construct:
01380   case T_new:
01381     if (_u._typecast._to != ot->_u._typecast._to) {
01382       return _u._typecast._to < ot->_u._typecast._to;
01383     }
01384     return *_u._typecast._op1 < *ot->_u._typecast._op1;
01385 
01386   case T_default_construct:
01387   case T_default_new:
01388   case T_sizeof:
01389     return _u._typecast._to < ot->_u._typecast._to;
01390 
01391   case T_trinary_operation:
01392     if (*_u._op._op3 != *ot->_u._op._op3) {
01393       return *_u._op._op3 < *ot->_u._op._op3;
01394     }
01395     // Fall through
01396 
01397   case T_binary_operation:
01398     if (*_u._op._op2 != *ot->_u._op._op2) {
01399       return *_u._op._op2 < *ot->_u._op._op2;
01400     }
01401     // Fall through
01402 
01403   case T_unary_operation:
01404     return *_u._op._op1 < *ot->_u._op._op1;
01405 
01406   default:
01407     cerr << "(** invalid operand type " << (int)_type << " **)";
01408   }
01409 
01410   return false;
01411 }
01412 

Generated on Thu May 1 22:12:51 2003 for DTool by doxygen1.3