Changeset c36298d for src/AST/Expr.hpp


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/AST/Expr.hpp

    r6a1dfda rc36298d  
    2222#include <utility>        // for move
    2323#include <vector>
     24#include <optional>
    2425
    2526#include "Fwd.hpp"        // for UniqueId
     
    3233
    3334class ConverterOldToNew;
     35class ConverterNewToOld;
    3436
    3537namespace ast {
     
    346348};
    347349
    348 /// A compile-time constant
     350/// A compile-time constant.
     351/// Mostly carries C-source text from parse to code-gen, without interpretation.  E.g. strings keep their outer quotes and never have backslashes interpreted.
     352/// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
    349353class ConstantExpr final : public Expr {
    350         union Val {
    351                 unsigned long long ival;
    352                 double dval;
    353 
    354                 Val( unsigned long long i ) : ival( i ) {}
    355                 Val( double d ) : dval( d ) {}
    356         } val;
    357 public:
     354public:
     355        // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
    358356        std::string rep;
    359         enum Kind { Integer, FloatingPoint, String } kind;
    360357
    361358        ConstantExpr(
    362                 const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v,
    363                 Kind k = Integer )
    364         : Expr( loc, ty ), val( v ), rep( r ), kind( k ) {}
    365         ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v )
    366         : Expr( loc, ty ), val( v ), rep( r ), kind( FloatingPoint ) {}
    367 
    368         /// Gets the value of this constant as an integer
     359                const CodeLocation & loc, const Type * ty, const std::string & r,
     360                        std::optional<unsigned long long> i )
     361        : Expr( loc, ty ), rep( r ), ival( i ) {}
     362
     363        /// Gets the integer value of this constant, if one is appropriate to its type.
     364        /// Throws a SemanticError if the type is not appropriate for value-as-integer.
     365        /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
    369366        long long int intValue() const;
    370         /// Gets the value of this constant as floating point
    371         double floatValue() const;
    372367
    373368        /// generates a boolean constant of the given bool
    374369        static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
    375         /// generates a char constant of the given char
    376         static ConstantExpr * from_char( const CodeLocation & loc, char c );
    377370        /// generates an integer constant of the given int
    378371        static ConstantExpr * from_int( const CodeLocation & loc, int i );
    379372        /// generates an integer constant of the given unsigned long int
    380373        static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
    381         /// generates a floating point constant of the given double
    382         static ConstantExpr * from_double( const CodeLocation & loc, double d );
    383374        /// generates a null pointer value for the given type. void * if omitted.
    384375        static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
     
    388379        ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
    389380        MUTATE_FRIEND
     381
     382        std::optional<unsigned long long> ival;
     383
     384        // Intended only for legacy support of roundtripping the old AST.
     385        // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
     386        // In the old AST it's constExpr->constant.type
     387        ptr<Type> underlyer;
     388        friend class ::ConverterOldToNew;
     389        friend class ::ConverterNewToOld;
    390390};
    391391
Note: See TracChangeset for help on using the changeset viewer.