C-Breeze
C Compiler Infrastructure

[ Project home page]

constant.h

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

Generated on February 1, 2006
Back to the C-Breeze home page