Changeset cf90b88
- Timestamp:
- Sep 25, 2017, 6:15:03 PM (6 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:
- fc72845d
- Parents:
- dc2334c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Specialize.cc
rdc2334c rcf90b88 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 … … 43 44 44 45 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 ); 46 struct Specialize final : public WithTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> { 47 Expression * postmutate( ApplicationExpr *applicationExpr ); 48 Expression * postmutate( AddressExpr *castExpr ); 49 Expression * postmutate( CastExpr *castExpr ); 54 50 55 51 void handleExplicitParams( ApplicationExpr *appExpr ); … … 204 200 } 205 201 206 struct EnvTrimmer : public Visitor{202 struct EnvTrimmer { 207 203 TypeSubstitution * env, * newEnv; 208 204 EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} 209 v irtual voidvisit( TypeDecl * tyDecl ) {205 void previsit( TypeDecl * tyDecl ) { 210 206 // transfer known bindings for seen type variables 211 if ( Type * t = env->lookup( tyDecl-> get_name()) ) {212 newEnv->add( tyDecl-> get_name(), t );207 if ( Type * t = env->lookup( tyDecl->name ) ) { 208 newEnv->add( tyDecl->name, t ); 213 209 } 214 210 } … … 219 215 if ( env ) { 220 216 TypeSubstitution * newEnv = new TypeSubstitution(); 221 EnvTrimmertrimmer( env, newEnv );217 PassVisitor<EnvTrimmer> trimmer( env, newEnv ); 222 218 expr->accept( trimmer ); 223 219 return newEnv; … … 277 273 std::string oldParamPrefix = paramPrefix; 278 274 paramPrefix += "p"; 279 // save stmtsToAdd in oldStmts275 // save stmtsToAddBefore in oldStmts 280 276 std::list< Statement* > oldStmts; 281 oldStmts.splice( oldStmts.end(), stmtsToAdd );282 mutate( appExpr );277 oldStmts.splice( oldStmts.end(), stmtsToAddBefore ); 278 appExpr->acceptMutator( *visitor ); 283 279 paramPrefix = oldParamPrefix; 284 280 // 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 );281 thunkFunc->statements->kids.splice( thunkFunc->statements->kids.end(), stmtsToAddBefore ); 282 // restore oldStmts into stmtsToAddBefore 283 stmtsToAddBefore.splice( stmtsToAddBefore.end(), oldStmts ); 288 284 289 285 // add return (or valueless expression) to the thunk 290 286 Statement *appStmt; 291 if ( funType-> get_returnVals().empty() ) {287 if ( funType->returnVals.empty() ) { 292 288 appStmt = new ExprStmt( noLabels, appExpr ); 293 289 } else { 294 290 appStmt = new ReturnStmt( noLabels, appExpr ); 295 291 } // if 296 thunkFunc-> get_statements()->get_kids().push_back( appStmt );292 thunkFunc->statements->kids.push_back( appStmt ); 297 293 298 294 // add thunk definition to queue of statements to add 299 stmtsToAdd .push_back( new DeclStmt( noLabels, thunkFunc ) );295 stmtsToAddBefore.push_back( new DeclStmt( noLabels, thunkFunc ) ); 300 296 // return address of thunk function as replacement expression 301 297 return new AddressExpr( new VariableExpr( thunkFunc ) ); … … 304 300 void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) { 305 301 // create thunks for the explicit parameters 306 assert( appExpr-> get_function()->has_result());307 FunctionType *function = getFunctionType( appExpr-> get_function()->get_result());302 assert( appExpr->function->result ); 303 FunctionType *function = getFunctionType( appExpr->function->result ); 308 304 assert( function ); 309 305 std::list< DeclarationWithType* >::iterator formal; 310 306 std::list< Expression* >::iterator actual; 311 307 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 308 *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() ); 309 } 310 } 311 312 Expression * Specialize::postmutate( ApplicationExpr *appExpr ) { 320 313 if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) { 321 314 // create thunks for the inferred parameters … … 331 324 } 332 325 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() ) ); 326 Expression * Specialize::postmutate( AddressExpr *addrExpr ) { 327 assert( addrExpr->result ); 328 addrExpr->set_arg( doSpecialization( addrExpr->result, addrExpr->arg ) ); 337 329 return addrExpr; 338 330 } 339 331 340 Expression * Specialize::mutate( CastExpr *castExpr ) { 341 castExpr->get_arg()->acceptMutator( *this ); 342 if ( castExpr->get_result()->isVoid() ) { 332 Expression * Specialize::postmutate( CastExpr *castExpr ) { 333 if ( castExpr->result->isVoid() ) { 343 334 // can't specialize if we don't have a return value 344 335 return castExpr; 345 336 } 346 Expression *specialized = doSpecialization( castExpr-> get_result(), castExpr->get_arg());347 if ( specialized != castExpr-> get_arg()) {337 Expression *specialized = doSpecialization( castExpr->result, castExpr->arg ); 338 if ( specialized != castExpr->arg ) { 348 339 // assume here that the specialization incorporates the cast 349 340 return specialized; … … 353 344 } 354 345 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 346 void convertSpecializations( std::list< Declaration* >& translationUnit ) { 372 Specializespec;347 PassVisitor<Specialize> spec; 373 348 mutateAll( translationUnit, spec ); 374 349 }
Note: See TracChangeset
for help on using the changeset viewer.