Changeset aba20d2 for src/SynTree
- Timestamp:
- Jun 17, 2019, 1:09:41 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 800bae1, 9d5089e
- Parents:
- 8b34df0 (diff), b4d34fa (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/SynTree
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/Constant.cc
r8b34df0 raba20d2 22 22 #include "Type.h" // for BasicType, Type, Type::Qualifiers, PointerType 23 23 24 Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {} 25 Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {} 24 Constant::Constant( Type * type, std::string rep, std::optional<unsigned long long> ival ) : type( type ), rep( rep ), ival( ival ) {} 26 25 27 Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), val( other.val ) {26 Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), ival( other.ival ) { 28 27 type = other.type->clone(); 29 28 } … … 35 34 } 36 35 37 Constant Constant::from_char( char c ) {38 return Constant( new BasicType( Type::Qualifiers(), BasicType::Char ), std::to_string( c ), (unsigned long long int)c );39 }40 41 36 Constant Constant::from_int( int i ) { 42 37 return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i ); … … 45 40 Constant Constant::from_ulong( unsigned long i ) { 46 41 return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i ); 47 }48 49 Constant Constant::from_double( double d ) {50 return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );51 }52 53 Constant Constant::from_string( std::string const & str ) {54 return Constant(55 new ArrayType(56 noQualifiers,57 new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),58 new ConstantExpr( Constant::from_int( str.size() + 1 /* \0 */ )),59 false, false ),60 std::string("\"") + str + "\"", (unsigned long long int)0 );61 42 } 62 43 … … 74 55 unsigned long long Constant::get_ival() const { 75 56 assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." ); 76 return val.ival; 77 } 78 79 double Constant::get_dval() const { 80 assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." ); 81 return val.dval; 57 return ival.value(); 82 58 } 83 59 84 60 void Constant::print( std::ostream &os, Indenter ) const { 85 os << "(" << rep << " " << val.ival;61 os << "(" << rep << " " << (ival ? toString(ival.value()) : "") ; 86 62 if ( type ) { 87 63 os << ": "; -
src/SynTree/Constant.h
r8b34df0 raba20d2 18 18 #include <iosfwd> // for ostream 19 19 #include <string> // for string 20 #include <optional> // for optional 20 21 21 22 #include "BaseSyntaxNode.h" … … 27 28 class Constant : public BaseSyntaxNode { 28 29 public: 29 Constant( Type * type, std::string rep, unsigned long long val ); 30 Constant( Type * type, std::string rep, double val ); 30 Constant( Type * type, std::string rep, std::optional<unsigned long long> i ); 31 31 Constant( const Constant & other ); 32 32 virtual ~Constant(); … … 39 39 void set_value( std::string newValue ) { rep = newValue; } 40 40 unsigned long long get_ival() const; 41 double get_dval() const;42 41 43 42 /// generates a boolean constant of the given bool 44 43 static Constant from_bool( bool b ); 45 /// generates a char constant of the given char46 static Constant from_char( char c );47 44 /// generates an integer constant of the given int 48 45 static Constant from_int( int i ); 49 46 /// generates an integer constant of the given unsigned long int 50 47 static Constant from_ulong( unsigned long i ); 51 /// generates a floating point constant of the given double52 static Constant from_double( double d );53 /// generates an array of chars constant of the given string54 static Constant from_string( std::string const & s );55 48 56 49 /// generates a null pointer value for the given type. void * if omitted. … … 63 56 Type * type; 64 57 std::string rep; 65 union Val { 66 unsigned long long ival; 67 double dval; 68 Val( unsigned long long ival ) : ival( ival ) {} 69 Val( double dval ) : dval( dval ) {} 70 } val; 58 std::optional<unsigned long long> ival; 59 60 friend class ConverterOldToNew; 71 61 }; 72 62
Note:
See TracChangeset
for help on using the changeset viewer.