Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

constant.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 //  C-Breeze
00004 //  C Compiler Framework
00005 // 
00006 //  Copyright (c) 2000 University of Texas at Austin
00007 // 
00008 //  Samuel Z. Guyer
00009 //  Daniel A. Jimenez
00010 //  Calvin Lin
00011 // 
00012 //  Permission is hereby granted, free of charge, to any person
00013 //  obtaining a copy of this software and associated documentation
00014 //  files (the "Software"), to deal in the Software without
00015 //  restriction, including without limitation the rights to use, copy,
00016 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00017 //  of the Software, and to permit persons to whom the Software is
00018 //  furnished to do so, subject to the following conditions:
00019 //  
00020 //  The above copyright notice and this permission notice shall be
00021 //  included in all copies or substantial portions of the Software.
00022 //  
00023 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00024 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00025 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00026 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00027 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00028 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00029 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030 //  THE SOFTWARE.
00031 //
00032 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00033 //  Computer Science for inspiring parts of the C-Breeze design.
00034 //
00035 // ----------------------------------------------------------------------
00036 
00037 #ifndef CBZ_CONSTANT_H
00038 #define CBZ_CONSTANT_H
00039 
00040 
00041 
00042 class constant {
00043 
00044 private:
00045 
00046   union {
00047     signed char SChar;
00048     unsigned char UChar;
00049     signed short int SShort;
00050     unsigned short int UShort;
00051     signed int SInt;
00052     unsigned int UInt;
00053     signed long int SLong;
00054     unsigned long int ULong;
00055     float Float;
00056     double Double;
00057 
00058     char * Str;
00059     void * Ptr;
00060   } _v;
00061 
00062   basic_type _bt;
00063   bool _is_ptr;
00064   bool _is_str;
00065   bool _no_val;
00066 
00067 public:
00068 
00069   inline const basic_type & basic() const { return _bt; }
00070 
00071   constant(signed char SChar)
00072     : _bt(basic_type::SChar), _is_ptr(false), _is_str(false), _no_val(false)
00073   { _v.SChar = SChar; }
00074 
00075   inline signed char SChar() const { return _v.SChar; }
00076 
00077   constant(unsigned char UChar)
00078     : _bt(basic_type::UChar), _is_ptr(false), _is_str(false), _no_val(false)
00079   { _v.UChar = UChar; }
00080 
00081   inline unsigned char UChar() const { return _v.UChar; }
00082 
00083   constant(signed short int SShort)
00084     : _bt(basic_type::SShort), _is_ptr(false), _is_str(false), _no_val(false)
00085   { _v.SShort = SShort; }
00086 
00087   inline signed short int SShort() const { return _v.SShort; }
00088 
00089   constant(unsigned short int UShort)
00090     : _bt(basic_type::UShort), _is_ptr(false), _is_str(false), _no_val(false)
00091   { _v.UShort = UShort; }
00092 
00093   inline unsigned short int UShort() const { return _v.UShort; }
00094 
00095   constant(signed int SInt)
00096     : _bt(basic_type::SInt), _is_ptr(false), _is_str(false), _no_val(false)
00097   { _v.SInt = SInt; }
00098 
00099   inline signed int SInt() const { return _v.SInt; }
00100 
00101   constant(unsigned int UInt)
00102     : _bt(basic_type::UInt), _is_ptr(false), _is_str(false), _no_val(false)
00103   { _v.UInt = UInt; }
00104 
00105   inline unsigned int UInt() const { return _v.UInt; }
00106 
00107   constant(signed long int SLong)
00108     : _bt(basic_type::SLong), _is_ptr(false), _is_str(false), _no_val(false)
00109   { _v.SLong = SLong; }
00110 
00111   inline signed long int SLong() const { return _v.SLong; }
00112 
00113   constant(unsigned long int ULong)
00114     : _bt(basic_type::ULong), _is_ptr(false), _is_str(false), _no_val(false)
00115   { _v.ULong = ULong; }
00116 
00117   inline unsigned long int ULong() const { return _v.ULong; }
00118 
00119   constant(float Float)
00120     : _bt(basic_type::Float), _is_ptr(false), _is_str(false), _no_val(false)
00121   { _v.Float = Float; }
00122 
00123   inline float Float() const { return _v.Float; }
00124 
00125   constant(double Double)
00126     : _bt(basic_type::Double), _is_ptr(false), _is_str(false), _no_val(false)
00127   { _v.Double = Double; }
00128 
00129   inline double Double() const { return _v.Double; }
00130 
00131   constant(const char * Str)
00132     : _bt(), _is_ptr(false), _is_str(true), _no_val(false)
00133   { _v.Str = strdup(Str); }
00134 
00135   inline char * Str() const { return _v.Str; }
00136   inline bool is_str() const { return _is_str; }
00137 
00138   constant(void * Ptr)
00139     : _bt(), _is_ptr(true), _is_str(false), _no_val(false)
00140   { _v.Ptr = Ptr; }
00141 
00142   inline void * Ptr() const { return _v.Ptr; }
00143   inline bool is_ptr() const { return _is_ptr; }
00144 
00145   constant()
00146     : _bt(), _is_ptr(false), _is_str(false), _no_val(true)
00147   {}
00148 
00149   inline bool no_val() const { return _no_val; }
00150   inline void set_no_val() { _no_val = true; }
00151 
00152   // -- Copy constructor
00153 
00154   constant(const constant & other);
00155 
00156   // -- Destructor
00157 
00158   ~constant();
00159 
00160   // -- Assignment operators
00161 
00162   constant & operator=(const constant & rhs);
00163 
00164   // -- More general operations..
00165 
00166   unsigned long Integer() const;
00167   bool Boolean() const;
00168   bool is_zero() const;
00169 
00170   // -- Comparison
00171 
00172   bool is_equal_to(const constant & other) const;
00173 
00174   // -- Less-than operator, for sets and maps
00175 
00176   bool operator<(const constant & other) const;
00177 
00178   // -- Evaluate expressions
00179 
00180   static constant eval(const Operator * op,
00181                        const constant & operand1,
00182                        const constant & operand2);
00183 
00184   static constant eval(const Operator * op,
00185                        const constant & operand);
00186 
00187   static constant cast(const basic_type & new_bt, const constant & con);
00188 
00189   // -- Output as a string...
00190 
00191   string to_string() const;
00192 
00193 private:
00194 
00195   static void print_char(int value, ostrstream & ost);
00196 
00197 };
00198 
00199 
00200 
00201 #endif // CBZ_CONSTANT_H

Generated on Thu Jan 10 12:06:19 2002 for C-Breeze by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001