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