| 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 | //
|
|---|
| 7 | // GenPoly.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Tue Dec 15 16:11:18 2015
|
|---|
| 13 | // Update Count : 13
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "GenPoly.h"
|
|---|
| 17 |
|
|---|
| 18 | #include "SymTab/Mangler.h"
|
|---|
| 19 | #include "SynTree/Expression.h"
|
|---|
| 20 | #include "SynTree/Type.h"
|
|---|
| 21 |
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 | using namespace std;
|
|---|
| 24 |
|
|---|
| 25 | namespace GenPoly {
|
|---|
| 26 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
|---|
| 27 | if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
|---|
| 28 | return true;
|
|---|
| 29 | } // if
|
|---|
| 30 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
|
|---|
| 31 | if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
|
|---|
| 32 | return true;
|
|---|
| 33 | } // if
|
|---|
| 34 | } // for
|
|---|
| 35 | return false;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | ReferenceToType *isPolyRet( FunctionType *function ) {
|
|---|
| 39 | if ( ! function->get_returnVals().empty() ) {
|
|---|
| 40 | TyVarMap forallTypes;
|
|---|
| 41 | makeTyVarMap( function, forallTypes );
|
|---|
| 42 | return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
|
|---|
| 43 | } // if
|
|---|
| 44 | return 0;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | namespace {
|
|---|
| 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
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 84 |
|
|---|
| 85 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
|---|
| 86 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
|---|
| 87 | if ( env ) {
|
|---|
| 88 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
|---|
| 89 | return isPolyType( newType, tyVars, env );
|
|---|
| 90 | } // if
|
|---|
| 91 | } // if
|
|---|
| 92 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
|---|
| 93 | return type;
|
|---|
| 94 | }
|
|---|
| 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;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 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 |
|
|---|
| 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;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | Type *hasPolyBase( Type *type, const TypeSubstitution *env ) {
|
|---|
| 130 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
|---|
| 131 | return hasPolyBase( ptr->get_base(), env );
|
|---|
| 132 | } else if ( env ) {
|
|---|
| 133 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
|---|
| 134 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
|---|
| 135 | return hasPolyBase( newType, env );
|
|---|
| 136 | } // if
|
|---|
| 137 | } // if
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | return isPolyType( type, env );
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | Type *hasPolyBase( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
|---|
| 144 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
|---|
| 145 | return hasPolyBase( ptr->get_base(), tyVars, env );
|
|---|
| 146 | } else if ( env ) {
|
|---|
| 147 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
|---|
| 148 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
|---|
| 149 | return hasPolyBase( newType, tyVars, env );
|
|---|
| 150 | } // if
|
|---|
| 151 | } // if
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | return isPolyType( type, tyVars, env );
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | FunctionType * getFunctionType( Type *ty ) {
|
|---|
| 158 | PointerType *ptrType;
|
|---|
| 159 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
|
|---|
| 160 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
|
|---|
| 161 | } else {
|
|---|
| 162 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | VariableExpr *getBaseVar( Expression *expr ) {
|
|---|
| 167 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
|
|---|
| 168 | // found the variable directly
|
|---|
| 169 | return varExpr;
|
|---|
| 170 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
|
|---|
| 171 | // look for compiler-inserted dereference operator
|
|---|
| 172 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
|
|---|
| 173 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
|
|---|
| 174 | return getBaseVar( *untypedExpr->begin_args() );
|
|---|
| 175 | } else return 0;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
|
|---|
| 179 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
|
|---|
| 180 | assert( *tyVar );
|
|---|
| 181 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
|
|---|
| 182 | }
|
|---|
| 183 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
|
|---|
| 184 | makeTyVarMap( pointer->get_base(), tyVarMap );
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
|---|
| 189 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
|---|
| 190 | os << i->first << " (" << i->second << ") ";
|
|---|
| 191 | } // for
|
|---|
| 192 | os << std::endl;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | std::string sizeofName( Type *ty ) {
|
|---|
| 196 | return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | std::string alignofName( Type *ty ) {
|
|---|
| 200 | return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | std::string offsetofName( Type* ty ) {
|
|---|
| 204 | return std::string( "_offsetof_" ) + SymTab::Mangler::mangleType( ty );
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | } // namespace GenPoly
|
|---|
| 208 |
|
|---|
| 209 | // Local Variables: //
|
|---|
| 210 | // tab-width: 4 //
|
|---|
| 211 | // mode: c++ //
|
|---|
| 212 | // compile-command: "make install" //
|
|---|
| 213 | // End: //
|
|---|