source: src/GenPoly/Box.cc @ ffad73a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since ffad73a was ffad73a, checked in by Aaron Moss <a3moss@…>, 8 years ago

Refactored isPolyType and friends to account for polymorphic generic types

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