source: src/SynTree/Constant.cc @ 4137e31

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 4137e31 was bb9d8e8, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Fix Constant print to use Indenter

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[0dd3a2f]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//
[62423350]7// Constant.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[2c37f34]11// Last Modified By : Andrew Beach
[6ea87486]12// Last Modified On : Fri Jul 14 14:50:00 2017
13// Update Count     : 29
[0dd3a2f]14//
15
[e3e16bc]16#include <cassert>   // for strict_dynamic_cast, assertf
[ea6332d]17#include <iostream>  // for operator<<, ostream, basic_ostream
18#include <string>    // for to_string, string, char_traits, operator<<
[51b7345]19
20#include "Constant.h"
[ea6332d]21#include "Type.h"    // for BasicType, Type, Type::Qualifiers, PointerType
[51b7345]22
[d56e5bc]23Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {}
24Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {}
[51b7345]25
[d56e5bc]26Constant::Constant( const Constant &other ) : rep( other.rep ), val( other.val ) {
[7f5566b]27        type = other.type->clone();
28}
29
30Constant::~Constant() { delete type; }
[51b7345]31
[2c37f34]32Constant Constant::from_bool( bool b ) {
33        return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b );
34}
35
[a2dbad10]36Constant Constant::from_char( char c ) {
37        return Constant( new BasicType( Type::Qualifiers(), BasicType::Char ), std::to_string( c ), (unsigned long long int)c );
38}
39
[cb4c607]40Constant Constant::from_int( int i ) {
[d56e5bc]41        return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
[9d7b3ea]42}
43
[cb4c607]44Constant Constant::from_ulong( unsigned long i ) {
[d56e5bc]45        return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
[9d7b3ea]46}
47
[cb4c607]48Constant Constant::from_double( double d ) {
[d56e5bc]49        return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
[9d7b3ea]50}
51
[6ea87486]52Constant Constant::null( Type * ptrtype ) {
53        if ( nullptr == ptrtype ) {
54                ptrtype = new PointerType(
55                        Type::Qualifiers(),
56                        new VoidType( Type::Qualifiers() )
57                        );
58        }
59
60        return Constant( ptrtype, "0", (unsigned long long int)0 );
61}
62
[62423350]63unsigned long long Constant::get_ival() const {
[e3e16bc]64        assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
[62423350]65        return val.ival;
66}
67
68double Constant::get_dval() const {
[e3e16bc]69        assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
[62423350]70        return val.dval;
71}
72
[bb9d8e8]73void Constant::print( std::ostream &os, Indenter ) const {
[d56e5bc]74        os << "(" << rep << " " << val.ival;
[0dd3a2f]75        if ( type ) {
[ea9b9d3]76                os << ": ";
[0dd3a2f]77                type->print( os );
78        } // if
[ea9b9d3]79  os << ")";
[51b7345]80}
81
[0dd3a2f]82// Local Variables: //
83// tab-width: 4 //
84// mode: c++ //
85// compile-command: "make install" //
86// End: //
Note: See TracBrowser for help on using the repository browser.