source: src/GenPoly/Box.cc@ dfee306

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 dfee306 was 82dd287, checked in by Aaron Moss <a3moss@…>, 10 years ago

Doc comments

  • Property mode set to 100644
File size: 44.6 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 : Wed Jun 24 16:19:07 2015
13// Update Count : 10
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 std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
530 std::list< FunctionType *> functions;
531 for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
532 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
533 findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter );
534 } // for
535 } // for
536 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
537 findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
538 } // for
539 std::set< std::string > adaptersDone;
540 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
541 FunctionType *realFunction = (*funType)->clone();
542 assert( env );
543 env->apply( realFunction );
544
545 std::string mangleName = SymTab::Mangler::mangle( realFunction );
546 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
547 AdapterMap & adapters = Pass1::adapters.top();
548 AdapterMap::iterator adapter = adapters.find( mangleName );
549
550 if ( needsAdapter( realFunction, exprTyVars, true ) ) {
551 // the function still contains type variables, which means we are in a polymorphic
552 // context and the adapter function is a parameter - call the parameter and don't
553 // create a new adapter.
554 appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
555 continue;
556 } else if ( adapter == adapters.end() ) {
557 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
558 adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
559 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
560 } // if
561 assert( adapter != adapters.end() );
562 appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
563 // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
564 adaptersDone.insert( adaptersDone.begin(), mangleName );
565 } // if
566 } // for
567 }
568
569 TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
570 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
571 return isPolyType( ptr->get_base(), env, tyVars );
572 } else if ( env ) {
573 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
574 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
575 return isPolyPtr( newType, env, tyVars );
576 } // if
577 } // if
578 } // if
579 return 0;
580 }
581
582 TypeInstType *isPolyPtrPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
583 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
584 return isPolyPtr( ptr->get_base(), env, tyVars );
585 } else if ( env ) {
586 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
587 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
588 return isPolyPtrPtr( newType, env, tyVars );
589 } // if
590 } // if
591 } // if
592 return 0;
593 }
594
595 Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, std::string polyName, bool isIncr ) {
596 NameExpr *opExpr;
597 if ( isIncr ) {
598 opExpr = new NameExpr( "?+=?" );
599 } else {
600 opExpr = new NameExpr( "?-=?" );
601 } // if
602 UntypedExpr *addAssign = new UntypedExpr( opExpr );
603 if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
604 addAssign->get_args().push_back( address->get_arg() );
605 } else {
606 addAssign->get_args().push_back( appExpr->get_args().front() );
607 } // if
608 addAssign->get_args().push_back( new NameExpr( polyName ) );
609 addAssign->get_results().front() = appExpr->get_results().front()->clone();
610 if ( appExpr->get_env() ) {
611 addAssign->set_env( appExpr->get_env() );
612 appExpr->set_env( 0 );
613 } // if
614 appExpr->get_args().clear();
615 delete appExpr;
616 return addAssign;
617 }
618
619 Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
620 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) {
621 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
622 if ( varExpr->get_var()->get_name() == "?[?]" ) {
623 assert( ! appExpr->get_results().empty() );
624 assert( appExpr->get_args().size() == 2 );
625 TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars );
626 TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars );
627 assert( ! typeInst1 || ! typeInst2 );
628 UntypedExpr *ret = 0;
629 if ( typeInst1 || typeInst2 ) {
630 ret = new UntypedExpr( new NameExpr( "?+?" ) );
631 } // if
632 if ( typeInst1 ) {
633 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
634 multiply->get_args().push_back( appExpr->get_args().back() );
635 multiply->get_args().push_back( new NameExpr( typeInst1->get_name() ) );
636 ret->get_args().push_back( appExpr->get_args().front() );
637 ret->get_args().push_back( multiply );
638 } else if ( typeInst2 ) {
639 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
640 multiply->get_args().push_back( appExpr->get_args().front() );
641 multiply->get_args().push_back( new NameExpr( typeInst2->get_name() ) );
642 ret->get_args().push_back( multiply );
643 ret->get_args().push_back( appExpr->get_args().back() );
644 } // if
645 if ( typeInst1 || typeInst2 ) {
646 ret->get_results().push_front( appExpr->get_results().front()->clone() );
647 if ( appExpr->get_env() ) {
648 ret->set_env( appExpr->get_env() );
649 appExpr->set_env( 0 );
650 } // if
651 appExpr->get_args().clear();
652 delete appExpr;
653 return ret;
654 } // if
655 } else if ( varExpr->get_var()->get_name() == "*?" ) {
656 assert( ! appExpr->get_results().empty() );
657 assert( ! appExpr->get_args().empty() );
658 if ( isPolyType( appExpr->get_results().front(), env, scopeTyVars ) ) {
659 Expression *ret = appExpr->get_args().front();
660 delete ret->get_results().front();
661 ret->get_results().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() == "?++" || varExpr->get_var()->get_name() == "?--" ) {
671 assert( ! appExpr->get_results().empty() );
672 assert( appExpr->get_args().size() == 1 );
673 if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) {
674 Type *tempType = appExpr->get_results().front()->clone();
675 if ( env ) {
676 env->apply( tempType );
677 } // if
678 ObjectDecl *newObj = makeTemporary( tempType );
679 VariableExpr *tempExpr = new VariableExpr( newObj );
680 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
681 assignExpr->get_args().push_back( tempExpr->clone() );
682 if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
683 assignExpr->get_args().push_back( address->get_arg()->clone() );
684 } else {
685 assignExpr->get_args().push_back( appExpr->get_args().front()->clone() );
686 } // if
687 CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, typeInst->get_name(), varExpr->get_var()->get_name() == "?++" ) );
688 return new CommaExpr( firstComma, tempExpr );
689 } // if
690 } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
691 assert( ! appExpr->get_results().empty() );
692 assert( appExpr->get_args().size() == 1 );
693 if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) {
694 return makeIncrDecrExpr( appExpr, typeInst->get_name(), varExpr->get_var()->get_name() == "++?" );
695 } // if
696 } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
697 assert( ! appExpr->get_results().empty() );
698 assert( appExpr->get_args().size() == 2 );
699 TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars );
700 TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars );
701 if ( typeInst1 && typeInst2 ) {
702 UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
703 divide->get_args().push_back( appExpr );
704 divide->get_args().push_back( new NameExpr( typeInst1->get_name() ) );
705 divide->get_results().push_front( appExpr->get_results().front()->clone() );
706 if ( appExpr->get_env() ) {
707 divide->set_env( appExpr->get_env() );
708 appExpr->set_env( 0 );
709 } // if
710 return divide;
711 } else if ( typeInst1 ) {
712 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
713 multiply->get_args().push_back( appExpr->get_args().back() );
714 multiply->get_args().push_back( new NameExpr( typeInst1->get_name() ) );
715 appExpr->get_args().back() = multiply;
716 } else if ( typeInst2 ) {
717 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
718 multiply->get_args().push_back( appExpr->get_args().front() );
719 multiply->get_args().push_back( new NameExpr( typeInst2->get_name() ) );
720 appExpr->get_args().front() = multiply;
721 } // if
722 } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
723 assert( ! appExpr->get_results().empty() );
724 assert( appExpr->get_args().size() == 2 );
725 TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars );
726 if ( typeInst ) {
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( typeInst->get_name() ) );
730 appExpr->get_args().back() = multiply;
731 } // if
732 } // if
733 return appExpr;
734 } // if
735 } // if
736 return 0;
737 }
738
739 Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
740/// std::cerr << "mutate appExpr: ";
741/// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
742/// std::cerr << i->first << " ";
743/// }
744/// std::cerr << "\n";
745 bool oldUseRetval = useRetval;
746 useRetval = false;
747 appExpr->get_function()->acceptMutator( *this );
748 mutateAll( appExpr->get_args(), *this );
749 useRetval = oldUseRetval;
750
751 assert( ! appExpr->get_function()->get_results().empty() );
752 PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
753 assert( pointer );
754 FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
755 assert( function );
756
757 if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
758 return newExpr;
759 } // if
760
761 Expression *ret = appExpr;
762
763 std::list< Expression *>::iterator arg = appExpr->get_args().begin();
764 std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
765
766 std::string typeName;
767 if ( isPolyRet( function, typeName ) ) {
768 ret = addPolyRetParam( appExpr, function, typeName, arg );
769 } else if ( needsAdapter( function, scopeTyVars ) ) {
770/// std::cerr << "needs adapter: ";
771/// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
772/// std::cerr << i->first << " ";
773/// }
774/// std::cerr << "\n";
775 // change the application so it calls the adapter rather than the passed function
776 ret = applyAdapter( appExpr, function, arg, scopeTyVars );
777 } // if
778 arg = appExpr->get_args().begin();
779
780 TyVarMap exprTyVars;
781 makeTyVarMap( function, exprTyVars );
782
783 passTypeVars( appExpr, arg, exprTyVars );
784 addInferredParams( appExpr, function, arg, exprTyVars );
785
786 arg = paramBegin;
787
788 boxParams( appExpr, function, arg, exprTyVars );
789
790 passAdapters( appExpr, function, exprTyVars );
791
792 return ret;
793 }
794
795 Expression *Pass1::mutate( UntypedExpr *expr ) {
796 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars ) ) {
797 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
798 if ( name->get_name() == "*?" ) {
799 Expression *ret = expr->get_args().front();
800 expr->get_args().clear();
801 delete expr;
802 return ret->acceptMutator( *this );
803 } // if
804 } // if
805 } // if
806 return PolyMutator::mutate( expr );
807 }
808
809 Expression *Pass1::mutate( AddressExpr *addrExpr ) {
810 assert( ! addrExpr->get_arg()->get_results().empty() );
811 addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
812 if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) ) {
813 Expression *ret = addrExpr->get_arg();
814 delete ret->get_results().front();
815 ret->get_results().front() = addrExpr->get_results().front()->clone();
816 addrExpr->set_arg( 0 );
817 delete addrExpr;
818 return ret;
819 } else {
820 return addrExpr;
821 } // if
822 }
823
824 Statement * Pass1::mutate(ReturnStmt *retStmt) {
825 // a cast expr on a polymorphic return value is either redundant or invalid
826 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( retStmt->get_expr() ) ) {
827 retStmt->set_expr( castExpr->get_arg() );
828 retStmt->get_expr()->set_env( castExpr->get_env() );
829 castExpr->set_env( 0 );
830 castExpr->set_arg( 0 );
831 delete castExpr;
832 }
833 if ( retval && retStmt->get_expr() ) {
834 assert( ! retStmt->get_expr()->get_results().empty() );
835 if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
836/// retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
837 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() );
838 assert( typeInst );
839 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
840 if ( assignIter == assignOps.end() ) {
841 throw SemanticError( "Attempt to return dtype or ftype object in ", retStmt->get_expr() );
842 } // if
843 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
844 Expression *retParm = new NameExpr( retval->get_name() );
845 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
846 assignExpr->get_args().push_back( retParm );
847 assignExpr->get_args().push_back( retStmt->get_expr() );
848 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
849 } else {
850 useRetval = true;
851 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( retStmt->get_expr() ) ) );
852 useRetval = false;
853 } // if
854 retStmt->set_expr( 0 );
855 } else {
856 retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
857 } // if
858 return retStmt;
859 }
860
861 Type * Pass1::mutate( PointerType *pointerType ) {
862 TyVarMap oldtyVars = scopeTyVars;
863 makeTyVarMap( pointerType, scopeTyVars );
864
865 Type *ret = Mutator::mutate( pointerType );
866
867 scopeTyVars = oldtyVars;
868 return ret;
869 }
870
871 Type * Pass1::mutate( FunctionType *functionType ) {
872 TyVarMap oldtyVars = scopeTyVars;
873 makeTyVarMap( functionType, scopeTyVars );
874
875 Type *ret = Mutator::mutate( functionType );
876
877 scopeTyVars = oldtyVars;
878 return ret;
879 }
880
881 void Pass1::doBeginScope() {
882 adapters.push(AdapterMap());
883 }
884
885 void Pass1::doEndScope() {
886 adapters.pop();
887 }
888
889////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
890
891 Pass2::Pass2() {}
892
893 void Pass2::addAdapters( FunctionType *functionType ) {
894 std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
895 std::list< FunctionType *> functions;
896 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
897 Type *orig = (*arg)->get_type();
898 findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
899 (*arg)->set_type( orig );
900 }
901 std::set< std::string > adaptersDone;
902 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
903 std::string mangleName = SymTab::Mangler::mangle( *funType );
904 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
905 std::string adapterName = makeAdapterName( mangleName );
906 paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
907 adaptersDone.insert( adaptersDone.begin(), mangleName );
908 }
909 }
910/// deleteAll( functions );
911 }
912
913 template< typename DeclClass >
914 DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
915 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
916
917 return ret;
918 }
919
920 DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
921 return handleDecl( functionDecl, functionDecl->get_functionType() );
922 }
923
924 ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
925 return handleDecl( objectDecl, objectDecl->get_type() );
926 }
927
928 TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) {
929 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
930 if ( typeDecl->get_base() ) {
931 return handleDecl( typeDecl, typeDecl->get_base() );
932 } else {
933 return Mutator::mutate( typeDecl );
934 }
935 }
936
937 TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
938 return handleDecl( typedefDecl, typedefDecl->get_base() );
939 }
940
941 Type * Pass2::mutate( PointerType *pointerType ) {
942 TyVarMap oldtyVars = scopeTyVars;
943 makeTyVarMap( pointerType, scopeTyVars );
944
945 Type *ret = Mutator::mutate( pointerType );
946
947 scopeTyVars = oldtyVars;
948 return ret;
949 }
950
951 Type *Pass2::mutate( FunctionType *funcType ) {
952 TyVarMap oldtyVars = scopeTyVars;
953 makeTyVarMap( funcType, scopeTyVars );
954
955 std::string typeName;
956 if ( isPolyRet( funcType, typeName ) ) {
957 DeclarationWithType *ret = funcType->get_returnVals().front();
958 ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
959 funcType->get_parameters().push_front( ret );
960 funcType->get_returnVals().pop_front();
961 }
962
963 std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
964 std::list< DeclarationWithType *> inferredParams;
965 ObjectDecl *newObj = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
966/// ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 );
967 for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
968 ObjectDecl *thisParm;
969 if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
970 thisParm = newObj->clone();
971 thisParm->set_name( (*tyParm)->get_name() );
972 last = funcType->get_parameters().insert( last, thisParm );
973 ++last;
974 }
975 for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
976/// *assert = (*assert)->acceptMutator( *this );
977 inferredParams.push_back( *assert );
978 }
979 (*tyParm)->get_assertions().clear();
980 }
981 delete newObj;
982 funcType->get_parameters().splice( last, inferredParams );
983 addAdapters( funcType );
984 mutateAll( funcType->get_returnVals(), *this );
985 mutateAll( funcType->get_parameters(), *this );
986
987 scopeTyVars = oldtyVars;
988 return funcType;
989 }
990
991////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
992
993 template< typename DeclClass >
994 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
995 TyVarMap oldtyVars = scopeTyVars;
996 makeTyVarMap( type, scopeTyVars );
997
998 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
999 ScrubTyVars::scrub( decl, scopeTyVars );
1000
1001 scopeTyVars = oldtyVars;
1002 return ret;
1003 }
1004
1005 ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
1006 return handleDecl( objectDecl, objectDecl->get_type() );
1007 }
1008
1009 DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
1010 return handleDecl( functionDecl, functionDecl->get_functionType() );
1011 }
1012
1013 TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
1014 return handleDecl( typedefDecl, typedefDecl->get_base() );
1015 }
1016
1017 TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
1018/// Initializer *init = 0;
1019/// std::list< Expression *> designators;
1020/// scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
1021/// if ( typeDecl->get_base() ) {
1022/// init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators );
1023/// }
1024/// return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init );
1025
1026 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
1027 return Mutator::mutate( typeDecl );
1028 }
1029
1030 Type * Pass3::mutate( PointerType *pointerType ) {
1031 TyVarMap oldtyVars = scopeTyVars;
1032 makeTyVarMap( pointerType, scopeTyVars );
1033
1034 Type *ret = Mutator::mutate( pointerType );
1035
1036 scopeTyVars = oldtyVars;
1037 return ret;
1038 }
1039
1040 Type * Pass3::mutate( FunctionType *functionType ) {
1041 TyVarMap oldtyVars = scopeTyVars;
1042 makeTyVarMap( functionType, scopeTyVars );
1043
1044 Type *ret = Mutator::mutate( functionType );
1045
1046 scopeTyVars = oldtyVars;
1047 return ret;
1048 }
1049
1050 Statement *Pass3::mutate( DeclStmt *declStmt ) {
1051 if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
1052 if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) {
1053 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() );
1054 assert( typeInst );
1055 UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
1056 alloc->get_args().push_back( new NameExpr( typeInst->get_name() ) );
1057 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
1058 assign->get_args().push_back( new VariableExpr( objectDecl ) );
1059 assign->get_args().push_back( alloc );
1060 stmtsToAddAfter.push_back( new ExprStmt( noLabels, assign ) );
1061 }
1062 }
1063 return Mutator::mutate( declStmt );
1064 }
1065 } // anonymous namespace
1066} // namespace GenPoly
1067
1068// Local Variables: //
1069// tab-width: 4 //
1070// mode: c++ //
1071// compile-command: "make install" //
1072// End: //
Note: See TracBrowser for help on using the repository browser.