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