source: src/GenPoly/Box.cc@ 48ca586

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string stuck-waitfor-destruct with_gc
Last change on this file since 48ca586 was 48ca586, checked in by Aaron Moss <a3moss@…>, 10 years ago

addPolyRetParam now handles polymorphic generic types

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