Changeset 201182a
- Timestamp:
- Sep 25, 2017, 7:10:17 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:
- a31b384
- Parents:
- fc72845d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rfc72845d r201182a 77 77 78 78 /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call 79 class Pass1 final : public PolyMutator{79 class Pass1 final : public BoxPass, public WithTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<Pass1>, public WithShortCircuiting { 80 80 public: 81 81 Pass1(); 82 82 83 using PolyMutator::mutate; 84 virtual Expression *mutate( ApplicationExpr *appExpr ) override; 85 virtual Expression *mutate( AddressExpr *addrExpr ) override; 86 virtual Expression *mutate( UntypedExpr *expr ) override; 87 virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override; 88 virtual TypeDecl *mutate( TypeDecl *typeDecl ) override; 89 virtual Expression *mutate( CommaExpr *commaExpr ) override; 90 virtual Expression *mutate( ConditionalExpr *condExpr ) override; 91 virtual Statement * mutate( ReturnStmt *returnStmt ) override; 92 virtual Type *mutate( PointerType *pointerType ) override; 93 virtual Type * mutate( FunctionType *functionType ) override; 94 95 virtual void doBeginScope() override; 96 virtual void doEndScope() override; 83 void premutate( FunctionDecl * functionDecl ); 84 void premutate( TypeDecl * typeDecl ); 85 void premutate( CommaExpr * commaExpr ); 86 Expression * postmutate( ApplicationExpr * appExpr ); 87 Expression * postmutate( UntypedExpr *expr ); 88 void premutate( AddressExpr * addrExpr ); 89 Expression * postmutate( AddressExpr * addrExpr ); 90 void premutate( ReturnStmt * returnStmt ); 91 void premutate( PointerType * pointerType ); 92 void premutate( FunctionType * functionType ); 93 94 void beginScope(); 95 void endScope(); 97 96 private: 98 97 /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application … … 160 159 /// * Calculates polymorphic offsetof expressions from offset array 161 160 /// * Inserts dynamic calculation of polymorphic type layouts where needed 162 class PolyGenericCalculator final : public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution {161 class PolyGenericCalculator final : public BoxPass, public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution { 163 162 public: 164 163 PolyGenericCalculator(); … … 199 198 ScopedSet< std::string > knownOffsets; ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName 200 199 UniqueName bufNamer; ///< Namer for VLA buffers 201 TyVarMap scopeTyVars;202 200 }; 203 201 … … 247 245 void box( std::list< Declaration *>& translationUnit ) { 248 246 PassVisitor<LayoutFunctionBuilder> layoutBuilder; 249 Pass 1pass1;247 PassVisitor<Pass1> pass1; 250 248 Pass2 pass2; 251 249 PassVisitor<PolyGenericCalculator> polyCalculator; … … 253 251 254 252 acceptAll( translationUnit, layoutBuilder ); 255 mutate TranslationUnit/*All*/( translationUnit, pass1 );253 mutateAll( translationUnit, pass1 ); 256 254 mutateTranslationUnit/*All*/( translationUnit, pass2 ); 257 255 mutateAll( translationUnit, polyCalculator ); … … 500 498 Pass1::Pass1() : tempNamer( "_temp" ) {} 501 499 502 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {500 void Pass1::premutate( FunctionDecl *functionDecl ) { 503 501 if ( functionDecl->get_statements() ) { // empty routine body ? 504 502 // std::cerr << "mutating function: " << functionDecl->get_mangleName() << std::endl; 505 doBeginScope(); 506 scopeTyVars.beginScope(); 507 508 DeclarationWithType *oldRetval = retval; 503 GuardScope( scopeTyVars ); 504 GuardValue( retval ); 509 505 510 506 // process polymorphic return value 511 507 retval = nullptr; 512 if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() != LinkageSpec::C ) { 513 retval = functionDecl->get_functionType()->get_returnVals().front(); 508 FunctionType *functionType = functionDecl->type; 509 if ( isDynRet( functionType ) && functionDecl->linkage != LinkageSpec::C ) { 510 retval = functionType->returnVals.front(); 514 511 515 512 // give names to unnamed return values 516 if ( retval-> get_name()== "" ) {517 retval-> set_name( "_retparm" );518 retval-> set_linkage( LinkageSpec::C );513 if ( retval->name == "" ) { 514 retval->name = "_retparm"; 515 retval->linkage = LinkageSpec::C; 519 516 } // if 520 517 } // if 521 518 522 FunctionType *functionType = functionDecl->get_functionType(); 523 makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); 524 525 std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); 519 makeTyVarMap( functionType, scopeTyVars ); 520 521 std::list< DeclarationWithType *> ¶mList = functionType->parameters; 526 522 std::list< FunctionType *> functions; 527 for ( Type::ForallList::iterator tyVar = functionType-> get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {528 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)-> get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {523 for ( Type::ForallList::iterator tyVar = functionType->forall.begin(); tyVar != functionType->forall.end(); ++tyVar ) { 524 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->assertions.begin(); assert != (*tyVar)->assertions.end(); ++assert ) { 529 525 findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter ); 530 526 } // for … … 541 537 } // if 542 538 } // for 543 544 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );545 546 scopeTyVars.endScope();547 retval = oldRetval;548 doEndScope();549 539 // std::cerr << "end function: " << functionDecl->get_mangleName() << std::endl; 550 540 } // if 551 return functionDecl; 552 } 553 554 TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { 541 } 542 543 void Pass1::premutate( TypeDecl *typeDecl ) { 555 544 addToTyVarMap( typeDecl, scopeTyVars ); 556 return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) ); 557 } 558 559 Expression *Pass1::mutate( CommaExpr *commaExpr ) { 545 } 546 547 void Pass1::premutate( CommaExpr *commaExpr ) { 560 548 // Attempting to find application expressions that were mutated by the copy constructor passes 561 549 // to use an explicit return variable, so that the variable can be reused as a parameter to the … … 573 561 } 574 562 } 575 576 commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) );577 commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) );578 return commaExpr;579 }580 581 Expression *Pass1::mutate( ConditionalExpr *condExpr ) {582 condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) );583 condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) );584 condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) );585 return condExpr;586 587 563 } 588 564 … … 658 634 ObjectDecl *Pass1::makeTemporary( Type *type ) { 659 635 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, type, 0 ); 660 stmtsToAdd .push_back( new DeclStmt( noLabels, newObj ) );636 stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) ); 661 637 return newObj; 662 638 } … … 774 750 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, newType, 0 ); 775 751 newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right??? 776 stmtsToAdd .push_back( new DeclStmt( noLabels, newObj ) );752 stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) ); 777 753 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); // TODO: why doesn't this just use initialization syntax? 778 754 assign->get_args().push_back( new VariableExpr( newObj ) ); 779 755 assign->get_args().push_back( arg ); 780 stmtsToAdd .push_back( new ExprStmt( noLabels, assign ) );756 stmtsToAddBefore.push_back( new ExprStmt( noLabels, assign ) ); 781 757 arg = new AddressExpr( new VariableExpr( newObj ) ); 782 758 } // if … … 960 936 std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) ); 961 937 adapter = answer.first; 962 stmtsToAdd .push_back( new DeclStmt( noLabels, newAdapter ) );938 stmtsToAddBefore.push_back( new DeclStmt( noLabels, newAdapter ) ); 963 939 } // if 964 940 assert( adapter != adapters.end() ); … … 1117 1093 } 1118 1094 1119 Expression *Pass1:: mutate( ApplicationExpr *appExpr ) {1095 Expression *Pass1::postmutate( ApplicationExpr *appExpr ) { 1120 1096 // std::cerr << "mutate appExpr: " << InitTweak::getFunctionName( appExpr ) << std::endl; 1121 1097 // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { … … 1123 1099 // } 1124 1100 // std::cerr << "\n"; 1125 appExpr->get_function()->acceptMutator( *this ); 1126 mutateAll( appExpr->get_args(), *this ); 1127 1128 assert( appExpr->get_function()->has_result() ); 1129 FunctionType * function = getFunctionType( appExpr->get_function()->get_result() ); 1130 assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->get_function()->get_result() ).c_str() ); 1101 1102 assert( appExpr->function->result ); 1103 FunctionType * function = getFunctionType( appExpr->function->result ); 1104 assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() ); 1131 1105 1132 1106 if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { … … 1181 1155 } 1182 1156 1183 Expression * Pass1::mutate( UntypedExpr *expr ) {1184 if ( expr-> has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) {1185 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr-> get_function()) ) {1157 Expression * Pass1::postmutate( UntypedExpr *expr ) { 1158 if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) { 1159 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->function ) ) { 1186 1160 if ( name->get_name() == "*?" ) { 1187 Expression *ret = expr-> get_args().front();1188 expr-> get_args().clear();1161 Expression *ret = expr->args.front(); 1162 expr->args.clear(); 1189 1163 delete expr; 1190 return ret ->acceptMutator( *this );1164 return ret; 1191 1165 } // if 1192 1166 } // if 1193 1167 } // if 1194 return PolyMutator::mutate( expr ); 1195 } 1196 1197 Expression *Pass1::mutate( AddressExpr *addrExpr ) { 1168 return expr; 1169 } 1170 1171 void Pass1::premutate( AddressExpr * ) { visit_children = false; } 1172 Expression * Pass1::postmutate( AddressExpr * addrExpr ) { 1198 1173 assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() ); 1199 1174 … … 1215 1190 // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward 1216 1191 // out of the if condition. 1217 addrExpr-> set_arg( mutateExpression( addrExpr->get_arg() ));1192 addrExpr->arg = addrExpr->get_arg()->acceptMutator( *visitor ); 1218 1193 // ... but must happen after mutate, since argument might change (e.g. intrinsic *?, ?[?]) - re-evaluate above comment 1219 1194 bool polytype = isPolyType( addrExpr->get_arg()->get_result(), scopeTyVars, env ); … … 1230 1205 } 1231 1206 1232 Statement * Pass1::mutate( ReturnStmt *returnStmt ) { 1233 if ( retval && returnStmt->get_expr() ) { 1234 assert( returnStmt->get_expr()->has_result() && ! returnStmt->get_expr()->get_result()->isVoid() ); 1235 delete returnStmt->get_expr(); 1236 returnStmt->set_expr( 0 ); 1237 } else { 1238 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); 1207 void Pass1::premutate( ReturnStmt *returnStmt ) { 1208 if ( retval && returnStmt->expr ) { 1209 assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() ); 1210 delete returnStmt->expr; 1211 returnStmt->expr = nullptr; 1239 1212 } // if 1240 return returnStmt; 1241 } 1242 1243 Type * Pass1::mutate( PointerType *pointerType ) { 1244 scopeTyVars.beginScope(); 1213 } 1214 1215 void Pass1::premutate( PointerType *pointerType ) { 1216 GuardScope( scopeTyVars ); 1245 1217 makeTyVarMap( pointerType, scopeTyVars ); 1246 1247 Type *ret = Mutator::mutate( pointerType ); 1248 1249 scopeTyVars.endScope(); 1250 return ret; 1251 } 1252 1253 Type * Pass1::mutate( FunctionType *functionType ) { 1254 scopeTyVars.beginScope(); 1218 } 1219 1220 void Pass1::premutate( FunctionType *functionType ) { 1221 GuardScope( scopeTyVars ); 1255 1222 makeTyVarMap( functionType, scopeTyVars ); 1256 1257 Type *ret = Mutator::mutate( functionType ); 1258 1259 scopeTyVars.endScope(); 1260 return ret; 1261 } 1262 1263 void Pass1::doBeginScope() { 1223 } 1224 1225 void Pass1::beginScope() { 1264 1226 adapters.beginScope(); 1265 1227 } 1266 1228 1267 void Pass1:: doEndScope() {1229 void Pass1::endScope() { 1268 1230 adapters.endScope(); 1269 1231 } … … 1467 1429 1468 1430 PolyGenericCalculator::PolyGenericCalculator() 1469 : knownLayouts(), knownOffsets(), bufNamer( "_buf" ) , scopeTyVars( TypeDecl::Data{} ){}1431 : knownLayouts(), knownOffsets(), bufNamer( "_buf" ) {} 1470 1432 1471 1433 void PolyGenericCalculator::beginTypeScope( Type *ty ) {
Note: See TracChangeset
for help on using the changeset viewer.