Changeset 74bba15 for src/GenPoly/Specialize.cc
- Timestamp:
- Sep 26, 2017, 5:19:32 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:
- 5dc26f5
- Parents:
- af58ee0 (diff), a7d151f (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/Specialize.cc
raf58ee0 r74bba15 22 22 #include <utility> // for pair 23 23 24 #include "Common/PassVisitor.h" 24 25 #include "Common/SemanticError.h" // for SemanticError 25 26 #include "Common/UniqueName.h" // for UniqueName … … 28 29 #include "InitTweak/InitTweak.h" // for isIntrinsicCallExpr 29 30 #include "Parser/LinkageSpec.h" // for C 30 #include "PolyMutator.h" // for PolyMutator31 31 #include "ResolvExpr/FindOpenVars.h" // for findOpenVars 32 32 #include "ResolvExpr/TypeEnvironment.h" // for OpenVarSet, AssertionSet … … 43 43 44 44 namespace GenPoly { 45 class Specialize final : public PolyMutator { 46 public: 47 using PolyMutator::mutate; 48 virtual Expression * mutate( ApplicationExpr *applicationExpr ) override; 49 virtual Expression * mutate( AddressExpr *castExpr ) override; 50 virtual Expression * mutate( CastExpr *castExpr ) override; 51 // virtual Expression * mutate( LogicalExpr *logicalExpr ); 52 // virtual Expression * mutate( ConditionalExpr *conditionalExpr ); 53 // virtual Expression * mutate( CommaExpr *commaExpr ); 45 struct Specialize final : public WithTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> { 46 Expression * postmutate( ApplicationExpr *applicationExpr ); 47 Expression * postmutate( AddressExpr *castExpr ); 48 Expression * postmutate( CastExpr *castExpr ); 54 49 55 50 void handleExplicitParams( ApplicationExpr *appExpr ); … … 204 199 } 205 200 206 struct EnvTrimmer : public Visitor{201 struct EnvTrimmer { 207 202 TypeSubstitution * env, * newEnv; 208 203 EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} 209 v irtual voidvisit( TypeDecl * tyDecl ) {204 void previsit( TypeDecl * tyDecl ) { 210 205 // transfer known bindings for seen type variables 211 if ( Type * t = env->lookup( tyDecl-> get_name()) ) {212 newEnv->add( tyDecl-> get_name(), t );206 if ( Type * t = env->lookup( tyDecl->name ) ) { 207 newEnv->add( tyDecl->name, t ); 213 208 } 214 209 } … … 219 214 if ( env ) { 220 215 TypeSubstitution * newEnv = new TypeSubstitution(); 221 EnvTrimmertrimmer( env, newEnv );216 PassVisitor<EnvTrimmer> trimmer( env, newEnv ); 222 217 expr->accept( trimmer ); 223 218 return newEnv; … … 277 272 std::string oldParamPrefix = paramPrefix; 278 273 paramPrefix += "p"; 279 // save stmtsToAdd in oldStmts274 // save stmtsToAddBefore in oldStmts 280 275 std::list< Statement* > oldStmts; 281 oldStmts.splice( oldStmts.end(), stmtsToAdd );282 mutate( appExpr );276 oldStmts.splice( oldStmts.end(), stmtsToAddBefore ); 277 appExpr->acceptMutator( *visitor ); 283 278 paramPrefix = oldParamPrefix; 284 279 // write any statements added for recursive specializations into the thunk body 285 thunkFunc-> get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd);286 // restore oldStmts into stmtsToAdd 287 stmtsToAdd .splice( stmtsToAdd.end(), oldStmts );280 thunkFunc->statements->kids.splice( thunkFunc->statements->kids.end(), stmtsToAddBefore ); 281 // restore oldStmts into stmtsToAddBefore 282 stmtsToAddBefore.splice( stmtsToAddBefore.end(), oldStmts ); 288 283 289 284 // add return (or valueless expression) to the thunk 290 285 Statement *appStmt; 291 if ( funType-> get_returnVals().empty() ) {286 if ( funType->returnVals.empty() ) { 292 287 appStmt = new ExprStmt( noLabels, appExpr ); 293 288 } else { 294 289 appStmt = new ReturnStmt( noLabels, appExpr ); 295 290 } // if 296 thunkFunc-> get_statements()->get_kids().push_back( appStmt );291 thunkFunc->statements->kids.push_back( appStmt ); 297 292 298 293 // add thunk definition to queue of statements to add 299 stmtsToAdd .push_back( new DeclStmt( noLabels, thunkFunc ) );294 stmtsToAddBefore.push_back( new DeclStmt( noLabels, thunkFunc ) ); 300 295 // return address of thunk function as replacement expression 301 296 return new AddressExpr( new VariableExpr( thunkFunc ) ); … … 304 299 void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) { 305 300 // create thunks for the explicit parameters 306 assert( appExpr-> get_function()->has_result());307 FunctionType *function = getFunctionType( appExpr-> get_function()->get_result());301 assert( appExpr->function->result ); 302 FunctionType *function = getFunctionType( appExpr->function->result ); 308 303 assert( function ); 309 304 std::list< DeclarationWithType* >::iterator formal; 310 305 std::list< Expression* >::iterator actual; 311 306 for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) { 312 *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() ); 313 } 314 } 315 316 Expression * Specialize::mutate( ApplicationExpr *appExpr ) { 317 appExpr->get_function()->acceptMutator( *this ); 318 mutateAll( appExpr->get_args(), *this ); 319 307 *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() ); 308 } 309 } 310 311 Expression * Specialize::postmutate( ApplicationExpr *appExpr ) { 320 312 if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) { 321 313 // create thunks for the inferred parameters … … 331 323 } 332 324 333 Expression * Specialize::mutate( AddressExpr *addrExpr ) { 334 addrExpr->get_arg()->acceptMutator( *this ); 335 assert( addrExpr->has_result() ); 336 addrExpr->set_arg( doSpecialization( addrExpr->get_result(), addrExpr->get_arg() ) ); 325 Expression * Specialize::postmutate( AddressExpr *addrExpr ) { 326 assert( addrExpr->result ); 327 addrExpr->set_arg( doSpecialization( addrExpr->result, addrExpr->arg ) ); 337 328 return addrExpr; 338 329 } 339 330 340 Expression * Specialize::mutate( CastExpr *castExpr ) { 341 castExpr->get_arg()->acceptMutator( *this ); 342 if ( castExpr->get_result()->isVoid() ) { 331 Expression * Specialize::postmutate( CastExpr *castExpr ) { 332 if ( castExpr->result->isVoid() ) { 343 333 // can't specialize if we don't have a return value 344 334 return castExpr; 345 335 } 346 Expression *specialized = doSpecialization( castExpr-> get_result(), castExpr->get_arg());347 if ( specialized != castExpr-> get_arg()) {336 Expression *specialized = doSpecialization( castExpr->result, castExpr->arg ); 337 if ( specialized != castExpr->arg ) { 348 338 // assume here that the specialization incorporates the cast 349 339 return specialized; … … 353 343 } 354 344 355 // Removing these for now. Richard put these in for some reason, but it's not clear why.356 // In particular, copy constructors produce a comma expression, and with this code the parts357 // of that comma expression are not specialized, which causes problems.358 359 // Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {360 // return logicalExpr;361 // }362 363 // Expression * Specialize::mutate( ConditionalExpr *condExpr ) {364 // return condExpr;365 // }366 367 // Expression * Specialize::mutate( CommaExpr *commaExpr ) {368 // return commaExpr;369 // }370 371 345 void convertSpecializations( std::list< Declaration* >& translationUnit ) { 372 Specializespec;346 PassVisitor<Specialize> spec; 373 347 mutateAll( translationUnit, spec ); 374 348 }
Note: See TracChangeset
for help on using the changeset viewer.