| 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 | // GenPoly.cc -- 
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Tue Dec 15 16:11:18 2015
 | 
|---|
| 13 | // Update Count     : 13
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "GenPoly.h"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "SynTree/Expression.h"
 | 
|---|
| 19 | #include "SynTree/Type.h"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include <iostream>
 | 
|---|
| 22 | using namespace std;
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace GenPoly {
 | 
|---|
| 25 |         bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
 | 
|---|
| 26 |                 if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
 | 
|---|
| 27 |                         return true;
 | 
|---|
| 28 |                 } // if
 | 
|---|
| 29 |                 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
 | 
|---|
| 30 |                         if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
 | 
|---|
| 31 |                                 return true;
 | 
|---|
| 32 |                         } // if
 | 
|---|
| 33 |                 } // for
 | 
|---|
| 34 |                 return false;
 | 
|---|
| 35 |         }
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         ReferenceToType *isPolyRet( FunctionType *function ) {
 | 
|---|
| 38 |                 if ( ! function->get_returnVals().empty() ) {
 | 
|---|
| 39 |                         TyVarMap forallTypes( (TypeDecl::Kind)-1 );
 | 
|---|
| 40 |                         makeTyVarMap( function, forallTypes );
 | 
|---|
| 41 |                         return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
 | 
|---|
| 42 |                 } // if
 | 
|---|
| 43 |                 return 0;
 | 
|---|
| 44 |         }
 | 
|---|
| 45 | 
 | 
|---|
| 46 |         namespace {
 | 
|---|
| 47 |                 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
 | 
|---|
| 48 |                 bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
 | 
|---|
| 49 |                         for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
 | 
|---|
| 50 |                                 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
 | 
|---|
| 51 |                                 assert(paramType && "Aggregate parameters should be type expressions");
 | 
|---|
| 52 |                                 if ( isPolyType( paramType->get_type(), env ) ) return true;
 | 
|---|
| 53 |                         }
 | 
|---|
| 54 |                         return false;
 | 
|---|
| 55 |                 }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |                 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
 | 
|---|
| 58 |                 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
 | 
|---|
| 59 |                         for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
 | 
|---|
| 60 |                                 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
 | 
|---|
| 61 |                                 assert(paramType && "Aggregate parameters should be type expressions");
 | 
|---|
| 62 |                                 if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
 | 
|---|
| 63 |                         }
 | 
|---|
| 64 |                         return false;
 | 
|---|
| 65 |                 }
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         Type *isPolyType( Type *type, const TypeSubstitution *env ) {
 | 
|---|
| 69 |                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
 | 
|---|
| 70 |                         if ( env ) {
 | 
|---|
| 71 |                                 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 72 |                                         return isPolyType( newType, env );
 | 
|---|
| 73 |                                 } // if
 | 
|---|
| 74 |                         } // if
 | 
|---|
| 75 |                         return type;
 | 
|---|
| 76 |                 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
 | 
|---|
| 77 |                         if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
 | 
|---|
| 78 |                 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
 | 
|---|
| 79 |                         if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
 | 
|---|
| 80 |                 }
 | 
|---|
| 81 |                 return 0;
 | 
|---|
| 82 |         }
 | 
|---|
| 83 |         
 | 
|---|
| 84 |         Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
 | 
|---|
| 85 |                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
 | 
|---|
| 86 |                         if ( env ) {
 | 
|---|
| 87 |                                 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 88 |                                         return isPolyType( newType, tyVars, env );
 | 
|---|
| 89 |                                 } // if
 | 
|---|
| 90 |                         } // if
 | 
|---|
| 91 |                         if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
 | 
|---|
| 92 |                                 return type;
 | 
|---|
| 93 |                         }
 | 
|---|
| 94 |                 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
 | 
|---|
| 95 |                         if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
 | 
|---|
| 96 |                 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
 | 
|---|
| 97 |                         if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
 | 
|---|
| 98 |                 }
 | 
|---|
| 99 |                 return 0;
 | 
|---|
| 100 |         }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
 | 
|---|
| 103 |                 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
 | 
|---|
| 104 |                         return isPolyType( ptr->get_base(), env );
 | 
|---|
| 105 |                 } else if ( env ) {
 | 
|---|
| 106 |                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
 | 
|---|
| 107 |                                 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 108 |                                         return isPolyPtr( newType, env );
 | 
|---|
| 109 |                                 } // if
 | 
|---|
| 110 |                         } // if
 | 
|---|
| 111 |                 } // if
 | 
|---|
| 112 |                 return 0;
 | 
|---|
| 113 |         }
 | 
|---|
| 114 |         
 | 
|---|
| 115 |         Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
 | 
|---|
| 116 |                 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
 | 
|---|
| 117 |                         return isPolyType( ptr->get_base(), tyVars, env );
 | 
|---|
| 118 |                 } else if ( env ) {
 | 
|---|
| 119 |                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
 | 
|---|
| 120 |                                 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 121 |                                         return isPolyPtr( newType, tyVars, env );
 | 
|---|
| 122 |                                 } // if
 | 
|---|
| 123 |                         } // if
 | 
|---|
| 124 |                 } // if
 | 
|---|
| 125 |                 return 0;
 | 
|---|
| 126 |         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
 | 
|---|
| 129 |                 int dummy;
 | 
|---|
| 130 |                 if ( ! levels ) { levels = &dummy; }
 | 
|---|
| 131 |                 *levels = 0;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |                 while ( true ) {
 | 
|---|
| 134 |                         if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
 | 
|---|
| 135 |                                 type = ptr->get_base();
 | 
|---|
| 136 |                                 ++(*levels);
 | 
|---|
| 137 |                         } else if ( env ) {
 | 
|---|
| 138 |                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
 | 
|---|
| 139 |                                         if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 140 |                                                 type = newType;
 | 
|---|
| 141 |                                         } else break;
 | 
|---|
| 142 |                                 } else break;
 | 
|---|
| 143 |                         } else break;
 | 
|---|
| 144 |                 }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |                 return isPolyType( type, env );
 | 
|---|
| 147 |         }
 | 
|---|
| 148 |         
 | 
|---|
| 149 |         Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
 | 
|---|
| 150 |                 int dummy;
 | 
|---|
| 151 |                 if ( ! levels ) { levels = &dummy; }
 | 
|---|
| 152 |                 *levels = 0;
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                 while ( true ) {
 | 
|---|
| 155 |                         if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
 | 
|---|
| 156 |                                 type = ptr->get_base();
 | 
|---|
| 157 |                                 ++(*levels);
 | 
|---|
| 158 |                         } else if ( env ) {
 | 
|---|
| 159 |                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
 | 
|---|
| 160 |                                         if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
 | 
|---|
| 161 |                                                 type = newType;
 | 
|---|
| 162 |                                         } else break;
 | 
|---|
| 163 |                                 } else break;
 | 
|---|
| 164 |                         } else break;
 | 
|---|
| 165 |                 }
 | 
|---|
| 166 | 
 | 
|---|
| 167 |                 return isPolyType( type, tyVars, env );
 | 
|---|
| 168 |         }
 | 
|---|
| 169 | 
 | 
|---|
| 170 |         FunctionType * getFunctionType( Type *ty ) {
 | 
|---|
| 171 |                 PointerType *ptrType;
 | 
|---|
| 172 |                 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
 | 
|---|
| 173 |                         return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
 | 
|---|
| 174 |                 } else {
 | 
|---|
| 175 |                         return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
 | 
|---|
| 176 |                 }
 | 
|---|
| 177 |         }
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         VariableExpr * getBaseVar( Expression *expr, int *levels ) {
 | 
|---|
| 180 |                 int dummy;
 | 
|---|
| 181 |                 if ( ! levels ) { levels = &dummy; }
 | 
|---|
| 182 |                 *levels = 0;
 | 
|---|
| 183 | 
 | 
|---|
| 184 |                 while ( true ) {
 | 
|---|
| 185 |                         if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
 | 
|---|
| 186 |                                 return varExpr;
 | 
|---|
| 187 |                         } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
 | 
|---|
| 188 |                                 expr = addressExpr->get_arg();
 | 
|---|
| 189 |                         } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
 | 
|---|
| 190 |                                 // look for compiler-inserted dereference operator
 | 
|---|
| 191 |                                 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
 | 
|---|
| 192 |                                 if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
 | 
|---|
| 193 |                                 expr = *untypedExpr->begin_args();
 | 
|---|
| 194 |                         } else break;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |                         ++(*levels);
 | 
|---|
| 197 |                 }
 | 
|---|
| 198 | 
 | 
|---|
| 199 |                 return 0;
 | 
|---|
| 200 |         }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |         void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
 | 
|---|
| 203 |                 for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
 | 
|---|
| 204 |                         assert( *tyVar );
 | 
|---|
| 205 |                         tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
 | 
|---|
| 206 |                 }
 | 
|---|
| 207 |                 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
 | 
|---|
| 208 |                         makeTyVarMap( pointer->get_base(), tyVarMap );
 | 
|---|
| 209 |                 }
 | 
|---|
| 210 |         }
 | 
|---|
| 211 |         
 | 
|---|
| 212 |         void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
 | 
|---|
| 213 |                 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
 | 
|---|
| 214 |                         os << i->first << " (" << i->second << ") ";
 | 
|---|
| 215 |                 } // for
 | 
|---|
| 216 |                 os << std::endl;
 | 
|---|
| 217 |         }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | } // namespace GenPoly
 | 
|---|
| 220 | 
 | 
|---|
| 221 | // Local Variables: //
 | 
|---|
| 222 | // tab-width: 4 //
 | 
|---|
| 223 | // mode: c++ //
 | 
|---|
| 224 | // compile-command: "make install" //
 | 
|---|
| 225 | // End: //
 | 
|---|