source: src/GenPoly/Box.cc@ 933667d

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 933667d 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
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Box.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Fri Dec 18 14:53:08 2015
13// Update Count : 217
14//
15
16#include <set>
17#include <stack>
18#include <string>
19#include <iterator>
20#include <algorithm>
21#include <cassert>
22
23#include "Box.h"
24#include "PolyMutator.h"
25#include "FindFunction.h"
26#include "ScrubTyVars.h"
27
28#include "Parser/ParseNode.h"
29
30#include "SynTree/Constant.h"
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"
36
37#include "ResolvExpr/TypeEnvironment.h"
38
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 {
48 namespace {
49 const std::list<Label> noLabels;
50
51 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
52
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
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 );
64 virtual Statement * mutate( ReturnStmt *returnStmt );
65 virtual Type *mutate( PointerType *pointerType );
66 virtual Type * mutate( FunctionType *functionType );
67
68 virtual void doBeginScope();
69 virtual void doEndScope();
70 private:
71 void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
72 /// wraps a function application with a new temporary for the out-parameter return value
73 Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
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 );
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 );
90
91 typedef std::map< std::string, DeclarationWithType *> AdapterMap;
92 std::map< std::string, DeclarationWithType *> assignOps;
93 std::stack< AdapterMap > adapters;
94 DeclarationWithType *retval;
95 bool useRetval;
96 UniqueName tempNamer;
97 };
98
99 /// Moves polymorphic returns in function types to pointer-type parameters, adds type size and assertion parameters to parameter lists as well
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 );
112
113 std::map< UniqueId, std::string > adapterName;
114 };
115
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
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;
138 } // if
139 } // for
140 }
141
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 );
149 }
150
151 ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
152
153 namespace {
154 std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
155 std::stringstream name;
156
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
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
162 // with the same mangled name, so we need to further mangle the names.
163 for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
164 if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
165 name << "P";
166 } else {
167 name << "M";
168 }
169 }
170 name << "_";
171 std::list< DeclarationWithType *> &paramList = function->get_parameters();
172 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
173 if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
174 name << "P";
175 } else {
176 name << "M";
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
186 std::string makeAdapterName( const std::string &mangleName ) {
187 return "_adapter" + mangleName;
188 }
189
190 Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {
191 adapters.push(AdapterMap());
192 }
193
194 // returns true if the given declaration is: (*?=?)(T *, T) for some T (return not checked, but maybe should be)
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() ) ) {
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
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 ) {
218 // what if a nested function uses an assignment operator?
219 // assignOps.clear();
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
230 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
231 if ( functionDecl->get_statements() ) { // empty routine body ?
232 doBeginScope();
233 TyVarMap oldtyVars = scopeTyVars;
234 std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;
235 DeclarationWithType *oldRetval = retval;
236 bool oldUseRetval = useRetval;
237
238 // process polymorphic return value
239 retval = 0;
240 if ( isPolyRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) {
241 retval = functionDecl->get_functionType()->get_returnVals().front();
242
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
249
250 FunctionType *functionType = functionDecl->get_functionType();
251 makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
252 findAssignOps( functionDecl->get_functionType()->get_forall() );
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 ) {
266 std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
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
273 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
274
275 scopeTyVars = oldtyVars;
276 assignOps = oldassignOps;
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";
282 retval = oldRetval;
283 useRetval = oldUseRetval;
284 doEndScope();
285 } // if
286 return functionDecl;
287 }
288
289 TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
290 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
291 return Mutator::mutate( typeDecl );
292 }
293
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 }
302
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;
311
312 }
313
314 void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
315 // pass size/align for type variables
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++;
324 arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
325 arg++;
326 } else {
327 throw SemanticError( "unbound type variable in application ", appExpr );
328 } // if
329 } // if
330 } // for
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 );
337
338 std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
339 std::list< Expression* >::const_iterator fnArg = arg;
340 std::set< std::string > seenTypes; //< names for generic types we've seen
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 }
357 }
358
359 ObjectDecl *Pass1::makeTemporary( Type *type ) {
360 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
361 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
362 return newObj;
363 }
364
365 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
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.
380 if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
381 paramExpr = new AddressExpr( paramExpr );
382 } // if
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;
392 }
393
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 ) {
426 assert( env );
427 Type *concrete = replaceWithConcrete( appExpr, polyType );
428 return addRetParam( appExpr, function, concrete, arg );
429 }
430
431 Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
432 Expression *ret = appExpr;
433 if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
434 ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
435 } // if
436 std::string mangleName = mangleAdapterName( function, tyVars );
437 std::string adapterName = makeAdapterName( mangleName );
438
439 appExpr->get_args().push_front( appExpr->get_function() );
440 appExpr->set_function( new NameExpr( adapterName ) );
441
442 return ret;
443 }
444
445 void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
446 assert( ! arg->get_results().empty() );
447 if ( isPolyType( param, exprTyVars ) ) {
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 {
455 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 );
456 newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
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 }
466
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 }
479
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 }
487
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() );
493 assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
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 }
501
502 void makeRetParm( FunctionType *funcType ) {
503 DeclarationWithType *retParm = funcType->get_returnVals().front();
504
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 );
508
509 // we don't need the return value any more
510 funcType->get_returnVals().clear();
511 }
512
513 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
514 // actually make the adapter type
515 FunctionType *adapter = adaptee->clone();
516 if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
517 makeRetParm( adapter );
518 } // if
519 adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
520 return adapter;
521 }
522
523 Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
524 assert( param );
525 assert( arg );
526 if ( isPolyType( realParam->get_type(), tyVars ) ) {
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
533 } // if
534 return new VariableExpr( param );
535 }
536
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 }
547
548
549
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;
557
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
571
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 );
579 } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
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 );
599 return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
600 }
601
602 void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
603 // collect a list of function types passed as parameters or implicit parameters (assertions)
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
614
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
617 std::set< std::string > adaptersDone;
618
619 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
620 FunctionType *originalFunction = (*funType)->clone();
621 FunctionType *realFunction = (*funType)->clone();
622 std::string mangleName = SymTab::Mangler::mangle( realFunction );
623
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.
626 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
627 adaptersDone.insert( adaptersDone.begin(), mangleName );
628
629 // apply substitution to type variables to figure out what the adapter's type should look like
630 assert( env );
631 env->apply( realFunction );
632 mangleName = SymTab::Mangler::mangle( realFunction );
633 mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
634
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 ) );
642 } // if
643 assert( adapter != adapters.end() );
644
645 // add the appropriate adapter as a parameter
646 appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
647 } // if
648 } // for
649 } // passAdapters
650
651 Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
652 NameExpr *opExpr;
653 if ( isIncr ) {
654 opExpr = new NameExpr( "?+=?" );
655 } else {
656 opExpr = new NameExpr( "?-=?" );
657 } // if
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() );
663 } // if
664 addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) );
665 addAssign->get_results().front() = appExpr->get_results().front()->clone();
666 if ( appExpr->get_env() ) {
667 addAssign->set_env( appExpr->get_env() );
668 appExpr->set_env( 0 );
669 } // if
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 );
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 );
683 assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers
684 UntypedExpr *ret = 0;
685 if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer
686 ret = new UntypedExpr( new NameExpr( "?+?" ) );
687 } // if
688 if ( baseType1 ) {
689 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
690 multiply->get_args().push_back( appExpr->get_args().back() );
691 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
692 ret->get_args().push_back( appExpr->get_args().front() );
693 ret->get_args().push_back( multiply );
694 } else if ( baseType2 ) {
695 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
696 multiply->get_args().push_back( appExpr->get_args().front() );
697 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
698 ret->get_args().push_back( multiply );
699 ret->get_args().push_back( appExpr->get_args().back() );
700 } // if
701 if ( baseType1 || baseType2 ) {
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() );
714 if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) {
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 );
729 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
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
743 CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
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 );
749 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
750 return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
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 );
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 ) {
758 UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
759 divide->get_args().push_back( appExpr );
760 divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
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;
767 } else if ( baseType1 ) {
768 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
769 multiply->get_args().push_back( appExpr->get_args().back() );
770 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
771 appExpr->get_args().back() = multiply;
772 } else if ( baseType2 ) {
773 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
774 multiply->get_args().push_back( appExpr->get_args().front() );
775 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
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 );
781 Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env );
782 if ( baseType ) {
783 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
784 multiply->get_args().push_back( appExpr->get_args().back() );
785 multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
786 appExpr->get_args().back() = multiply;
787 } // if
788 } // if
789 return appExpr;
790 } // if
791 } // if
792 return 0;
793 }
794
795 Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
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";
801 bool oldUseRetval = useRetval;
802 useRetval = false;
803 appExpr->get_function()->acceptMutator( *this );
804 mutateAll( appExpr->get_args(), *this );
805 useRetval = oldUseRetval;
806
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 );
812
813 if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
814 return newExpr;
815 } // if
816
817 Expression *ret = appExpr;
818
819 std::list< Expression *>::iterator arg = appExpr->get_args().begin();
820 std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
821
822 if ( ReferenceToType *polyType = isPolyRet( function ) ) {
823 ret = addPolyRetParam( appExpr, function, polyType, arg );
824 } else if ( needsAdapter( function, scopeTyVars ) ) {
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";
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();
834
835 TyVarMap exprTyVars;
836 makeTyVarMap( function, exprTyVars );
837
838 passTypeVars( appExpr, arg, exprTyVars );
839 addInferredParams( appExpr, function, arg, exprTyVars );
840
841 arg = paramBegin;
842
843 boxParams( appExpr, function, arg, exprTyVars );
844
845 passAdapters( appExpr, function, exprTyVars );
846
847 return ret;
848 }
849
850 Expression *Pass1::mutate( UntypedExpr *expr ) {
851 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
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 }
863
864 Expression *Pass1::mutate( AddressExpr *addrExpr ) {
865 assert( ! addrExpr->get_arg()->get_results().empty() );
866
867 bool needs = false;
868 if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
869 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
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
884 addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
885 if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) {
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 }
896
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() ) {
902 // by this point, a cast expr on a polymorphic return value is redundant
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;
909 } //while
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() );
915 } // if
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 );
928 } else {
929 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
930 } // if
931 return returnStmt;
932 }
933
934 Type * Pass1::mutate( PointerType *pointerType ) {
935 TyVarMap oldtyVars = scopeTyVars;
936 makeTyVarMap( pointerType, scopeTyVars );
937
938 Type *ret = Mutator::mutate( pointerType );
939
940 scopeTyVars = oldtyVars;
941 return ret;
942 }
943
944 Type * Pass1::mutate( FunctionType *functionType ) {
945 TyVarMap oldtyVars = scopeTyVars;
946 makeTyVarMap( functionType, scopeTyVars );
947
948 Type *ret = Mutator::mutate( functionType );
949
950 scopeTyVars = oldtyVars;
951 return ret;
952 }
953
954 void Pass1::doBeginScope() {
955 // push a copy of the current map
956 adapters.push(adapters.top());
957 }
958
959 void Pass1::doEndScope() {
960 adapters.pop();
961 }
962
963////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
964
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 ) {
975 std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
976 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
977 std::string adapterName = makeAdapterName( mangleName );
978 paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
979 adaptersDone.insert( adaptersDone.begin(), mangleName );
980 }
981 }
982// deleteAll( functions );
983 }
984
985 template< typename DeclClass >
986 DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
987 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
988
989 return ret;
990 }
991
992 DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
993 return handleDecl( functionDecl, functionDecl->get_functionType() );
994 }
995
996 ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
997 return handleDecl( objectDecl, objectDecl->get_type() );
998 }
999
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 }
1008
1009 TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
1010 return handleDecl( typedefDecl, typedefDecl->get_base() );
1011 }
1012
1013 Type * Pass2::mutate( PointerType *pointerType ) {
1014 TyVarMap oldtyVars = scopeTyVars;
1015 makeTyVarMap( pointerType, scopeTyVars );
1016
1017 Type *ret = Mutator::mutate( pointerType );
1018
1019 scopeTyVars = oldtyVars;
1020 return ret;
1021 }
1022
1023 Type *Pass2::mutate( FunctionType *funcType ) {
1024 TyVarMap oldtyVars = scopeTyVars;
1025 makeTyVarMap( funcType, scopeTyVars );
1026
1027 // move polymorphic return type to parameter list
1028 if ( isPolyRet( funcType ) ) {
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 }
1034
1035 // add size/align and assertions for type parameters to parameter list
1036 std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
1037 std::list< DeclarationWithType *> inferredParams;
1038 ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
1039// ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 );
1040 for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
1041 ObjectDecl *sizeParm, *alignParm;
1042 // add all size and alignment parameters to parameter list
1043 if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
1044 TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
1045
1046 sizeParm = newObj.clone();
1047 sizeParm->set_name( sizeofName( &parmType ) );
1048 last = funcType->get_parameters().insert( last, sizeParm );
1049 ++last;
1050
1051 alignParm = newObj.clone();
1052 alignParm->set_name( alignofName( &parmType ) );
1053 last = funcType->get_parameters().insert( last, alignParm );
1054 ++last;
1055 }
1056 // move all assertions into parameter list
1057 for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
1058// *assert = (*assert)->acceptMutator( *this );
1059 inferredParams.push_back( *assert );
1060 }
1061 (*tyParm)->get_assertions().clear();
1062 }
1063
1064 // add size/align for generic types to parameter list
1065 std::set< std::string > seenTypes; // sizeofName for generic types we've seen
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;
1071
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
1088 funcType->get_parameters().splice( last, inferredParams );
1089 addAdapters( funcType );
1090 mutateAll( funcType->get_returnVals(), *this );
1091 mutateAll( funcType->get_parameters(), *this );
1092
1093 scopeTyVars = oldtyVars;
1094 return funcType;
1095 }
1096
1097////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
1098
1099 template< typename DeclClass >
1100 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
1101 TyVarMap oldtyVars = scopeTyVars;
1102 makeTyVarMap( type, scopeTyVars );
1103
1104 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
1105 ScrubTyVars::scrub( decl, scopeTyVars );
1106
1107 scopeTyVars = oldtyVars;
1108 return ret;
1109 }
1110
1111 ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
1112 return handleDecl( objectDecl, objectDecl->get_type() );
1113 }
1114
1115 DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
1116 return handleDecl( functionDecl, functionDecl->get_functionType() );
1117 }
1118
1119 TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
1120 return handleDecl( typedefDecl, typedefDecl->get_base() );
1121 }
1122
1123 TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
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 );
1131
1132 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
1133 return Mutator::mutate( typeDecl );
1134 }
1135
1136 Type * Pass3::mutate( PointerType *pointerType ) {
1137 TyVarMap oldtyVars = scopeTyVars;
1138 makeTyVarMap( pointerType, scopeTyVars );
1139
1140 Type *ret = Mutator::mutate( pointerType );
1141
1142 scopeTyVars = oldtyVars;
1143 return ret;
1144 }
1145
1146 Type * Pass3::mutate( FunctionType *functionType ) {
1147 TyVarMap oldtyVars = scopeTyVars;
1148 makeTyVarMap( functionType, scopeTyVars );
1149
1150 Type *ret = Mutator::mutate( functionType );
1151
1152 scopeTyVars = oldtyVars;
1153 return ret;
1154 }
1155
1156 Statement *Pass3::mutate( DeclStmt *declStmt ) {
1157 if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
1158 if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) {
1159 // change initialization of a polymorphic value object
1160 // to allocate storage with alloca
1161 Type *declType = objectDecl->get_type();
1162 UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
1163 alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
1164
1165 delete objectDecl->get_init();
1166
1167 std::list<Expression*> designators;
1168 objectDecl->set_init( new SingleInit( alloc, designators ) );
1169 }
1170 }
1171 return Mutator::mutate( declStmt );
1172 }
1173 } // anonymous namespace
1174} // namespace GenPoly
1175
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.