Changeset aba20d2 for src/SynTree


Ignore:
Timestamp:
Jun 17, 2019, 1:09:41 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
800bae1, 9d5089e
Parents:
8b34df0 (diff), b4d34fa (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/SynTree
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Constant.cc

    r8b34df0 raba20d2  
    2222#include "Type.h"    // for BasicType, Type, Type::Qualifiers, PointerType
    2323
    24 Constant::Constant( Type * type, std::string rep, unsigned long long val ) : type( type ), rep( rep ), val( val ) {}
    25 Constant::Constant( Type * type, std::string rep, double val ) : type( type ), rep( rep ), val( val ) {}
     24Constant::Constant( Type * type, std::string rep, std::optional<unsigned long long> ival ) : type( type ), rep( rep ), ival( ival ) {}
    2625
    27 Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), val( other.val ) {
     26Constant::Constant( const Constant &other ) : BaseSyntaxNode( other ), rep( other.rep ), ival( other.ival ) {
    2827        type = other.type->clone();
    2928}
     
    3534}
    3635
    37 Constant 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 
    4136Constant Constant::from_int( int i ) {
    4237        return Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), std::to_string( i ), (unsigned long long int)i );
     
    4540Constant Constant::from_ulong( unsigned long i ) {
    4641        return Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::to_string( i ), (unsigned long long int)i );
    47 }
    48 
    49 Constant Constant::from_double( double d ) {
    50         return Constant( new BasicType( Type::Qualifiers(), BasicType::Double ), std::to_string( d ), d );
    51 }
    52 
    53 Constant 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 );
    6142}
    6243
     
    7455unsigned long long Constant::get_ival() const {
    7556        assertf( strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
    76         return val.ival;
    77 }
    78 
    79 double Constant::get_dval() const {
    80         assertf( ! strict_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
    81         return val.dval;
     57        return ival.value();
    8258}
    8359
    8460void Constant::print( std::ostream &os, Indenter ) const {
    85         os << "(" << rep << " " << val.ival;
     61        os << "(" << rep << " " << (ival ? toString(ival.value()) : "") ;
    8662        if ( type ) {
    8763                os << ": ";
  • src/SynTree/Constant.h

    r8b34df0 raba20d2  
    1818#include <iosfwd>     // for ostream
    1919#include <string>     // for string
     20#include <optional>   // for optional
    2021
    2122#include "BaseSyntaxNode.h"
     
    2728class Constant : public BaseSyntaxNode {
    2829  public:
    29         Constant( Type * type, std::string rep, unsigned long long val );
    30         Constant( Type * type, std::string rep, double val );
     30        Constant( Type * type, std::string rep, std::optional<unsigned long long> i );
    3131        Constant( const Constant & other );
    3232        virtual ~Constant();
     
    3939        void set_value( std::string newValue ) { rep = newValue; }
    4040        unsigned long long get_ival() const;
    41         double get_dval() const;
    4241
    4342        /// generates a boolean constant of the given bool
    4443        static Constant from_bool( bool b );
    45         /// generates a char constant of the given char
    46         static Constant from_char( char c );
    4744        /// generates an integer constant of the given int
    4845        static Constant from_int( int i );
    4946        /// generates an integer constant of the given unsigned long int
    5047        static Constant from_ulong( unsigned long i );
    51         /// generates a floating point constant of the given double
    52         static Constant from_double( double d );
    53         /// generates an array of chars constant of the given string
    54         static Constant from_string( std::string const & s );
    5548
    5649        /// generates a null pointer value for the given type. void * if omitted.
     
    6356        Type * type;
    6457        std::string rep;
    65         union Val {
    66                 unsigned long long ival;
    67                 double dval;
    68                 Val( unsigned long long ival ) : ival( ival ) {}
    69                 Val( double dval ) : dval( dval ) {}
    70         } val;
     58        std::optional<unsigned long long> ival;
     59
     60        friend class ConverterOldToNew;
    7161};
    7262
Note: See TracChangeset for help on using the changeset viewer.