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