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