source: src/SynTree/Constant.cc @ 6c89ecc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 6c89ecc was 6c89ecc, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Added Constant::from_string constructor wrapper.

  • Property mode set to 100644
File size: 3.1 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
[6c89ecc]12// Last Modified On : Fri Spt 28 14:49:00 2018
13// Update Count     : 30
[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"
[6c89ecc]21#include "Expression.h" // for ConstantExpr
[ea6332d]22#include "Type.h"    // for BasicType, Type, Type::Qualifiers, PointerType
[51b7345]23
[d56e5bc]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 ) {}
[51b7345]26
[d56e5bc]27Constant::Constant( const Constant &other ) : rep( other.rep ), val( other.val ) {
[7f5566b]28        type = other.type->clone();
29}
30
31Constant::~Constant() { delete type; }
[51b7345]32
[2c37f34]33Constant Constant::from_bool( bool b ) {
34        return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b );
35}
36
[a2dbad10]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
[cb4c607]41Constant Constant::from_int( int i ) {
[d56e5bc]42        return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
[9d7b3ea]43}
44
[cb4c607]45Constant Constant::from_ulong( unsigned long i ) {
[d56e5bc]46        return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
[9d7b3ea]47}
48
[cb4c607]49Constant Constant::from_double( double d ) {
[d56e5bc]50        return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
[9d7b3ea]51}
52
[6c89ecc]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 );
61}
62
[6ea87486]63Constant Constant::null( Type * ptrtype ) {
64        if ( nullptr == ptrtype ) {
65                ptrtype = new PointerType(
66                        Type::Qualifiers(),
67                        new VoidType( Type::Qualifiers() )
68                        );
69        }
70
71        return Constant( ptrtype, "0", (unsigned long long int)0 );
72}
73
[62423350]74unsigned long long Constant::get_ival() const {
[e3e16bc]75        assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
[62423350]76        return val.ival;
77}
78
79double Constant::get_dval() const {
[e3e16bc]80        assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
[62423350]81        return val.dval;
82}
83
[bb9d8e8]84void Constant::print( std::ostream &os, Indenter ) const {
[d56e5bc]85        os << "(" << rep << " " << val.ival;
[0dd3a2f]86        if ( type ) {
[ea9b9d3]87                os << ": ";
[0dd3a2f]88                type->print( os );
89        } // if
[ea9b9d3]90  os << ")";
[51b7345]91}
92
[0dd3a2f]93// Local Variables: //
94// tab-width: 4 //
95// mode: c++ //
96// compile-command: "make install" //
97// End: //
Note: See TracBrowser for help on using the repository browser.