source: src/GenPoly/Box.cc@ c23f807

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string stuck-waitfor-destruct with_gc
Last change on this file since c23f807 was 69911c11, checked in by Aaron Moss <a3moss@…>, 10 years ago

Restore Mangler to closer to previous behaviour

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