source: src/SynTree/Constant.cc @ d9f0ed4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since d9f0ed4 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
Line 
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 : Andrew Beach
12// Last Modified On : Fri Spt 28 14:49:00 2018
13// Update Count     : 30
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
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 ) {}
26
27Constant::Constant( const Constant &other ) : rep( other.rep ), val( other.val ) {
28        type = other.type->clone();
29}
30
31Constant::~Constant() { delete type; }
32
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
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
41Constant Constant::from_int( int i ) {
42        return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
43}
44
45Constant Constant::from_ulong( unsigned long i ) {
46        return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
47}
48
49Constant Constant::from_double( double d ) {
50        return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
51}
52
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
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
74unsigned long long Constant::get_ival() const {
75        assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
76        return val.ival;
77}
78
79double Constant::get_dval() const {
80        assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
81        return val.dval;
82}
83
84void Constant::print( std::ostream &os, Indenter ) const {
85        os << "(" << rep << " " << val.ival;
86        if ( type ) {
87                os << ": ";
88                type->print( os );
89        } // if
90  os << ")";
91}
92
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.