Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Constant.cc

    rc36298d r17129659  
    2222#include "Type.h"    // for BasicType, Type, Type::Qualifiers, PointerType
    2323
    24 Constant::Constant( Type * type, std::string rep, std::optional<unsigned long long> ival ) : type( type ), rep( rep ), ival( ival ) {}
     24Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {}
     25Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {}
    2526
    26 Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), ival( other.ival ) {
     27Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), val( other.val ) {
    2728        type = other.type->clone();
    2829}
     
    3435}
    3536
     37Constant 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
    3641Constant Constant::from_int( int i ) {
    3742        return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
     
    4045Constant Constant::from_ulong( unsigned long i ) {
    4146        return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
     47}
     48
     49Constant Constant::from_double( double d ) {
     50        return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
     51}
     52
     53Constant 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 );
    4261}
    4362
     
    5574unsigned long long Constant::get_ival() const {
    5675        assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
    57         return ival.value();
     76        return val.ival;
     77}
     78
     79double Constant::get_dval() const {
     80        assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
     81        return val.dval;
    5882}
    5983
    6084void Constant::print( std::ostream &os, Indenter ) const {
    61         os << "(" << rep << " " << (ival ? toString(ival.value()) : "") ;
     85        os << "(" << rep << " " << val.ival;
    6286        if ( type ) {
    6387                os << ": ";
Note: See TracChangeset for help on using the changeset viewer.