[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 | // |
---|
[540de412] | 7 | // GenPoly.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[ca35c51] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Wed Jun 29 21:45:53 2016 |
---|
| 13 | // Update Count : 14 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "GenPoly.h" |
---|
[ffad73a] | 17 | |
---|
| 18 | #include "SynTree/Expression.h" |
---|
[51b7345] | 19 | #include "SynTree/Type.h" |
---|
| 20 | |
---|
[b1a6d6b] | 21 | #include <iostream> |
---|
| 22 | using namespace std; |
---|
[51b7345] | 23 | |
---|
| 24 | namespace GenPoly { |
---|
[e56cfdb0] | 25 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
[ffad73a] | 26 | if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
[e56cfdb0] | 27 | return true; |
---|
| 28 | } // if |
---|
| 29 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { |
---|
[ffad73a] | 30 | if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { |
---|
[e56cfdb0] | 31 | return true; |
---|
| 32 | } // if |
---|
| 33 | } // for |
---|
| 34 | return false; |
---|
| 35 | } |
---|
| 36 | |
---|
[aadc9a4] | 37 | ReferenceToType *isPolyRet( FunctionType *function ) { |
---|
[e56cfdb0] | 38 | if ( ! function->get_returnVals().empty() ) { |
---|
[bfae637] | 39 | TyVarMap forallTypes( (TypeDecl::Kind)-1 ); |
---|
[aadc9a4] | 40 | makeTyVarMap( function, forallTypes ); |
---|
| 41 | return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes ); |
---|
[e56cfdb0] | 42 | } // if |
---|
[aadc9a4] | 43 | return 0; |
---|
[01aeade] | 44 | } |
---|
[b1a6d6b] | 45 | |
---|
[ffad73a] | 46 | namespace { |
---|
[0f889a77] | 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 |
---|
[ffad73a] | 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 | } |
---|
[c2ad3c9] | 66 | } |
---|
[83de11e] | 67 | |
---|
[c2ad3c9] | 68 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { |
---|
| 69 | if ( ! env ) return type; |
---|
| 70 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
| 71 | Type *newType = env->lookup( typeInst->get_name() ); |
---|
| 72 | if ( newType ) return newType; |
---|
[e24955a] | 73 | } |
---|
[c2ad3c9] | 74 | return type; |
---|
[ffad73a] | 75 | } |
---|
[0f889a77] | 76 | |
---|
| 77 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { |
---|
[e24955a] | 78 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 79 | |
---|
[ca35c51] | 80 | if ( dynamic_cast< TypeInstType * >( type ) ) { |
---|
[0f889a77] | 81 | return type; |
---|
| 82 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 83 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; |
---|
| 84 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 85 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; |
---|
| 86 | } |
---|
| 87 | return 0; |
---|
| 88 | } |
---|
[540de412] | 89 | |
---|
[ffad73a] | 90 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
[e24955a] | 91 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 92 | |
---|
[e56cfdb0] | 93 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
[ffad73a] | 94 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { |
---|
| 95 | return type; |
---|
[0f889a77] | 96 | } |
---|
[ffad73a] | 97 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 98 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; |
---|
| 99 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 100 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; |
---|
| 101 | } |
---|
| 102 | return 0; |
---|
[01aeade] | 103 | } |
---|
[b1a6d6b] | 104 | |
---|
[0f889a77] | 105 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { |
---|
[e24955a] | 106 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 107 | |
---|
[0f889a77] | 108 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 109 | return isPolyType( ptr->get_base(), env ); |
---|
[e24955a] | 110 | } |
---|
[0f889a77] | 111 | return 0; |
---|
| 112 | } |
---|
[540de412] | 113 | |
---|
[ffad73a] | 114 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
[e24955a] | 115 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 116 | |
---|
[ffad73a] | 117 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 118 | return isPolyType( ptr->get_base(), tyVars, env ); |
---|
[e24955a] | 119 | } |
---|
[ffad73a] | 120 | return 0; |
---|
[bdf1954] | 121 | } |
---|
| 122 | |
---|
[8488c715] | 123 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { |
---|
| 124 | int dummy; |
---|
| 125 | if ( ! levels ) { levels = &dummy; } |
---|
| 126 | *levels = 0; |
---|
| 127 | |
---|
| 128 | while ( true ) { |
---|
[e24955a] | 129 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 130 | |
---|
[8488c715] | 131 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 132 | type = ptr->get_base(); |
---|
| 133 | ++(*levels); |
---|
| 134 | } else break; |
---|
[05d47278] | 135 | } |
---|
| 136 | |
---|
| 137 | return isPolyType( type, env ); |
---|
| 138 | } |
---|
[540de412] | 139 | |
---|
[8488c715] | 140 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { |
---|
| 141 | int dummy; |
---|
| 142 | if ( ! levels ) { levels = &dummy; } |
---|
| 143 | *levels = 0; |
---|
| 144 | |
---|
| 145 | while ( true ) { |
---|
[e24955a] | 146 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 147 | |
---|
[8488c715] | 148 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 149 | type = ptr->get_base(); |
---|
| 150 | ++(*levels); |
---|
| 151 | } else break; |
---|
[05d47278] | 152 | } |
---|
| 153 | |
---|
| 154 | return isPolyType( type, tyVars, env ); |
---|
| 155 | } |
---|
| 156 | |
---|
[7754cde] | 157 | FunctionType * getFunctionType( Type *ty ) { |
---|
| 158 | PointerType *ptrType; |
---|
| 159 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { |
---|
| 160 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise |
---|
| 161 | } else { |
---|
| 162 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
[8488c715] | 166 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { |
---|
| 167 | int dummy; |
---|
| 168 | if ( ! levels ) { levels = &dummy; } |
---|
| 169 | *levels = 0; |
---|
| 170 | |
---|
| 171 | while ( true ) { |
---|
| 172 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { |
---|
| 173 | return varExpr; |
---|
[d9f1b2d] | 174 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { |
---|
| 175 | expr = memberExpr->get_aggregate(); |
---|
[8488c715] | 176 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { |
---|
| 177 | expr = addressExpr->get_arg(); |
---|
| 178 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { |
---|
| 179 | // look for compiler-inserted dereference operator |
---|
| 180 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); |
---|
| 181 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; |
---|
| 182 | expr = *untypedExpr->begin_args(); |
---|
[540de412] | 183 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { |
---|
| 184 | // copy constructors insert comma exprs, look at second argument which contains the variable |
---|
| 185 | expr = commaExpr->get_arg2(); |
---|
| 186 | continue; |
---|
[8488c715] | 187 | } else break; |
---|
| 188 | |
---|
| 189 | ++(*levels); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | return 0; |
---|
[05d47278] | 193 | } |
---|
| 194 | |
---|
[aadc9a4] | 195 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { |
---|
| 196 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { |
---|
| 197 | assert( *tyVar ); |
---|
| 198 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); |
---|
| 199 | } |
---|
| 200 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { |
---|
| 201 | makeTyVarMap( pointer->get_base(), tyVarMap ); |
---|
| 202 | } |
---|
| 203 | } |
---|
[540de412] | 204 | |
---|
[01aeade] | 205 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { |
---|
| 206 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { |
---|
| 207 | os << i->first << " (" << i->second << ") "; |
---|
| 208 | } // for |
---|
| 209 | os << std::endl; |
---|
| 210 | } |
---|
[ffad73a] | 211 | |
---|
[51b7345] | 212 | } // namespace GenPoly |
---|
[01aeade] | 213 | |
---|
[51587aa] | 214 | // Local Variables: // |
---|
| 215 | // tab-width: 4 // |
---|
| 216 | // mode: c++ // |
---|
| 217 | // compile-command: "make install" // |
---|
| 218 | // End: // |
---|