Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

datatype.cpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #include "datatype.hpp"
00011 #include "exception.hpp"
00012 
00013 #include <sstream>
00014 
00015 using namespace std;
00016 
00018 CkDataType::CkDataType()
00019 {
00020     commontInit();
00021 }
00022 
00024 /*
00025  * Initializes this type to a non array type of the basic type given
00026  * \param type The basic type of this type
00027  */
00028 CkDataType::CkDataType( Types type )
00029 {
00030     commontInit();
00031     Type = type;
00032 }
00033 
00035 /*
00036  * Initializes this type to a single dimension array of the basic type given
00037  * \param type The basic type of this type
00038  * \param dim the Size of the dimension
00039  */
00040 CkDataType::CkDataType( Types type, int dim )
00041 {
00042     commontInit();
00043     Type = type;
00044     Dimensions[0] = dim;
00045 }
00046 
00048 /*
00049  * Initializes this type to a double dimension array of the basic type given
00050  * \param type The basic type of this type
00051  * \param dim1 the Size of the first dimension
00052  * \param dim2 the Size of the second dimension
00053  */
00054 CkDataType::CkDataType( Types type, int dim1, int dim2 )
00055 {
00056     commontInit();
00057     Type = type;
00058     Dimensions[0] = dim1;
00059     Dimensions[1] = dim2;
00060 }
00061 
00063 CkDataType::CkDataType( const CkDataType &other )
00064 {
00065     commontInit();
00066     *this = other;
00067 }
00068 
00070 void CkDataType::commontInit()
00071 {
00072     Dimensions[0] = Dimensions[1] = 0;
00073     Type = Undefined;
00074 }
00075 
00077 
00081 void CkDataType::expandDimension( int dim )
00082 {
00083     Dimensions[1] = Dimensions[0];
00084     Dimensions[0] = dim;
00085 }
00086 
00087 
00089 unsigned int CkDataType::getNumDimensions() const
00090 {
00091     if( Dimensions[0] != 0 && Dimensions[1] != 0 )
00092         return 2;
00093     else if( Dimensions[0] != 0 )
00094         return 1;
00095     else
00096         return 0;
00097 }
00098 
00100 
00104 string CkDataType::dataTypeToString( Types type )
00105 {
00106     string result = "undefined";
00107     switch( type )
00108     {
00109         case Undefined: result = "undefined"; break;
00110         case PrimVoid: result = "void"; break;
00111         case PrimBool: result = "bool"; break;
00112         case PrimChar: result = "char"; break;
00113         case PrimInt: result = "int"; break;
00114         case Custom:  result = "custom"; break;
00115     }
00116     return result;
00117 }
00118 
00120 
00125 string CkDataType::toString() const
00126 {
00127     stringstream array;
00128     if( Dimensions[0] > 0)
00129         array << "["  << Dimensions[0] << "]";
00130     if( Dimensions[0] == -1)
00131         array << "[]";
00132 
00133     if( Dimensions[1] > 0)
00134         array << "["  << Dimensions[1] << "]";
00135     if( Dimensions[1] == -1)
00136         array << "[]";
00137 
00138     return dataTypeToString( Type ) + array.str();
00139 }
00140 
00142 
00145 string CkDataType::toJVMString() const
00146 {
00147     string retString;
00148     if( Dimensions[0] != 0 )
00149         retString = "[";
00150     if( Dimensions[1] != 0 )
00151         retString += "[";
00152 
00153     switch( Type )
00154     {
00155         case PrimVoid: retString = "V"; break; // = is intentional
00156         case PrimBool:
00157         case PrimInt: retString += "I"; break;
00158         case PrimChar: retString += "C"; break;
00159         default: break; // make gcc happy
00160     }
00161     return retString;
00162 }
00163 
00165 /*
00166  * \return Returns true if this datatype is a non dimensional variable with the same type. Otherwise false is returned.
00167  */
00168 bool CkDataType::operator==( Types type ) const
00169 {
00170     if( getNumDimensions() == 0 && Type == type ) return true;
00171     return false;
00172 }
00173 
00175 /*
00176  * \return Returns false if this datatype is a non dimensional variable with the same type. Otherwise true is returned.
00177  */
00178 bool CkDataType::operator!=( Types type ) const
00179 {
00180     if( getNumDimensions() == 0 && Type != type ) return true;
00181     return false;
00182 }
00183 
00185 /*
00186  * \return Returns true if the datatypes have the same basic type and the same dimensions. Or one 
00187  * of the basic types has an undefined size. 
00188  * 
00189  * Otherwise false is returned.
00190  */
00191 bool CkDataType::operator==( const CkDataType &other ) const
00192 {
00193     if( Type == other.Type && 
00194                         ((Dimensions[0] == other.Dimensions[0] && Dimensions[1] == other.Dimensions[1]) || 
00195                          (Dimensions[1] == -1 && other.Dimensions[1] != 0) || 
00196                          (other.Dimensions[1] == -1 && Dimensions[1] != 0)) ||
00197 
00198                          ((Dimensions[0] == -1 && other.Dimensions[0] != 0 || 
00199                            Dimensions[0] != 0 && other.Dimensions[0] == -1)&&
00200                           (Dimensions[1] == other.Dimensions[1] ||
00201                            Dimensions[1] == -1 && other.Dimensions[1] != 0 ||
00202                            Dimensions[1] != 0 && other.Dimensions[1] == -1)))
00203         return true;
00204     return false;
00205 }
00206 
00208 /*
00209  * \return Returns false if the datatypes have the same basic type and the same dimensions. Otherwise true is returned.
00210  */
00211 bool CkDataType::operator!=( const CkDataType &other ) const
00212 {
00213     return !(*this == other);
00214 }
00215 
00217 
00223 CkDataType CkDataType::reduceDimensions( unsigned int varDimensions ) const
00224 {
00225     if( varDimensions < 1 || varDimensions > getNumDimensions() )
00226         throw Exception( "invalid types for array subscript" );
00227 
00228     CkDataType newType = Type;
00229     if( getNumDimensions() == 2 )
00230     {
00231         // reduce from int[10][2] -> int[2]
00232         if( varDimensions == 1 ) newType.setDimensions( Dimensions[1], 0 );
00233         // if varDimensions == 2 .. there is nothing to do int[10][2] -> int
00234     }
00235     // if our dimension size == 1, then varDimensions also was 1.. nothing to do
00236     return newType;
00237 }

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3