source: src/GenPoly/Box.cc@ 36874e4

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 with_gc
Last change on this file since 36874e4 was b18b0b5, checked in by Aaron Moss <a3moss@…>, 10 years ago

Replace generic type function parameters with void*

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