| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Constant.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Fri Jul 14 14:50:00 2017
 | 
|---|
| 13 | // Update Count     : 29
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <cassert>   // for strict_dynamic_cast, assertf
 | 
|---|
| 17 | #include <iostream>  // for operator<<, ostream, basic_ostream
 | 
|---|
| 18 | #include <string>    // for to_string, string, char_traits, operator<<
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Constant.h"
 | 
|---|
| 21 | #include "Type.h"    // for BasicType, Type, Type::Qualifiers, PointerType
 | 
|---|
| 22 | 
 | 
|---|
| 23 | Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {}
 | 
|---|
| 24 | Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {}
 | 
|---|
| 25 | 
 | 
|---|
| 26 | Constant::Constant( const Constant &other ) : rep( other.rep ), val( other.val ) {
 | 
|---|
| 27 |         type = other.type->clone();
 | 
|---|
| 28 | }
 | 
|---|
| 29 | 
 | 
|---|
| 30 | Constant Constant::from_bool( bool b ) {
 | 
|---|
| 31 |         return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b );
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | Constant Constant::from_char( char c ) {
 | 
|---|
| 35 |         return Constant( new BasicType( Type::Qualifiers(), BasicType::Char ), std::to_string( c ), (unsigned long long int)c );
 | 
|---|
| 36 | }
 | 
|---|
| 37 | 
 | 
|---|
| 38 | Constant Constant::from_int( int i ) {
 | 
|---|
| 39 |         return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | Constant Constant::from_ulong( unsigned long i ) {
 | 
|---|
| 43 |         return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | Constant Constant::from_double( double d ) {
 | 
|---|
| 47 |         return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | Constant Constant::null( Type * ptrtype ) {
 | 
|---|
| 51 |         if ( nullptr == ptrtype ) {
 | 
|---|
| 52 |                 ptrtype = new PointerType(
 | 
|---|
| 53 |                         Type::Qualifiers(),
 | 
|---|
| 54 |                         new VoidType( Type::Qualifiers() )
 | 
|---|
| 55 |                         );
 | 
|---|
| 56 |         }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         return Constant( ptrtype, "0", (unsigned long long int)0 );
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | unsigned long long Constant::get_ival() const {
 | 
|---|
| 62 |         assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
 | 
|---|
| 63 |         return val.ival;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | double Constant::get_dval() const {
 | 
|---|
| 67 |         assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
 | 
|---|
| 68 |         return val.dval;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void Constant::print( std::ostream &os, Indenter ) const {
 | 
|---|
| 72 |         os << "(" << rep << " " << val.ival;
 | 
|---|
| 73 |         if ( type ) {
 | 
|---|
| 74 |                 os << ": ";
 | 
|---|
| 75 |                 type->print( os );
 | 
|---|
| 76 |         } // if
 | 
|---|
| 77 |   os << ")";
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | // Local Variables: //
 | 
|---|
| 81 | // tab-width: 4 //
 | 
|---|
| 82 | // mode: c++ //
 | 
|---|
| 83 | // compile-command: "make install" //
 | 
|---|
| 84 | // End: //
 | 
|---|