Ignore:
Timestamp:
Jun 17, 2019, 11:01:04 AM (5 years ago)
Author:
Michael Brooks <mlbrooks@…>
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:
b4d34fa
Parents:
6a1dfda
Message:

Fixed handling of "literals.cfa" string-detail test cases by simplifying constant analysis. Now a ConstantExpr? is a minial passthrough from parser to code generator, with special-case analysis only for integer values. Awareness of how to build a string-constant type is back in ExpressionNode?.cc; now, this knowlede is only needed there. AST conversion no longer specializes string-int-float constants; it just converts types and passes values through. Unused constant API features are removed, notably from-to-float and from-string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Constant.cc

    r6a1dfda rc36298d  
    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 & cEscapedVal, Type *charType ) {
    54         assert(cEscapedVal.length() >= 2);
    55         assert(cEscapedVal.front() == '"');
    56         assert(cEscapedVal.back() == '"');
    57         ArrayType * at = new ArrayType( noQualifiers, charType,
    58                                                                         new ConstantExpr( Constant::from_ulong( cEscapedVal.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
    59                                                                         false, false );
    60         return Constant( at, cEscapedVal, (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 << ": ";
Note: See TracChangeset for help on using the changeset viewer.