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