Changeset 5f6c42c for src/GenPoly/Box.cc
- Timestamp:
- Dec 15, 2015, 5:33:25 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 35304009, 8360977
- Parents:
- ffad73a (diff), 4389966 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rffad73a r5f6c42c 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Rob Schluntz12 // Last Modified On : T hu Nov 26 17:01:55201513 // Update Count : 19111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Dec 15 15:30:31 2015 13 // Update Count : 215 14 14 // 15 15 … … 62 62 virtual Expression *mutate( CommaExpr *commaExpr ); 63 63 virtual Expression *mutate( ConditionalExpr *condExpr ); 64 virtual Statement * mutate(ReturnStmt *catchStmt);64 virtual Statement * mutate( ReturnStmt *returnStmt ); 65 65 virtual Type *mutate( PointerType *pointerType ); 66 virtual Type * mutate( FunctionType *pointerType );66 virtual Type * mutate( FunctionType *functionType ); 67 67 68 68 virtual void doBeginScope(); … … 192 192 if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { 193 193 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { 194 name = typeInst->get_name(); 195 return true; 194 if ( TypeInstType *typeInst2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { 195 if ( typeInst->get_name() == typeInst2->get_name() ) { 196 name = typeInst->get_name(); 197 return true; 198 } // if 199 } // if 196 200 } // if 197 201 } // if … … 277 281 278 282 TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { 279 /// std::cerr << "add " << typeDecl->get_name() << "\n";280 283 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); 281 284 return Mutator::mutate( typeDecl ); … … 327 330 328 331 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { 329 if ( useRetval ) { 330 assert( retval ); 331 arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); 332 arg++; 333 } else { 334 ObjectDecl *newObj = makeTemporary( retType->clone() ); 335 Expression *paramExpr = new VariableExpr( newObj ); 336 if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { 337 paramExpr = new AddressExpr( paramExpr ); 338 } // if 339 arg = appExpr->get_args().insert( arg, paramExpr ); 340 arg++; 341 /// stmtsToAdd.push_back( new ExprStmt( noLabels, appExpr ) ); 342 CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); 343 commaExpr->set_env( appExpr->get_env() ); 344 appExpr->set_env( 0 ); 345 return commaExpr; 346 } // if 347 return appExpr; 332 // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. 333 // if ( useRetval ) { 334 // assert( retval ); 335 // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); 336 // arg++; 337 // } else { 338 339 // Create temporary to hold return value of polymorphic function and produce that temporary as a result 340 // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple 341 // return values. 342 ObjectDecl *newObj = makeTemporary( retType->clone() ); 343 Expression *paramExpr = new VariableExpr( newObj ); 344 // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the 345 // temporary is already boxed and can be used directly. 346 if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { 347 paramExpr = new AddressExpr( paramExpr ); 348 } // if 349 arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call 350 arg++; 351 // Build a comma expression to call the function and emulate a normal return. 352 CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); 353 commaExpr->set_env( appExpr->get_env() ); 354 appExpr->set_env( 0 ); 355 return commaExpr; 356 // } // if 357 // return appExpr; 348 358 } 349 359 … … 411 421 412 422 void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { 413 /// std::cout << "function is ";414 /// function->print( std::cout );415 423 for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { 416 /// std::cout << "parameter is ";417 /// (*param)->print( std::fcout );418 /// std::cout << std::endl << "argument is ";419 /// (*arg)->print( std::cout );420 424 assert( arg != appExpr->get_args().end() ); 421 425 addCast( *arg, (*param)->get_type(), exprTyVars ); … … 807 811 Expression *Pass1::mutate( AddressExpr *addrExpr ) { 808 812 assert( ! addrExpr->get_arg()->get_results().empty() ); 813 814 bool needs = false; 815 if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { 816 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { 817 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { 818 if ( name->get_name() == "*?" ) { 819 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { 820 assert( ! appExpr->get_function()->get_results().empty() ); 821 PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); 822 assert( pointer ); 823 FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); 824 assert( function ); 825 needs = needsAdapter( function, scopeTyVars ); 826 } // if 827 } // if 828 } // if 829 } // if 830 } // if 809 831 addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); 810 if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) ) {832 if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) { 811 833 Expression *ret = addrExpr->get_arg(); 812 834 delete ret->get_results().front(); … … 820 842 } 821 843 822 Statement * Pass1::mutate(ReturnStmt *retStmt) { 823 // a cast expr on a polymorphic return value is either redundant or invalid 824 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( retStmt->get_expr() ) ) { 825 retStmt->set_expr( castExpr->get_arg() ); 826 retStmt->get_expr()->set_env( castExpr->get_env() ); 827 castExpr->set_env( 0 ); 828 castExpr->set_arg( 0 ); 829 delete castExpr; 830 } 831 if ( retval && retStmt->get_expr() ) { 832 assert( ! retStmt->get_expr()->get_results().empty() ); 833 if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) { 834 /// retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); 835 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ); 836 assert( typeInst ); 837 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 838 if ( assignIter == assignOps.end() ) { 839 throw SemanticError( "Attempt to return dtype or ftype object in ", retStmt->get_expr() ); 840 } // if 841 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); 842 Expression *retParm = new NameExpr( retval->get_name() ); 843 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); 844 assignExpr->get_args().push_back( retParm ); 845 assignExpr->get_args().push_back( retStmt->get_expr() ); 846 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); 847 } else { 848 useRetval = true; 849 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( retStmt->get_expr() ) ) ); 850 useRetval = false; 844 Statement * Pass1::mutate( ReturnStmt *returnStmt ) { 845 if ( retval && returnStmt->get_expr() ) { 846 assert( ! returnStmt->get_expr()->get_results().empty() ); 847 // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. 848 // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { 849 // a cast expr on a polymorphic return value is either redundant or invalid 850 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { 851 returnStmt->set_expr( castExpr->get_arg() ); 852 returnStmt->get_expr()->set_env( castExpr->get_env() ); 853 castExpr->set_env( 0 ); 854 castExpr->set_arg( 0 ); 855 delete castExpr; 856 } //while 857 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ); 858 assert( typeInst ); 859 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 860 if ( assignIter == assignOps.end() ) { 861 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); 851 862 } // if 852 retStmt->set_expr( 0 ); 863 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); 864 Expression *retParm = new NameExpr( retval->get_name() ); 865 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); 866 assignExpr->get_args().push_back( retParm ); 867 assignExpr->get_args().push_back( returnStmt->get_expr() ); 868 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); 869 // } else { 870 // useRetval = true; 871 // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); 872 // useRetval = false; 873 // } // if 874 returnStmt->set_expr( 0 ); 853 875 } else { 854 ret Stmt->set_expr( mutateExpression( retStmt->get_expr() ) );855 } // if 856 return ret Stmt;876 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); 877 } // if 878 return returnStmt; 857 879 } 858 880 … … 905 927 } 906 928 } 907 // /deleteAll( functions );929 // deleteAll( functions ); 908 930 } 909 931
Note: See TracChangeset
for help on using the changeset viewer.