Ignore:
Timestamp:
Dec 15, 2015, 4:57:31 PM (10 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
5f6c42c
Parents:
78dd0da
Message:

Refactored isPolyType and friends to account for polymorphic generic types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/GenPoly.cc

    r78dd0da rffad73a  
    1515
    1616#include "GenPoly.h"
     17
     18#include "SymTab/Mangler.h"
     19#include "SynTree/Expression.h"
    1720#include "SynTree/Type.h"
    1821
     
    2124
    2225namespace GenPoly {
    23         /// A function needs an adapter if it returns a polymorphic value or if any of its
    24         /// parameters have polymorphic type
    2526        bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
    26                 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
     27                if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
    2728                        return true;
    2829                } // if
    2930                for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
    30                         if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
     31                        if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
    3132                                return true;
    3233                        } // if
     
    6667        }
    6768
    68         bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
    69                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
     69        namespace {
     70                /// Checks a parameter list for polymorphic parameters
     71                bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, 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(), tyVars, env ) ) return true;
     76                        }
     77                        return false;
     78                }
     79        }
     80       
     81        Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
     82                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
     83                        if ( env ) {
     84                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
     85                                        return isPolyType( newType, tyVars, env );
     86                                } // if
     87                        } // if
    7088                        if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
    71                                 return true;
     89                                return type;
     90                        }
     91                } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
     92                        if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
     93                } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
     94                        if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
     95                }
     96                return 0;
     97        }
     98
     99        Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
     100                if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
     101                        return isPolyType( ptr->get_base(), tyVars, env );
     102                } else if ( env ) {
     103                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
     104                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
     105                                        return isPolyPtr( newType, tyVars, env );
     106                                } // if
    72107                        } // if
    73108                } // if
    74                 return false;
    75         }
    76 
    77         bool isPolyObj( Type *type, const TyVarMap &tyVars ) {
    78                 if ( isPolyVal( type, tyVars ) ) {
    79                         return true;
    80                 } else if ( PointerType *pt = dynamic_cast<PointerType*>( type ) ) {
    81                         return isPolyObj( pt->get_base(), tyVars );
    82                 } else {
    83                         return false;
    84                 }
     109                return 0;
    85110        }
    86111
     
    91116                os << std::endl;
    92117        }
     118
     119        std::string sizeofName( Type *ty ) {
     120                return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false );
     121        }
     122
     123        std::string alignofName( Type *ty ) {
     124                return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false );
     125        }
    93126} // namespace GenPoly
    94127
Note: See TracChangeset for help on using the changeset viewer.