[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 |
---|
[aac99da] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e15853c] | 12 | // Last Modified On : Wed Feb 13 18:11:22 2019 |
---|
| 13 | // Update Count : 32 |
---|
[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 | |
---|
[c36298d] | 24 | Constant::Constant( Type * type, std::string rep, std::optional<unsigned long long> ival ) : type( type ), rep( rep ), ival( ival ) {} |
---|
[51b7345] | 25 | |
---|
[c36298d] | 26 | Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), ival( other.ival ) { |
---|
[7f5566b] | 27 | type = other.type->clone(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | Constant::~Constant() { delete type; } |
---|
[51b7345] | 31 | |
---|
[2c37f34] | 32 | Constant Constant::from_bool( bool b ) { |
---|
[e15853c] | 33 | return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b ); |
---|
[2c37f34] | 34 | } |
---|
| 35 | |
---|
[cb4c607] | 36 | Constant Constant::from_int( int i ) { |
---|
[d56e5bc] | 37 | return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i ); |
---|
[9d7b3ea] | 38 | } |
---|
| 39 | |
---|
[cb4c607] | 40 | Constant Constant::from_ulong( unsigned long i ) { |
---|
[d56e5bc] | 41 | return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i ); |
---|
[9d7b3ea] | 42 | } |
---|
| 43 | |
---|
[b91bfde] | 44 | Constant Constant::from_string( const std::string & str ) { |
---|
| 45 | Type * charType = new BasicType( noQualifiers, BasicType::Char ); |
---|
| 46 | // Adjust the length of the string for the terminator. |
---|
| 47 | Expression * strSize = new ConstantExpr( Constant::from_ulong( str.size() + 1 ) ); |
---|
| 48 | Type * strType = new ArrayType( noQualifiers, charType, strSize, false, false ); |
---|
| 49 | const std::string strValue = "\"" + str + "\""; |
---|
| 50 | return Constant( strType, strValue, std::nullopt ); |
---|
| 51 | } |
---|
| 52 | |
---|
[6ea87486] | 53 | Constant Constant::null( Type * ptrtype ) { |
---|
| 54 | if ( nullptr == ptrtype ) { |
---|
| 55 | ptrtype = new PointerType( |
---|
| 56 | Type::Qualifiers(), |
---|
| 57 | new VoidType( Type::Qualifiers() ) |
---|
| 58 | ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | return Constant( ptrtype, "0", (unsigned long long int)0 ); |
---|
| 62 | } |
---|
| 63 | |
---|
[62423350] | 64 | unsigned long long Constant::get_ival() const { |
---|
[e3e16bc] | 65 | assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." ); |
---|
[c36298d] | 66 | return ival.value(); |
---|
[62423350] | 67 | } |
---|
| 68 | |
---|
[bb9d8e8] | 69 | void Constant::print( std::ostream &os, Indenter ) const { |
---|
[c36298d] | 70 | os << "(" << rep << " " << (ival ? toString(ival.value()) : "") ; |
---|
[0dd3a2f] | 71 | if ( type ) { |
---|
[ea9b9d3] | 72 | os << ": "; |
---|
[0dd3a2f] | 73 | type->print( os ); |
---|
| 74 | } // if |
---|
[ea9b9d3] | 75 | os << ")"; |
---|
[51b7345] | 76 | } |
---|
| 77 | |
---|
[0dd3a2f] | 78 | // Local Variables: // |
---|
| 79 | // tab-width: 4 // |
---|
| 80 | // mode: c++ // |
---|
| 81 | // compile-command: "make install" // |
---|
| 82 | // End: // |
---|