| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // Box.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue May 19 07:31:41 2015 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <set> | 
|---|
| 17 | #include <stack> | 
|---|
| 18 | #include <string> | 
|---|
| 19 | #include <iterator> | 
|---|
| 20 | #include <algorithm> | 
|---|
| 21 | #include <cassert> | 
|---|
| 22 |  | 
|---|
| 23 | #include "Box.h" | 
|---|
| 24 | #include "PolyMutator.h" | 
|---|
| 25 | #include "FindFunction.h" | 
|---|
| 26 | #include "ScrubTyVars.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "SynTree/Declaration.h" | 
|---|
| 29 | #include "SynTree/Type.h" | 
|---|
| 30 | #include "SynTree/Expression.h" | 
|---|
| 31 | #include "SynTree/Initializer.h" | 
|---|
| 32 | #include "SynTree/Statement.h" | 
|---|
| 33 | #include "SynTree/Mutator.h" | 
|---|
| 34 | #include "ResolvExpr/TypeEnvironment.h" | 
|---|
| 35 | #include "SymTab/Mangler.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "SemanticError.h" | 
|---|
| 38 | #include "UniqueName.h" | 
|---|
| 39 | #include "utility.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include <ext/functional> // temporary | 
|---|
| 42 |  | 
|---|
| 43 | namespace GenPoly { | 
|---|
| 44 | namespace { | 
|---|
| 45 | const std::list<Label> noLabels; | 
|---|
| 46 |  | 
|---|
| 47 | class Pass1 : public PolyMutator { | 
|---|
| 48 | public: | 
|---|
| 49 | Pass1(); | 
|---|
| 50 | virtual Expression *mutate( ApplicationExpr *appExpr ); | 
|---|
| 51 | virtual Expression *mutate( AddressExpr *addrExpr ); | 
|---|
| 52 | virtual Expression *mutate( UntypedExpr *expr ); | 
|---|
| 53 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ); | 
|---|
| 54 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); | 
|---|
| 55 | virtual Expression *mutate( CommaExpr *commaExpr ); | 
|---|
| 56 | virtual Expression *mutate( ConditionalExpr *condExpr ); | 
|---|
| 57 | virtual Statement *mutate(ReturnStmt *catchStmt); | 
|---|
| 58 | virtual Type *mutate( PointerType *pointerType ); | 
|---|
| 59 | virtual Type *mutate( FunctionType *pointerType ); | 
|---|
| 60 |  | 
|---|
| 61 | virtual void doBeginScope(); | 
|---|
| 62 | virtual void doEndScope(); | 
|---|
| 63 | private: | 
|---|
| 64 | void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
| 65 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); | 
|---|
| 66 | Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ); | 
|---|
| 67 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
| 68 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars ); | 
|---|
| 69 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
| 70 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ); | 
|---|
| 71 | void findAssignOps( const std::list< TypeDecl *> &forall ); | 
|---|
| 72 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ); | 
|---|
| 73 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); | 
|---|
| 74 | Expression *handleIntrinsics( ApplicationExpr *appExpr ); | 
|---|
| 75 | ObjectDecl *makeTemporary( Type *type ); | 
|---|
| 76 |  | 
|---|
| 77 | std::map< std::string, DeclarationWithType *> assignOps; | 
|---|
| 78 | typedef std::map< std::string, FunctionDecl *> AdapterMap; | 
|---|
| 79 | std::stack< AdapterMap > adapters; | 
|---|
| 80 | DeclarationWithType *retval; | 
|---|
| 81 | bool useRetval; | 
|---|
| 82 | UniqueName tempNamer; | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | class Pass2 : public PolyMutator { | 
|---|
| 86 | public: | 
|---|
| 87 | Pass2(); | 
|---|
| 88 | template< typename DeclClass > | 
|---|
| 89 | DeclClass *handleDecl( DeclClass *decl, Type *type ); | 
|---|
| 90 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); | 
|---|
| 91 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); | 
|---|
| 92 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); | 
|---|
| 93 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl ); | 
|---|
| 94 | virtual Type *mutate( PointerType *pointerType ); | 
|---|
| 95 | virtual Type *mutate( FunctionType *funcType ); | 
|---|
| 96 | private: | 
|---|
| 97 | void addAdapters( FunctionType *functionType ); | 
|---|
| 98 |  | 
|---|
| 99 | std::map< UniqueId, std::string > adapterName; | 
|---|
| 100 | }; | 
|---|
| 101 |  | 
|---|
| 102 | class Pass3 : public PolyMutator { | 
|---|
| 103 | public: | 
|---|
| 104 | template< typename DeclClass > | 
|---|
| 105 | DeclClass *handleDecl( DeclClass *decl, Type *type ); | 
|---|
| 106 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); | 
|---|
| 107 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); | 
|---|
| 108 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ); | 
|---|
| 109 | virtual TypeDecl *mutate( TypeDecl *objectDecl ); | 
|---|
| 110 | virtual Statement *mutate( DeclStmt *declStmt ); | 
|---|
| 111 | virtual Type *mutate( PointerType *pointerType ); | 
|---|
| 112 | virtual Type *mutate( FunctionType *funcType ); | 
|---|
| 113 | private: | 
|---|
| 114 | }; | 
|---|
| 115 |  | 
|---|
| 116 | } // anonymous namespace | 
|---|
| 117 |  | 
|---|
| 118 | void printAllNotBuiltin( const std::list< Declaration *>& translationUnit, std::ostream &os ) { | 
|---|
| 119 | for ( std::list< Declaration *>::const_iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { | 
|---|
| 120 | if ( ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) { | 
|---|
| 121 | (*i)->print( os ); | 
|---|
| 122 | os << std::endl; | 
|---|
| 123 | } // if | 
|---|
| 124 | } // for | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void box( std::list< Declaration *>& translationUnit ) { | 
|---|
| 128 | Pass1 pass1; | 
|---|
| 129 | Pass2 pass2; | 
|---|
| 130 | Pass3 pass3; | 
|---|
| 131 | mutateAll( translationUnit, pass1 ); | 
|---|
| 132 | mutateAll( translationUnit, pass2 ); | 
|---|
| 133 | mutateAll( translationUnit, pass3 ); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | ////////////////////////////////////////// Pass1 //////////////////////////////////////////////////// | 
|---|
| 137 |  | 
|---|
| 138 | namespace { | 
|---|
| 139 | std::string makeAdapterName( const std::string &mangleName ) { | 
|---|
| 140 | return "_adapter" + mangleName; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) { | 
|---|
| 144 | bool doTransform = false; | 
|---|
| 145 | if ( ! function->get_returnVals().empty() ) { | 
|---|
| 146 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) { | 
|---|
| 147 |  | 
|---|
| 148 | // figure out if the return type is specified by a type parameter | 
|---|
| 149 | for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) { | 
|---|
| 150 | if ( (*tyVar)->get_name() == typeInst->get_name() ) { | 
|---|
| 151 | doTransform = true; | 
|---|
| 152 | name = typeInst->get_name(); | 
|---|
| 153 | break; | 
|---|
| 154 | } // if | 
|---|
| 155 | } // for | 
|---|
| 156 | if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) { | 
|---|
| 157 | doTransform = true; | 
|---|
| 158 | } // if | 
|---|
| 159 | } // if | 
|---|
| 160 | } // if | 
|---|
| 161 | return doTransform; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | bool isPolyRet( FunctionType *function, std::string &name ) { | 
|---|
| 165 | TyVarMap dummyTyVars; | 
|---|
| 166 | return isPolyRet( function, name, dummyTyVars ); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | Pass1::Pass1() | 
|---|
| 170 | : useRetval( false ), tempNamer( "_temp" ) { | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | bool checkAssignment( DeclarationWithType *decl, std::string &name ) { | 
|---|
| 174 | if ( decl->get_name() == "?=?" ) { | 
|---|
| 175 | if ( PointerType *ptrType = dynamic_cast< PointerType *>( decl->get_type() ) ) { | 
|---|
| 176 | if ( FunctionType *funType = dynamic_cast< FunctionType *>( ptrType->get_base() ) ) { | 
|---|
| 177 | if ( funType->get_parameters().size() == 2 ) { | 
|---|
| 178 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { | 
|---|
| 179 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { | 
|---|
| 180 | name = typeInst->get_name(); | 
|---|
| 181 | return true; | 
|---|
| 182 | } // if | 
|---|
| 183 | } // if | 
|---|
| 184 | } // if | 
|---|
| 185 | } // if | 
|---|
| 186 | } // if | 
|---|
| 187 | } // if | 
|---|
| 188 | return false; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) { | 
|---|
| 192 | assignOps.clear(); | 
|---|
| 193 | for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) { | 
|---|
| 194 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { | 
|---|
| 195 | std::string typeName; | 
|---|
| 196 | if ( checkAssignment( *assert, typeName ) ) { | 
|---|
| 197 | assignOps[ typeName ] = *assert; | 
|---|
| 198 | } // if | 
|---|
| 199 | } // for | 
|---|
| 200 | } // for | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | DeclarationWithType * | 
|---|
| 204 | Pass1::mutate( FunctionDecl *functionDecl ) { | 
|---|
| 205 | if ( functionDecl->get_statements() ) { | 
|---|
| 206 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 207 | DeclarationWithType *oldRetval = retval; | 
|---|
| 208 | bool oldUseRetval = useRetval; | 
|---|
| 209 |  | 
|---|
| 210 | retval = 0; | 
|---|
| 211 | std::string typeName; | 
|---|
| 212 | if ( isPolyRet( functionDecl->get_functionType(), typeName ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) { | 
|---|
| 213 | retval = functionDecl->get_functionType()->get_returnVals().front(); | 
|---|
| 214 |  | 
|---|
| 215 | // give names to unnamed return values | 
|---|
| 216 | if ( retval->get_name() == "" ) { | 
|---|
| 217 | retval->set_name( "_retparm" ); | 
|---|
| 218 | retval->set_linkage( LinkageSpec::C ); | 
|---|
| 219 | } // if | 
|---|
| 220 | } // if | 
|---|
| 221 |  | 
|---|
| 222 | scopeTyVars.clear(); | 
|---|
| 223 | ///     std::cerr << "clear\n"; | 
|---|
| 224 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); | 
|---|
| 225 | findAssignOps( functionDecl->get_functionType()->get_forall() ); | 
|---|
| 226 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); | 
|---|
| 227 |  | 
|---|
| 228 | scopeTyVars = oldtyVars; | 
|---|
| 229 | ///     std::cerr << "end FunctionDecl: "; | 
|---|
| 230 | ///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { | 
|---|
| 231 | ///       std::cerr << i->first << " "; | 
|---|
| 232 | ///     } | 
|---|
| 233 | ///     std::cerr << "\n"; | 
|---|
| 234 | retval = oldRetval; | 
|---|
| 235 | useRetval = oldUseRetval; | 
|---|
| 236 | // doEndScope(); | 
|---|
| 237 | } // if | 
|---|
| 238 | return functionDecl; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { | 
|---|
| 242 | ///     std::cerr << "add " << typeDecl->get_name() << "\n"; | 
|---|
| 243 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); | 
|---|
| 244 | return Mutator::mutate( typeDecl ); | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | Expression *Pass1::mutate( CommaExpr *commaExpr ) { | 
|---|
| 248 | bool oldUseRetval = useRetval; | 
|---|
| 249 | useRetval = false; | 
|---|
| 250 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) ); | 
|---|
| 251 | useRetval = oldUseRetval; | 
|---|
| 252 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) ); | 
|---|
| 253 | return commaExpr; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) { | 
|---|
| 257 | bool oldUseRetval = useRetval; | 
|---|
| 258 | useRetval = false; | 
|---|
| 259 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) ); | 
|---|
| 260 | useRetval = oldUseRetval; | 
|---|
| 261 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) ); | 
|---|
| 262 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) ); | 
|---|
| 263 | return condExpr; | 
|---|
| 264 |  | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { | 
|---|
| 268 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { | 
|---|
| 269 | ResolvExpr::EqvClass eqvClass; | 
|---|
| 270 | assert( env ); | 
|---|
| 271 | if ( tyParm->second == TypeDecl::Any ) { | 
|---|
| 272 | Type *concrete = env->lookup( tyParm->first ); | 
|---|
| 273 | if ( concrete ) { | 
|---|
| 274 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); | 
|---|
| 275 | arg++; | 
|---|
| 276 | } else { | 
|---|
| 277 | throw SemanticError( "unbound type variable in application ", appExpr ); | 
|---|
| 278 | } // if | 
|---|
| 279 | } // if | 
|---|
| 280 | } // for | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | ObjectDecl *Pass1::makeTemporary( Type *type ) { | 
|---|
| 284 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Declaration::NoStorageClass, LinkageSpec::C, 0, type, 0 ); | 
|---|
| 285 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); | 
|---|
| 286 | return newObj; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | TypeInstType *isPolyType( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) { | 
|---|
| 290 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { | 
|---|
| 291 | if ( env ) { | 
|---|
| 292 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { | 
|---|
| 293 | return isPolyType( newType, env, tyVars ); | 
|---|
| 294 | } // if | 
|---|
| 295 | } // if | 
|---|
| 296 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { | 
|---|
| 297 | return typeInst; | 
|---|
| 298 | } else { | 
|---|
| 299 | return 0; | 
|---|
| 300 | } // if | 
|---|
| 301 | } else { | 
|---|
| 302 | return 0; | 
|---|
| 303 | } // if | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { | 
|---|
| 307 | if ( useRetval ) { | 
|---|
| 308 | assert( retval ); | 
|---|
| 309 | arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); | 
|---|
| 310 | arg++; | 
|---|
| 311 | } else { | 
|---|
| 312 | ObjectDecl *newObj = makeTemporary( retType->clone() ); | 
|---|
| 313 | Expression *paramExpr = new VariableExpr( newObj ); | 
|---|
| 314 | if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) { | 
|---|
| 315 | paramExpr = new AddressExpr( paramExpr ); | 
|---|
| 316 | } // if | 
|---|
| 317 | arg = appExpr->get_args().insert( arg, paramExpr ); | 
|---|
| 318 | arg++; | 
|---|
| 319 | ///     stmtsToAdd.push_back( new ExprStmt( noLabels, appExpr ) ); | 
|---|
| 320 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); | 
|---|
| 321 | commaExpr->set_env( appExpr->get_env() ); | 
|---|
| 322 | appExpr->set_env( 0 ); | 
|---|
| 323 | return commaExpr; | 
|---|
| 324 | } // if | 
|---|
| 325 | return appExpr; | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ) { | 
|---|
| 329 | ResolvExpr::EqvClass eqvClass; | 
|---|
| 330 | assert( env ); | 
|---|
| 331 | Type *concrete = env->lookup( typeName ); | 
|---|
| 332 | if ( concrete == 0 ) { | 
|---|
| 333 | throw SemanticError( "Unbound type variable " + typeName + " in ", appExpr ); | 
|---|
| 334 | } // if | 
|---|
| 335 | return addRetParam( appExpr, function, concrete, arg ); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { | 
|---|
| 339 | Expression *ret = appExpr; | 
|---|
| 340 | if ( ! function->get_returnVals().empty() && isPolyVal( function->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
| 341 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); | 
|---|
| 342 | } // if | 
|---|
| 343 | std::string mangleName = SymTab::Mangler::mangle( function ); | 
|---|
| 344 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 345 |  | 
|---|
| 346 | appExpr->get_args().push_front( appExpr->get_function() ); | 
|---|
| 347 | appExpr->set_function( new NameExpr( adapterName ) ); | 
|---|
| 348 |  | 
|---|
| 349 | return ret; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { | 
|---|
| 353 | assert( ! arg->get_results().empty() ); | 
|---|
| 354 | ///   if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) { | 
|---|
| 355 | TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param ); | 
|---|
| 356 | if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) { | 
|---|
| 357 | if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) { | 
|---|
| 358 | // if the argument's type is a type parameter, we don't need to box again! | 
|---|
| 359 | return; | 
|---|
| 360 | } else if ( arg->get_results().front()->get_isLvalue() ) { | 
|---|
| 361 | // VariableExpr and MemberExpr are lvalues | 
|---|
| 362 | arg = new AddressExpr( arg ); | 
|---|
| 363 | } else { | 
|---|
| 364 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Declaration::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 ); | 
|---|
| 365 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); | 
|---|
| 366 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); | 
|---|
| 367 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
| 368 | assign->get_args().push_back( new VariableExpr( newObj ) ); | 
|---|
| 369 | assign->get_args().push_back( arg ); | 
|---|
| 370 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) ); | 
|---|
| 371 | arg = new AddressExpr( new VariableExpr( newObj ) ); | 
|---|
| 372 | } // if | 
|---|
| 373 | } // if | 
|---|
| 374 | ///   } | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) { | 
|---|
| 378 | Type *newType = formal->clone(); | 
|---|
| 379 | std::list< FunctionType *> functions; | 
|---|
| 380 | // instead of functions needing adapters, this really ought to look for | 
|---|
| 381 | // any function mentioning a polymorphic type | 
|---|
| 382 | findAndReplaceFunction( newType, functions, tyVars, needsAdapter ); | 
|---|
| 383 | if ( ! functions.empty() ) { | 
|---|
| 384 | actual = new CastExpr( actual, newType ); | 
|---|
| 385 | } else { | 
|---|
| 386 | delete newType; | 
|---|
| 387 | } // if | 
|---|
| 388 | } | 
|---|
| 389 |  | 
|---|
| 390 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { | 
|---|
| 391 | ///   std::cout << "function is "; | 
|---|
| 392 | ///   function->print( std::cout ); | 
|---|
| 393 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { | 
|---|
| 394 | ///     std::cout << "parameter is "; | 
|---|
| 395 | ///     (*param)->print( std::fcout ); | 
|---|
| 396 | ///     std::cout << std::endl << "argument is "; | 
|---|
| 397 | ///     (*arg)->print( std::cout ); | 
|---|
| 398 | assert( arg != appExpr->get_args().end() ); | 
|---|
| 399 | addCast( *arg, (*param)->get_type(), exprTyVars ); | 
|---|
| 400 | boxParam( (*param)->get_type(), *arg, exprTyVars ); | 
|---|
| 401 | } // for | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { | 
|---|
| 405 | std::list< Expression *>::iterator cur = arg; | 
|---|
| 406 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { | 
|---|
| 407 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { | 
|---|
| 408 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() ); | 
|---|
| 409 | assert( inferParam != appExpr->get_inferParams().end() ); | 
|---|
| 410 | Expression *newExpr = inferParam->second.expr->clone(); | 
|---|
| 411 | addCast( newExpr, (*assert)->get_type(), tyVars ); | 
|---|
| 412 | boxParam( (*assert)->get_type(), newExpr, tyVars ); | 
|---|
| 413 | appExpr->get_args().insert( cur, newExpr ); | 
|---|
| 414 | } // for | 
|---|
| 415 | } // for | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | void makeRetParm( FunctionType *funcType ) { | 
|---|
| 419 | DeclarationWithType *retParm = funcType->get_returnVals().front(); | 
|---|
| 420 |  | 
|---|
| 421 | // make a new parameter that is a pointer to the type of the old return value | 
|---|
| 422 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); | 
|---|
| 423 | funcType->get_parameters().push_front( retParm ); | 
|---|
| 424 |  | 
|---|
| 425 | // we don't need the return value any more | 
|---|
| 426 | funcType->get_returnVals().clear(); | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) { | 
|---|
| 430 | // actually make the adapter type | 
|---|
| 431 | FunctionType *adapter = adaptee->clone(); | 
|---|
| 432 | if ( ! adapter->get_returnVals().empty() && isPolyVal( adapter->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
| 433 | makeRetParm( adapter ); | 
|---|
| 434 | } // if | 
|---|
| 435 | adapter->get_parameters().push_front( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) ); | 
|---|
| 436 | return adapter; | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { | 
|---|
| 440 | assert( param ); | 
|---|
| 441 | assert( arg ); | 
|---|
| 442 | ///   std::cout << "arg type is "; | 
|---|
| 443 | ///   arg->get_type()->print( std::cout ); | 
|---|
| 444 | ///   std::cout << "param type is "; | 
|---|
| 445 | ///   param->get_type()->print( std::cout ); | 
|---|
| 446 | ///   std::cout << " tyVars are: "; | 
|---|
| 447 | ///   printTyVarMap( std::cout, tyVars ); | 
|---|
| 448 | if ( isPolyVal( realParam->get_type(), tyVars ) ) { | 
|---|
| 449 | ///     if ( dynamic_cast< PointerType *>( arg->get_type() ) ) { | 
|---|
| 450 | ///       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() ); | 
|---|
| 451 | ///     } else { | 
|---|
| 452 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
| 453 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); | 
|---|
| 454 | deref->get_results().push_back( arg->get_type()->clone() ); | 
|---|
| 455 | return deref; | 
|---|
| 456 | ///     } | 
|---|
| 457 | } // if | 
|---|
| 458 | return new VariableExpr( param ); | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) { | 
|---|
| 462 | UniqueName paramNamer( "_p" ); | 
|---|
| 463 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { | 
|---|
| 464 | if ( (*param)->get_name() == "" ) { | 
|---|
| 465 | (*param)->set_name( paramNamer.newName() ); | 
|---|
| 466 | (*param)->set_linkage( LinkageSpec::C ); | 
|---|
| 467 | } // if | 
|---|
| 468 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) ); | 
|---|
| 469 | } // for | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 |  | 
|---|
| 473 |  | 
|---|
| 474 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { | 
|---|
| 475 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); | 
|---|
| 476 | adapterType = ScrubTyVars::scrub( adapterType, tyVars ); | 
|---|
| 477 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front(); | 
|---|
| 478 | adapteeDecl->set_name( "_adaptee" ); | 
|---|
| 479 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) ); | 
|---|
| 480 | Statement *bodyStmt; | 
|---|
| 481 |  | 
|---|
| 482 | std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin(); | 
|---|
| 483 | std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin(); | 
|---|
| 484 | std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin(); | 
|---|
| 485 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { | 
|---|
| 486 | assert( tyArg != realType->get_forall().end() ); | 
|---|
| 487 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin(); | 
|---|
| 488 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin(); | 
|---|
| 489 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin(); | 
|---|
| 490 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { | 
|---|
| 491 | assert( assertArg != (*tyArg)->get_assertions().end() ); | 
|---|
| 492 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) ); | 
|---|
| 493 | } // for | 
|---|
| 494 | } // for | 
|---|
| 495 |  | 
|---|
| 496 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin(); | 
|---|
| 497 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin(); | 
|---|
| 498 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin(); | 
|---|
| 499 | param++;                // skip adaptee parameter | 
|---|
| 500 | if ( realType->get_returnVals().empty() ) { | 
|---|
| 501 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
| 502 | bodyStmt = new ExprStmt( noLabels, adapteeApp ); | 
|---|
| 503 | } else if ( isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
| 504 | if ( (*param)->get_name() == "" ) { | 
|---|
| 505 | (*param)->set_name( "_ret" ); | 
|---|
| 506 | (*param)->set_linkage( LinkageSpec::C ); | 
|---|
| 507 | } // if | 
|---|
| 508 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
| 509 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
| 510 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) ); | 
|---|
| 511 | assign->get_args().push_back( deref ); | 
|---|
| 512 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
| 513 | assign->get_args().push_back( adapteeApp ); | 
|---|
| 514 | bodyStmt = new ExprStmt( noLabels, assign ); | 
|---|
| 515 | } else { | 
|---|
| 516 | // adapter for a function that returns a monomorphic value | 
|---|
| 517 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
| 518 | bodyStmt = new ReturnStmt( noLabels, adapteeApp ); | 
|---|
| 519 | } // if | 
|---|
| 520 | CompoundStmt *adapterBody = new CompoundStmt( noLabels ); | 
|---|
| 521 | adapterBody->get_kids().push_back( bodyStmt ); | 
|---|
| 522 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 523 | return new FunctionDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false ); | 
|---|
| 524 | } | 
|---|
| 525 |  | 
|---|
| 526 | void Pass1::passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ) { | 
|---|
| 527 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); | 
|---|
| 528 | std::list< FunctionType *> functions; | 
|---|
| 529 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { | 
|---|
| 530 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { | 
|---|
| 531 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter ); | 
|---|
| 532 | } // for | 
|---|
| 533 | } // for | 
|---|
| 534 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
| 535 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter ); | 
|---|
| 536 | } // for | 
|---|
| 537 | std::set< std::string > adaptersDone; | 
|---|
| 538 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { | 
|---|
| 539 | FunctionType *realFunction = (*funType)->clone(); | 
|---|
| 540 | assert( env ); | 
|---|
| 541 | env->apply( realFunction ); | 
|---|
| 542 |  | 
|---|
| 543 | std::string mangleName = SymTab::Mangler::mangle( realFunction ); | 
|---|
| 544 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { | 
|---|
| 545 | AdapterMap & adapters = Pass1::adapters.top(); | 
|---|
| 546 | AdapterMap::iterator adapter = adapters.find( mangleName ); | 
|---|
| 547 |  | 
|---|
| 548 | if ( needsAdapter( realFunction, exprTyVars, true ) ) { | 
|---|
| 549 | // the function still contains type variables, which means we are in a polymorphic | 
|---|
| 550 | // context and the adapter function is a parameter - call the parameter and don't | 
|---|
| 551 | // create a new adapter. | 
|---|
| 552 | appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) ); | 
|---|
| 553 | continue; | 
|---|
| 554 | } else if ( adapter == adapters.end() ) { | 
|---|
| 555 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); | 
|---|
| 556 | adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) ); | 
|---|
| 557 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); | 
|---|
| 558 | } // if | 
|---|
| 559 | assert( adapter != adapters.end() ); | 
|---|
| 560 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); | 
|---|
| 561 | // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) ); | 
|---|
| 562 | adaptersDone.insert( adaptersDone.begin(), mangleName ); | 
|---|
| 563 | } // if | 
|---|
| 564 | } // for | 
|---|
| 565 | } | 
|---|
| 566 |  | 
|---|
| 567 | TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) { | 
|---|
| 568 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
| 569 | return isPolyType( ptr->get_base(), env, tyVars ); | 
|---|
| 570 | } else if ( env ) { | 
|---|
| 571 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { | 
|---|
| 572 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { | 
|---|
| 573 | return isPolyPtr( newType, env, tyVars ); | 
|---|
| 574 | } // if | 
|---|
| 575 | } // if | 
|---|
| 576 | } // if | 
|---|
| 577 | return 0; | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | TypeInstType *isPolyPtrPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) { | 
|---|
| 581 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
| 582 | return isPolyPtr( ptr->get_base(), env, tyVars ); | 
|---|
| 583 | } else if ( env ) { | 
|---|
| 584 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { | 
|---|
| 585 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { | 
|---|
| 586 | return isPolyPtrPtr( newType, env, tyVars ); | 
|---|
| 587 | } // if | 
|---|
| 588 | } // if | 
|---|
| 589 | } // if | 
|---|
| 590 | return 0; | 
|---|
| 591 | } | 
|---|
| 592 |  | 
|---|
| 593 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, std::string polyName, bool isIncr ) { | 
|---|
| 594 | NameExpr *opExpr; | 
|---|
| 595 | if ( isIncr ) { | 
|---|
| 596 | opExpr = new NameExpr( "?+=?" ); | 
|---|
| 597 | } else { | 
|---|
| 598 | opExpr = new NameExpr( "?-=?" ); | 
|---|
| 599 | } // if | 
|---|
| 600 | UntypedExpr *addAssign = new UntypedExpr( opExpr ); | 
|---|
| 601 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { | 
|---|
| 602 | addAssign->get_args().push_back( address->get_arg() ); | 
|---|
| 603 | } else { | 
|---|
| 604 | addAssign->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| 605 | } // if | 
|---|
| 606 | addAssign->get_args().push_back( new NameExpr( polyName ) ); | 
|---|
| 607 | addAssign->get_results().front() = appExpr->get_results().front()->clone(); | 
|---|
| 608 | if ( appExpr->get_env() ) { | 
|---|
| 609 | addAssign->set_env( appExpr->get_env() ); | 
|---|
| 610 | appExpr->set_env( 0 ); | 
|---|
| 611 | } // if | 
|---|
| 612 | appExpr->get_args().clear(); | 
|---|
| 613 | delete appExpr; | 
|---|
| 614 | return addAssign; | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) { | 
|---|
| 618 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) { | 
|---|
| 619 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { | 
|---|
| 620 | if ( varExpr->get_var()->get_name() == "?[?]" ) { | 
|---|
| 621 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 622 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| 623 | TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars ); | 
|---|
| 624 | TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars ); | 
|---|
| 625 | assert( ! typeInst1 || ! typeInst2 ); | 
|---|
| 626 | UntypedExpr *ret = 0; | 
|---|
| 627 | if ( typeInst1 || typeInst2 ) { | 
|---|
| 628 | ret = new UntypedExpr( new NameExpr( "?+?" ) ); | 
|---|
| 629 | } // if | 
|---|
| 630 | if ( typeInst1 ) { | 
|---|
| 631 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
| 632 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| 633 | multiply->get_args().push_back( new NameExpr( typeInst1->get_name() ) ); | 
|---|
| 634 | ret->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| 635 | ret->get_args().push_back( multiply ); | 
|---|
| 636 | } else if ( typeInst2 ) { | 
|---|
| 637 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
| 638 | multiply->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| 639 | multiply->get_args().push_back( new NameExpr( typeInst2->get_name() ) ); | 
|---|
| 640 | ret->get_args().push_back( multiply ); | 
|---|
| 641 | ret->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| 642 | } // if | 
|---|
| 643 | if ( typeInst1 || typeInst2 ) { | 
|---|
| 644 | ret->get_results().push_front( appExpr->get_results().front()->clone() ); | 
|---|
| 645 | if ( appExpr->get_env() ) { | 
|---|
| 646 | ret->set_env( appExpr->get_env() ); | 
|---|
| 647 | appExpr->set_env( 0 ); | 
|---|
| 648 | } // if | 
|---|
| 649 | appExpr->get_args().clear(); | 
|---|
| 650 | delete appExpr; | 
|---|
| 651 | return ret; | 
|---|
| 652 | } // if | 
|---|
| 653 | } else if ( varExpr->get_var()->get_name() == "*?" ) { | 
|---|
| 654 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 655 | assert( ! appExpr->get_args().empty() ); | 
|---|
| 656 | if ( isPolyType( appExpr->get_results().front(), env, scopeTyVars ) ) { | 
|---|
| 657 | Expression *ret = appExpr->get_args().front(); | 
|---|
| 658 | delete ret->get_results().front(); | 
|---|
| 659 | ret->get_results().front() = appExpr->get_results().front()->clone(); | 
|---|
| 660 | if ( appExpr->get_env() ) { | 
|---|
| 661 | ret->set_env( appExpr->get_env() ); | 
|---|
| 662 | appExpr->set_env( 0 ); | 
|---|
| 663 | } // if | 
|---|
| 664 | appExpr->get_args().clear(); | 
|---|
| 665 | delete appExpr; | 
|---|
| 666 | return ret; | 
|---|
| 667 | } // if | 
|---|
| 668 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) { | 
|---|
| 669 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 670 | assert( appExpr->get_args().size() == 1 ); | 
|---|
| 671 | if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) { | 
|---|
| 672 | Type *tempType = appExpr->get_results().front()->clone(); | 
|---|
| 673 | if ( env ) { | 
|---|
| 674 | env->apply( tempType ); | 
|---|
| 675 | } // if | 
|---|
| 676 | ObjectDecl *newObj = makeTemporary( tempType ); | 
|---|
| 677 | VariableExpr *tempExpr = new VariableExpr( newObj ); | 
|---|
| 678 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
| 679 | assignExpr->get_args().push_back( tempExpr->clone() ); | 
|---|
| 680 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { | 
|---|
| 681 | assignExpr->get_args().push_back( address->get_arg()->clone() ); | 
|---|
| 682 | } else { | 
|---|
| 683 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); | 
|---|
| 684 | } // if | 
|---|
| 685 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, typeInst->get_name(), varExpr->get_var()->get_name() == "?++" ) ); | 
|---|
| 686 | return new CommaExpr( firstComma, tempExpr ); | 
|---|
| 687 | } // if | 
|---|
| 688 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) { | 
|---|
| 689 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 690 | assert( appExpr->get_args().size() == 1 ); | 
|---|
| 691 | if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) { | 
|---|
| 692 | return makeIncrDecrExpr( appExpr, typeInst->get_name(), varExpr->get_var()->get_name() == "++?" ); | 
|---|
| 693 | } // if | 
|---|
| 694 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { | 
|---|
| 695 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 696 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| 697 | TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars ); | 
|---|
| 698 | TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars ); | 
|---|
| 699 | if ( typeInst1 && typeInst2 ) { | 
|---|
| 700 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); | 
|---|
| 701 | divide->get_args().push_back( appExpr ); | 
|---|
| 702 | divide->get_args().push_back( new NameExpr( typeInst1->get_name() ) ); | 
|---|
| 703 | divide->get_results().push_front( appExpr->get_results().front()->clone() ); | 
|---|
| 704 | if ( appExpr->get_env() ) { | 
|---|
| 705 | divide->set_env( appExpr->get_env() ); | 
|---|
| 706 | appExpr->set_env( 0 ); | 
|---|
| 707 | } // if | 
|---|
| 708 | return divide; | 
|---|
| 709 | } else if ( typeInst1 ) { | 
|---|
| 710 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
| 711 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| 712 | multiply->get_args().push_back( new NameExpr( typeInst1->get_name() ) ); | 
|---|
| 713 | appExpr->get_args().back() = multiply; | 
|---|
| 714 | } else if ( typeInst2 ) { | 
|---|
| 715 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
| 716 | multiply->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| 717 | multiply->get_args().push_back( new NameExpr( typeInst2->get_name() ) ); | 
|---|
| 718 | appExpr->get_args().front() = multiply; | 
|---|
| 719 | } // if | 
|---|
| 720 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) { | 
|---|
| 721 | assert( ! appExpr->get_results().empty() ); | 
|---|
| 722 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| 723 | TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ); | 
|---|
| 724 | if ( typeInst ) { | 
|---|
| 725 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
| 726 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| 727 | multiply->get_args().push_back( new NameExpr( typeInst->get_name() ) ); | 
|---|
| 728 | appExpr->get_args().back() = multiply; | 
|---|
| 729 | } // if | 
|---|
| 730 | } // if | 
|---|
| 731 | return appExpr; | 
|---|
| 732 | } // if | 
|---|
| 733 | } // if | 
|---|
| 734 | return 0; | 
|---|
| 735 | } | 
|---|
| 736 |  | 
|---|
| 737 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) { | 
|---|
| 738 | ///     std::cerr << "mutate appExpr: "; | 
|---|
| 739 | ///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { | 
|---|
| 740 | ///       std::cerr << i->first << " "; | 
|---|
| 741 | ///     } | 
|---|
| 742 | ///     std::cerr << "\n"; | 
|---|
| 743 | bool oldUseRetval = useRetval; | 
|---|
| 744 | useRetval = false; | 
|---|
| 745 | appExpr->get_function()->acceptMutator( *this ); | 
|---|
| 746 | mutateAll( appExpr->get_args(), *this ); | 
|---|
| 747 | useRetval = oldUseRetval; | 
|---|
| 748 |  | 
|---|
| 749 | assert( ! appExpr->get_function()->get_results().empty() ); | 
|---|
| 750 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); | 
|---|
| 751 | assert( pointer ); | 
|---|
| 752 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); | 
|---|
| 753 | assert( function ); | 
|---|
| 754 |  | 
|---|
| 755 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { | 
|---|
| 756 | return newExpr; | 
|---|
| 757 | } // if | 
|---|
| 758 |  | 
|---|
| 759 | Expression *ret = appExpr; | 
|---|
| 760 |  | 
|---|
| 761 | std::list< Expression *>::iterator arg = appExpr->get_args().begin(); | 
|---|
| 762 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); | 
|---|
| 763 |  | 
|---|
| 764 | std::string typeName; | 
|---|
| 765 | if ( isPolyRet( function, typeName ) ) { | 
|---|
| 766 | ret = addPolyRetParam( appExpr, function, typeName, arg ); | 
|---|
| 767 | } else if ( needsAdapter( function, scopeTyVars ) ) { | 
|---|
| 768 | ///     std::cerr << "needs adapter: "; | 
|---|
| 769 | ///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { | 
|---|
| 770 | ///       std::cerr << i->first << " "; | 
|---|
| 771 | ///     } | 
|---|
| 772 | ///     std::cerr << "\n"; | 
|---|
| 773 | // change the application so it calls the adapter rather than the passed function | 
|---|
| 774 | ret = applyAdapter( appExpr, function, arg, scopeTyVars ); | 
|---|
| 775 | } // if | 
|---|
| 776 | arg = appExpr->get_args().begin(); | 
|---|
| 777 |  | 
|---|
| 778 | TyVarMap exprTyVars; | 
|---|
| 779 | makeTyVarMap( function, exprTyVars ); | 
|---|
| 780 |  | 
|---|
| 781 | passTypeVars( appExpr, arg, exprTyVars ); | 
|---|
| 782 | addInferredParams( appExpr, function, arg, exprTyVars ); | 
|---|
| 783 |  | 
|---|
| 784 | arg = paramBegin; | 
|---|
| 785 |  | 
|---|
| 786 | boxParams( appExpr, function, arg, exprTyVars ); | 
|---|
| 787 |  | 
|---|
| 788 | passAdapters( appExpr, function, exprTyVars ); | 
|---|
| 789 |  | 
|---|
| 790 | return ret; | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 | Expression *Pass1::mutate( UntypedExpr *expr ) { | 
|---|
| 794 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars ) ) { | 
|---|
| 795 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { | 
|---|
| 796 | if ( name->get_name() == "*?" ) { | 
|---|
| 797 | Expression *ret = expr->get_args().front(); | 
|---|
| 798 | expr->get_args().clear(); | 
|---|
| 799 | delete expr; | 
|---|
| 800 | return ret->acceptMutator( *this ); | 
|---|
| 801 | } // if | 
|---|
| 802 | } // if | 
|---|
| 803 | } // if | 
|---|
| 804 | return PolyMutator::mutate( expr ); | 
|---|
| 805 | } | 
|---|
| 806 |  | 
|---|
| 807 | Expression *Pass1::mutate( AddressExpr *addrExpr ) { | 
|---|
| 808 | assert( ! addrExpr->get_arg()->get_results().empty() ); | 
|---|
| 809 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); | 
|---|
| 810 | if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) ) { | 
|---|
| 811 | Expression *ret = addrExpr->get_arg(); | 
|---|
| 812 | delete ret->get_results().front(); | 
|---|
| 813 | ret->get_results().front() = addrExpr->get_results().front()->clone(); | 
|---|
| 814 | addrExpr->set_arg( 0 ); | 
|---|
| 815 | delete addrExpr; | 
|---|
| 816 | return ret; | 
|---|
| 817 | } else { | 
|---|
| 818 | return addrExpr; | 
|---|
| 819 | } // if | 
|---|
| 820 | } | 
|---|
| 821 |  | 
|---|
| 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; | 
|---|
| 851 | } // if | 
|---|
| 852 | retStmt->set_expr( 0 ); | 
|---|
| 853 | } else { | 
|---|
| 854 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); | 
|---|
| 855 | } // if | 
|---|
| 856 | return retStmt; | 
|---|
| 857 | } | 
|---|
| 858 |  | 
|---|
| 859 | Type * Pass1::mutate( PointerType *pointerType ) { | 
|---|
| 860 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 861 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
| 862 |  | 
|---|
| 863 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| 864 |  | 
|---|
| 865 | scopeTyVars = oldtyVars; | 
|---|
| 866 | return ret; | 
|---|
| 867 | } | 
|---|
| 868 |  | 
|---|
| 869 | Type * Pass1::mutate( FunctionType *functionType ) { | 
|---|
| 870 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 871 | makeTyVarMap( functionType, scopeTyVars ); | 
|---|
| 872 |  | 
|---|
| 873 | Type *ret = Mutator::mutate( functionType ); | 
|---|
| 874 |  | 
|---|
| 875 | scopeTyVars = oldtyVars; | 
|---|
| 876 | return ret; | 
|---|
| 877 | } | 
|---|
| 878 |  | 
|---|
| 879 | void Pass1::doBeginScope() { | 
|---|
| 880 | adapters.push(AdapterMap()); | 
|---|
| 881 | } | 
|---|
| 882 |  | 
|---|
| 883 | void Pass1::doEndScope() { | 
|---|
| 884 | adapters.pop(); | 
|---|
| 885 | } | 
|---|
| 886 |  | 
|---|
| 887 | ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// | 
|---|
| 888 |  | 
|---|
| 889 | Pass2::Pass2() {} | 
|---|
| 890 |  | 
|---|
| 891 | void Pass2::addAdapters( FunctionType *functionType ) { | 
|---|
| 892 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); | 
|---|
| 893 | std::list< FunctionType *> functions; | 
|---|
| 894 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
| 895 | Type *orig = (*arg)->get_type(); | 
|---|
| 896 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter ); | 
|---|
| 897 | (*arg)->set_type( orig ); | 
|---|
| 898 | } | 
|---|
| 899 | std::set< std::string > adaptersDone; | 
|---|
| 900 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { | 
|---|
| 901 | std::string mangleName = SymTab::Mangler::mangle( *funType ); | 
|---|
| 902 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { | 
|---|
| 903 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 904 | paramList.push_front( new ObjectDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); | 
|---|
| 905 | adaptersDone.insert( adaptersDone.begin(), mangleName ); | 
|---|
| 906 | } | 
|---|
| 907 | } | 
|---|
| 908 | ///  deleteAll( functions ); | 
|---|
| 909 | } | 
|---|
| 910 |  | 
|---|
| 911 | template< typename DeclClass > | 
|---|
| 912 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) { | 
|---|
| 913 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); | 
|---|
| 914 |  | 
|---|
| 915 | return ret; | 
|---|
| 916 | } | 
|---|
| 917 |  | 
|---|
| 918 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) { | 
|---|
| 919 | return handleDecl( functionDecl, functionDecl->get_functionType() ); | 
|---|
| 920 | } | 
|---|
| 921 |  | 
|---|
| 922 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) { | 
|---|
| 923 | return handleDecl( objectDecl, objectDecl->get_type() ); | 
|---|
| 924 | } | 
|---|
| 925 |  | 
|---|
| 926 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) { | 
|---|
| 927 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); | 
|---|
| 928 | if ( typeDecl->get_base() ) { | 
|---|
| 929 | return handleDecl( typeDecl, typeDecl->get_base() ); | 
|---|
| 930 | } else { | 
|---|
| 931 | return Mutator::mutate( typeDecl ); | 
|---|
| 932 | } | 
|---|
| 933 | } | 
|---|
| 934 |  | 
|---|
| 935 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) { | 
|---|
| 936 | return handleDecl( typedefDecl, typedefDecl->get_base() ); | 
|---|
| 937 | } | 
|---|
| 938 |  | 
|---|
| 939 | Type * Pass2::mutate( PointerType *pointerType ) { | 
|---|
| 940 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 941 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
| 942 |  | 
|---|
| 943 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| 944 |  | 
|---|
| 945 | scopeTyVars = oldtyVars; | 
|---|
| 946 | return ret; | 
|---|
| 947 | } | 
|---|
| 948 |  | 
|---|
| 949 | Type *Pass2::mutate( FunctionType *funcType ) { | 
|---|
| 950 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 951 | makeTyVarMap( funcType, scopeTyVars ); | 
|---|
| 952 |  | 
|---|
| 953 | std::string typeName; | 
|---|
| 954 | if ( isPolyRet( funcType, typeName ) ) { | 
|---|
| 955 | DeclarationWithType *ret = funcType->get_returnVals().front(); | 
|---|
| 956 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) ); | 
|---|
| 957 | funcType->get_parameters().push_front( ret ); | 
|---|
| 958 | funcType->get_returnVals().pop_front(); | 
|---|
| 959 | } | 
|---|
| 960 |  | 
|---|
| 961 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); | 
|---|
| 962 | std::list< DeclarationWithType *> inferredParams; | 
|---|
| 963 | ObjectDecl *newObj = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); | 
|---|
| 964 | ///   ObjectDecl *newFunPtr = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ); | 
|---|
| 965 | for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { | 
|---|
| 966 | ObjectDecl *thisParm; | 
|---|
| 967 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) { | 
|---|
| 968 | thisParm = newObj->clone(); | 
|---|
| 969 | thisParm->set_name( (*tyParm)->get_name() ); | 
|---|
| 970 | last = funcType->get_parameters().insert( last, thisParm ); | 
|---|
| 971 | ++last; | 
|---|
| 972 | } | 
|---|
| 973 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { | 
|---|
| 974 | ///      *assert = (*assert)->acceptMutator( *this ); | 
|---|
| 975 | inferredParams.push_back( *assert ); | 
|---|
| 976 | } | 
|---|
| 977 | (*tyParm)->get_assertions().clear(); | 
|---|
| 978 | } | 
|---|
| 979 | delete newObj; | 
|---|
| 980 | funcType->get_parameters().splice( last, inferredParams ); | 
|---|
| 981 | addAdapters( funcType ); | 
|---|
| 982 | mutateAll( funcType->get_returnVals(), *this ); | 
|---|
| 983 | mutateAll( funcType->get_parameters(), *this ); | 
|---|
| 984 |  | 
|---|
| 985 | scopeTyVars = oldtyVars; | 
|---|
| 986 | return funcType; | 
|---|
| 987 | } | 
|---|
| 988 |  | 
|---|
| 989 | ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// | 
|---|
| 990 |  | 
|---|
| 991 | template< typename DeclClass > | 
|---|
| 992 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { | 
|---|
| 993 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 994 | makeTyVarMap( type, scopeTyVars ); | 
|---|
| 995 |  | 
|---|
| 996 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); | 
|---|
| 997 | ScrubTyVars::scrub( decl, scopeTyVars ); | 
|---|
| 998 |  | 
|---|
| 999 | scopeTyVars = oldtyVars; | 
|---|
| 1000 | return ret; | 
|---|
| 1001 | } | 
|---|
| 1002 |  | 
|---|
| 1003 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) { | 
|---|
| 1004 | return handleDecl( objectDecl, objectDecl->get_type() ); | 
|---|
| 1005 | } | 
|---|
| 1006 |  | 
|---|
| 1007 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) { | 
|---|
| 1008 | return handleDecl( functionDecl, functionDecl->get_functionType() ); | 
|---|
| 1009 | } | 
|---|
| 1010 |  | 
|---|
| 1011 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) { | 
|---|
| 1012 | return handleDecl( typedefDecl, typedefDecl->get_base() ); | 
|---|
| 1013 | } | 
|---|
| 1014 |  | 
|---|
| 1015 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) { | 
|---|
| 1016 | ///   Initializer *init = 0; | 
|---|
| 1017 | ///   std::list< Expression *> designators; | 
|---|
| 1018 | ///   scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); | 
|---|
| 1019 | ///   if ( typeDecl->get_base() ) { | 
|---|
| 1020 | ///     init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators ); | 
|---|
| 1021 | ///   } | 
|---|
| 1022 | ///   return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init ); | 
|---|
| 1023 |  | 
|---|
| 1024 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); | 
|---|
| 1025 | return Mutator::mutate( typeDecl ); | 
|---|
| 1026 | } | 
|---|
| 1027 |  | 
|---|
| 1028 | Type * Pass3::mutate( PointerType *pointerType ) { | 
|---|
| 1029 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 1030 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
| 1031 |  | 
|---|
| 1032 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| 1033 |  | 
|---|
| 1034 | scopeTyVars = oldtyVars; | 
|---|
| 1035 | return ret; | 
|---|
| 1036 | } | 
|---|
| 1037 |  | 
|---|
| 1038 | Type * Pass3::mutate( FunctionType *functionType ) { | 
|---|
| 1039 | TyVarMap oldtyVars = scopeTyVars; | 
|---|
| 1040 | makeTyVarMap( functionType, scopeTyVars ); | 
|---|
| 1041 |  | 
|---|
| 1042 | Type *ret = Mutator::mutate( functionType ); | 
|---|
| 1043 |  | 
|---|
| 1044 | scopeTyVars = oldtyVars; | 
|---|
| 1045 | return ret; | 
|---|
| 1046 | } | 
|---|
| 1047 |  | 
|---|
| 1048 | Statement *Pass3::mutate( DeclStmt *declStmt ) { | 
|---|
| 1049 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { | 
|---|
| 1050 | if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) { | 
|---|
| 1051 | TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() ); | 
|---|
| 1052 | assert( typeInst ); | 
|---|
| 1053 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); | 
|---|
| 1054 | alloc->get_args().push_back( new NameExpr( typeInst->get_name() ) ); | 
|---|
| 1055 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
| 1056 | assign->get_args().push_back( new VariableExpr( objectDecl ) ); | 
|---|
| 1057 | assign->get_args().push_back( alloc ); | 
|---|
| 1058 | stmtsToAddAfter.push_back( new ExprStmt( noLabels, assign ) ); | 
|---|
| 1059 | } | 
|---|
| 1060 | } | 
|---|
| 1061 | return Mutator::mutate( declStmt ); | 
|---|
| 1062 | } | 
|---|
| 1063 | } // anonymous namespace | 
|---|
| 1064 | } // namespace GenPoly | 
|---|
| 1065 |  | 
|---|
| 1066 | // Local Variables: // | 
|---|
| 1067 | // tab-width: 4 // | 
|---|
| 1068 | // mode: c++ // | 
|---|
| 1069 | // compile-command: "make install" // | 
|---|
| 1070 | // End: // | 
|---|