source: src/GenPoly/Box.cc@ e497c1d

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 e497c1d was e497c1d, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

fix adapter passing in function application when two types unify to the same type

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