[51587aa] | 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 | // |
---|
[ae63a18] | 7 | // Box.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[bdf1954] | 11 | // Last Modified By : Rob Schluntz |
---|
[ae63a18] | 12 | // Last Modified On : Fri Dec 18 14:53:08 2015 |
---|
| 13 | // Update Count : 217 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <set> |
---|
[b1a6d6b] | 17 | #include <stack> |
---|
[51b7345] | 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 | |
---|
[68cd1ce] | 28 | #include "Parser/ParseNode.h" |
---|
| 29 | |
---|
[cdec5af] | 30 | #include "SynTree/Constant.h" |
---|
[51b7345] | 31 | #include "SynTree/Type.h" |
---|
| 32 | #include "SynTree/Expression.h" |
---|
| 33 | #include "SynTree/Initializer.h" |
---|
| 34 | #include "SynTree/Statement.h" |
---|
| 35 | #include "SynTree/Mutator.h" |
---|
[68cd1ce] | 36 | |
---|
[51b7345] | 37 | #include "ResolvExpr/TypeEnvironment.h" |
---|
[68cd1ce] | 38 | |
---|
[51b7345] | 39 | #include "SymTab/Mangler.h" |
---|
| 40 | |
---|
| 41 | #include "SemanticError.h" |
---|
| 42 | #include "UniqueName.h" |
---|
| 43 | #include "utility.h" |
---|
| 44 | |
---|
| 45 | #include <ext/functional> // temporary |
---|
| 46 | |
---|
| 47 | namespace GenPoly { |
---|
[01aeade] | 48 | namespace { |
---|
| 49 | const std::list<Label> noLabels; |
---|
| 50 | |
---|
[e56cfdb0] | 51 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); |
---|
| 52 | |
---|
[f8b961b] | 53 | /// 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 |
---|
[01aeade] | 54 | class Pass1 : public PolyMutator { |
---|
| 55 | public: |
---|
| 56 | Pass1(); |
---|
| 57 | virtual Expression *mutate( ApplicationExpr *appExpr ); |
---|
| 58 | virtual Expression *mutate( AddressExpr *addrExpr ); |
---|
| 59 | virtual Expression *mutate( UntypedExpr *expr ); |
---|
| 60 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ); |
---|
| 61 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
| 62 | virtual Expression *mutate( CommaExpr *commaExpr ); |
---|
| 63 | virtual Expression *mutate( ConditionalExpr *condExpr ); |
---|
[cf16f94] | 64 | virtual Statement * mutate( ReturnStmt *returnStmt ); |
---|
[01aeade] | 65 | virtual Type *mutate( PointerType *pointerType ); |
---|
[cf16f94] | 66 | virtual Type * mutate( FunctionType *functionType ); |
---|
[ae63a18] | 67 | |
---|
[01aeade] | 68 | virtual void doBeginScope(); |
---|
| 69 | virtual void doEndScope(); |
---|
| 70 | private: |
---|
| 71 | void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
| 72 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); |
---|
| 73 | Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ); |
---|
| 74 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
| 75 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars ); |
---|
| 76 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
| 77 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ); |
---|
| 78 | void findAssignOps( const std::list< TypeDecl *> &forall ); |
---|
| 79 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ); |
---|
| 80 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); |
---|
| 81 | Expression *handleIntrinsics( ApplicationExpr *appExpr ); |
---|
| 82 | ObjectDecl *makeTemporary( Type *type ); |
---|
[c29d9ce] | 83 | |
---|
[e56cfdb0] | 84 | typedef std::map< std::string, DeclarationWithType *> AdapterMap; |
---|
[c29d9ce] | 85 | std::map< std::string, DeclarationWithType *> assignOps; |
---|
[01aeade] | 86 | std::stack< AdapterMap > adapters; |
---|
| 87 | DeclarationWithType *retval; |
---|
| 88 | bool useRetval; |
---|
| 89 | UniqueName tempNamer; |
---|
| 90 | }; |
---|
| 91 | |
---|
[f8b961b] | 92 | /// Moves polymorphic returns in function types to pointer-type parameters, adds type size and assertion parameters to parameter lists as well |
---|
[01aeade] | 93 | class Pass2 : public PolyMutator { |
---|
| 94 | public: |
---|
| 95 | template< typename DeclClass > |
---|
| 96 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
| 97 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
| 98 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); |
---|
| 99 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
| 100 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl ); |
---|
| 101 | virtual Type *mutate( PointerType *pointerType ); |
---|
| 102 | virtual Type *mutate( FunctionType *funcType ); |
---|
| 103 | private: |
---|
| 104 | void addAdapters( FunctionType *functionType ); |
---|
[ae63a18] | 105 | |
---|
[01aeade] | 106 | std::map< UniqueId, std::string > adapterName; |
---|
| 107 | }; |
---|
| 108 | |
---|
[f8b961b] | 109 | /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable |
---|
[01aeade] | 110 | class Pass3 : public PolyMutator { |
---|
| 111 | public: |
---|
| 112 | template< typename DeclClass > |
---|
| 113 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
| 114 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
| 115 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); |
---|
| 116 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ); |
---|
| 117 | virtual TypeDecl *mutate( TypeDecl *objectDecl ); |
---|
| 118 | virtual Statement *mutate( DeclStmt *declStmt ); |
---|
| 119 | virtual Type *mutate( PointerType *pointerType ); |
---|
| 120 | virtual Type *mutate( FunctionType *funcType ); |
---|
| 121 | private: |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | } // anonymous namespace |
---|
| 125 | |
---|
| 126 | void printAllNotBuiltin( const std::list< Declaration *>& translationUnit, std::ostream &os ) { |
---|
| 127 | for ( std::list< Declaration *>::const_iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 128 | if ( ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) { |
---|
| 129 | (*i)->print( os ); |
---|
| 130 | os << std::endl; |
---|
[6c3744e] | 131 | } // if |
---|
[01aeade] | 132 | } // for |
---|
[6c3744e] | 133 | } |
---|
| 134 | |
---|
[01aeade] | 135 | void box( std::list< Declaration *>& translationUnit ) { |
---|
| 136 | Pass1 pass1; |
---|
| 137 | Pass2 pass2; |
---|
| 138 | Pass3 pass3; |
---|
| 139 | mutateAll( translationUnit, pass1 ); |
---|
| 140 | mutateAll( translationUnit, pass2 ); |
---|
| 141 | mutateAll( translationUnit, pass3 ); |
---|
[6c3744e] | 142 | } |
---|
| 143 | |
---|
[01aeade] | 144 | ////////////////////////////////////////// Pass1 //////////////////////////////////////////////////// |
---|
| 145 | |
---|
| 146 | namespace { |
---|
[bdf1954] | 147 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) { |
---|
| 148 | std::stringstream name; |
---|
| 149 | |
---|
[ed1065c] | 150 | // NOTE: this function previously used isPolyObj, which failed to produce |
---|
| 151 | // the correct thing in some situations. It's not clear to me why this wasn't working. |
---|
| 152 | |
---|
[ae63a18] | 153 | // if the return type or a parameter type involved polymorphic types, then the adapter will need |
---|
| 154 | // to take those polymorphic types as pointers. Therefore, there can be two different functions |
---|
[bdf1954] | 155 | // with the same mangled name, so we need to further mangle the names. |
---|
[ed1065c] | 156 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) { |
---|
[ffad73a] | 157 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) { |
---|
[ed1065c] | 158 | name << "P"; |
---|
| 159 | } else { |
---|
| 160 | name << "M"; |
---|
| 161 | } |
---|
[bdf1954] | 162 | } |
---|
| 163 | name << "_"; |
---|
| 164 | std::list< DeclarationWithType *> ¶mList = function->get_parameters(); |
---|
| 165 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
[ffad73a] | 166 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) { |
---|
[bdf1954] | 167 | name << "P"; |
---|
| 168 | } else { |
---|
[ae63a18] | 169 | name << "M"; |
---|
[bdf1954] | 170 | } |
---|
| 171 | } // for |
---|
| 172 | return name.str(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) { |
---|
| 176 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars ); |
---|
| 177 | } |
---|
| 178 | |
---|
[01aeade] | 179 | std::string makeAdapterName( const std::string &mangleName ) { |
---|
| 180 | return "_adapter" + mangleName; |
---|
| 181 | } |
---|
[6c3744e] | 182 | |
---|
[56fcd77] | 183 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) { |
---|
[e56cfdb0] | 184 | adapters.push(AdapterMap()); |
---|
[01aeade] | 185 | } |
---|
| 186 | |
---|
[f2b2029] | 187 | // returns true if the given declaration is: (*?=?)(T *, T) for some T (return not checked, but maybe should be) |
---|
[01aeade] | 188 | bool checkAssignment( DeclarationWithType *decl, std::string &name ) { |
---|
| 189 | if ( decl->get_name() == "?=?" ) { |
---|
| 190 | if ( PointerType *ptrType = dynamic_cast< PointerType *>( decl->get_type() ) ) { |
---|
| 191 | if ( FunctionType *funType = dynamic_cast< FunctionType *>( ptrType->get_base() ) ) { |
---|
| 192 | if ( funType->get_parameters().size() == 2 ) { |
---|
| 193 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { |
---|
| 194 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { |
---|
[cf16f94] | 195 | if ( TypeInstType *typeInst2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { |
---|
| 196 | if ( typeInst->get_name() == typeInst2->get_name() ) { |
---|
| 197 | name = typeInst->get_name(); |
---|
| 198 | return true; |
---|
| 199 | } // if |
---|
| 200 | } // if |
---|
[01aeade] | 201 | } // if |
---|
| 202 | } // if |
---|
| 203 | } // if |
---|
| 204 | } // if |
---|
| 205 | } // if |
---|
| 206 | } // if |
---|
| 207 | return false; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) { |
---|
[ed1065c] | 211 | // what if a nested function uses an assignment operator? |
---|
| 212 | // assignOps.clear(); |
---|
[01aeade] | 213 | for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) { |
---|
| 214 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
| 215 | std::string typeName; |
---|
| 216 | if ( checkAssignment( *assert, typeName ) ) { |
---|
| 217 | assignOps[ typeName ] = *assert; |
---|
| 218 | } // if |
---|
| 219 | } // for |
---|
| 220 | } // for |
---|
| 221 | } |
---|
| 222 | |
---|
[82dd287] | 223 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { |
---|
[e56cfdb0] | 224 | if ( functionDecl->get_statements() ) { // empty routine body ? |
---|
| 225 | doBeginScope(); |
---|
[01aeade] | 226 | TyVarMap oldtyVars = scopeTyVars; |
---|
[ed1065c] | 227 | std::map< std::string, DeclarationWithType *> oldassignOps = assignOps; |
---|
[01aeade] | 228 | DeclarationWithType *oldRetval = retval; |
---|
| 229 | bool oldUseRetval = useRetval; |
---|
[e56cfdb0] | 230 | |
---|
| 231 | // process polymorphic return value |
---|
[01aeade] | 232 | retval = 0; |
---|
| 233 | std::string typeName; |
---|
| 234 | if ( isPolyRet( functionDecl->get_functionType(), typeName ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) { |
---|
| 235 | retval = functionDecl->get_functionType()->get_returnVals().front(); |
---|
[ae63a18] | 236 | |
---|
[01aeade] | 237 | // give names to unnamed return values |
---|
| 238 | if ( retval->get_name() == "" ) { |
---|
| 239 | retval->set_name( "_retparm" ); |
---|
| 240 | retval->set_linkage( LinkageSpec::C ); |
---|
| 241 | } // if |
---|
| 242 | } // if |
---|
[ae63a18] | 243 | |
---|
[e56cfdb0] | 244 | FunctionType *functionType = functionDecl->get_functionType(); |
---|
[01aeade] | 245 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); |
---|
| 246 | findAssignOps( functionDecl->get_functionType()->get_forall() ); |
---|
[e56cfdb0] | 247 | |
---|
| 248 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
| 249 | std::list< FunctionType *> functions; |
---|
| 250 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
| 251 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
| 252 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
| 253 | } // for |
---|
| 254 | } // for |
---|
| 255 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
| 256 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
| 257 | } // for |
---|
| 258 | AdapterMap & adapters = Pass1::adapters.top(); |
---|
| 259 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
[bdf1954] | 260 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
[e56cfdb0] | 261 | if ( adapters.find( mangleName ) == adapters.end() ) { |
---|
| 262 | std::string adapterName = makeAdapterName( mangleName ); |
---|
| 263 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) ); |
---|
| 264 | } // if |
---|
| 265 | } // for |
---|
| 266 | |
---|
[01aeade] | 267 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); |
---|
[ae63a18] | 268 | |
---|
[01aeade] | 269 | scopeTyVars = oldtyVars; |
---|
[ed1065c] | 270 | assignOps = oldassignOps; |
---|
[e56cfdb0] | 271 | // std::cerr << "end FunctionDecl: "; |
---|
| 272 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
| 273 | // std::cerr << i->first << " "; |
---|
| 274 | // } |
---|
| 275 | // std::cerr << "\n"; |
---|
[01aeade] | 276 | retval = oldRetval; |
---|
| 277 | useRetval = oldUseRetval; |
---|
[e56cfdb0] | 278 | doEndScope(); |
---|
[01aeade] | 279 | } // if |
---|
| 280 | return functionDecl; |
---|
| 281 | } |
---|
[6c3744e] | 282 | |
---|
[01aeade] | 283 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { |
---|
| 284 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
| 285 | return Mutator::mutate( typeDecl ); |
---|
| 286 | } |
---|
[6c3744e] | 287 | |
---|
[01aeade] | 288 | Expression *Pass1::mutate( CommaExpr *commaExpr ) { |
---|
| 289 | bool oldUseRetval = useRetval; |
---|
| 290 | useRetval = false; |
---|
| 291 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) ); |
---|
| 292 | useRetval = oldUseRetval; |
---|
| 293 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) ); |
---|
| 294 | return commaExpr; |
---|
| 295 | } |
---|
[6c3744e] | 296 | |
---|
[01aeade] | 297 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) { |
---|
| 298 | bool oldUseRetval = useRetval; |
---|
| 299 | useRetval = false; |
---|
| 300 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) ); |
---|
| 301 | useRetval = oldUseRetval; |
---|
| 302 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) ); |
---|
| 303 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) ); |
---|
| 304 | return condExpr; |
---|
[6c3744e] | 305 | |
---|
[01aeade] | 306 | } |
---|
[6c3744e] | 307 | |
---|
[01aeade] | 308 | void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
[7754cde] | 309 | // pass size/align for type variables |
---|
[01aeade] | 310 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { |
---|
| 311 | ResolvExpr::EqvClass eqvClass; |
---|
| 312 | assert( env ); |
---|
| 313 | if ( tyParm->second == TypeDecl::Any ) { |
---|
| 314 | Type *concrete = env->lookup( tyParm->first ); |
---|
| 315 | if ( concrete ) { |
---|
| 316 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); |
---|
| 317 | arg++; |
---|
[db0b3ce] | 318 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) ); |
---|
| 319 | arg++; |
---|
[01aeade] | 320 | } else { |
---|
| 321 | throw SemanticError( "unbound type variable in application ", appExpr ); |
---|
| 322 | } // if |
---|
| 323 | } // if |
---|
| 324 | } // for |
---|
[7754cde] | 325 | |
---|
| 326 | // add size/align for generic types to parameter list |
---|
| 327 | //assert( ! appExpr->get_function()->get_results().empty() ); |
---|
| 328 | if ( appExpr->get_function()->get_results().empty() ) return; |
---|
| 329 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
| 330 | assert( funcType ); |
---|
[ae63a18] | 331 | |
---|
[7754cde] | 332 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); |
---|
| 333 | std::list< Expression* >::const_iterator fnArg = arg; |
---|
[69911c11] | 334 | std::set< std::string > seenTypes; //< names for generic types we've seen |
---|
[7754cde] | 335 | for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) { |
---|
| 336 | Type *parmType = (*fnParm)->get_type(); |
---|
| 337 | if ( ! dynamic_cast< TypeInstType* >( parmType ) && isPolyType( parmType, exprTyVars ) ) { |
---|
| 338 | std::string sizeName = sizeofName( parmType ); |
---|
| 339 | if ( seenTypes.count( sizeName ) ) continue; |
---|
| 340 | |
---|
| 341 | assert( ! (*fnArg)->get_results().empty() ); |
---|
| 342 | Type *argType = (*fnArg)->get_results().front(); |
---|
| 343 | arg = appExpr->get_args().insert( arg, new SizeofExpr( argType->clone() ) ); |
---|
| 344 | arg++; |
---|
| 345 | arg = appExpr->get_args().insert( arg, new AlignofExpr( argType->clone() ) ); |
---|
| 346 | arg++; |
---|
| 347 | |
---|
| 348 | seenTypes.insert( sizeName ); |
---|
| 349 | } |
---|
| 350 | } |
---|
[01aeade] | 351 | } |
---|
[6c3744e] | 352 | |
---|
[01aeade] | 353 | ObjectDecl *Pass1::makeTemporary( Type *type ) { |
---|
[68cd1ce] | 354 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 ); |
---|
[01aeade] | 355 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 356 | return newObj; |
---|
| 357 | } |
---|
[6c3744e] | 358 | |
---|
[01aeade] | 359 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { |
---|
[cf16f94] | 360 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
| 361 | // if ( useRetval ) { |
---|
| 362 | // assert( retval ); |
---|
| 363 | // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); |
---|
| 364 | // arg++; |
---|
| 365 | // } else { |
---|
| 366 | |
---|
| 367 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result |
---|
| 368 | // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple |
---|
| 369 | // return values. |
---|
| 370 | ObjectDecl *newObj = makeTemporary( retType->clone() ); |
---|
| 371 | Expression *paramExpr = new VariableExpr( newObj ); |
---|
| 372 | // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the |
---|
| 373 | // temporary is already boxed and can be used directly. |
---|
[5f6c42c] | 374 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { |
---|
[cf16f94] | 375 | paramExpr = new AddressExpr( paramExpr ); |
---|
[01aeade] | 376 | } // if |
---|
[cf16f94] | 377 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call |
---|
| 378 | arg++; |
---|
| 379 | // Build a comma expression to call the function and emulate a normal return. |
---|
| 380 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); |
---|
| 381 | commaExpr->set_env( appExpr->get_env() ); |
---|
| 382 | appExpr->set_env( 0 ); |
---|
| 383 | return commaExpr; |
---|
| 384 | // } // if |
---|
| 385 | // return appExpr; |
---|
[01aeade] | 386 | } |
---|
[6c3744e] | 387 | |
---|
[01aeade] | 388 | Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ) { |
---|
| 389 | ResolvExpr::EqvClass eqvClass; |
---|
| 390 | assert( env ); |
---|
| 391 | Type *concrete = env->lookup( typeName ); |
---|
| 392 | if ( concrete == 0 ) { |
---|
| 393 | throw SemanticError( "Unbound type variable " + typeName + " in ", appExpr ); |
---|
| 394 | } // if |
---|
| 395 | return addRetParam( appExpr, function, concrete, arg ); |
---|
| 396 | } |
---|
[6c3744e] | 397 | |
---|
[01aeade] | 398 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
| 399 | Expression *ret = appExpr; |
---|
[ffad73a] | 400 | if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
[01aeade] | 401 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); |
---|
| 402 | } // if |
---|
[bdf1954] | 403 | std::string mangleName = mangleAdapterName( function, tyVars ); |
---|
[01aeade] | 404 | std::string adapterName = makeAdapterName( mangleName ); |
---|
[b1a6d6b] | 405 | |
---|
[01aeade] | 406 | appExpr->get_args().push_front( appExpr->get_function() ); |
---|
| 407 | appExpr->set_function( new NameExpr( adapterName ) ); |
---|
[ae63a18] | 408 | |
---|
[01aeade] | 409 | return ret; |
---|
| 410 | } |
---|
[6c3744e] | 411 | |
---|
[01aeade] | 412 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { |
---|
| 413 | assert( ! arg->get_results().empty() ); |
---|
[1cced28] | 414 | if ( isPolyType( param, exprTyVars ) ) { |
---|
[01aeade] | 415 | if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) { |
---|
| 416 | // if the argument's type is a type parameter, we don't need to box again! |
---|
| 417 | return; |
---|
| 418 | } else if ( arg->get_results().front()->get_isLvalue() ) { |
---|
| 419 | // VariableExpr and MemberExpr are lvalues |
---|
| 420 | arg = new AddressExpr( arg ); |
---|
| 421 | } else { |
---|
[68cd1ce] | 422 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 ); |
---|
[f6d7e0f] | 423 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right??? |
---|
[01aeade] | 424 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 425 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 426 | assign->get_args().push_back( new VariableExpr( newObj ) ); |
---|
| 427 | assign->get_args().push_back( arg ); |
---|
| 428 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) ); |
---|
| 429 | arg = new AddressExpr( new VariableExpr( newObj ) ); |
---|
| 430 | } // if |
---|
| 431 | } // if |
---|
| 432 | } |
---|
[6c3744e] | 433 | |
---|
[01aeade] | 434 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) { |
---|
| 435 | Type *newType = formal->clone(); |
---|
| 436 | std::list< FunctionType *> functions; |
---|
| 437 | // instead of functions needing adapters, this really ought to look for |
---|
| 438 | // any function mentioning a polymorphic type |
---|
| 439 | findAndReplaceFunction( newType, functions, tyVars, needsAdapter ); |
---|
| 440 | if ( ! functions.empty() ) { |
---|
| 441 | actual = new CastExpr( actual, newType ); |
---|
| 442 | } else { |
---|
| 443 | delete newType; |
---|
| 444 | } // if |
---|
| 445 | } |
---|
[6c3744e] | 446 | |
---|
[01aeade] | 447 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
| 448 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { |
---|
| 449 | assert( arg != appExpr->get_args().end() ); |
---|
| 450 | addCast( *arg, (*param)->get_type(), exprTyVars ); |
---|
| 451 | boxParam( (*param)->get_type(), *arg, exprTyVars ); |
---|
| 452 | } // for |
---|
| 453 | } |
---|
[6c3744e] | 454 | |
---|
[01aeade] | 455 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
| 456 | std::list< Expression *>::iterator cur = arg; |
---|
| 457 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
| 458 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
| 459 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() ); |
---|
[698664b3] | 460 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" ); |
---|
[01aeade] | 461 | Expression *newExpr = inferParam->second.expr->clone(); |
---|
| 462 | addCast( newExpr, (*assert)->get_type(), tyVars ); |
---|
| 463 | boxParam( (*assert)->get_type(), newExpr, tyVars ); |
---|
| 464 | appExpr->get_args().insert( cur, newExpr ); |
---|
| 465 | } // for |
---|
| 466 | } // for |
---|
| 467 | } |
---|
[6c3744e] | 468 | |
---|
[01aeade] | 469 | void makeRetParm( FunctionType *funcType ) { |
---|
| 470 | DeclarationWithType *retParm = funcType->get_returnVals().front(); |
---|
[6c3744e] | 471 | |
---|
[01aeade] | 472 | // make a new parameter that is a pointer to the type of the old return value |
---|
| 473 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); |
---|
| 474 | funcType->get_parameters().push_front( retParm ); |
---|
[6c3744e] | 475 | |
---|
[01aeade] | 476 | // we don't need the return value any more |
---|
| 477 | funcType->get_returnVals().clear(); |
---|
| 478 | } |
---|
[6c3744e] | 479 | |
---|
[01aeade] | 480 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
| 481 | // actually make the adapter type |
---|
| 482 | FunctionType *adapter = adaptee->clone(); |
---|
[ffad73a] | 483 | if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
[01aeade] | 484 | makeRetParm( adapter ); |
---|
| 485 | } // if |
---|
[68cd1ce] | 486 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) ); |
---|
[01aeade] | 487 | return adapter; |
---|
| 488 | } |
---|
[6c3744e] | 489 | |
---|
[01aeade] | 490 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { |
---|
| 491 | assert( param ); |
---|
| 492 | assert( arg ); |
---|
[ffad73a] | 493 | if ( isPolyType( realParam->get_type(), tyVars ) ) { |
---|
| 494 | // if ( dynamic_cast< PointerType *>( arg->get_type() ) ) { |
---|
| 495 | // return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() ); |
---|
| 496 | // } else { |
---|
[e56cfdb0] | 497 | if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) { |
---|
| 498 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 499 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); |
---|
| 500 | deref->get_results().push_back( arg->get_type()->clone() ); |
---|
| 501 | return deref; |
---|
| 502 | } // if |
---|
[ffad73a] | 503 | // } |
---|
[01aeade] | 504 | } // if |
---|
| 505 | return new VariableExpr( param ); |
---|
| 506 | } |
---|
[6c3744e] | 507 | |
---|
[01aeade] | 508 | 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 ) { |
---|
| 509 | UniqueName paramNamer( "_p" ); |
---|
| 510 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { |
---|
| 511 | if ( (*param)->get_name() == "" ) { |
---|
| 512 | (*param)->set_name( paramNamer.newName() ); |
---|
| 513 | (*param)->set_linkage( LinkageSpec::C ); |
---|
| 514 | } // if |
---|
| 515 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) ); |
---|
| 516 | } // for |
---|
| 517 | } |
---|
[6c3744e] | 518 | |
---|
[b1a6d6b] | 519 | |
---|
| 520 | |
---|
[01aeade] | 521 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { |
---|
| 522 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); |
---|
| 523 | adapterType = ScrubTyVars::scrub( adapterType, tyVars ); |
---|
| 524 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front(); |
---|
| 525 | adapteeDecl->set_name( "_adaptee" ); |
---|
| 526 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) ); |
---|
| 527 | Statement *bodyStmt; |
---|
[ae63a18] | 528 | |
---|
[01aeade] | 529 | std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin(); |
---|
| 530 | std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin(); |
---|
| 531 | std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin(); |
---|
| 532 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { |
---|
| 533 | assert( tyArg != realType->get_forall().end() ); |
---|
| 534 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin(); |
---|
| 535 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin(); |
---|
| 536 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin(); |
---|
| 537 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { |
---|
| 538 | assert( assertArg != (*tyArg)->get_assertions().end() ); |
---|
| 539 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) ); |
---|
| 540 | } // for |
---|
| 541 | } // for |
---|
[ae63a18] | 542 | |
---|
[01aeade] | 543 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin(); |
---|
| 544 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin(); |
---|
| 545 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin(); |
---|
| 546 | param++; // skip adaptee parameter |
---|
| 547 | if ( realType->get_returnVals().empty() ) { |
---|
| 548 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
| 549 | bodyStmt = new ExprStmt( noLabels, adapteeApp ); |
---|
[ffad73a] | 550 | } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
[01aeade] | 551 | if ( (*param)->get_name() == "" ) { |
---|
| 552 | (*param)->set_name( "_ret" ); |
---|
| 553 | (*param)->set_linkage( LinkageSpec::C ); |
---|
| 554 | } // if |
---|
| 555 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 556 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 557 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) ); |
---|
| 558 | assign->get_args().push_back( deref ); |
---|
| 559 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
| 560 | assign->get_args().push_back( adapteeApp ); |
---|
| 561 | bodyStmt = new ExprStmt( noLabels, assign ); |
---|
| 562 | } else { |
---|
| 563 | // adapter for a function that returns a monomorphic value |
---|
| 564 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
| 565 | bodyStmt = new ReturnStmt( noLabels, adapteeApp ); |
---|
| 566 | } // if |
---|
| 567 | CompoundStmt *adapterBody = new CompoundStmt( noLabels ); |
---|
| 568 | adapterBody->get_kids().push_back( bodyStmt ); |
---|
| 569 | std::string adapterName = makeAdapterName( mangleName ); |
---|
[de62360d] | 570 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false ); |
---|
[01aeade] | 571 | } |
---|
[6c3744e] | 572 | |
---|
[c29d9ce] | 573 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { |
---|
[e497c1d] | 574 | // collect a list of function types passed as parameters or implicit parameters (assertions) |
---|
[01aeade] | 575 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
| 576 | std::list< FunctionType *> functions; |
---|
| 577 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
| 578 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
| 579 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
| 580 | } // for |
---|
| 581 | } // for |
---|
| 582 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
| 583 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
| 584 | } // for |
---|
[e497c1d] | 585 | |
---|
[e56cfdb0] | 586 | // parameter function types for which an appropriate adapter has been generated. we cannot use the types |
---|
| 587 | // after applying substitutions, since two different parameter types may be unified to the same type |
---|
[01aeade] | 588 | std::set< std::string > adaptersDone; |
---|
[e497c1d] | 589 | |
---|
[01aeade] | 590 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
[c29d9ce] | 591 | FunctionType *originalFunction = (*funType)->clone(); |
---|
[01aeade] | 592 | FunctionType *realFunction = (*funType)->clone(); |
---|
| 593 | std::string mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
[e497c1d] | 594 | |
---|
[e56cfdb0] | 595 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this |
---|
| 596 | // pre-substitution parameter function type. |
---|
[01aeade] | 597 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
[e497c1d] | 598 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
[ae63a18] | 599 | |
---|
[e56cfdb0] | 600 | // apply substitution to type variables to figure out what the adapter's type should look like |
---|
[e497c1d] | 601 | assert( env ); |
---|
| 602 | env->apply( realFunction ); |
---|
[ae63a18] | 603 | mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
[bdf1954] | 604 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars ); |
---|
[e497c1d] | 605 | |
---|
[e56cfdb0] | 606 | AdapterMap & adapters = Pass1::adapters.top(); |
---|
| 607 | AdapterMap::iterator adapter = adapters.find( mangleName ); |
---|
| 608 | if ( adapter == adapters.end() ) { |
---|
| 609 | // adapter has not been created yet in the current scope, so define it |
---|
| 610 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); |
---|
| 611 | adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) ); |
---|
| 612 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); |
---|
[c29d9ce] | 613 | } // if |
---|
[e56cfdb0] | 614 | assert( adapter != adapters.end() ); |
---|
| 615 | |
---|
| 616 | // add the appropriate adapter as a parameter |
---|
| 617 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); |
---|
[01aeade] | 618 | } // if |
---|
| 619 | } // for |
---|
[e56cfdb0] | 620 | } // passAdapters |
---|
[6c3744e] | 621 | |
---|
[78dd0da] | 622 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { |
---|
[01aeade] | 623 | NameExpr *opExpr; |
---|
| 624 | if ( isIncr ) { |
---|
| 625 | opExpr = new NameExpr( "?+=?" ); |
---|
| 626 | } else { |
---|
| 627 | opExpr = new NameExpr( "?-=?" ); |
---|
[6c3744e] | 628 | } // if |
---|
[01aeade] | 629 | UntypedExpr *addAssign = new UntypedExpr( opExpr ); |
---|
| 630 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
| 631 | addAssign->get_args().push_back( address->get_arg() ); |
---|
| 632 | } else { |
---|
| 633 | addAssign->get_args().push_back( appExpr->get_args().front() ); |
---|
[6c3744e] | 634 | } // if |
---|
[78dd0da] | 635 | addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) ); |
---|
[01aeade] | 636 | addAssign->get_results().front() = appExpr->get_results().front()->clone(); |
---|
| 637 | if ( appExpr->get_env() ) { |
---|
| 638 | addAssign->set_env( appExpr->get_env() ); |
---|
[6c3744e] | 639 | appExpr->set_env( 0 ); |
---|
| 640 | } // if |
---|
[01aeade] | 641 | appExpr->get_args().clear(); |
---|
| 642 | delete appExpr; |
---|
| 643 | return addAssign; |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) { |
---|
| 647 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) { |
---|
| 648 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
| 649 | if ( varExpr->get_var()->get_name() == "?[?]" ) { |
---|
| 650 | assert( ! appExpr->get_results().empty() ); |
---|
| 651 | assert( appExpr->get_args().size() == 2 ); |
---|
[ffad73a] | 652 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); |
---|
| 653 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); |
---|
[ae63a18] | 654 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers |
---|
[01aeade] | 655 | UntypedExpr *ret = 0; |
---|
[ae63a18] | 656 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer |
---|
[01aeade] | 657 | ret = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
| 658 | } // if |
---|
[ffad73a] | 659 | if ( baseType1 ) { |
---|
[01aeade] | 660 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
| 661 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
[ffad73a] | 662 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); |
---|
[01aeade] | 663 | ret->get_args().push_back( appExpr->get_args().front() ); |
---|
| 664 | ret->get_args().push_back( multiply ); |
---|
[ffad73a] | 665 | } else if ( baseType2 ) { |
---|
[01aeade] | 666 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
| 667 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
[ffad73a] | 668 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); |
---|
[01aeade] | 669 | ret->get_args().push_back( multiply ); |
---|
| 670 | ret->get_args().push_back( appExpr->get_args().back() ); |
---|
| 671 | } // if |
---|
[ffad73a] | 672 | if ( baseType1 || baseType2 ) { |
---|
[01aeade] | 673 | ret->get_results().push_front( appExpr->get_results().front()->clone() ); |
---|
| 674 | if ( appExpr->get_env() ) { |
---|
| 675 | ret->set_env( appExpr->get_env() ); |
---|
| 676 | appExpr->set_env( 0 ); |
---|
| 677 | } // if |
---|
| 678 | appExpr->get_args().clear(); |
---|
| 679 | delete appExpr; |
---|
| 680 | return ret; |
---|
| 681 | } // if |
---|
| 682 | } else if ( varExpr->get_var()->get_name() == "*?" ) { |
---|
| 683 | assert( ! appExpr->get_results().empty() ); |
---|
| 684 | assert( ! appExpr->get_args().empty() ); |
---|
[ffad73a] | 685 | if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
[01aeade] | 686 | Expression *ret = appExpr->get_args().front(); |
---|
| 687 | delete ret->get_results().front(); |
---|
| 688 | ret->get_results().front() = appExpr->get_results().front()->clone(); |
---|
| 689 | if ( appExpr->get_env() ) { |
---|
| 690 | ret->set_env( appExpr->get_env() ); |
---|
| 691 | appExpr->set_env( 0 ); |
---|
| 692 | } // if |
---|
| 693 | appExpr->get_args().clear(); |
---|
| 694 | delete appExpr; |
---|
| 695 | return ret; |
---|
| 696 | } // if |
---|
| 697 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) { |
---|
| 698 | assert( ! appExpr->get_results().empty() ); |
---|
| 699 | assert( appExpr->get_args().size() == 1 ); |
---|
[ffad73a] | 700 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
[01aeade] | 701 | Type *tempType = appExpr->get_results().front()->clone(); |
---|
| 702 | if ( env ) { |
---|
| 703 | env->apply( tempType ); |
---|
| 704 | } // if |
---|
| 705 | ObjectDecl *newObj = makeTemporary( tempType ); |
---|
| 706 | VariableExpr *tempExpr = new VariableExpr( newObj ); |
---|
| 707 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 708 | assignExpr->get_args().push_back( tempExpr->clone() ); |
---|
| 709 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
| 710 | assignExpr->get_args().push_back( address->get_arg()->clone() ); |
---|
| 711 | } else { |
---|
| 712 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); |
---|
| 713 | } // if |
---|
[ffad73a] | 714 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) ); |
---|
[01aeade] | 715 | return new CommaExpr( firstComma, tempExpr ); |
---|
| 716 | } // if |
---|
| 717 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) { |
---|
| 718 | assert( ! appExpr->get_results().empty() ); |
---|
| 719 | assert( appExpr->get_args().size() == 1 ); |
---|
[ffad73a] | 720 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
| 721 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" ); |
---|
[01aeade] | 722 | } // if |
---|
| 723 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { |
---|
| 724 | assert( ! appExpr->get_results().empty() ); |
---|
| 725 | assert( appExpr->get_args().size() == 2 ); |
---|
[ffad73a] | 726 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); |
---|
| 727 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); |
---|
| 728 | if ( baseType1 && baseType2 ) { |
---|
[01aeade] | 729 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); |
---|
| 730 | divide->get_args().push_back( appExpr ); |
---|
[ffad73a] | 731 | divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); |
---|
[01aeade] | 732 | divide->get_results().push_front( appExpr->get_results().front()->clone() ); |
---|
| 733 | if ( appExpr->get_env() ) { |
---|
| 734 | divide->set_env( appExpr->get_env() ); |
---|
| 735 | appExpr->set_env( 0 ); |
---|
| 736 | } // if |
---|
| 737 | return divide; |
---|
[ffad73a] | 738 | } else if ( baseType1 ) { |
---|
[01aeade] | 739 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
| 740 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
[ffad73a] | 741 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); |
---|
[01aeade] | 742 | appExpr->get_args().back() = multiply; |
---|
[ffad73a] | 743 | } else if ( baseType2 ) { |
---|
[01aeade] | 744 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
| 745 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
[ffad73a] | 746 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); |
---|
[01aeade] | 747 | appExpr->get_args().front() = multiply; |
---|
| 748 | } // if |
---|
| 749 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) { |
---|
| 750 | assert( ! appExpr->get_results().empty() ); |
---|
| 751 | assert( appExpr->get_args().size() == 2 ); |
---|
[ffad73a] | 752 | Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ); |
---|
| 753 | if ( baseType ) { |
---|
[01aeade] | 754 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
| 755 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
[ffad73a] | 756 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) ); |
---|
[01aeade] | 757 | appExpr->get_args().back() = multiply; |
---|
| 758 | } // if |
---|
| 759 | } // if |
---|
| 760 | return appExpr; |
---|
| 761 | } // if |
---|
[6c3744e] | 762 | } // if |
---|
[01aeade] | 763 | return 0; |
---|
| 764 | } |
---|
[6c3744e] | 765 | |
---|
[01aeade] | 766 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) { |
---|
[e56cfdb0] | 767 | // std::cerr << "mutate appExpr: "; |
---|
| 768 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
| 769 | // std::cerr << i->first << " "; |
---|
| 770 | // } |
---|
| 771 | // std::cerr << "\n"; |
---|
[01aeade] | 772 | bool oldUseRetval = useRetval; |
---|
| 773 | useRetval = false; |
---|
| 774 | appExpr->get_function()->acceptMutator( *this ); |
---|
| 775 | mutateAll( appExpr->get_args(), *this ); |
---|
| 776 | useRetval = oldUseRetval; |
---|
[ae63a18] | 777 | |
---|
[01aeade] | 778 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
| 779 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); |
---|
| 780 | assert( pointer ); |
---|
| 781 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
| 782 | assert( function ); |
---|
[ae63a18] | 783 | |
---|
[01aeade] | 784 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { |
---|
| 785 | return newExpr; |
---|
| 786 | } // if |
---|
[ae63a18] | 787 | |
---|
[01aeade] | 788 | Expression *ret = appExpr; |
---|
[ae63a18] | 789 | |
---|
[01aeade] | 790 | std::list< Expression *>::iterator arg = appExpr->get_args().begin(); |
---|
| 791 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); |
---|
[ae63a18] | 792 | |
---|
[01aeade] | 793 | std::string typeName; |
---|
| 794 | if ( isPolyRet( function, typeName ) ) { |
---|
| 795 | ret = addPolyRetParam( appExpr, function, typeName, arg ); |
---|
| 796 | } else if ( needsAdapter( function, scopeTyVars ) ) { |
---|
[e56cfdb0] | 797 | // std::cerr << "needs adapter: "; |
---|
| 798 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
| 799 | // std::cerr << i->first << " "; |
---|
| 800 | // } |
---|
| 801 | // std::cerr << "\n"; |
---|
[01aeade] | 802 | // change the application so it calls the adapter rather than the passed function |
---|
| 803 | ret = applyAdapter( appExpr, function, arg, scopeTyVars ); |
---|
| 804 | } // if |
---|
| 805 | arg = appExpr->get_args().begin(); |
---|
[ae63a18] | 806 | |
---|
[01aeade] | 807 | TyVarMap exprTyVars; |
---|
| 808 | makeTyVarMap( function, exprTyVars ); |
---|
[ae63a18] | 809 | |
---|
[01aeade] | 810 | passTypeVars( appExpr, arg, exprTyVars ); |
---|
| 811 | addInferredParams( appExpr, function, arg, exprTyVars ); |
---|
[51b7345] | 812 | |
---|
[01aeade] | 813 | arg = paramBegin; |
---|
[ae63a18] | 814 | |
---|
[01aeade] | 815 | boxParams( appExpr, function, arg, exprTyVars ); |
---|
[6c3744e] | 816 | |
---|
[01aeade] | 817 | passAdapters( appExpr, function, exprTyVars ); |
---|
[6c3744e] | 818 | |
---|
[01aeade] | 819 | return ret; |
---|
| 820 | } |
---|
[6c3744e] | 821 | |
---|
[01aeade] | 822 | Expression *Pass1::mutate( UntypedExpr *expr ) { |
---|
[ffad73a] | 823 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { |
---|
[01aeade] | 824 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
| 825 | if ( name->get_name() == "*?" ) { |
---|
| 826 | Expression *ret = expr->get_args().front(); |
---|
| 827 | expr->get_args().clear(); |
---|
| 828 | delete expr; |
---|
| 829 | return ret->acceptMutator( *this ); |
---|
| 830 | } // if |
---|
| 831 | } // if |
---|
| 832 | } // if |
---|
| 833 | return PolyMutator::mutate( expr ); |
---|
| 834 | } |
---|
[6c3744e] | 835 | |
---|
[01aeade] | 836 | Expression *Pass1::mutate( AddressExpr *addrExpr ) { |
---|
| 837 | assert( ! addrExpr->get_arg()->get_results().empty() ); |
---|
[cf16f94] | 838 | |
---|
| 839 | bool needs = false; |
---|
| 840 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { |
---|
[5f6c42c] | 841 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { |
---|
[cf16f94] | 842 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
| 843 | if ( name->get_name() == "*?" ) { |
---|
| 844 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { |
---|
| 845 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
| 846 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); |
---|
| 847 | assert( pointer ); |
---|
| 848 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
| 849 | assert( function ); |
---|
| 850 | needs = needsAdapter( function, scopeTyVars ); |
---|
| 851 | } // if |
---|
| 852 | } // if |
---|
| 853 | } // if |
---|
| 854 | } // if |
---|
| 855 | } // if |
---|
[01aeade] | 856 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); |
---|
[5f6c42c] | 857 | if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) { |
---|
[01aeade] | 858 | Expression *ret = addrExpr->get_arg(); |
---|
| 859 | delete ret->get_results().front(); |
---|
| 860 | ret->get_results().front() = addrExpr->get_results().front()->clone(); |
---|
| 861 | addrExpr->set_arg( 0 ); |
---|
| 862 | delete addrExpr; |
---|
| 863 | return ret; |
---|
| 864 | } else { |
---|
| 865 | return addrExpr; |
---|
| 866 | } // if |
---|
| 867 | } |
---|
[6c3744e] | 868 | |
---|
[cf16f94] | 869 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) { |
---|
| 870 | if ( retval && returnStmt->get_expr() ) { |
---|
| 871 | assert( ! returnStmt->get_expr()->get_results().empty() ); |
---|
| 872 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
| 873 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { |
---|
[ae63a18] | 874 | // by this point, a cast expr on a polymorphic return value is redundant |
---|
[cf16f94] | 875 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { |
---|
| 876 | returnStmt->set_expr( castExpr->get_arg() ); |
---|
| 877 | returnStmt->get_expr()->set_env( castExpr->get_env() ); |
---|
| 878 | castExpr->set_env( 0 ); |
---|
| 879 | castExpr->set_arg( 0 ); |
---|
| 880 | delete castExpr; |
---|
[5f6c42c] | 881 | } //while |
---|
[cf16f94] | 882 | TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ); |
---|
| 883 | assert( typeInst ); |
---|
| 884 | std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); |
---|
| 885 | if ( assignIter == assignOps.end() ) { |
---|
| 886 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); |
---|
[01aeade] | 887 | } // if |
---|
[cf16f94] | 888 | ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); |
---|
| 889 | Expression *retParm = new NameExpr( retval->get_name() ); |
---|
| 890 | retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); |
---|
| 891 | assignExpr->get_args().push_back( retParm ); |
---|
| 892 | assignExpr->get_args().push_back( returnStmt->get_expr() ); |
---|
| 893 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); |
---|
| 894 | // } else { |
---|
| 895 | // useRetval = true; |
---|
| 896 | // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); |
---|
| 897 | // useRetval = false; |
---|
| 898 | // } // if |
---|
| 899 | returnStmt->set_expr( 0 ); |
---|
[01aeade] | 900 | } else { |
---|
[cf16f94] | 901 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); |
---|
[01aeade] | 902 | } // if |
---|
[cf16f94] | 903 | return returnStmt; |
---|
[01aeade] | 904 | } |
---|
[6c3744e] | 905 | |
---|
[01aeade] | 906 | Type * Pass1::mutate( PointerType *pointerType ) { |
---|
| 907 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 908 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
[ae63a18] | 909 | |
---|
[01aeade] | 910 | Type *ret = Mutator::mutate( pointerType ); |
---|
[ae63a18] | 911 | |
---|
[01aeade] | 912 | scopeTyVars = oldtyVars; |
---|
| 913 | return ret; |
---|
| 914 | } |
---|
[6c3744e] | 915 | |
---|
[01aeade] | 916 | Type * Pass1::mutate( FunctionType *functionType ) { |
---|
| 917 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 918 | makeTyVarMap( functionType, scopeTyVars ); |
---|
[ae63a18] | 919 | |
---|
[01aeade] | 920 | Type *ret = Mutator::mutate( functionType ); |
---|
[ae63a18] | 921 | |
---|
[01aeade] | 922 | scopeTyVars = oldtyVars; |
---|
| 923 | return ret; |
---|
| 924 | } |
---|
[51b7345] | 925 | |
---|
[01aeade] | 926 | void Pass1::doBeginScope() { |
---|
[bdf1954] | 927 | // push a copy of the current map |
---|
[e56cfdb0] | 928 | adapters.push(adapters.top()); |
---|
[01aeade] | 929 | } |
---|
[b1a6d6b] | 930 | |
---|
[01aeade] | 931 | void Pass1::doEndScope() { |
---|
| 932 | adapters.pop(); |
---|
| 933 | } |
---|
[51b7345] | 934 | |
---|
| 935 | ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// |
---|
| 936 | |
---|
[01aeade] | 937 | void Pass2::addAdapters( FunctionType *functionType ) { |
---|
| 938 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
| 939 | std::list< FunctionType *> functions; |
---|
| 940 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
| 941 | Type *orig = (*arg)->get_type(); |
---|
| 942 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter ); |
---|
| 943 | (*arg)->set_type( orig ); |
---|
| 944 | } |
---|
| 945 | std::set< std::string > adaptersDone; |
---|
| 946 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
[bdf1954] | 947 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
[01aeade] | 948 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
| 949 | std::string adapterName = makeAdapterName( mangleName ); |
---|
[68cd1ce] | 950 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); |
---|
[01aeade] | 951 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
| 952 | } |
---|
| 953 | } |
---|
[5f6c42c] | 954 | // deleteAll( functions ); |
---|
[01aeade] | 955 | } |
---|
[6c3744e] | 956 | |
---|
[01aeade] | 957 | template< typename DeclClass > |
---|
| 958 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) { |
---|
| 959 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
[6c3744e] | 960 | |
---|
[01aeade] | 961 | return ret; |
---|
| 962 | } |
---|
[6c3744e] | 963 | |
---|
[01aeade] | 964 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) { |
---|
| 965 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
| 966 | } |
---|
[6c3744e] | 967 | |
---|
[01aeade] | 968 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) { |
---|
| 969 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
| 970 | } |
---|
[6c3744e] | 971 | |
---|
[01aeade] | 972 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) { |
---|
| 973 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
| 974 | if ( typeDecl->get_base() ) { |
---|
| 975 | return handleDecl( typeDecl, typeDecl->get_base() ); |
---|
| 976 | } else { |
---|
| 977 | return Mutator::mutate( typeDecl ); |
---|
| 978 | } |
---|
| 979 | } |
---|
[6c3744e] | 980 | |
---|
[01aeade] | 981 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) { |
---|
| 982 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
| 983 | } |
---|
[6c3744e] | 984 | |
---|
[01aeade] | 985 | Type * Pass2::mutate( PointerType *pointerType ) { |
---|
| 986 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 987 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
[ae63a18] | 988 | |
---|
[01aeade] | 989 | Type *ret = Mutator::mutate( pointerType ); |
---|
[ae63a18] | 990 | |
---|
[01aeade] | 991 | scopeTyVars = oldtyVars; |
---|
| 992 | return ret; |
---|
| 993 | } |
---|
[6c3744e] | 994 | |
---|
[01aeade] | 995 | Type *Pass2::mutate( FunctionType *funcType ) { |
---|
| 996 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 997 | makeTyVarMap( funcType, scopeTyVars ); |
---|
[7754cde] | 998 | |
---|
| 999 | // move polymorphic return type to parameter list |
---|
[01aeade] | 1000 | std::string typeName; |
---|
| 1001 | if ( isPolyRet( funcType, typeName ) ) { |
---|
| 1002 | DeclarationWithType *ret = funcType->get_returnVals().front(); |
---|
| 1003 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) ); |
---|
| 1004 | funcType->get_parameters().push_front( ret ); |
---|
| 1005 | funcType->get_returnVals().pop_front(); |
---|
| 1006 | } |
---|
[7754cde] | 1007 | |
---|
| 1008 | // add size/align and assertions for type parameters to parameter list |
---|
[01aeade] | 1009 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); |
---|
| 1010 | std::list< DeclarationWithType *> inferredParams; |
---|
[78dd0da] | 1011 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); |
---|
[f8b961b] | 1012 | // ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ); |
---|
[01aeade] | 1013 | for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { |
---|
[db0b3ce] | 1014 | ObjectDecl *sizeParm, *alignParm; |
---|
| 1015 | // add all size and alignment parameters to parameter list |
---|
[01aeade] | 1016 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) { |
---|
[78dd0da] | 1017 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); |
---|
[ae63a18] | 1018 | |
---|
[78dd0da] | 1019 | sizeParm = newObj.clone(); |
---|
| 1020 | sizeParm->set_name( sizeofName( &parmType ) ); |
---|
[db0b3ce] | 1021 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
| 1022 | ++last; |
---|
[78dd0da] | 1023 | |
---|
| 1024 | alignParm = newObj.clone(); |
---|
| 1025 | alignParm->set_name( alignofName( &parmType ) ); |
---|
[db0b3ce] | 1026 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
[01aeade] | 1027 | ++last; |
---|
| 1028 | } |
---|
[e56cfdb0] | 1029 | // move all assertions into parameter list |
---|
[01aeade] | 1030 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { |
---|
[f8b961b] | 1031 | // *assert = (*assert)->acceptMutator( *this ); |
---|
[01aeade] | 1032 | inferredParams.push_back( *assert ); |
---|
| 1033 | } |
---|
| 1034 | (*tyParm)->get_assertions().clear(); |
---|
| 1035 | } |
---|
[7754cde] | 1036 | |
---|
| 1037 | // add size/align for generic types to parameter list |
---|
| 1038 | std::set< std::string > seenTypes; //< sizeofName for generic types we've seen |
---|
| 1039 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { |
---|
| 1040 | Type *parmType = (*fnParm)->get_type(); |
---|
| 1041 | if ( ! dynamic_cast< TypeInstType* >( parmType ) && isPolyType( parmType, scopeTyVars ) ) { |
---|
| 1042 | std::string sizeName = sizeofName( parmType ); |
---|
| 1043 | if ( seenTypes.count( sizeName ) ) continue; |
---|
[ae63a18] | 1044 | |
---|
[7754cde] | 1045 | ObjectDecl *sizeParm, *alignParm; |
---|
| 1046 | sizeParm = newObj.clone(); |
---|
| 1047 | sizeParm->set_name( sizeName ); |
---|
| 1048 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
| 1049 | ++last; |
---|
| 1050 | |
---|
| 1051 | alignParm = newObj.clone(); |
---|
| 1052 | alignParm->set_name( alignofName( parmType ) ); |
---|
| 1053 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
| 1054 | ++last; |
---|
| 1055 | |
---|
| 1056 | seenTypes.insert( sizeName ); |
---|
| 1057 | } |
---|
| 1058 | } |
---|
| 1059 | |
---|
| 1060 | // splice assertion parameters into parameter list |
---|
[01aeade] | 1061 | funcType->get_parameters().splice( last, inferredParams ); |
---|
| 1062 | addAdapters( funcType ); |
---|
| 1063 | mutateAll( funcType->get_returnVals(), *this ); |
---|
| 1064 | mutateAll( funcType->get_parameters(), *this ); |
---|
[ae63a18] | 1065 | |
---|
[01aeade] | 1066 | scopeTyVars = oldtyVars; |
---|
| 1067 | return funcType; |
---|
| 1068 | } |
---|
[51b7345] | 1069 | |
---|
| 1070 | ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// |
---|
| 1071 | |
---|
[01aeade] | 1072 | template< typename DeclClass > |
---|
| 1073 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { |
---|
| 1074 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 1075 | makeTyVarMap( type, scopeTyVars ); |
---|
[ae63a18] | 1076 | |
---|
[01aeade] | 1077 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
| 1078 | ScrubTyVars::scrub( decl, scopeTyVars ); |
---|
[6c3744e] | 1079 | |
---|
[01aeade] | 1080 | scopeTyVars = oldtyVars; |
---|
| 1081 | return ret; |
---|
| 1082 | } |
---|
[6c3744e] | 1083 | |
---|
[01aeade] | 1084 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) { |
---|
| 1085 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
| 1086 | } |
---|
[6c3744e] | 1087 | |
---|
[01aeade] | 1088 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) { |
---|
| 1089 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
| 1090 | } |
---|
[6c3744e] | 1091 | |
---|
[01aeade] | 1092 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) { |
---|
| 1093 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
| 1094 | } |
---|
[6c3744e] | 1095 | |
---|
[01aeade] | 1096 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) { |
---|
[f8b961b] | 1097 | // Initializer *init = 0; |
---|
| 1098 | // std::list< Expression *> designators; |
---|
| 1099 | // scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
| 1100 | // if ( typeDecl->get_base() ) { |
---|
| 1101 | // init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators ); |
---|
| 1102 | // } |
---|
| 1103 | // return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init ); |
---|
[51b7345] | 1104 | |
---|
[01aeade] | 1105 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
| 1106 | return Mutator::mutate( typeDecl ); |
---|
| 1107 | } |
---|
[51b7345] | 1108 | |
---|
[01aeade] | 1109 | Type * Pass3::mutate( PointerType *pointerType ) { |
---|
| 1110 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 1111 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
[ae63a18] | 1112 | |
---|
[01aeade] | 1113 | Type *ret = Mutator::mutate( pointerType ); |
---|
[ae63a18] | 1114 | |
---|
[01aeade] | 1115 | scopeTyVars = oldtyVars; |
---|
| 1116 | return ret; |
---|
| 1117 | } |
---|
[6c3744e] | 1118 | |
---|
[01aeade] | 1119 | Type * Pass3::mutate( FunctionType *functionType ) { |
---|
| 1120 | TyVarMap oldtyVars = scopeTyVars; |
---|
| 1121 | makeTyVarMap( functionType, scopeTyVars ); |
---|
[ae63a18] | 1122 | |
---|
[01aeade] | 1123 | Type *ret = Mutator::mutate( functionType ); |
---|
[ae63a18] | 1124 | |
---|
[01aeade] | 1125 | scopeTyVars = oldtyVars; |
---|
| 1126 | return ret; |
---|
[6c3744e] | 1127 | } |
---|
[51b7345] | 1128 | |
---|
[01aeade] | 1129 | Statement *Pass3::mutate( DeclStmt *declStmt ) { |
---|
| 1130 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { |
---|
[ffad73a] | 1131 | if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) { |
---|
[e01559c] | 1132 | // change initialization of a polymorphic value object |
---|
| 1133 | // to allocate storage with alloca |
---|
[ffad73a] | 1134 | Type *declType = objectDecl->get_type(); |
---|
[01aeade] | 1135 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); |
---|
[ffad73a] | 1136 | alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) ); |
---|
[e01559c] | 1137 | |
---|
| 1138 | delete objectDecl->get_init(); |
---|
| 1139 | |
---|
| 1140 | std::list<Expression*> designators; |
---|
| 1141 | objectDecl->set_init( new SingleInit( alloc, designators ) ); |
---|
[01aeade] | 1142 | } |
---|
| 1143 | } |
---|
| 1144 | return Mutator::mutate( declStmt ); |
---|
| 1145 | } |
---|
| 1146 | } // anonymous namespace |
---|
[51b7345] | 1147 | } // namespace GenPoly |
---|
[01aeade] | 1148 | |
---|
[51587aa] | 1149 | // Local Variables: // |
---|
| 1150 | // tab-width: 4 // |
---|
| 1151 | // mode: c++ // |
---|
| 1152 | // compile-command: "make install" // |
---|
| 1153 | // End: // |
---|