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