cppExpression.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Filename: cppExpression.h
  2. // Created by: drose (25Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #ifndef CPPEXPRESSION_H
  6. #define CPPEXPRESSION_H
  7. #include <dtoolbase.h>
  8. #include "cppDeclaration.h"
  9. class CPPIdentifier;
  10. class CPPType;
  11. class CPPPreprocessor;
  12. class CPPFunctionGroup;
  13. ///////////////////////////////////////////////////////////////////
  14. // Class : CPPExpression
  15. // Description :
  16. ////////////////////////////////////////////////////////////////////
  17. class CPPExpression : public CPPDeclaration {
  18. public:
  19. CPPExpression(int value);
  20. CPPExpression(const string &value);
  21. CPPExpression(double value);
  22. CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
  23. CPPScope *global_scope, CPPPreprocessor *error_sink = NULL);
  24. CPPExpression(int unary_operator, CPPExpression *op1);
  25. CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2);
  26. CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2, CPPExpression *op3);
  27. static CPPExpression typecast_op(CPPType *type, CPPExpression *op1);
  28. static CPPExpression construct_op(CPPType *type, CPPExpression *op1);
  29. static CPPExpression new_op(CPPType *type, CPPExpression *op1 = NULL);
  30. static CPPExpression sizeof_func(CPPType *type);
  31. ~CPPExpression();
  32. enum ResultType {
  33. RT_integer,
  34. RT_real,
  35. RT_pointer,
  36. RT_error
  37. };
  38. class Result {
  39. public:
  40. Result();
  41. Result(int value);
  42. Result(double value);
  43. Result(void *value);
  44. int as_integer() const;
  45. double as_real() const;
  46. void *as_pointer() const;
  47. void output(ostream &out) const;
  48. ResultType _type;
  49. union {
  50. int _integer;
  51. double _real;
  52. void *_pointer;
  53. } _u;
  54. };
  55. Result evaluate() const;
  56. CPPType *determine_type() const;
  57. bool is_tbd() const;
  58. virtual CPPDeclaration *substitute_decl(SubstDecl &subst,
  59. CPPScope *current_scope,
  60. CPPScope *global_scope);
  61. virtual void output(ostream &out, int indent_level, CPPScope *scope,
  62. bool complete) const;
  63. virtual SubType get_subtype() const;
  64. virtual CPPExpression *as_expression();
  65. enum Type {
  66. T_integer,
  67. T_real,
  68. T_string,
  69. T_variable,
  70. T_function,
  71. T_unknown_ident,
  72. T_typecast,
  73. T_construct,
  74. T_default_construct,
  75. T_new,
  76. T_default_new,
  77. T_sizeof,
  78. T_unary_operation,
  79. T_binary_operation,
  80. T_trinary_operation,
  81. };
  82. Type _type;
  83. string _str;
  84. union {
  85. int _integer;
  86. double _real;
  87. CPPInstance *_variable;
  88. CPPFunctionGroup *_fgroup;
  89. CPPIdentifier *_ident;
  90. class {
  91. public:
  92. CPPType *_to;
  93. CPPExpression *_op1;
  94. } _typecast;
  95. class {
  96. public:
  97. // One of the yytoken values: a character, or something
  98. // like EQCOMPARE.
  99. int _operator;
  100. CPPExpression *_op1;
  101. CPPExpression *_op2;
  102. CPPExpression *_op3;
  103. } _op;
  104. } _u;
  105. protected:
  106. static CPPType *elevate_type(CPPType *t1, CPPType *t2);
  107. virtual bool is_equal(const CPPDeclaration *other) const;
  108. virtual bool is_less(const CPPDeclaration *other) const;
  109. };
  110. inline ostream &
  111. operator << (ostream &out, const CPPExpression::Result &result) {
  112. result.output(out);
  113. return out;
  114. }
  115. #endif