Changeset e4d829b for src/SynTree
- Timestamp:
- Jun 20, 2017, 1:19:53 PM (7 years ago)
- 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)
- Location:
- src/SynTree
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/Expression.cc
rc6d2e93 re4d829b 92 92 93 93 Declaration *decl = get_var(); 94 // if ( decl != 0) decl->print(os, indent + 2);95 94 if ( decl != 0) decl->printShort(os, indent + 2); 96 95 os << std::endl; … … 670 669 } 671 670 671 InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {} 672 InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {} 673 InitAlternative::~InitAlternative() { 674 delete type; 675 delete designation; 676 } 677 678 UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {} 679 UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {} 680 UntypedInitExpr::~UntypedInitExpr() { 681 delete expr; 682 } 683 684 void 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 696 InitExpr::InitExpr( Expression * expr, Type * type, Designation * designation ) : expr( expr ), designation( designation ) { 697 set_result( type ); 698 } 699 InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {} 700 InitExpr::~InitExpr() { 701 delete expr; 702 delete designation; 703 } 704 705 void 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 672 713 std::ostream & operator<<( std::ostream & out, const Expression * expr ) { 673 714 if ( expr ) { -
src/SynTree/Expression.h
rc6d2e93 re4d829b 781 781 }; 782 782 783 struct InitAlternative { 784 public: 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 793 class UntypedInitExpr : public Expression { 794 public: 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; 808 private: 809 Expression * expr; 810 std::list<InitAlternative> initAlts; 811 }; 812 813 class InitExpr : public Expression { 814 public: 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; 829 private: 830 Expression * expr; 831 Designation * designation; 832 }; 833 834 783 835 std::ostream & operator<<( std::ostream & out, const Expression * expr ); 784 836 -
src/SynTree/Initializer.cc
rc6d2e93 re4d829b 19 19 #include "Common/utility.h" 20 20 21 Designation::Designation( const std::list< Expression * > & designators ) : designators( designators ) {} 22 Designation::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 28 Designation::~Designation() { 29 // std::cerr << "destroying designation" << std::endl; 30 deleteAll( designators ); 31 // std::cerr << "finished destroying designation" << std::endl; 32 } 33 34 void 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 21 46 Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {} 22 47 Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) { 23 48 } 24 25 26 49 Initializer::~Initializer() {} 27 50 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 ) { 51 SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) { 38 52 } 39 53 40 54 SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) { 41 cloneAll(other.designators, designators );42 55 } 43 56 44 57 SingleInit::~SingleInit() { 45 58 delete value; 46 deleteAll(designators);47 59 } 48 60 49 void SingleInit::print( std::ostream &os, int indent ) {50 os << std:: endl << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;61 void SingleInit::print( std::ostream &os, int indent ) const { 62 os << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl; 51 63 os << std::string(indent+4, ' ' ); 52 64 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 } // if61 65 } 62 66 63 ListInit::ListInit( const std::list<Initializer*> &_initializers, const std::list<Expression *> &_designators, bool maybeConstructed ) 64 : Initializer( maybeConstructed ), initializers( _initializers ), designators( _designators ) { 67 68 ListInit::ListInit( const std::list<Initializer*> &initializers, const std::list<Designation *> &designations, bool maybeConstructed ) 69 : Initializer( maybeConstructed ), initializers( initializers ), designations( designations ) { 65 70 } 66 71 67 72 ListInit::ListInit( const ListInit & other ) : Initializer( other ) { 68 73 cloneAll( other.initializers, initializers ); 69 cloneAll( other.designat ors, designators );74 cloneAll( other.designations, designations ); 70 75 } 71 72 76 73 77 ListInit::~ListInit() { 74 78 deleteAll( initializers ); 75 deleteAll( designat ors );79 deleteAll( designations ); 76 80 } 77 81 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 82 void 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 } 86 87 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 } 92 92 } 93 93 … … 103 103 } 104 104 105 void ConstructorInit::print( std::ostream &os, int indent ) {105 void ConstructorInit::print( std::ostream &os, int indent ) const { 106 106 os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl; 107 107 if ( ctor ) { … … 124 124 } 125 125 126 std::ostream & operator<<( std::ostream & out, Initializer * init ) { 127 init->print( out ); 126 std::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 135 std::ostream & operator<<( std::ostream & out, const Designation * des ) { 136 if ( des ) { 137 des->print( out ); 138 } else { 139 out << "nullptr"; 140 } 128 141 return out; 129 142 } -
src/SynTree/Initializer.h
rc6d2e93 re4d829b 25 25 #include "Visitor.h" 26 26 27 const std::list<Expression*> noDesignators; 27 // Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized. 28 class Designation : public BaseSyntaxNode { 29 public: 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; 40 private: 41 std::list< Expression * > designators; 42 }; 43 44 const std::list<Designation *> noDesignators; 28 45 29 46 // Initializer: base class for object initializers (provide default values) 30 47 class Initializer : public BaseSyntaxNode { 31 48 public: 32 // Initializer( std::string _name = std::string(""), int _pos = 0 );33 49 Initializer( bool maybeConstructed ); 34 50 Initializer( const Initializer & other ); 35 51 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 reached48 }49 52 50 53 bool get_maybeConstructed() { return maybeConstructed; } … … 53 56 virtual void accept( Visitor &v ) = 0; 54 57 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; 56 59 private: 57 // std::string name;58 // int pos;59 60 bool maybeConstructed; 60 61 }; … … 63 64 class SingleInit : public Initializer { 64 65 public: 65 SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(),bool maybeConstructed = false );66 SingleInit( Expression *value, bool maybeConstructed = false ); 66 67 SingleInit( const SingleInit &other ); 67 68 virtual ~SingleInit(); … … 70 71 void set_value( Expression *newValue ) { value = newValue; } 71 72 72 std::list<Expression *> &get_designators() { return designators; }73 void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }74 75 73 virtual SingleInit *clone() const { return new SingleInit( *this); } 76 74 virtual void accept( Visitor &v ) { v.visit( this ); } 77 75 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; 79 77 private: 80 78 //Constant *value; 81 79 Expression *value; // has to be a compile-time constant 82 std::list< Expression * > designators;83 80 }; 84 81 … … 88 85 public: 89 86 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 ); 91 88 ListInit( const ListInit & other ); 92 89 virtual ~ListInit(); 93 90 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; } 98 93 99 94 typedef std::list<Initializer*>::iterator iterator; 95 typedef std::list<Initializer*>::const_iterator const_iterator; 100 96 iterator begin() { return initializers.begin(); } 101 97 iterator end() { return initializers.end(); } 98 const_iterator begin() const { return initializers.begin(); } 99 const_iterator end() const { return initializers.end(); } 102 100 103 101 virtual ListInit *clone() const { return new ListInit( *this ); } 104 102 virtual void accept( Visitor &v ) { v.visit( this ); } 105 103 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; 107 105 private: 108 std::list<Initializer *> initializers; // order *is* important109 std::list< Expression *> designators;106 std::list<Initializer *> initializers; // order *is* important 107 std::list<Designation *> designations; // order/length is consistent with initializers 110 108 }; 111 109 … … 130 128 virtual void accept( Visitor &v ) { v.visit( this ); } 131 129 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; 133 131 134 132 private: … … 140 138 }; 141 139 142 std::ostream & operator<<( std::ostream & out, Initializer * init ); 140 std::ostream & operator<<( std::ostream & out, const Initializer * init ); 141 std::ostream & operator<<( std::ostream & out, const Designation * des ); 143 142 144 143 #endif // INITIALIZER_H -
src/SynTree/Mutator.cc
rc6d2e93 re4d829b 439 439 } 440 440 441 Expression *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 449 Expression *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 441 457 442 458 Type *Mutator::mutate( VoidType *voidType ) { … … 541 557 542 558 559 Designation *Mutator::mutate( Designation * designation ) { 560 mutateAll( designation->get_designators(), *this ); 561 return designation; 562 } 563 543 564 Initializer *Mutator::mutate( SingleInit *singleInit ) { 544 565 singleInit->set_value( singleInit->get_value()->acceptMutator( *this ) ); … … 547 568 548 569 Initializer *Mutator::mutate( ListInit *listInit ) { 549 mutateAll( listInit->get_designat ors(), *this );570 mutateAll( listInit->get_designations(), *this ); 550 571 mutateAll( listInit->get_initializers(), *this ); 551 572 return listInit; -
src/SynTree/Mutator.h
rc6d2e93 re4d829b 86 86 virtual Expression* mutate( StmtExpr * stmtExpr ); 87 87 virtual Expression* mutate( UniqueExpr * uniqueExpr ); 88 virtual Expression* mutate( UntypedInitExpr * initExpr ); 89 virtual Expression* mutate( InitExpr * initExpr ); 88 90 89 91 virtual Type* mutate( VoidType *basicType ); … … 104 106 virtual Type* mutate( OneType *oneType ); 105 107 108 virtual Designation* mutate( Designation *designation ); 106 109 virtual Initializer* mutate( SingleInit *singleInit ); 107 110 virtual Initializer* mutate( ListInit *listInit ); -
src/SynTree/SynTree.h
rc6d2e93 re4d829b 93 93 class StmtExpr; 94 94 class UniqueExpr; 95 class UntypedInitExpr; 96 class InitExpr; 95 97 96 98 class Type; … … 113 115 class OneType; 114 116 117 class Designation; 115 118 class Initializer; 116 119 class SingleInit; -
src/SynTree/Visitor.cc
rc6d2e93 re4d829b 344 344 } 345 345 346 void 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 352 void Visitor::visit( InitExpr * initExpr ) { 353 maybeAccept( initExpr->get_result(), *this ); 354 maybeAccept( initExpr->get_expr(), *this ); 355 maybeAccept( initExpr->get_designation(), *this ); 356 } 357 346 358 347 359 void Visitor::visit( VoidType *voidType ) { … … 428 440 } 429 441 442 void Visitor::visit( Designation * designation ) { 443 acceptAll( designation->get_designators(), *this ); 444 } 430 445 431 446 void Visitor::visit( SingleInit *singleInit ) { … … 434 449 435 450 void Visitor::visit( ListInit *listInit ) { 436 acceptAll( listInit->get_designat ors(), *this );451 acceptAll( listInit->get_designations(), *this ); 437 452 acceptAll( listInit->get_initializers(), *this ); 438 453 } -
src/SynTree/Visitor.h
rc6d2e93 re4d829b 89 89 virtual void visit( StmtExpr * stmtExpr ); 90 90 virtual void visit( UniqueExpr * uniqueExpr ); 91 virtual void visit( UntypedInitExpr * initExpr ); 92 virtual void visit( InitExpr * initExpr ); 91 93 92 94 virtual void visit( VoidType *basicType ); … … 107 109 virtual void visit( OneType *oneType ); 108 110 111 virtual void visit( Designation *designation ); 109 112 virtual void visit( SingleInit *singleInit ); 110 113 virtual void visit( ListInit *listInit ); … … 163 166 } // if 164 167 } catch( SemanticError &e ) { 165 e.set_location( (*i)->location ); 168 e.set_location( (*i)->location ); 166 169 errors.append( e ); 167 170 } // try
Note: See TracChangeset
for help on using the changeset viewer.