| [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 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include "GenPoly.h" | 
|---|
| [ffad73a] | 17 |  | 
|---|
| [08fc48f] | 18 | #include <cassert>                      // for assertf, assert | 
|---|
|  | 19 | #include <iostream>                     // for operator<<, ostream, basic_os... | 
|---|
|  | 20 | #include <iterator>                     // for back_insert_iterator, back_in... | 
|---|
|  | 21 | #include <list>                         // for list, _List_iterator, list<>:... | 
|---|
|  | 22 | #include <typeindex>                    // for type_index | 
|---|
|  | 23 | #include <utility>                      // for pair | 
|---|
|  | 24 | #include <vector>                       // for vector | 
|---|
|  | 25 |  | 
|---|
| [d76c588] | 26 | #include "AST/Type.hpp" | 
|---|
| [08fc48f] | 27 | #include "GenPoly/ErasableScopedMap.h"  // for ErasableScopedMap<>::const_it... | 
|---|
|  | 28 | #include "ResolvExpr/typeops.h"         // for flatten | 
|---|
|  | 29 | #include "SynTree/Constant.h"           // for Constant | 
|---|
|  | 30 | #include "SynTree/Expression.h"         // for Expression, TypeExpr, Constan... | 
|---|
|  | 31 | #include "SynTree/Type.h"               // for Type, StructInstType, UnionIn... | 
|---|
|  | 32 | #include "SynTree/TypeSubstitution.h"   // for TypeSubstitution | 
|---|
| [51b73452] | 33 |  | 
|---|
| [b1a6d6b] | 34 | using namespace std; | 
|---|
| [51b73452] | 35 |  | 
|---|
|  | 36 | namespace GenPoly { | 
|---|
| [ffad73a] | 37 | namespace { | 
|---|
| [0f889a77] | 38 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present | 
|---|
|  | 39 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { | 
|---|
|  | 40 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 41 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
| [7350ff97] | 42 | assertf(paramType, "Aggregate parameters should be type expressions"); | 
|---|
| [0f889a77] | 43 | if ( isPolyType( paramType->get_type(), env ) ) return true; | 
|---|
|  | 44 | } | 
|---|
|  | 45 | return false; | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
| [490fb92e] | 48 | bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env) { | 
|---|
|  | 49 | for (auto ¶m : params) { | 
|---|
|  | 50 | auto paramType = param.strict_as<ast::TypeExpr>(); | 
|---|
|  | 51 | if (isPolyType(paramType->type, env)) return true; | 
|---|
|  | 52 | } | 
|---|
|  | 53 | return false; | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
| [0f889a77] | 56 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present | 
|---|
| [ffad73a] | 57 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 58 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 59 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
| [5a3ac84] | 60 | assertf(paramType, "Aggregate parameters should be type expressions"); | 
|---|
| [ffad73a] | 61 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; | 
|---|
|  | 62 | } | 
|---|
|  | 63 | return false; | 
|---|
|  | 64 | } | 
|---|
| [3bb195cb] | 65 |  | 
|---|
| [490fb92e] | 66 | bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const TyVarMap & tyVars, const ast::TypeSubstitution * env) { | 
|---|
|  | 67 | for (auto ¶m : params) { | 
|---|
|  | 68 | auto paramType = param.strict_as<ast::TypeExpr>(); | 
|---|
|  | 69 | if (isPolyType(paramType->type, tyVars, env)) return true; | 
|---|
|  | 70 | } | 
|---|
|  | 71 | return false; | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [3bb195cb] | 74 | /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present | 
|---|
|  | 75 | bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 76 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 77 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
| [5a3ac84] | 78 | assertf(paramType, "Aggregate parameters should be type expressions"); | 
|---|
| [3bb195cb] | 79 | if ( isDynType( paramType->get_type(), tyVars, env ) ) return true; | 
|---|
|  | 80 | } | 
|---|
|  | 81 | return false; | 
|---|
|  | 82 | } | 
|---|
| [5a3ac84] | 83 |  | 
|---|
|  | 84 | /// Checks a parameter list for inclusion of polymorphic parameters; will substitute according to env if present | 
|---|
|  | 85 | bool includesPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { | 
|---|
|  | 86 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 87 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 88 | assertf(paramType, "Aggregate parameters should be type expressions"); | 
|---|
|  | 89 | if ( includesPolyType( paramType->get_type(), env ) ) return true; | 
|---|
|  | 90 | } | 
|---|
|  | 91 | return false; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /// Checks a parameter list for inclusion of polymorphic parameters from tyVars; will substitute according to env if present | 
|---|
|  | 95 | bool includesPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 96 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 97 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 98 | assertf(paramType, "Aggregate parameters should be type expressions"); | 
|---|
|  | 99 | if ( includesPolyType( paramType->get_type(), tyVars, env ) ) return true; | 
|---|
|  | 100 | } | 
|---|
|  | 101 | return false; | 
|---|
|  | 102 | } | 
|---|
| [c2ad3c9] | 103 | } | 
|---|
| [83de11e] | 104 |  | 
|---|
| [c2ad3c9] | 105 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { | 
|---|
|  | 106 | if ( ! env ) return type; | 
|---|
|  | 107 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { | 
|---|
|  | 108 | Type *newType = env->lookup( typeInst->get_name() ); | 
|---|
|  | 109 | if ( newType ) return newType; | 
|---|
| [e24955a] | 110 | } | 
|---|
| [c2ad3c9] | 111 | return type; | 
|---|
| [ffad73a] | 112 | } | 
|---|
| [0f889a77] | 113 |  | 
|---|
| [490fb92e] | 114 | const ast::Type * replaceTypeInst(const ast::Type * type, const ast::TypeSubstitution * env) { | 
|---|
|  | 115 | if (!env) return type; | 
|---|
|  | 116 | if (auto typeInst = dynamic_cast<const ast::TypeInstType*> (type)) { | 
|---|
|  | 117 | auto newType = env->lookup(typeInst->name); | 
|---|
|  | 118 | if (newType) return newType; | 
|---|
|  | 119 | } | 
|---|
|  | 120 | return type; | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
| [0f889a77] | 123 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 124 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 125 |  | 
|---|
| [ca35c51] | 126 | if ( dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
| [0f889a77] | 127 | return type; | 
|---|
| [5f61546] | 128 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
|  | 129 | return isPolyType( arrayType->base, env ); | 
|---|
| [0f889a77] | 130 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 131 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; | 
|---|
|  | 132 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 133 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | return 0; | 
|---|
|  | 136 | } | 
|---|
| [540de412] | 137 |  | 
|---|
| [490fb92e] | 138 | const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env) { | 
|---|
|  | 139 | type = replaceTypeInst( type, env ); | 
|---|
|  | 140 |  | 
|---|
|  | 141 | if ( dynamic_cast< const ast::TypeInstType * >( type ) ) { | 
|---|
|  | 142 | return type; | 
|---|
|  | 143 | } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { | 
|---|
|  | 144 | return isPolyType( arrayType->base, env ); | 
|---|
|  | 145 | } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) { | 
|---|
|  | 146 | if ( hasPolyParams( structType->params, env ) ) return type; | 
|---|
|  | 147 | } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) { | 
|---|
|  | 148 | if ( hasPolyParams( unionType->params, env ) ) return type; | 
|---|
|  | 149 | } | 
|---|
|  | 150 | return 0; | 
|---|
|  | 151 | } | 
|---|
|  | 152 |  | 
|---|
| [ffad73a] | 153 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 154 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 155 |  | 
|---|
| [e56cfdb0] | 156 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
| [ffad73a] | 157 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { | 
|---|
|  | 158 | return type; | 
|---|
| [0f889a77] | 159 | } | 
|---|
| [5f61546] | 160 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
|  | 161 | return isPolyType( arrayType->base, tyVars, env ); | 
|---|
| [ffad73a] | 162 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 163 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 164 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 165 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 166 | } | 
|---|
|  | 167 | return 0; | 
|---|
| [01aeade] | 168 | } | 
|---|
| [b1a6d6b] | 169 |  | 
|---|
| [490fb92e] | 170 | const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env) { | 
|---|
|  | 171 | type = replaceTypeInst( type, env ); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( type ) ) { | 
|---|
|  | 174 | return tyVars.find(typeInst->name) != tyVars.end() ? type : nullptr; | 
|---|
|  | 175 | } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { | 
|---|
|  | 176 | return isPolyType( arrayType->base, env ); | 
|---|
|  | 177 | } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) { | 
|---|
|  | 178 | if ( hasPolyParams( structType->params, env ) ) return type; | 
|---|
|  | 179 | } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) { | 
|---|
|  | 180 | if ( hasPolyParams( unionType->params, env ) ) return type; | 
|---|
|  | 181 | } | 
|---|
|  | 182 | return nullptr; | 
|---|
|  | 183 | } | 
|---|
|  | 184 |  | 
|---|
| [33a7b6d] | 185 | ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [3bb195cb] | 186 | type = replaceTypeInst( type, env ); | 
|---|
|  | 187 |  | 
|---|
|  | 188 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 189 | auto var = tyVars.find( typeInst->get_name() ); | 
|---|
| [2c57025] | 190 | if ( var != tyVars.end() && var->second.isComplete ) { | 
|---|
| [33a7b6d] | 191 | return typeInst; | 
|---|
| [3bb195cb] | 192 | } | 
|---|
|  | 193 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
| [33a7b6d] | 194 | if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return structType; | 
|---|
| [3bb195cb] | 195 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
| [33a7b6d] | 196 | if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return unionType; | 
|---|
| [3bb195cb] | 197 | } | 
|---|
|  | 198 | return 0; | 
|---|
|  | 199 | } | 
|---|
|  | 200 |  | 
|---|
|  | 201 | ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) { | 
|---|
|  | 202 | if ( function->get_returnVals().empty() ) return 0; | 
|---|
| [8c49c0e] | 203 |  | 
|---|
| [3bb195cb] | 204 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | ReferenceToType *isDynRet( FunctionType *function ) { | 
|---|
|  | 208 | if ( function->get_returnVals().empty() ) return 0; | 
|---|
|  | 209 |  | 
|---|
| [2c57025] | 210 | TyVarMap forallTypes( TypeDecl::Data{} ); | 
|---|
| [3bb195cb] | 211 | makeTyVarMap( function, forallTypes ); | 
|---|
|  | 212 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); | 
|---|
|  | 213 | } | 
|---|
|  | 214 |  | 
|---|
|  | 215 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { | 
|---|
|  | 216 | //              if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
|  | 217 | //                      return true; | 
|---|
|  | 218 | //              } // if | 
|---|
|  | 219 | if ( isDynRet( adaptee, tyVars ) ) return true; | 
|---|
| [8c49c0e] | 220 |  | 
|---|
| [3bb195cb] | 221 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { | 
|---|
|  | 222 | //                      if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { | 
|---|
|  | 223 | if ( isDynType( (*innerArg)->get_type(), tyVars ) ) { | 
|---|
|  | 224 | return true; | 
|---|
|  | 225 | } // if | 
|---|
|  | 226 | } // for | 
|---|
|  | 227 | return false; | 
|---|
|  | 228 | } | 
|---|
|  | 229 |  | 
|---|
| [0f889a77] | 230 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 231 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 232 |  | 
|---|
| [0f889a77] | 233 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 234 | return isPolyType( ptr->get_base(), env ); | 
|---|
| [e24955a] | 235 | } | 
|---|
| [0f889a77] | 236 | return 0; | 
|---|
|  | 237 | } | 
|---|
| [540de412] | 238 |  | 
|---|
| [ffad73a] | 239 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 240 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 241 |  | 
|---|
| [ffad73a] | 242 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 243 | return isPolyType( ptr->get_base(), tyVars, env ); | 
|---|
| [e24955a] | 244 | } | 
|---|
| [ffad73a] | 245 | return 0; | 
|---|
| [bdf1954] | 246 | } | 
|---|
|  | 247 |  | 
|---|
| [8488c715] | 248 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 249 | int dummy; | 
|---|
|  | 250 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 251 | *levels = 0; | 
|---|
|  | 252 |  | 
|---|
|  | 253 | while ( true ) { | 
|---|
| [e24955a] | 254 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 255 |  | 
|---|
| [8488c715] | 256 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 257 | type = ptr->get_base(); | 
|---|
|  | 258 | ++(*levels); | 
|---|
|  | 259 | } else break; | 
|---|
| [05d47278] | 260 | } | 
|---|
|  | 261 |  | 
|---|
|  | 262 | return isPolyType( type, env ); | 
|---|
|  | 263 | } | 
|---|
| [540de412] | 264 |  | 
|---|
| [8488c715] | 265 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 266 | int dummy; | 
|---|
|  | 267 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 268 | *levels = 0; | 
|---|
|  | 269 |  | 
|---|
|  | 270 | while ( true ) { | 
|---|
| [e24955a] | 271 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 272 |  | 
|---|
| [8488c715] | 273 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 274 | type = ptr->get_base(); | 
|---|
|  | 275 | ++(*levels); | 
|---|
|  | 276 | } else break; | 
|---|
| [05d47278] | 277 | } | 
|---|
|  | 278 |  | 
|---|
|  | 279 | return isPolyType( type, tyVars, env ); | 
|---|
|  | 280 | } | 
|---|
|  | 281 |  | 
|---|
| [5a3ac84] | 282 | bool includesPolyType( Type *type, const TypeSubstitution *env ) { | 
|---|
|  | 283 | type = replaceTypeInst( type, env ); | 
|---|
|  | 284 |  | 
|---|
|  | 285 | if ( dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 286 | return true; | 
|---|
|  | 287 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) { | 
|---|
|  | 288 | if ( includesPolyType( pointerType->get_base(), env ) ) return true; | 
|---|
|  | 289 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 290 | if ( includesPolyParams( structType->get_parameters(), env ) ) return true; | 
|---|
|  | 291 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 292 | if ( includesPolyParams( unionType->get_parameters(), env ) ) return true; | 
|---|
|  | 293 | } | 
|---|
|  | 294 | return false; | 
|---|
|  | 295 | } | 
|---|
|  | 296 |  | 
|---|
|  | 297 | bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 298 | type = replaceTypeInst( type, env ); | 
|---|
|  | 299 |  | 
|---|
|  | 300 | if ( TypeInstType *typeInstType = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 301 | if ( tyVars.find( typeInstType->get_name() ) != tyVars.end() ) { | 
|---|
|  | 302 | return true; | 
|---|
|  | 303 | } | 
|---|
|  | 304 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) { | 
|---|
|  | 305 | if ( includesPolyType( pointerType->get_base(), tyVars, env ) ) return true; | 
|---|
|  | 306 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 307 | if ( includesPolyParams( structType->get_parameters(), tyVars, env ) ) return true; | 
|---|
|  | 308 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 309 | if ( includesPolyParams( unionType->get_parameters(), tyVars, env ) ) return true; | 
|---|
|  | 310 | } | 
|---|
|  | 311 | return false; | 
|---|
|  | 312 | } | 
|---|
|  | 313 |  | 
|---|
| [7754cde] | 314 | FunctionType * getFunctionType( Type *ty ) { | 
|---|
|  | 315 | PointerType *ptrType; | 
|---|
|  | 316 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { | 
|---|
|  | 317 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 318 | } else { | 
|---|
|  | 319 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 320 | } | 
|---|
|  | 321 | } | 
|---|
|  | 322 |  | 
|---|
| [d76c588] | 323 | const ast::FunctionType * getFunctionType( const ast::Type * ty ) { | 
|---|
|  | 324 | if ( auto pty = dynamic_cast< const ast::PointerType * >( ty ) ) { | 
|---|
|  | 325 | return pty->base.as< ast::FunctionType >(); | 
|---|
|  | 326 | } else { | 
|---|
|  | 327 | return dynamic_cast< const ast::FunctionType * >( ty ); | 
|---|
|  | 328 | } | 
|---|
|  | 329 | } | 
|---|
|  | 330 |  | 
|---|
| [8488c715] | 331 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { | 
|---|
|  | 332 | int dummy; | 
|---|
|  | 333 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 334 | *levels = 0; | 
|---|
|  | 335 |  | 
|---|
|  | 336 | while ( true ) { | 
|---|
|  | 337 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { | 
|---|
|  | 338 | return varExpr; | 
|---|
| [d9f1b2d] | 339 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { | 
|---|
|  | 340 | expr = memberExpr->get_aggregate(); | 
|---|
| [8488c715] | 341 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { | 
|---|
|  | 342 | expr = addressExpr->get_arg(); | 
|---|
|  | 343 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { | 
|---|
|  | 344 | // look for compiler-inserted dereference operator | 
|---|
|  | 345 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); | 
|---|
|  | 346 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; | 
|---|
|  | 347 | expr = *untypedExpr->begin_args(); | 
|---|
| [540de412] | 348 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { | 
|---|
|  | 349 | // copy constructors insert comma exprs, look at second argument which contains the variable | 
|---|
|  | 350 | expr = commaExpr->get_arg2(); | 
|---|
|  | 351 | continue; | 
|---|
| [1aa4b71] | 352 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( expr ) ) { | 
|---|
|  | 353 | int lvl1; | 
|---|
|  | 354 | int lvl2; | 
|---|
|  | 355 | VariableExpr * var1 = getBaseVar( condExpr->get_arg2(), &lvl1 ); | 
|---|
|  | 356 | VariableExpr * var2 = getBaseVar( condExpr->get_arg3(), &lvl2 ); | 
|---|
|  | 357 | if ( lvl1 == lvl2 && var1 && var2 && var1->get_var() == var2->get_var() ) { | 
|---|
|  | 358 | *levels = lvl1; | 
|---|
|  | 359 | return var1; | 
|---|
|  | 360 | } | 
|---|
|  | 361 | break; | 
|---|
| [8488c715] | 362 | } else break; | 
|---|
|  | 363 |  | 
|---|
|  | 364 | ++(*levels); | 
|---|
|  | 365 | } | 
|---|
|  | 366 |  | 
|---|
|  | 367 | return 0; | 
|---|
| [05d47278] | 368 | } | 
|---|
|  | 369 |  | 
|---|
| [5a3ac84] | 370 | namespace { | 
|---|
|  | 371 | /// Checks if is a pointer to D | 
|---|
|  | 372 | template<typename D, typename B> | 
|---|
|  | 373 | bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; } | 
|---|
|  | 374 |  | 
|---|
|  | 375 | /// Converts to a pointer to D without checking for safety | 
|---|
|  | 376 | template<typename D, typename B> | 
|---|
|  | 377 | inline D* as( B* p ) { return reinterpret_cast<D*>(p); } | 
|---|
|  | 378 |  | 
|---|
|  | 379 | /// Flattens a declaration list | 
|---|
|  | 380 | template<typename Output> | 
|---|
|  | 381 | void flattenList( list< DeclarationWithType* > src, Output out ) { | 
|---|
|  | 382 | for ( DeclarationWithType* decl : src ) { | 
|---|
|  | 383 | ResolvExpr::flatten( decl->get_type(), out ); | 
|---|
|  | 384 | } | 
|---|
|  | 385 | } | 
|---|
|  | 386 |  | 
|---|
|  | 387 | /// Flattens a list of types | 
|---|
|  | 388 | template<typename Output> | 
|---|
|  | 389 | void flattenList( list< Type* > src, Output out ) { | 
|---|
|  | 390 | for ( Type* ty : src ) { | 
|---|
|  | 391 | ResolvExpr::flatten( ty, out ); | 
|---|
|  | 392 | } | 
|---|
|  | 393 | } | 
|---|
|  | 394 |  | 
|---|
|  | 395 | /// Checks if two lists of parameters are equal up to polymorphic substitution. | 
|---|
|  | 396 | bool paramListsPolyCompatible( const list< Expression* >& aparams, const list< Expression* >& bparams ) { | 
|---|
|  | 397 | if ( aparams.size() != bparams.size() ) return false; | 
|---|
|  | 398 |  | 
|---|
|  | 399 | for ( list< Expression* >::const_iterator at = aparams.begin(), bt = bparams.begin(); | 
|---|
|  | 400 | at != aparams.end(); ++at, ++bt ) { | 
|---|
|  | 401 | TypeExpr *aparam = dynamic_cast< TypeExpr* >(*at); | 
|---|
|  | 402 | assertf(aparam, "Aggregate parameters should be type expressions"); | 
|---|
|  | 403 | TypeExpr *bparam = dynamic_cast< TypeExpr* >(*bt); | 
|---|
|  | 404 | assertf(bparam, "Aggregate parameters should be type expressions"); | 
|---|
|  | 405 |  | 
|---|
| [cccc534] | 406 | // xxx - might need to let VoidType be a wildcard here too; could have some voids | 
|---|
| [5a3ac84] | 407 | // stuffed in for dtype-statics. | 
|---|
|  | 408 | // if ( is<VoidType>( aparam->get_type() ) || is<VoidType>( bparam->get_type() ) ) continue; | 
|---|
|  | 409 | if ( ! typesPolyCompatible( aparam->get_type(), bparam->get_type() ) ) return false; | 
|---|
|  | 410 | } | 
|---|
| [cccc534] | 411 |  | 
|---|
| [5a3ac84] | 412 | return true; | 
|---|
|  | 413 | } | 
|---|
|  | 414 | } | 
|---|
|  | 415 |  | 
|---|
|  | 416 | bool typesPolyCompatible( Type *a, Type *b ) { | 
|---|
|  | 417 | type_index aid{ typeid(*a) }; | 
|---|
|  | 418 | // polymorphic types always match | 
|---|
|  | 419 | if ( aid == type_index{typeid(TypeInstType)} ) return true; | 
|---|
| [cccc534] | 420 |  | 
|---|
| [5a3ac84] | 421 | type_index bid{ typeid(*b) }; | 
|---|
|  | 422 | // polymorphic types always match | 
|---|
|  | 423 | if ( bid == type_index{typeid(TypeInstType)} ) return true; | 
|---|
| [cccc534] | 424 |  | 
|---|
| [5a3ac84] | 425 | // can't match otherwise if different types | 
|---|
|  | 426 | if ( aid != bid ) return false; | 
|---|
|  | 427 |  | 
|---|
|  | 428 | // recurse through type structure (conditions borrowed from Unify.cc) | 
|---|
|  | 429 | if ( aid == type_index{typeid(BasicType)} ) { | 
|---|
|  | 430 | return as<BasicType>(a)->get_kind() == as<BasicType>(b)->get_kind(); | 
|---|
|  | 431 | } else if ( aid == type_index{typeid(PointerType)} ) { | 
|---|
|  | 432 | PointerType *ap = as<PointerType>(a), *bp = as<PointerType>(b); | 
|---|
|  | 433 |  | 
|---|
|  | 434 | // void pointers should match any other pointer type | 
|---|
|  | 435 | return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() ) | 
|---|
|  | 436 | || typesPolyCompatible( ap->get_base(), bp->get_base() ); | 
|---|
| [682dcae] | 437 | } else if ( aid == type_index{typeid(ReferenceType)} ) { | 
|---|
|  | 438 | ReferenceType *ap = as<ReferenceType>(a), *bp = as<ReferenceType>(b); | 
|---|
|  | 439 | return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() ) | 
|---|
|  | 440 | || typesPolyCompatible( ap->get_base(), bp->get_base() ); | 
|---|
| [5a3ac84] | 441 | } else if ( aid == type_index{typeid(ArrayType)} ) { | 
|---|
|  | 442 | ArrayType *aa = as<ArrayType>(a), *ba = as<ArrayType>(b); | 
|---|
|  | 443 |  | 
|---|
|  | 444 | if ( aa->get_isVarLen() ) { | 
|---|
|  | 445 | if ( ! ba->get_isVarLen() ) return false; | 
|---|
|  | 446 | } else { | 
|---|
|  | 447 | if ( ba->get_isVarLen() ) return false; | 
|---|
|  | 448 |  | 
|---|
|  | 449 | ConstantExpr *ad = dynamic_cast<ConstantExpr*>( aa->get_dimension() ); | 
|---|
|  | 450 | ConstantExpr *bd = dynamic_cast<ConstantExpr*>( ba->get_dimension() ); | 
|---|
| [cccc534] | 451 | if ( ad && bd | 
|---|
| [5a3ac84] | 452 | && ad->get_constant()->get_value() != bd->get_constant()->get_value() ) | 
|---|
|  | 453 | return false; | 
|---|
|  | 454 | } | 
|---|
|  | 455 |  | 
|---|
|  | 456 | return typesPolyCompatible( aa->get_base(), ba->get_base() ); | 
|---|
|  | 457 | } else if ( aid == type_index{typeid(FunctionType)} ) { | 
|---|
|  | 458 | FunctionType *af = as<FunctionType>(a), *bf = as<FunctionType>(b); | 
|---|
|  | 459 |  | 
|---|
|  | 460 | vector<Type*> aparams, bparams; | 
|---|
|  | 461 | flattenList( af->get_parameters(), back_inserter( aparams ) ); | 
|---|
|  | 462 | flattenList( bf->get_parameters(), back_inserter( bparams ) ); | 
|---|
|  | 463 | if ( aparams.size() != bparams.size() ) return false; | 
|---|
|  | 464 |  | 
|---|
|  | 465 | vector<Type*> areturns, breturns; | 
|---|
|  | 466 | flattenList( af->get_returnVals(), back_inserter( areturns ) ); | 
|---|
|  | 467 | flattenList( bf->get_returnVals(), back_inserter( breturns ) ); | 
|---|
|  | 468 | if ( areturns.size() != breturns.size() ) return false; | 
|---|
|  | 469 |  | 
|---|
|  | 470 | for ( unsigned i = 0; i < aparams.size(); ++i ) { | 
|---|
|  | 471 | if ( ! typesPolyCompatible( aparams[i], bparams[i] ) ) return false; | 
|---|
|  | 472 | } | 
|---|
|  | 473 | for ( unsigned i = 0; i < areturns.size(); ++i ) { | 
|---|
|  | 474 | if ( ! typesPolyCompatible( areturns[i], breturns[i] ) ) return false; | 
|---|
|  | 475 | } | 
|---|
|  | 476 | return true; | 
|---|
|  | 477 | } else if ( aid == type_index{typeid(StructInstType)} ) { | 
|---|
|  | 478 | StructInstType *aa = as<StructInstType>(a), *ba = as<StructInstType>(b); | 
|---|
|  | 479 |  | 
|---|
|  | 480 | if ( aa->get_name() != ba->get_name() ) return false; | 
|---|
|  | 481 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() ); | 
|---|
|  | 482 | } else if ( aid == type_index{typeid(UnionInstType)} ) { | 
|---|
|  | 483 | UnionInstType *aa = as<UnionInstType>(a), *ba = as<UnionInstType>(b); | 
|---|
|  | 484 |  | 
|---|
|  | 485 | if ( aa->get_name() != ba->get_name() ) return false; | 
|---|
|  | 486 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() ); | 
|---|
|  | 487 | } else if ( aid == type_index{typeid(EnumInstType)} ) { | 
|---|
|  | 488 | return as<EnumInstType>(a)->get_name() == as<EnumInstType>(b)->get_name(); | 
|---|
|  | 489 | } else if ( aid == type_index{typeid(TraitInstType)} ) { | 
|---|
|  | 490 | return as<TraitInstType>(a)->get_name() == as<TraitInstType>(b)->get_name(); | 
|---|
|  | 491 | } else if ( aid == type_index{typeid(TupleType)} ) { | 
|---|
|  | 492 | TupleType *at = as<TupleType>(a), *bt = as<TupleType>(b); | 
|---|
|  | 493 |  | 
|---|
|  | 494 | vector<Type*> atypes, btypes; | 
|---|
|  | 495 | flattenList( at->get_types(), back_inserter( atypes ) ); | 
|---|
|  | 496 | flattenList( bt->get_types(), back_inserter( btypes ) ); | 
|---|
|  | 497 | if ( atypes.size() != btypes.size() ) return false; | 
|---|
|  | 498 |  | 
|---|
|  | 499 | for ( unsigned i = 0; i < atypes.size(); ++i ) { | 
|---|
|  | 500 | if ( ! typesPolyCompatible( atypes[i], btypes[i] ) ) return false; | 
|---|
|  | 501 | } | 
|---|
|  | 502 | return true; | 
|---|
|  | 503 | } else return true; // VoidType, VarArgsType, ZeroType & OneType just need the same type | 
|---|
|  | 504 | } | 
|---|
|  | 505 |  | 
|---|
| [490fb92e] | 506 | namespace { | 
|---|
|  | 507 | // temporary hack to avoid re-implementing anything related to TyVarMap | 
|---|
|  | 508 | // does this work? these two structs have identical definitions. | 
|---|
|  | 509 | inline TypeDecl::Data convData(const ast::TypeDecl::Data & data) { | 
|---|
|  | 510 | return *reinterpret_cast<const TypeDecl::Data *>(&data); | 
|---|
|  | 511 | } | 
|---|
|  | 512 | } | 
|---|
|  | 513 |  | 
|---|
| [02fdb8e] | 514 | bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ) { | 
|---|
| [ae1b9ea] | 515 | // is parameter is not polymorphic, don't need to box | 
|---|
|  | 516 | if ( ! isPolyType( param, exprTyVars ) ) return false; | 
|---|
|  | 517 | Type * newType = arg->clone(); | 
|---|
|  | 518 | if ( env ) env->apply( newType ); | 
|---|
|  | 519 | std::unique_ptr<Type> manager( newType ); | 
|---|
|  | 520 | // if the argument's type is polymorphic, we don't need to box again! | 
|---|
|  | 521 | return ! isPolyType( newType ); | 
|---|
|  | 522 | } | 
|---|
|  | 523 |  | 
|---|
| [490fb92e] | 524 | bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TyVarMap &exprTyVars, const ast::TypeSubstitution * env) { | 
|---|
|  | 525 | // is parameter is not polymorphic, don't need to box | 
|---|
|  | 526 | if ( ! isPolyType( param, exprTyVars ) ) return false; | 
|---|
|  | 527 | ast::ptr<ast::Type> newType = arg; | 
|---|
|  | 528 | if ( env ) env->apply( newType ); | 
|---|
|  | 529 | // if the argument's type is polymorphic, we don't need to box again! | 
|---|
|  | 530 | return ! isPolyType( newType ); | 
|---|
|  | 531 | } | 
|---|
|  | 532 |  | 
|---|
| [02fdb8e] | 533 | bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ) { | 
|---|
| [ae1b9ea] | 534 | FunctionType * function = getFunctionType( appExpr->function->result ); | 
|---|
|  | 535 | assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() ); | 
|---|
|  | 536 | TyVarMap exprTyVars( TypeDecl::Data{} ); | 
|---|
|  | 537 | makeTyVarMap( function, exprTyVars ); | 
|---|
|  | 538 | return needsBoxing( param, arg, exprTyVars, env ); | 
|---|
|  | 539 | } | 
|---|
|  | 540 |  | 
|---|
| [490fb92e] | 541 | bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * appExpr, const ast::TypeSubstitution * env) { | 
|---|
|  | 542 | const ast::FunctionType * function = getFunctionType(appExpr->func->result); | 
|---|
|  | 543 | assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->func->result ).c_str() ); | 
|---|
|  | 544 | TyVarMap exprTyVars(TypeDecl::Data{}); | 
|---|
|  | 545 | makeTyVarMap(function, exprTyVars); | 
|---|
|  | 546 | return needsBoxing(param, arg, exprTyVars, env); | 
|---|
|  | 547 |  | 
|---|
|  | 548 | } | 
|---|
|  | 549 |  | 
|---|
| [2c57025] | 550 | void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) { | 
|---|
| [7ba1324] | 551 | tyVarMap.insert( tyVar->name, TypeDecl::Data{ tyVar } ); | 
|---|
| [2c57025] | 552 | } | 
|---|
|  | 553 |  | 
|---|
| [490fb92e] | 554 | void addToTyVarMap( const ast::TypeDecl * tyVar, TyVarMap & tyVarMap) { | 
|---|
|  | 555 | tyVarMap.insert(tyVar->name, convData(ast::TypeDecl::Data{tyVar})); | 
|---|
|  | 556 | } | 
|---|
|  | 557 |  | 
|---|
| [aadc9a4] | 558 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { | 
|---|
| [8c49c0e] | 559 | for ( Type::ForallList::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { | 
|---|
| [aadc9a4] | 560 | assert( *tyVar ); | 
|---|
| [2c57025] | 561 | addToTyVarMap( *tyVar, tyVarMap ); | 
|---|
| [aadc9a4] | 562 | } | 
|---|
|  | 563 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { | 
|---|
|  | 564 | makeTyVarMap( pointer->get_base(), tyVarMap ); | 
|---|
|  | 565 | } | 
|---|
|  | 566 | } | 
|---|
| [540de412] | 567 |  | 
|---|
| [490fb92e] | 568 | void makeTyVarMap(const ast::Type * type, TyVarMap & tyVarMap) { | 
|---|
|  | 569 | if (auto ptype = dynamic_cast<const ast::ParameterizedType *>(type)) { | 
|---|
|  | 570 | for (auto & tyVar : ptype->forall) { | 
|---|
|  | 571 | assert (tyVar); | 
|---|
|  | 572 | addToTyVarMap(tyVar, tyVarMap); | 
|---|
|  | 573 | } | 
|---|
|  | 574 | } | 
|---|
|  | 575 | if (auto pointer = dynamic_cast<const ast::PointerType *>(type)) { | 
|---|
|  | 576 | makeTyVarMap(pointer->base, tyVarMap); | 
|---|
|  | 577 | } | 
|---|
|  | 578 | } | 
|---|
|  | 579 |  | 
|---|
| [01aeade] | 580 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { | 
|---|
|  | 581 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { | 
|---|
|  | 582 | os << i->first << " (" << i->second << ") "; | 
|---|
|  | 583 | } // for | 
|---|
|  | 584 | os << std::endl; | 
|---|
|  | 585 | } | 
|---|
| [ffad73a] | 586 |  | 
|---|
| [51b73452] | 587 | } // namespace GenPoly | 
|---|
| [01aeade] | 588 |  | 
|---|
| [51587aa] | 589 | // Local Variables: // | 
|---|
|  | 590 | // tab-width: 4 // | 
|---|
|  | 591 | // mode: c++ // | 
|---|
|  | 592 | // compile-command: "make install" // | 
|---|
|  | 593 | // End: // | 
|---|