[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 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "GenPoly.h"
|
---|
[ffad73a] | 17 |
|
---|
| 18 | #include "SymTab/Mangler.h"
|
---|
| 19 | #include "SynTree/Expression.h"
|
---|
[51b73452] | 20 | #include "SynTree/Type.h"
|
---|
| 21 |
|
---|
[b1a6d6b] | 22 | #include <iostream>
|
---|
| 23 | using namespace std;
|
---|
[51b73452] | 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 |
|
---|
| 38 | bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
|
---|
| 39 | bool doTransform = false;
|
---|
| 40 | if ( ! function->get_returnVals().empty() ) {
|
---|
| 41 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
|
---|
| 42 |
|
---|
| 43 | // figure out if the return type is specified by a type parameter
|
---|
| 44 | for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
|
---|
| 45 | if ( (*tyVar)->get_name() == typeInst->get_name() ) {
|
---|
| 46 | doTransform = true;
|
---|
| 47 | name = typeInst->get_name();
|
---|
| 48 | break;
|
---|
| 49 | } // if
|
---|
| 50 | } // for
|
---|
| 51 | if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
|
---|
| 52 | doTransform = true;
|
---|
| 53 | } // if
|
---|
| 54 | } // if
|
---|
| 55 | } // if
|
---|
| 56 | return doTransform;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | bool isPolyRet( FunctionType *function, std::string &name ) {
|
---|
| 60 | TyVarMap dummyTyVars;
|
---|
| 61 | return isPolyRet( function, name, dummyTyVars );
|
---|
[01aeade] | 62 | }
|
---|
[51b73452] | 63 |
|
---|
[e56cfdb0] | 64 | bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
|
---|
| 65 | std::string dummyString;
|
---|
| 66 | return isPolyRet( function, dummyString, otherTyVars );
|
---|
[01aeade] | 67 | }
|
---|
[b1a6d6b] | 68 |
|
---|
[ffad73a] | 69 | namespace {
|
---|
[0f889a77] | 70 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
|
---|
| 71 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
|
---|
| 72 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 73 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 74 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
| 75 | if ( isPolyType( paramType->get_type(), env ) ) return true;
|
---|
| 76 | }
|
---|
| 77 | return false;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
|
---|
[ffad73a] | 81 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 82 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 83 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 84 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
| 85 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
| 86 | }
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[0f889a77] | 90 |
|
---|
| 91 | Type *isPolyType( Type *type, const TypeSubstitution *env ) {
|
---|
| 92 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 93 | if ( env ) {
|
---|
| 94 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 95 | return isPolyType( newType, env );
|
---|
| 96 | } // if
|
---|
| 97 | } // if
|
---|
| 98 | return type;
|
---|
| 99 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 100 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
|
---|
| 101 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 102 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
|
---|
| 103 | }
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
[ffad73a] | 106 |
|
---|
| 107 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
[e56cfdb0] | 108 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[ffad73a] | 109 | if ( env ) {
|
---|
| 110 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 111 | return isPolyType( newType, tyVars, env );
|
---|
[0f889a77] | 112 | } // if
|
---|
[01aeade] | 113 | } // if
|
---|
[ffad73a] | 114 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
---|
| 115 | return type;
|
---|
[0f889a77] | 116 | }
|
---|
[ffad73a] | 117 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 118 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 119 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 120 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 121 | }
|
---|
| 122 | return 0;
|
---|
[01aeade] | 123 | }
|
---|
[b1a6d6b] | 124 |
|
---|
[0f889a77] | 125 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
|
---|
| 126 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 127 | return isPolyType( ptr->get_base(), env );
|
---|
| 128 | } else if ( env ) {
|
---|
| 129 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 130 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 131 | return isPolyPtr( newType, env );
|
---|
| 132 | } // if
|
---|
| 133 | } // if
|
---|
| 134 | } // if
|
---|
| 135 | return 0;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[ffad73a] | 138 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 139 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 140 | return isPolyType( ptr->get_base(), tyVars, env );
|
---|
| 141 | } else if ( env ) {
|
---|
| 142 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 143 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 144 | return isPolyPtr( newType, tyVars, env );
|
---|
| 145 | } // if
|
---|
| 146 | } // if
|
---|
| 147 | } // if
|
---|
| 148 | return 0;
|
---|
[bdf1954] | 149 | }
|
---|
| 150 |
|
---|
[7754cde] | 151 | FunctionType * getFunctionType( Type *ty ) {
|
---|
| 152 | PointerType *ptrType;
|
---|
| 153 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
|
---|
| 154 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
|
---|
| 155 | } else {
|
---|
| 156 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[01aeade] | 160 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
---|
| 161 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
---|
| 162 | os << i->first << " (" << i->second << ") ";
|
---|
| 163 | } // for
|
---|
| 164 | os << std::endl;
|
---|
| 165 | }
|
---|
[ffad73a] | 166 |
|
---|
| 167 | std::string sizeofName( Type *ty ) {
|
---|
[69911c11] | 168 | return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
|
---|
[ffad73a] | 169 | }
|
---|
| 170 |
|
---|
| 171 | std::string alignofName( Type *ty ) {
|
---|
[69911c11] | 172 | return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
|
---|
[ffad73a] | 173 | }
|
---|
[51b73452] | 174 | } // namespace GenPoly
|
---|
[01aeade] | 175 |
|
---|
[51587aa] | 176 | // Local Variables: //
|
---|
| 177 | // tab-width: 4 //
|
---|
| 178 | // mode: c++ //
|
---|
| 179 | // compile-command: "make install" //
|
---|
| 180 | // End: //
|
---|