Changes in / [5da9d6a:3ca540f]
- Location:
- src
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r5da9d6a r3ca540f 1012 1012 } 1013 1013 1014 void CodeGenerator::postvisit( WithStmt * with ) { 1015 if ( ! genC ) { 1016 output << "with ( "; 1017 genCommaList( with->exprs.begin(), with->exprs.end() ); 1018 output << " ) "; 1019 } 1020 with->stmt->accept( *visitor ); 1021 } 1014 1022 1015 1023 void CodeGenerator::postvisit( WhileStmt * whileStmt ) { -
src/CodeGen/CodeGenerator.h
r5da9d6a r3ca540f 102 102 void postvisit( CatchStmt * ); 103 103 void postvisit( WaitForStmt * ); 104 void postvisit( WithStmt * ); 104 105 void postvisit( WhileStmt * ); 105 106 void postvisit( ForStmt * ); -
src/Common/PassVisitor.h
r5da9d6a r3ca540f 81 81 virtual void visit( FinallyStmt * finallyStmt ) override final; 82 82 virtual void visit( WaitForStmt * waitforStmt ) override final; 83 virtual void visit( WithStmt * withStmt ) override final; 83 84 virtual void visit( NullStmt * nullStmt ) override final; 84 85 virtual void visit( DeclStmt * declStmt ) override final; … … 172 173 virtual Statement * mutate( FinallyStmt * finallyStmt ) override final; 173 174 virtual Statement * mutate( WaitForStmt * waitforStmt ) override final; 175 virtual Statement * mutate( WithStmt * withStmt ) override final; 174 176 virtual NullStmt * mutate( NullStmt * nullStmt ) override final; 175 177 virtual Statement * mutate( DeclStmt * declStmt ) override final; … … 296 298 void indexerAddUnionFwd ( UnionDecl * node ) { indexer_impl_addUnionFwd ( pass, 0, node ); } 297 299 void indexerAddTrait ( TraitDecl * node ) { indexer_impl_addTrait ( pass, 0, node ); } 300 void indexerAddWith ( WithStmt * node ) { indexer_impl_addWith ( pass, 0, node ); } 301 298 302 299 303 template< typename TreeType, typename VisitorType > -
src/Common/PassVisitor.impl.h
r5da9d6a r3ca540f 985 985 } 986 986 987 988 989 //-------------------------------------------------------------------------- 990 // NullStmt 991 template< typename pass_type > 992 void PassVisitor< pass_type >::visit( WithStmt * node ) { 993 VISIT_START( node ); 994 maybeAccept_impl( node->exprs, *this ); 995 { 996 // catch statements introduce a level of scope (for the caught exception) 997 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 998 indexerAddWith( node ); 999 maybeAccept_impl( node->stmt, *this ); 1000 } 1001 VISIT_END( node ); 1002 } 1003 1004 template< typename pass_type > 1005 Statement * PassVisitor< pass_type >::mutate( WithStmt * node ) { 1006 MUTATE_START( node ); 1007 maybeMutate_impl( node->exprs, *this ); 1008 { 1009 // catch statements introduce a level of scope (for the caught exception) 1010 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 1011 indexerAddWith( node ); 1012 maybeMutate_impl( node->stmt, *this ); 1013 } 1014 MUTATE_END( Statement, node ); 1015 } 1016 987 1017 //-------------------------------------------------------------------------- 988 1018 // NullStmt -
src/Common/PassVisitor.proto.h
r5da9d6a r3ca540f 208 208 INDEXER_FUNC( addUnion , UnionDecl * ); 209 209 INDEXER_FUNC( addTrait , TraitDecl * ); 210 INDEXER_FUNC( addWith , WithStmt * ); 210 211 211 212 -
src/GenPoly/InstantiateGeneric.cc
r5da9d6a r3ca540f 453 453 return false; 454 454 } 455 456 AggregateDecl * getAggr( Type * t ) {457 if ( StructInstType * inst = dynamic_cast< StructInstType * >( t ) ) {458 return inst->baseStruct;459 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) {460 return inst->baseUnion;461 }462 assertf( false, "Non-aggregate type: %s", toString( t ).c_str() );463 }464 455 } 465 456 … … 469 460 if ( isGenericType( memberExpr->aggregate->result ) ) { 470 461 // find the location of the member 471 AggregateDecl * aggr = getAggr( memberExpr->aggregate->result);462 AggregateDecl * aggr = memberExpr->aggregate->result->getAggr(); 472 463 std::list< Declaration * > & members = aggr->members; 473 464 memberIndex = std::distance( members.begin(), std::find( members.begin(), members.end(), memberExpr->member ) ); … … 479 470 if ( memberIndex != -1 ) { 480 471 // using the location from the generic type, find the member in the instantiation and rebuild the member expression 481 AggregateDecl * aggr = getAggr( memberExpr->aggregate->result);472 AggregateDecl * aggr = memberExpr->aggregate->result->getAggr(); 482 473 assertf( memberIndex < (int)aggr->members.size(), "Instantiation somehow has fewer members than the generic type." ); 483 474 Declaration * member = *std::next( aggr->members.begin(), memberIndex ); -
src/Parser/ParseNode.h
r5da9d6a r3ca540f 408 408 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ); 409 409 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ); 410 WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ); 410 411 411 412 //############################################################################## -
src/Parser/StatementNode.cc
r5da9d6a r3ca540f 282 282 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 283 283 284 node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );284 node->orelse.statement = maybeMoveBuild<Statement >( else_stmt ); 285 285 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) ); 286 286 287 287 return node; 288 } 289 290 WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) { 291 std::list< Expression * > e; 292 buildMoveList( exprs, e ); 293 Statement * s = maybeMoveBuild<Statement>( stmt ); 294 return new WithStmt( e, s ); 288 295 } 289 296 -
src/Parser/parser.yy
r5da9d6a r3ca540f 1058 1058 with_statement: 1059 1059 WITH '(' tuple_expression_list ')' statement 1060 { throw SemanticError("With clause is currently unimplemented."); $$ = nullptr; } // FIX ME 1060 { 1061 $$ = new StatementNode( build_with( $3, $5 ) ); 1062 } 1061 1063 ; 1062 1064 -
src/ResolvExpr/AlternativeFinder.cc
r5da9d6a r3ca540f 83 83 } 84 84 85 void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt ) { 86 Indenter indent = { Indenter::tabsize, indentAmt }; 87 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { 88 i->print( os, indent ); 89 os << std::endl; 90 } 91 } 92 85 93 namespace { 86 void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ) {87 Indenter indent = { Indenter::tabsize, indentAmt };88 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {89 i->print( os, indent );90 os << std::endl;91 }92 }93 94 94 void makeExprList( const AltList &in, std::list< Expression* > &out ) { 95 95 for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) { -
src/ResolvExpr/AlternativeFinder.h
r5da9d6a r3ca540f 174 174 175 175 Cost sumCost( const AltList &in ); 176 void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ); 176 177 177 178 template< typename InputIterator > … … 181 182 } 182 183 } 184 183 185 } // namespace ResolvExpr 184 186 -
src/ResolvExpr/Resolver.cc
r5da9d6a r3ca540f 74 74 void previsit( CatchStmt *catchStmt ); 75 75 void previsit( WaitForStmt * stmt ); 76 void previsit( WithStmt * withStmt ); 76 77 77 78 void previsit( SingleInit *singleInit ); … … 571 572 } 572 573 574 bool isStructOrUnion( Type * t ) { 575 t = t->stripReferences(); 576 return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); 577 } 578 579 void Resolver::previsit( WithStmt * withStmt ) { 580 for ( Expression *& expr : withStmt->exprs ) { 581 TypeEnvironment env; 582 AlternativeFinder finder( indexer, env ); 583 finder.findWithAdjustment( expr ); 584 585 // only struct- and union-typed expressions are viable candidates 586 AltList candidates; 587 for ( Alternative & alt : finder.get_alternatives() ) { 588 if ( isStructOrUnion( alt.expr->result ) ) { 589 candidates.push_back( std::move( alt ) ); 590 } 591 } 592 593 // choose the lowest cost expression among the candidates 594 AltList winners; 595 findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) ); 596 if ( winners.size() == 0 ) { 597 throw SemanticError( "No reasonable alternatives for with statement expression: ", expr ); 598 } else if ( winners.size() != 1 ) { 599 std::ostringstream stream; 600 stream << "Cannot choose between " << winners.size() << " alternatives for with statement expression\n"; 601 expr->print( stream ); 602 stream << "Alternatives are:\n"; 603 printAlts( winners, stream, 1 ); 604 throw SemanticError( stream.str() ); 605 } 606 607 // there is one unambiguous interpretation - move the expression into the with statement 608 Alternative & alt = winners.front(); 609 finishExpr( alt.expr, alt.env, expr->env ); 610 delete expr; 611 expr = alt.expr; 612 alt.expr = nullptr; 613 } 614 } 615 573 616 template< typename T > 574 617 bool isCharType( T t ) { -
src/SymTab/Indexer.cc
r5da9d6a r3ca540f 567 567 } 568 568 569 void Indexer::addWith( WithStmt * stmt ) { 570 for ( Expression * expr : stmt->exprs ) { 571 if ( expr->result ) { 572 AggregateDecl * aggr = expr->result->getAggr(); 573 assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); 574 575 // xxx - this is wrong, needs to somehow hook up chain of objects 576 for ( Declaration * decl : aggr->members ) { 577 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 578 addId( dwt ); 579 } 580 } 581 } 582 } 583 } 584 569 585 void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { 570 586 for ( auto d : decls ) { -
src/SymTab/Indexer.h
r5da9d6a r3ca540f 76 76 void addTrait( TraitDecl *decl ); 77 77 78 /// adds all of the IDs from WithStmt exprs 79 void addWith( WithStmt * ); 80 78 81 /// convenience function for adding a list of Ids to the indexer 79 82 void addIds( const std::list< DeclarationWithType * > & decls ); -
src/SynTree/Mutator.cc
r5da9d6a r3ca540f 203 203 } 204 204 205 Statement * Mutator::mutate( WithStmt * withStmt ) { 206 mutateAll( withStmt->exprs, *this ); 207 withStmt->stmt = maybeMutate( withStmt->stmt, *this ); 208 return withStmt; 209 } 210 205 211 NullStmt * Mutator::mutate( NullStmt *nullStmt ) { 206 212 return nullStmt; -
src/SynTree/Mutator.h
r5da9d6a r3ca540f 50 50 virtual Statement * mutate( FinallyStmt * catchStmt ); 51 51 virtual Statement * mutate( WaitForStmt * waitforStmt ); 52 virtual Statement * mutate( WithStmt * withStmt ); 52 53 virtual NullStmt * mutate( NullStmt * nullStmt ); 53 54 virtual Statement * mutate( DeclStmt * declStmt ); -
src/SynTree/ReferenceToType.cc
r5da9d6a r3ca540f 70 70 bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; } 71 71 72 AggregateDecl * StructInstType::getAggr() { return baseStruct; } 73 72 74 void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { 73 75 assert( baseStruct ); … … 101 103 102 104 bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; } 105 106 AggregateDecl * UnionInstType::getAggr() { return baseUnion; } 103 107 104 108 void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { -
src/SynTree/Statement.cc
r5da9d6a r3ca540f 456 456 } 457 457 458 459 WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {} 460 WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) { 461 cloneAll( other.exprs, exprs ); 462 } 463 WithStmt::~WithStmt() { 464 deleteAll( exprs ); 465 delete stmt; 466 } 467 468 void WithStmt::print( std::ostream & os, Indenter indent ) const { 469 os << "With statement" << endl; 470 os << indent << "... with statement:" << endl << indent+1; 471 stmt->print( os, indent+1 ); 472 } 473 474 458 475 NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) { 459 476 } -
src/SynTree/Statement.h
r5da9d6a r3ca540f 431 431 }; 432 432 433 class WithStmt : public Statement { 434 public: 435 std::list< Expression * > exprs; 436 Statement * stmt; 437 438 WithStmt( const std::list< Expression * > & exprs, Statement * stmt ); 439 WithStmt( const WithStmt & other ); 440 virtual ~WithStmt(); 441 442 virtual WithStmt * clone() const override { return new WithStmt( *this ); } 443 virtual void accept( Visitor & v ) override { v.visit( this ); } 444 virtual Statement * acceptMutator( Mutator & m ) override { return m.mutate( this ); } 445 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 446 }; 447 433 448 434 449 // represents a declaration that occurs as part of a compound statement -
src/SynTree/SynTree.h
r5da9d6a r3ca540f 55 55 class FinallyStmt; 56 56 class WaitForStmt; 57 class WithStmt; 57 58 class NullStmt; 58 59 class DeclStmt; -
src/SynTree/Type.h
r5da9d6a r3ca540f 178 178 virtual bool isComplete() const { return true; } 179 179 180 virtual AggregateDecl * getAggr() { assertf( false, "Non-aggregate type: %s", toString( this ).c_str() ); } 181 180 182 virtual Type *clone() const = 0; 181 183 virtual void accept( Visitor & v ) = 0; … … 405 407 virtual bool isComplete() const override; 406 408 409 virtual AggregateDecl * getAggr() override; 410 407 411 /// Looks up the members of this struct named "name" and places them into "foundDecls". 408 412 /// Clones declarations into "foundDecls", caller responsible for freeing … … 436 440 437 441 virtual bool isComplete() const override; 442 443 virtual AggregateDecl * getAggr() override; 438 444 439 445 /// looks up the members of this union named "name" and places them into "foundDecls" -
src/SynTree/Visitor.cc
r5da9d6a r3ca540f 174 174 } 175 175 176 void Visitor::visit( __attribute__((unused)) NullStmt *nullStmt ) { 176 void Visitor::visit( WithStmt * withStmt ) { 177 acceptAll( withStmt->exprs, *this ); 178 maybeAccept( withStmt->stmt, *this ); 179 } 180 181 void Visitor::visit( NullStmt * ) { 177 182 } 178 183 -
src/SynTree/Visitor.h
r5da9d6a r3ca540f 52 52 virtual void visit( FinallyStmt * finallyStmt ); 53 53 virtual void visit( WaitForStmt * waitforStmt ); 54 virtual void visit( WithStmt * withStmt ); 54 55 virtual void visit( NullStmt * nullStmt ); 55 56 virtual void visit( DeclStmt * declStmt );
Note: See TracChangeset
for help on using the changeset viewer.