source: src/SynTree/Constant.cc@ 0698aa1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0698aa1 was 62423350, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType.

  • Property mode set to 100644
File size: 2.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
12// Last Modified On : Thr Jun 22 10:11:00 2017
13// Update Count : 28
[0dd3a2f]14//
15
[51b73452]16#include <iostream>
17#include <list>
[9d7b3ea]18#include <string>
[51b73452]19
20#include "Constant.h"
21#include "Type.h"
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 ) {}
[51b73452]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; }
[51b73452]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
[cb4c607]36Constant 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]40Constant 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
[cb4c607]44Constant Constant::from_double( double d ) {
[d56e5bc]45 return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
[9d7b3ea]46}
47
[62423350]48unsigned long long Constant::get_ival() const {
49 assertf( safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
50 return val.ival;
51}
52
53double Constant::get_dval() const {
54 assertf( ! safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
55 return val.dval;
56}
57
[51b73452]58void Constant::print( std::ostream &os ) const {
[d56e5bc]59 os << "(" << rep << " " << val.ival;
[0dd3a2f]60 if ( type ) {
[ea9b9d3]61 os << ": ";
[0dd3a2f]62 type->print( os );
63 } // if
[ea9b9d3]64 os << ")";
[51b73452]65}
66
[0dd3a2f]67// Local Variables: //
68// tab-width: 4 //
69// mode: c++ //
70// compile-command: "make install" //
71// End: //
Note: See TracBrowser for help on using the repository browser.