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 : Peter A. Buhr
|
---|
12 | // Last Modified On : Wed Feb 13 18:11:22 2019
|
---|
13 | // Update Count : 32
|
---|
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 "Expression.h" // for ConstantExpr
|
---|
22 | #include "Type.h" // for BasicType, Type, Type::Qualifiers, PointerType
|
---|
23 |
|
---|
24 | Constant::Constant( Type * type, std::string rep, std::optional<unsigned long long> ival ) : type( type ), rep( rep ), ival( ival ) {}
|
---|
25 |
|
---|
26 | Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), ival( other.ival ) {
|
---|
27 | type = other.type->clone();
|
---|
28 | }
|
---|
29 |
|
---|
30 | Constant::~Constant() { delete type; }
|
---|
31 |
|
---|
32 | Constant Constant::from_bool( bool b ) {
|
---|
33 | return Constant( new BasicType( Type::Qualifiers(), BasicType::Bool ), b ? "1" : "0" , (unsigned long long int)b );
|
---|
34 | }
|
---|
35 |
|
---|
36 | Constant Constant::from_int( int i ) {
|
---|
37 | return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
|
---|
38 | }
|
---|
39 |
|
---|
40 | Constant Constant::from_ulong( unsigned long i ) {
|
---|
41 | return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
|
---|
42 | }
|
---|
43 |
|
---|
44 | Constant Constant::null( Type * ptrtype ) {
|
---|
45 | if ( nullptr == ptrtype ) {
|
---|
46 | ptrtype = new PointerType(
|
---|
47 | Type::Qualifiers(),
|
---|
48 | new VoidType( Type::Qualifiers() )
|
---|
49 | );
|
---|
50 | }
|
---|
51 |
|
---|
52 | return Constant( ptrtype, "0", (unsigned long long int)0 );
|
---|
53 | }
|
---|
54 |
|
---|
55 | unsigned long long Constant::get_ival() const {
|
---|
56 | assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
|
---|
57 | return ival.value();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Constant::print( std::ostream &os, Indenter ) const {
|
---|
61 | os << "(" << rep << " " << (ival ? toString(ival.value()) : "") ;
|
---|
62 | if ( type ) {
|
---|
63 | os << ": ";
|
---|
64 | type->print( os );
|
---|
65 | } // if
|
---|
66 | os << ")";
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Local Variables: //
|
---|
70 | // tab-width: 4 //
|
---|
71 | // mode: c++ //
|
---|
72 | // compile-command: "make install" //
|
---|
73 | // End: //
|
---|