Changeset e4d829b for src/SynTree


Ignore:
Timestamp:
Jun 20, 2017, 1:19:53 PM (7 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:
579263a
Parents:
c6d2e93
git-author:
Rob Schluntz <rschlunt@…> (06/20/17 13:16:13)
git-committer:
Rob Schluntz <rschlunt@…> (06/20/17 13:19:53)
Message:

major effort on designations, works in many cases

Location:
src/SynTree
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Expression.cc

    rc6d2e93 re4d829b  
    9292
    9393        Declaration *decl = get_var();
    94         // if ( decl != 0) decl->print(os, indent + 2);
    9594        if ( decl != 0) decl->printShort(os, indent + 2);
    9695        os << std::endl;
     
    670669}
    671670
     671InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
     672InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
     673InitAlternative::~InitAlternative() {
     674        delete type;
     675        delete designation;
     676}
     677
     678UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
     679UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
     680UntypedInitExpr::~UntypedInitExpr() {
     681        delete expr;
     682}
     683
     684void UntypedInitExpr::print( std::ostream & os, int indent ) const {
     685        os << "Untyped Init Expression" << std::endl << std::string( indent+2, ' ' );
     686        expr->print( os, indent+2 );
     687        if ( ! initAlts.empty() ) {
     688                for ( const InitAlternative & alt : initAlts ) {
     689                        os << std::string( indent+2, ' ' ) <<  "InitAlternative: ";
     690                        alt.type->print( os, indent+2 );
     691                        alt.designation->print( os, indent+2 );
     692                }
     693        }
     694}
     695
     696InitExpr::InitExpr( Expression * expr, Type * type, Designation * designation ) : expr( expr ), designation( designation ) {
     697        set_result( type );
     698}
     699InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
     700InitExpr::~InitExpr() {
     701        delete expr;
     702        delete designation;
     703}
     704
     705void InitExpr::print( std::ostream & os, int indent ) const {
     706        os << "Init Expression" << std::endl << std::string( indent+2, ' ' );
     707        expr->print( os, indent+2 );
     708        os << std::string( indent+2, ' ' ) << "with designation: ";
     709        designation->print( os, indent+2 );
     710}
     711
     712
    672713std::ostream & operator<<( std::ostream & out, const Expression * expr ) {
    673714        if ( expr ) {
  • src/SynTree/Expression.h

    rc6d2e93 re4d829b  
    781781};
    782782
     783struct InitAlternative {
     784public:
     785        Type * type = nullptr;
     786        Designation * designation = nullptr;
     787        InitAlternative( Type * type, Designation * designation );
     788        InitAlternative( const InitAlternative & other );
     789        InitAlternative & operator=( const Initializer & other ) = delete; // at the moment this isn't used, and I don't want to implement it
     790        ~InitAlternative();
     791};
     792
     793class UntypedInitExpr : public Expression {
     794public:
     795        UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts );
     796        UntypedInitExpr( const UntypedInitExpr & other );
     797        ~UntypedInitExpr();
     798
     799        Expression * get_expr() const { return expr; }
     800        UntypedInitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
     801
     802        std::list<InitAlternative> & get_initAlts() { return initAlts; }
     803
     804        virtual UntypedInitExpr * clone() const { return new UntypedInitExpr( * this ); }
     805        virtual void accept( Visitor & v ) { v.visit( this ); }
     806        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     807        virtual void print( std::ostream & os, int indent = 0 ) const;
     808private:
     809        Expression * expr;
     810        std::list<InitAlternative> initAlts;
     811};
     812
     813class InitExpr : public Expression {
     814public:
     815        InitExpr( Expression * expr, Type * type, Designation * designation );
     816        InitExpr( const InitExpr & other );
     817        ~InitExpr();
     818
     819        Expression * get_expr() const { return expr; }
     820        InitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
     821
     822        Designation * get_designation() const { return designation; }
     823        InitExpr * set_designation( Designation * newValue ) { designation = newValue; return this; }
     824
     825        virtual InitExpr * clone() const { return new InitExpr( * this ); }
     826        virtual void accept( Visitor & v ) { v.visit( this ); }
     827        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     828        virtual void print( std::ostream & os, int indent = 0 ) const;
     829private:
     830        Expression * expr;
     831        Designation * designation;
     832};
     833
     834
    783835std::ostream & operator<<( std::ostream & out, const Expression * expr );
    784836
  • src/SynTree/Initializer.cc

    rc6d2e93 re4d829b  
    1919#include "Common/utility.h"
    2020
     21Designation::Designation( const std::list< Expression * > & designators ) : designators( designators ) {}
     22Designation::Designation( const Designation & other ) : BaseSyntaxNode( other ) {
     23        // std::cerr << "cloning designation" << std::endl;
     24        cloneAll( other.designators, designators );
     25        // std::cerr << "finished cloning designation" << std::endl;
     26}
     27
     28Designation::~Designation() {
     29        // std::cerr << "destroying designation" << std::endl;
     30        deleteAll( designators );
     31        // std::cerr << "finished destroying designation" << std::endl;
     32}
     33
     34void Designation::print( std::ostream &os, int indent ) const {
     35        if ( ! designators.empty() ) {
     36                os << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
     37                for ( std::list < Expression * >::const_iterator i = designators.begin(); i != designators.end(); i++ ) {
     38                        os << std::string(indent + 4, ' ' );
     39                        ( *i )->print(os, indent + 4 );
     40                }
     41                os << std::endl;
     42        } // if
     43}
     44
     45
    2146Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {}
    2247Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) {
    2348}
    24 
    25 
    2649Initializer::~Initializer() {}
    2750
    28 std::string Initializer::designator_name( Expression *des ) {
    29         if ( NameExpr *n = dynamic_cast<NameExpr *>(des) )
    30                 return n->get_name();
    31         else
    32                 throw 0;
    33 }
    34 
    35 void Initializer::print( std::ostream &os, int indent ) {}
    36 
    37 SingleInit::SingleInit( Expression *v, const std::list< Expression *> &_designators, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ), designators( _designators ) {
     51SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) {
    3852}
    3953
    4054SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
    41         cloneAll(other.designators, designators );
    4255}
    4356
    4457SingleInit::~SingleInit() {
    4558        delete value;
    46         deleteAll(designators);
    4759}
    4860
    49 void SingleInit::print( std::ostream &os, int indent ) {
    50         os << std::endl << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
     61void SingleInit::print( std::ostream &os, int indent ) const {
     62        os << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
    5163        os << std::string(indent+4, ' ' );
    5264        value->print( os, indent+4 );
    53 
    54         if ( ! designators.empty() ) {
    55                 os << std::endl << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
    56                 for ( std::list < Expression * >::iterator i = designators.begin(); i != designators.end(); i++ ) {
    57                         os << std::string(indent + 4, ' ' );
    58                         ( *i )->print(os, indent + 4 );
    59                 }
    60         } // if
    6165}
    6266
    63 ListInit::ListInit( const std::list<Initializer*> &_initializers, const std::list<Expression *> &_designators, bool maybeConstructed )
    64         : Initializer( maybeConstructed ), initializers( _initializers ), designators( _designators ) {
     67
     68ListInit::ListInit( const std::list<Initializer*> &initializers, const std::list<Designation *> &designations, bool maybeConstructed )
     69        : Initializer( maybeConstructed ), initializers( initializers ), designations( designations ) {
    6570}
    6671
    6772ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
    6873        cloneAll( other.initializers, initializers );
    69         cloneAll( other.designators, designators );
     74        cloneAll( other.designations, designations );
    7075}
    71 
    7276
    7377ListInit::~ListInit() {
    7478        deleteAll( initializers );
    75         deleteAll( designators );
     79        deleteAll( designations );
    7680}
    7781
    78 void ListInit::print( std::ostream &os, int indent ) {
    79         os << std::endl << std::string(indent, ' ') << "Compound initializer:  ";
    80         if ( ! designators.empty() ) {
    81                 os << std::string(indent + 2, ' ' ) << "designated by: [";
    82                 for ( std::list < Expression * >::iterator i = designators.begin();
    83                           i != designators.end(); i++ ) {
    84                         ( *i )->print(os, indent + 4 );
    85                 } // for
     82void ListInit::print( std::ostream &os, int indent ) const {
     83        os << std::string(indent, ' ') << "Compound initializer:  " << std::endl;
     84        for ( Designation * d : designations ) {
     85                d->print( os, indent + 2 );
     86        }
    8687
    87                 os << std::string(indent + 2, ' ' ) << "]";
    88         } // if
    89 
    90         for ( std::list<Initializer *>::iterator i = initializers.begin(); i != initializers.end(); i++ )
    91                 (*i)->print( os, indent + 2 );
     88        for ( const Initializer * init : initializers ) {
     89                init->print( os, indent + 2 );
     90                os << std::endl;
     91        }
    9292}
    9393
     
    103103}
    104104
    105 void ConstructorInit::print( std::ostream &os, int indent ) {
     105void ConstructorInit::print( std::ostream &os, int indent ) const {
    106106        os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl;
    107107        if ( ctor ) {
     
    124124}
    125125
    126 std::ostream & operator<<( std::ostream & out, Initializer * init ) {
    127         init->print( out );
     126std::ostream & operator<<( std::ostream & out, const Initializer * init ) {
     127        if ( init ) {
     128                init->print( out );
     129        } else {
     130                out << "nullptr";
     131        }
     132        return out;
     133}
     134
     135std::ostream & operator<<( std::ostream & out, const Designation * des ) {
     136        if ( des ) {
     137                des->print( out );
     138        } else {
     139                out << "nullptr";
     140        }
    128141        return out;
    129142}
  • src/SynTree/Initializer.h

    rc6d2e93 re4d829b  
    2525#include "Visitor.h"
    2626
    27 const std::list<Expression*> noDesignators;
     27// Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized.
     28class Designation : public BaseSyntaxNode {
     29public:
     30        Designation( const std::list< Expression * > & designators );
     31        Designation( const Designation & other );
     32        virtual ~Designation();
     33
     34        std::list< Expression * > & get_designators() { return designators; }
     35
     36        virtual Designation * clone() const { return new Designation( *this ); };
     37        virtual void accept( Visitor &v ) { v.visit( this ); }
     38        virtual Designation * acceptMutator( Mutator &m ) { return m.mutate( this ); }
     39        virtual void print( std::ostream &os, int indent = 0 ) const;
     40private:
     41        std::list< Expression * > designators;
     42};
     43
     44const std::list<Designation *> noDesignators;
    2845
    2946// Initializer: base class for object initializers (provide default values)
    3047class Initializer : public BaseSyntaxNode {
    3148  public:
    32         //      Initializer( std::string _name = std::string(""), int _pos = 0 );
    3349        Initializer( bool maybeConstructed );
    3450        Initializer( const Initializer & other );
    3551        virtual ~Initializer();
    36 
    37         static std::string designator_name( Expression *designator );
    38 
    39         //      void set_name( std::string newValue ) { name = newValue; }
    40         //      std::string get_name() const { return name; }
    41 
    42         //      void set_pos( int newValue ) { pos = newValue; }
    43         //      int get_pos() const { return pos; }
    44         virtual void set_designators( std::list<Expression *> & ) { assert(false); }
    45         virtual std::list<Expression *> &get_designators() {
    46                 assert(false);
    47                 std::list<Expression *> *ret = 0; return *ret;  // never reached
    48         }
    4952
    5053        bool get_maybeConstructed() { return maybeConstructed; }
     
    5356        virtual void accept( Visitor &v ) = 0;
    5457        virtual Initializer *acceptMutator( Mutator &m ) = 0;
    55         virtual void print( std::ostream &os, int indent = 0 );
     58        virtual void print( std::ostream &os, int indent = 0 ) const = 0;
    5659  private:
    57         //      std::string name;
    58         //      int pos;
    5960        bool maybeConstructed;
    6061};
     
    6364class SingleInit : public Initializer {
    6465  public:
    65         SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
     66        SingleInit( Expression *value, bool maybeConstructed = false );
    6667        SingleInit( const SingleInit &other );
    6768        virtual ~SingleInit();
     
    7071        void set_value( Expression *newValue ) { value = newValue; }
    7172
    72         std::list<Expression *> &get_designators() { return designators; }
    73         void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
    74 
    7573        virtual SingleInit *clone() const { return new SingleInit( *this); }
    7674        virtual void accept( Visitor &v ) { v.visit( this ); }
    7775        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    78         virtual void print( std::ostream &os, int indent = 0 );
     76        virtual void print( std::ostream &os, int indent = 0 ) const;
    7977  private:
    8078        //Constant *value;
    8179        Expression *value;      // has to be a compile-time constant
    82         std::list< Expression * > designators;
    8380};
    8481
     
    8885  public:
    8986        ListInit( const std::list<Initializer*> &initializers,
    90                           const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
     87                          const std::list<Designation *> &designators = {}, bool maybeConstructed = false );
    9188        ListInit( const ListInit & other );
    9289        virtual ~ListInit();
    9390
    94         void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
    95         std::list<Expression *> &get_designators() { return designators; }
    96         void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; }
    97         std::list<Initializer*> &get_initializers() { return initializers; }
     91        std::list<Designation *> & get_designations() { return designations; }
     92        std::list<Initializer *> & get_initializers() { return initializers; }
    9893
    9994        typedef std::list<Initializer*>::iterator iterator;
     95        typedef std::list<Initializer*>::const_iterator const_iterator;
    10096        iterator begin() { return initializers.begin(); }
    10197        iterator end() { return initializers.end(); }
     98        const_iterator begin() const { return initializers.begin(); }
     99        const_iterator end() const { return initializers.end(); }
    102100
    103101        virtual ListInit *clone() const { return new ListInit( *this ); }
    104102        virtual void accept( Visitor &v ) { v.visit( this ); }
    105103        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    106         virtual void print( std::ostream &os, int indent = 0 );
     104        virtual void print( std::ostream &os, int indent = 0 ) const;
    107105  private:
    108         std::list<Initializer*> initializers;  // order *is* important
    109         std::list<Expression *> designators;
     106        std::list<Initializer *> initializers;  // order *is* important
     107        std::list<Designation *> designations;  // order/length is consistent with initializers
    110108};
    111109
     
    130128        virtual void accept( Visitor &v ) { v.visit( this ); }
    131129        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    132         virtual void print( std::ostream &os, int indent = 0 );
     130        virtual void print( std::ostream &os, int indent = 0 ) const;
    133131
    134132  private:
     
    140138};
    141139
    142 std::ostream & operator<<( std::ostream & out, Initializer * init );
     140std::ostream & operator<<( std::ostream & out, const Initializer * init );
     141std::ostream & operator<<( std::ostream & out, const Designation * des );
    143142
    144143#endif // INITIALIZER_H
  • src/SynTree/Mutator.cc

    rc6d2e93 re4d829b  
    439439}
    440440
     441Expression *Mutator::mutate( UntypedInitExpr * initExpr ) {
     442        initExpr->set_env( maybeMutate( initExpr->get_env(), *this ) );
     443        initExpr->set_result( maybeMutate( initExpr->get_result(), *this ) );
     444        initExpr->set_expr( maybeMutate( initExpr->get_expr(), *this ) );
     445        // not currently mutating initAlts, but this doesn't matter since this node is only used in the resolver.
     446        return initExpr;
     447}
     448
     449Expression *Mutator::mutate( InitExpr * initExpr ) {
     450        initExpr->set_env( maybeMutate( initExpr->get_env(), *this ) );
     451        initExpr->set_result( maybeMutate( initExpr->get_result(), *this ) );
     452        initExpr->set_expr( maybeMutate( initExpr->get_expr(), *this ) );
     453        initExpr->set_designation( maybeMutate( initExpr->get_designation(), *this ) );
     454        return initExpr;
     455}
     456
    441457
    442458Type *Mutator::mutate( VoidType *voidType ) {
     
    541557
    542558
     559Designation *Mutator::mutate( Designation * designation ) {
     560        mutateAll( designation->get_designators(), *this );
     561        return designation;
     562}
     563
    543564Initializer *Mutator::mutate( SingleInit *singleInit ) {
    544565        singleInit->set_value( singleInit->get_value()->acceptMutator( *this ) );
     
    547568
    548569Initializer *Mutator::mutate( ListInit *listInit ) {
    549         mutateAll( listInit->get_designators(), *this );
     570        mutateAll( listInit->get_designations(), *this );
    550571        mutateAll( listInit->get_initializers(), *this );
    551572        return listInit;
  • src/SynTree/Mutator.h

    rc6d2e93 re4d829b  
    8686        virtual Expression* mutate( StmtExpr * stmtExpr );
    8787        virtual Expression* mutate( UniqueExpr * uniqueExpr );
     88        virtual Expression* mutate( UntypedInitExpr * initExpr );
     89        virtual Expression* mutate( InitExpr * initExpr );
    8890
    8991        virtual Type* mutate( VoidType *basicType );
     
    104106        virtual Type* mutate( OneType *oneType );
    105107
     108        virtual Designation* mutate( Designation *designation );
    106109        virtual Initializer* mutate( SingleInit *singleInit );
    107110        virtual Initializer* mutate( ListInit *listInit );
  • src/SynTree/SynTree.h

    rc6d2e93 re4d829b  
    9393class StmtExpr;
    9494class UniqueExpr;
     95class UntypedInitExpr;
     96class InitExpr;
    9597
    9698class Type;
     
    113115class OneType;
    114116
     117class Designation;
    115118class Initializer;
    116119class SingleInit;
  • src/SynTree/Visitor.cc

    rc6d2e93 re4d829b  
    344344}
    345345
     346void Visitor::visit( UntypedInitExpr * initExpr ) {
     347        maybeAccept( initExpr->get_result(), *this );
     348        maybeAccept( initExpr->get_expr(), *this );
     349        // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.
     350}
     351
     352void Visitor::visit( InitExpr * initExpr ) {
     353        maybeAccept( initExpr->get_result(), *this );
     354        maybeAccept( initExpr->get_expr(), *this );
     355        maybeAccept( initExpr->get_designation(), *this );
     356}
     357
    346358
    347359void Visitor::visit( VoidType *voidType ) {
     
    428440}
    429441
     442void Visitor::visit( Designation * designation ) {
     443        acceptAll( designation->get_designators(), *this );
     444}
    430445
    431446void Visitor::visit( SingleInit *singleInit ) {
     
    434449
    435450void Visitor::visit( ListInit *listInit ) {
    436         acceptAll( listInit->get_designators(), *this );
     451        acceptAll( listInit->get_designations(), *this );
    437452        acceptAll( listInit->get_initializers(), *this );
    438453}
  • src/SynTree/Visitor.h

    rc6d2e93 re4d829b  
    8989        virtual void visit( StmtExpr * stmtExpr );
    9090        virtual void visit( UniqueExpr * uniqueExpr );
     91        virtual void visit( UntypedInitExpr * initExpr );
     92        virtual void visit( InitExpr * initExpr );
    9193
    9294        virtual void visit( VoidType *basicType );
     
    107109        virtual void visit( OneType *oneType );
    108110
     111        virtual void visit( Designation *designation );
    109112        virtual void visit( SingleInit *singleInit );
    110113        virtual void visit( ListInit *listInit );
     
    163166                        } // if
    164167                } catch( SemanticError &e ) {
    165                         e.set_location( (*i)->location );                       
     168                        e.set_location( (*i)->location );
    166169                        errors.append( e );
    167170                } // try
Note: See TracChangeset for help on using the changeset viewer.