Changeset 3c13c03 for src/SynTree


Ignore:
Timestamp:
Sep 17, 2016, 8:27:51 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
8c49c0e
Parents:
12bc63a
Message:

expand TupleExpr? and TupleIndexExpr?, add UniqueExpr?

Location:
src/SynTree
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Expression.cc

    r12bc63a r3c13c03  
    522522
    523523CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
     524        assert( type && initializer );
    524525        set_result( type->clone() );
    525526}
    526527
    527 CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
     528CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( other.type->clone() ), initializer( other.initializer->clone() ) {}
    528529
    529530CompoundLiteralExpr::~CompoundLiteralExpr() {
     
    534535void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
    535536        os << "Compound Literal Expression: " << std::endl;
    536         if ( type ) type->print( os, indent + 2 );
    537         if ( initializer ) initializer->print( os, indent + 2 );
     537        os << std::string( indent+2, ' ' );
     538        type->print( os, indent + 2 );
     539        os << std::string( indent+2, ' ' );
     540        initializer->print( os, indent + 2 );
    538541}
    539542
     
    549552
    550553RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
    551 RangeExpr::RangeExpr( const RangeExpr &other ) : low( other.low->clone() ), high( other.high->clone() ) {}
     554RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
    552555void RangeExpr::print( std::ostream &os, int indent ) const {
    553         os << std::string( indent, ' ' ) << "Range Expression: ";
     556        os << "Range Expression: ";
    554557        low->print( os, indent );
    555558        os << " ... ";
     
    566569        }
    567570}
    568 StmtExpr::StmtExpr( const StmtExpr &other ) : statements( other.statements->clone() ) {}
     571StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {}
    569572StmtExpr::~StmtExpr() {
    570573        delete statements;
    571574}
    572575void StmtExpr::print( std::ostream &os, int indent ) const {
    573         os << std::string( indent, ' ' ) << "Statement Expression: " << std::endl;
     576        os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
    574577        statements->print( os, indent+2 );
     578}
     579
     580
     581UniqueExpr::UniqueExpr( Expression *expr ) : expr( new Expression* ) {
     582        set_expr( expr );
     583        assert( expr );
     584        assert( expr->has_result() );
     585        set_result( expr->get_result()->clone() );
     586}
     587UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ) {
     588}
     589UniqueExpr::~UniqueExpr() {
     590        if ( expr.unique() ) {
     591                delete *expr;
     592        }
     593}
     594void UniqueExpr::print( std::ostream &os, int indent ) const {
     595        os << "Unique Expression: " << std::endl << std::string( indent+2, ' ' );
     596        get_expr()->print( os, indent+2 );
    575597}
    576598
  • src/SynTree/Expression.h

    r12bc63a r3c13c03  
    639639class TupleExpr : public Expression {
    640640  public:
    641         TupleExpr( Expression *_aname = nullptr );
     641        TupleExpr( const std::list< Expression * > & exprs = std::list< Expression * >(), Expression *_aname = nullptr );
    642642        TupleExpr( const TupleExpr &other );
    643643        virtual ~TupleExpr();
     
    733733};
    734734
     735class UniqueExpr : public Expression {
     736public:
     737        UniqueExpr( Expression * expr );
     738        UniqueExpr( const UniqueExpr & other );
     739        ~UniqueExpr();
     740
     741        Expression * get_expr() const { return *expr; }
     742        UniqueExpr * set_expr( Expression * newValue ) { *expr = newValue; return this; }
     743
     744        virtual UniqueExpr *clone() const { return new UniqueExpr( *this ); }
     745        virtual void accept( Visitor &v ) { v.visit( this ); }
     746        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
     747        virtual void print( std::ostream &os, int indent = 0 ) const;
     748private:
     749        std::shared_ptr< Expression * > expr;
     750};
     751
    735752std::ostream & operator<<( std::ostream & out, const Expression * expr );
    736753
  • src/SynTree/Mutator.cc

    r12bc63a r3c13c03  
    384384}
    385385
     386Expression *Mutator::mutate( UniqueExpr *uniqueExpr ) {
     387        uniqueExpr->set_result( maybeMutate( uniqueExpr->get_result(), *this ) );
     388        uniqueExpr->set_expr( maybeMutate( uniqueExpr->get_expr(), *this ) );
     389        return uniqueExpr;
     390}
     391
    386392Type *Mutator::mutate( VoidType *voidType ) {
    387393        mutateAll( voidType->get_forall(), *this );
  • src/SynTree/Mutator.h

    r12bc63a r3c13c03  
    8383        virtual Expression* mutate( TupleAssignExpr *assignExpr );
    8484        virtual Expression* mutate( StmtExpr * stmtExpr );
     85        virtual Expression* mutate( UniqueExpr * uniqueExpr );
    8586
    8687        virtual Type* mutate( VoidType *basicType );
  • src/SynTree/SynTree.h

    r12bc63a r3c13c03  
    8888class TupleAssignExpr;
    8989class StmtExpr;
     90class UniqueExpr;
    9091
    9192class Type;
  • src/SynTree/TupleExpr.cc

    r12bc63a r3c13c03  
    1818#include "Type.h"
    1919#include "Declaration.h"
     20#include "Tuples/Tuples.h"
    2021
    21 TupleExpr::TupleExpr( Expression *_aname ) : Expression( _aname ) {
     22TupleExpr::TupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
     23        if ( ! exprs.empty() ) {
     24                if ( std::all_of( exprs.begin(), exprs.end(), [](Expression * expr) { return expr->get_result(); } ) ) {
     25                        set_result( Tuples::makeTupleType( exprs ) );
     26                }
     27        }
    2228}
    2329
     
    3642}
    3743
    38 TupleIndexExpr::TupleIndexExpr( Expression * tuple, unsigned int index ) {
     44TupleIndexExpr::TupleIndexExpr( Expression * tuple, unsigned int index ) : tuple( tuple ), index( index )  {
    3945        TupleType * type = safe_dynamic_cast< TupleType * >( tuple->get_result() );
    40         assert( type->size() >= index );
    41         set_result( *std::next( type->get_types().begin(), index ) );
     46        assert( type->size() > index );
     47        set_result( (*std::next( type->get_types().begin(), index ))->clone() );
    4248}
    4349
     
    5157void TupleIndexExpr::print( std::ostream &os, int indent ) const {
    5258        os << "Tuple Index Expression, with tuple:" << std::endl;
     59        os << std::string( indent+2, ' ' );
    5360        tuple->print( os, indent+2 );
    5461        os << std::string( indent+2, ' ' ) << "with index: " << index << std::endl;
     
    7077void MemberTupleExpr::print( std::ostream &os, int indent ) const {
    7178        os << "Member Tuple Expression, with aggregate:" << std::endl;
     79        os << std::string( indent+2, ' ' );
    7280        aggregate->print( os, indent+2 );
    7381        os << std::string( indent+2, ' ' ) << "with member: " << std::endl;
     82        os << std::string( indent+2, ' ' );
    7483        member->print( os, indent+2 );
    7584        Expression::print( os, indent );
     
    97106
    98107void TupleAssignExpr::print( std::ostream &os, int indent ) const {
    99         os << std::string( indent, ' ' ) << "Tuple Assignment Expression, with temporaries:" << std::endl;
     108        os << "Tuple Assignment Expression, with temporaries:" << std::endl;
    100109        printAll( tempDecls, os, indent+4 );
    101110        os << std::string( indent+2, ' ' ) << "with assignments: " << std::endl;
  • src/SynTree/Visitor.cc

    r12bc63a r3c13c03  
    326326}
    327327
     328void Visitor::visit( UniqueExpr *uniqueExpr ) {
     329        maybeAccept( uniqueExpr->get_result(), *this );
     330        maybeAccept( uniqueExpr->get_expr(), *this );
     331}
     332
    328333void Visitor::visit( VoidType *voidType ) {
    329334        acceptAll( voidType->get_forall(), *this );
  • src/SynTree/Visitor.h

    r12bc63a r3c13c03  
    8383        virtual void visit( TupleAssignExpr *assignExpr );
    8484        virtual void visit( StmtExpr * stmtExpr );
     85        virtual void visit( UniqueExpr * uniqueExpr );
    8586
    8687        virtual void visit( VoidType *basicType );
Note: See TracChangeset for help on using the changeset viewer.