//
// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// Constant.cc --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Andrew Beach
// Last Modified On : Thr Jun 22 10:11:00 2017
// Update Count     : 28
//

#include <iostream>
#include <list>
#include <string>

#include "Constant.h"
#include "Type.h"

Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {}
Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {}

Constant::Constant( const Constant &other ) : rep( other.rep ), val( other.val ) {
	type = other.type->clone();
}

Constant::~Constant() { delete type; }

Constant Constant::from_bool( bool b ) {
	return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b );
}

Constant Constant::from_int( int i ) {
	return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
}

Constant Constant::from_ulong( unsigned long i ) {
	return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
}

Constant Constant::from_double( double d ) {
	return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
}

unsigned long long Constant::get_ival() const {
	assertf( safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
	return val.ival;
}

double Constant::get_dval() const {
	assertf( ! safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
	return val.dval;
}

void Constant::print( std::ostream &os ) const {
	os << "(" << rep << " " << val.ival;
	if ( type ) {
		os << ": ";
		type->print( os );
	} // if
  os << ")";
}

// Local Variables: //
// tab-width: 4 //
// mode: c++ //
// compile-command: "make install" //
// End: //
