[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 | //
|
---|
[01aeade] | 7 | // Box.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[f6d7e0f] | 11 | // Last Modified By : Rob Schluntz
|
---|
| 12 | // Last Modified On : Wed Jun 24 16:19:07 2015
|
---|
| 13 | // Update Count : 10
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include <set>
|
---|
[b1a6d6b] | 17 | #include <stack>
|
---|
[51b73452] | 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 |
|
---|
[51b73452] | 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"
|
---|
[68cd1ce] | 35 |
|
---|
[51b73452] | 36 | #include "ResolvExpr/TypeEnvironment.h"
|
---|
[68cd1ce] | 37 |
|
---|
[51b73452] | 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 {
|
---|
[01aeade] | 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 );
|
---|
[51b73452] | 63 |
|
---|
[01aeade] | 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 );
|
---|
[51b73452] | 79 |
|
---|
[01aeade] | 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 );
|
---|
[51b73452] | 101 |
|
---|
[01aeade] | 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;
|
---|
[6c3744e] | 126 | } // if
|
---|
[01aeade] | 127 | } // for
|
---|
[6c3744e] | 128 | }
|
---|
| 129 |
|
---|
[01aeade] | 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 );
|
---|
[6c3744e] | 137 | }
|
---|
| 138 |
|
---|
[01aeade] | 139 | ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
|
---|
| 140 |
|
---|
| 141 | namespace {
|
---|
| 142 | std::string makeAdapterName( const std::string &mangleName ) {
|
---|
| 143 | return "_adapter" + mangleName;
|
---|
| 144 | }
|
---|
[6c3744e] | 145 |
|
---|
[01aeade] | 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
|
---|
[6c3744e] | 162 | } // if
|
---|
| 163 | } // if
|
---|
[01aeade] | 164 | return doTransform;
|
---|
| 165 | }
|
---|
[6c3744e] | 166 |
|
---|
[01aeade] | 167 | bool isPolyRet( FunctionType *function, std::string &name ) {
|
---|
| 168 | TyVarMap dummyTyVars;
|
---|
| 169 | return isPolyRet( function, name, dummyTyVars );
|
---|
| 170 | }
|
---|
[6c3744e] | 171 |
|
---|
[01aeade] | 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();
|
---|
[51b73452] | 217 |
|
---|
[01aeade] | 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();
|
---|
[51b73452] | 226 | /// std::cerr << "clear\n";
|
---|
[01aeade] | 227 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
|
---|
| 228 | findAssignOps( functionDecl->get_functionType()->get_forall() );
|
---|
| 229 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
|
---|
[51b73452] | 230 |
|
---|
[01aeade] | 231 | scopeTyVars = oldtyVars;
|
---|
[51b73452] | 232 | /// std::cerr << "end FunctionDecl: ";
|
---|
[6c3744e] | 233 | /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
[51b73452] | 234 | /// std::cerr << i->first << " ";
|
---|
| 235 | /// }
|
---|
| 236 | /// std::cerr << "\n";
|
---|
[01aeade] | 237 | retval = oldRetval;
|
---|
| 238 | useRetval = oldUseRetval;
|
---|
| 239 | // doEndScope();
|
---|
| 240 | } // if
|
---|
| 241 | return functionDecl;
|
---|
| 242 | }
|
---|
[6c3744e] | 243 |
|
---|
[01aeade] | 244 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
|
---|
[51b73452] | 245 | /// std::cerr << "add " << typeDecl->get_name() << "\n";
|
---|
[01aeade] | 246 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
| 247 | return Mutator::mutate( typeDecl );
|
---|
| 248 | }
|
---|
[6c3744e] | 249 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 258 |
|
---|
[01aeade] | 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;
|
---|
[6c3744e] | 267 |
|
---|
[01aeade] | 268 | }
|
---|
[6c3744e] | 269 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 285 |
|
---|
[01aeade] | 286 | ObjectDecl *Pass1::makeTemporary( Type *type ) {
|
---|
[68cd1ce] | 287 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
|
---|
[01aeade] | 288 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
| 289 | return newObj;
|
---|
| 290 | }
|
---|
[6c3744e] | 291 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 308 |
|
---|
[01aeade] | 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++;
|
---|
[51b73452] | 322 | /// stmtsToAdd.push_back( new ExprStmt( noLabels, appExpr ) );
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 330 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 340 |
|
---|
[01aeade] | 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 );
|
---|
[b1a6d6b] | 348 |
|
---|
[01aeade] | 349 | appExpr->get_args().push_front( appExpr->get_function() );
|
---|
| 350 | appExpr->set_function( new NameExpr( adapterName ) );
|
---|
[51b73452] | 351 |
|
---|
[01aeade] | 352 | return ret;
|
---|
| 353 | }
|
---|
[6c3744e] | 354 |
|
---|
[01aeade] | 355 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
|
---|
| 356 | assert( ! arg->get_results().empty() );
|
---|
[a32b204] | 357 | /// if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) {
|
---|
[01aeade] | 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 {
|
---|
[68cd1ce] | 367 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 );
|
---|
[f6d7e0f] | 368 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
|
---|
[01aeade] | 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
|
---|
[51b73452] | 377 | /// }
|
---|
[01aeade] | 378 | }
|
---|
[6c3744e] | 379 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 392 |
|
---|
[01aeade] | 393 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
[51b73452] | 394 | /// std::cout << "function is ";
|
---|
| 395 | /// function->print( std::cout );
|
---|
[01aeade] | 396 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
|
---|
[51b73452] | 397 | /// std::cout << "parameter is ";
|
---|
[b1a6d6b] | 398 | /// (*param)->print( std::fcout );
|
---|
[51b73452] | 399 | /// std::cout << std::endl << "argument is ";
|
---|
| 400 | /// (*arg)->print( std::cout );
|
---|
[01aeade] | 401 | assert( arg != appExpr->get_args().end() );
|
---|
| 402 | addCast( *arg, (*param)->get_type(), exprTyVars );
|
---|
| 403 | boxParam( (*param)->get_type(), *arg, exprTyVars );
|
---|
| 404 | } // for
|
---|
| 405 | }
|
---|
[6c3744e] | 406 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 420 |
|
---|
[01aeade] | 421 | void makeRetParm( FunctionType *funcType ) {
|
---|
| 422 | DeclarationWithType *retParm = funcType->get_returnVals().front();
|
---|
[6c3744e] | 423 |
|
---|
[01aeade] | 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 );
|
---|
[6c3744e] | 427 |
|
---|
[01aeade] | 428 | // we don't need the return value any more
|
---|
| 429 | funcType->get_returnVals().clear();
|
---|
| 430 | }
|
---|
[6c3744e] | 431 |
|
---|
[01aeade] | 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
|
---|
[68cd1ce] | 438 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
|
---|
[01aeade] | 439 | return adapter;
|
---|
| 440 | }
|
---|
[6c3744e] | 441 |
|
---|
[01aeade] | 442 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
|
---|
| 443 | assert( param );
|
---|
| 444 | assert( arg );
|
---|
[51b73452] | 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 );
|
---|
[01aeade] | 451 | if ( isPolyVal( realParam->get_type(), tyVars ) ) {
|
---|
[6c3744e] | 452 | /// if ( dynamic_cast< PointerType *>( arg->get_type() ) ) {
|
---|
[51b73452] | 453 | /// return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
|
---|
| 454 | /// } else {
|
---|
[01aeade] | 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;
|
---|
[51b73452] | 459 | /// }
|
---|
[01aeade] | 460 | } // if
|
---|
| 461 | return new VariableExpr( param );
|
---|
| 462 | }
|
---|
[6c3744e] | 463 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 474 |
|
---|
[b1a6d6b] | 475 |
|
---|
| 476 |
|
---|
[01aeade] | 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;
|
---|
[51b73452] | 484 |
|
---|
[01aeade] | 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
|
---|
[51b73452] | 498 |
|
---|
[01aeade] | 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 );
|
---|
[de62360d] | 526 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
|
---|
[01aeade] | 527 | }
|
---|
[6c3744e] | 528 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 569 |
|
---|
[01aeade] | 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
|
---|
[6c3744e] | 579 | } // if
|
---|
[01aeade] | 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
|
---|
[6c3744e] | 592 | } // if
|
---|
[01aeade] | 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( "?-=?" );
|
---|
[6c3744e] | 602 | } // if
|
---|
[01aeade] | 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() );
|
---|
[6c3744e] | 608 | } // if
|
---|
[01aeade] | 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() );
|
---|
[6c3744e] | 613 | appExpr->set_env( 0 );
|
---|
| 614 | } // if
|
---|
[01aeade] | 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
|
---|
[6c3744e] | 736 | } // if
|
---|
[01aeade] | 737 | return 0;
|
---|
| 738 | }
|
---|
[6c3744e] | 739 |
|
---|
[01aeade] | 740 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
|
---|
[51b73452] | 741 | /// std::cerr << "mutate appExpr: ";
|
---|
[6c3744e] | 742 | /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
[51b73452] | 743 | /// std::cerr << i->first << " ";
|
---|
| 744 | /// }
|
---|
| 745 | /// std::cerr << "\n";
|
---|
[01aeade] | 746 | bool oldUseRetval = useRetval;
|
---|
| 747 | useRetval = false;
|
---|
| 748 | appExpr->get_function()->acceptMutator( *this );
|
---|
| 749 | mutateAll( appExpr->get_args(), *this );
|
---|
| 750 | useRetval = oldUseRetval;
|
---|
[51b73452] | 751 |
|
---|
[01aeade] | 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 );
|
---|
[51b73452] | 757 |
|
---|
[01aeade] | 758 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
|
---|
| 759 | return newExpr;
|
---|
| 760 | } // if
|
---|
[51b73452] | 761 |
|
---|
[01aeade] | 762 | Expression *ret = appExpr;
|
---|
[51b73452] | 763 |
|
---|
[01aeade] | 764 | std::list< Expression *>::iterator arg = appExpr->get_args().begin();
|
---|
| 765 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
|
---|
[51b73452] | 766 |
|
---|
[01aeade] | 767 | std::string typeName;
|
---|
| 768 | if ( isPolyRet( function, typeName ) ) {
|
---|
| 769 | ret = addPolyRetParam( appExpr, function, typeName, arg );
|
---|
| 770 | } else if ( needsAdapter( function, scopeTyVars ) ) {
|
---|
[51b73452] | 771 | /// std::cerr << "needs adapter: ";
|
---|
[6c3744e] | 772 | /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
[51b73452] | 773 | /// std::cerr << i->first << " ";
|
---|
| 774 | /// }
|
---|
| 775 | /// std::cerr << "\n";
|
---|
[01aeade] | 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();
|
---|
[51b73452] | 780 |
|
---|
[01aeade] | 781 | TyVarMap exprTyVars;
|
---|
| 782 | makeTyVarMap( function, exprTyVars );
|
---|
[51b73452] | 783 |
|
---|
[01aeade] | 784 | passTypeVars( appExpr, arg, exprTyVars );
|
---|
| 785 | addInferredParams( appExpr, function, arg, exprTyVars );
|
---|
[51b73452] | 786 |
|
---|
[01aeade] | 787 | arg = paramBegin;
|
---|
[51b73452] | 788 |
|
---|
[01aeade] | 789 | boxParams( appExpr, function, arg, exprTyVars );
|
---|
[6c3744e] | 790 |
|
---|
[01aeade] | 791 | passAdapters( appExpr, function, exprTyVars );
|
---|
[6c3744e] | 792 |
|
---|
[01aeade] | 793 | return ret;
|
---|
| 794 | }
|
---|
[6c3744e] | 795 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 809 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 824 |
|
---|
[01aeade] | 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() ) {
|
---|
[51b73452] | 837 | /// retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 861 |
|
---|
[01aeade] | 862 | Type * Pass1::mutate( PointerType *pointerType ) {
|
---|
| 863 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 864 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
[51b73452] | 865 |
|
---|
[01aeade] | 866 | Type *ret = Mutator::mutate( pointerType );
|
---|
[51b73452] | 867 |
|
---|
[01aeade] | 868 | scopeTyVars = oldtyVars;
|
---|
| 869 | return ret;
|
---|
| 870 | }
|
---|
[6c3744e] | 871 |
|
---|
[01aeade] | 872 | Type * Pass1::mutate( FunctionType *functionType ) {
|
---|
| 873 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 874 | makeTyVarMap( functionType, scopeTyVars );
|
---|
[51b73452] | 875 |
|
---|
[01aeade] | 876 | Type *ret = Mutator::mutate( functionType );
|
---|
[51b73452] | 877 |
|
---|
[01aeade] | 878 | scopeTyVars = oldtyVars;
|
---|
| 879 | return ret;
|
---|
| 880 | }
|
---|
[51b73452] | 881 |
|
---|
[01aeade] | 882 | void Pass1::doBeginScope() {
|
---|
| 883 | adapters.push(AdapterMap());
|
---|
| 884 | }
|
---|
[b1a6d6b] | 885 |
|
---|
[01aeade] | 886 | void Pass1::doEndScope() {
|
---|
| 887 | adapters.pop();
|
---|
| 888 | }
|
---|
[51b73452] | 889 |
|
---|
| 890 | ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
|
---|
| 891 |
|
---|
[01aeade] | 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 );
|
---|
[68cd1ce] | 907 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
|
---|
[01aeade] | 908 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
| 909 | }
|
---|
| 910 | }
|
---|
[51b73452] | 911 | /// deleteAll( functions );
|
---|
[01aeade] | 912 | }
|
---|
[6c3744e] | 913 |
|
---|
[01aeade] | 914 | template< typename DeclClass >
|
---|
| 915 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
|
---|
| 916 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
[6c3744e] | 917 |
|
---|
[01aeade] | 918 | return ret;
|
---|
| 919 | }
|
---|
[6c3744e] | 920 |
|
---|
[01aeade] | 921 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
|
---|
| 922 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
| 923 | }
|
---|
[6c3744e] | 924 |
|
---|
[01aeade] | 925 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
|
---|
| 926 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
| 927 | }
|
---|
[6c3744e] | 928 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 937 |
|
---|
[01aeade] | 938 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
|
---|
| 939 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
| 940 | }
|
---|
[6c3744e] | 941 |
|
---|
[01aeade] | 942 | Type * Pass2::mutate( PointerType *pointerType ) {
|
---|
| 943 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 944 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
[51b73452] | 945 |
|
---|
[01aeade] | 946 | Type *ret = Mutator::mutate( pointerType );
|
---|
[51b73452] | 947 |
|
---|
[01aeade] | 948 | scopeTyVars = oldtyVars;
|
---|
| 949 | return ret;
|
---|
| 950 | }
|
---|
[6c3744e] | 951 |
|
---|
[01aeade] | 952 | Type *Pass2::mutate( FunctionType *funcType ) {
|
---|
| 953 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 954 | makeTyVarMap( funcType, scopeTyVars );
|
---|
[51b73452] | 955 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 963 |
|
---|
[01aeade] | 964 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
|
---|
| 965 | std::list< DeclarationWithType *> inferredParams;
|
---|
[68cd1ce] | 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 );
|
---|
[01aeade] | 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 ) {
|
---|
[6c3744e] | 977 | /// *assert = (*assert)->acceptMutator( *this );
|
---|
[01aeade] | 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 );
|
---|
[51b73452] | 987 |
|
---|
[01aeade] | 988 | scopeTyVars = oldtyVars;
|
---|
| 989 | return funcType;
|
---|
| 990 | }
|
---|
[51b73452] | 991 |
|
---|
| 992 | ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
|
---|
| 993 |
|
---|
[01aeade] | 994 | template< typename DeclClass >
|
---|
| 995 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
|
---|
| 996 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 997 | makeTyVarMap( type, scopeTyVars );
|
---|
[51b73452] | 998 |
|
---|
[01aeade] | 999 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
| 1000 | ScrubTyVars::scrub( decl, scopeTyVars );
|
---|
[6c3744e] | 1001 |
|
---|
[01aeade] | 1002 | scopeTyVars = oldtyVars;
|
---|
| 1003 | return ret;
|
---|
| 1004 | }
|
---|
[6c3744e] | 1005 |
|
---|
[01aeade] | 1006 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
|
---|
| 1007 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
| 1008 | }
|
---|
[6c3744e] | 1009 |
|
---|
[01aeade] | 1010 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
|
---|
| 1011 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
| 1012 | }
|
---|
[6c3744e] | 1013 |
|
---|
[01aeade] | 1014 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
|
---|
| 1015 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
| 1016 | }
|
---|
[6c3744e] | 1017 |
|
---|
[01aeade] | 1018 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
|
---|
[51b73452] | 1019 | /// Initializer *init = 0;
|
---|
| 1020 | /// std::list< Expression *> designators;
|
---|
| 1021 | /// scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
[6c3744e] | 1022 | /// if ( typeDecl->get_base() ) {
|
---|
[51b73452] | 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 |
|
---|
[01aeade] | 1027 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
| 1028 | return Mutator::mutate( typeDecl );
|
---|
| 1029 | }
|
---|
[51b73452] | 1030 |
|
---|
[01aeade] | 1031 | Type * Pass3::mutate( PointerType *pointerType ) {
|
---|
| 1032 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 1033 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
[51b73452] | 1034 |
|
---|
[01aeade] | 1035 | Type *ret = Mutator::mutate( pointerType );
|
---|
[51b73452] | 1036 |
|
---|
[01aeade] | 1037 | scopeTyVars = oldtyVars;
|
---|
| 1038 | return ret;
|
---|
| 1039 | }
|
---|
[6c3744e] | 1040 |
|
---|
[01aeade] | 1041 | Type * Pass3::mutate( FunctionType *functionType ) {
|
---|
| 1042 | TyVarMap oldtyVars = scopeTyVars;
|
---|
| 1043 | makeTyVarMap( functionType, scopeTyVars );
|
---|
[51b73452] | 1044 |
|
---|
[01aeade] | 1045 | Type *ret = Mutator::mutate( functionType );
|
---|
[51b73452] | 1046 |
|
---|
[01aeade] | 1047 | scopeTyVars = oldtyVars;
|
---|
| 1048 | return ret;
|
---|
[6c3744e] | 1049 | }
|
---|
[51b73452] | 1050 |
|
---|
[01aeade] | 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
|
---|
[51b73452] | 1067 | } // namespace GenPoly
|
---|
[01aeade] | 1068 |
|
---|
[51587aa] | 1069 | // Local Variables: //
|
---|
| 1070 | // tab-width: 4 //
|
---|
| 1071 | // mode: c++ //
|
---|
| 1072 | // compile-command: "make install" //
|
---|
| 1073 | // End: //
|
---|