Changeset 6840e7c for src/GenPoly


Ignore:
Timestamp:
Oct 19, 2017, 12:01:04 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
Children:
837ce06
Parents:
b96ec83 (diff), a15b72c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

Location:
src/GenPoly
Files:
2 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rb96ec83 r6840e7c  
    600600
    601601                        // add size/align for generic types to parameter list
    602                         if ( ! appExpr->get_function()->has_result() ) return;
     602                        if ( ! appExpr->get_function()->result ) return;
    603603                        FunctionType *funcType = getFunctionType( appExpr->get_function()->get_result() );
    604604                        assert( funcType );
     
    714714
    715715                void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
    716                         assertf( arg->has_result(), "arg does not have result: %s", toString( arg ).c_str() );
    717                         if ( isPolyType( param, exprTyVars ) ) {
    718                                 Type * newType = arg->get_result()->clone();
     716                        assertf( arg->result, "arg does not have result: %s", toString( arg ).c_str() );
     717                        if ( ! needsBoxing( param, arg->result, exprTyVars, env ) ) return;
     718
     719                        if ( arg->result->get_lvalue() ) {
     720                                // argument expression may be CFA lvalue, but not C lvalue -- apply generalizedLvalue transformations.
     721                                // if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( arg ) ) {
     722                                //      if ( dynamic_cast<ArrayType *>( varExpr->var->get_type() ) ){
     723                                //              // temporary hack - don't box arrays, because &arr is not the same as &arr[0]
     724                                //              return;
     725                                //      }
     726                                // }
     727                                arg =  generalizedLvalue( new AddressExpr( arg ) );
     728                                if ( ! ResolvExpr::typesCompatible( param, arg->get_result(), SymTab::Indexer() ) ) {
     729                                        // silence warnings by casting boxed parameters when the actual type does not match up with the formal type.
     730                                        arg = new CastExpr( arg, param->clone() );
     731                                }
     732                        } else {
     733                                // use type computed in unification to declare boxed variables
     734                                Type * newType = param->clone();
    719735                                if ( env ) env->apply( newType );
    720                                 std::unique_ptr<Type> manager( newType );
    721                                 if ( isPolyType( newType ) ) {
    722                                         // if the argument's type is polymorphic, we don't need to box again!
    723                                         return;
    724                                 } else if ( arg->get_result()->get_lvalue() ) {
    725                                         // argument expression may be CFA lvalue, but not C lvalue -- apply generalizedLvalue transformations.
    726                                         // if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( arg ) ) {
    727                                         //      if ( dynamic_cast<ArrayType *>( varExpr->var->get_type() ) ){
    728                                         //              // temporary hack - don't box arrays, because &arr is not the same as &arr[0]
    729                                         //              return;
    730                                         //      }
    731                                         // }
    732                                         arg =  generalizedLvalue( new AddressExpr( arg ) );
    733                                         if ( ! ResolvExpr::typesCompatible( param, arg->get_result(), SymTab::Indexer() ) ) {
    734                                                 // silence warnings by casting boxed parameters when the actual type does not match up with the formal type.
    735                                                 arg = new CastExpr( arg, param->clone() );
    736                                         }
    737                                 } else {
    738                                         // use type computed in unification to declare boxed variables
    739                                         Type * newType = param->clone();
    740                                         if ( env ) env->apply( newType );
    741                                         ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, newType, 0 );
    742                                         newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
    743                                         stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
    744                                         UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); // TODO: why doesn't this just use initialization syntax?
    745                                         assign->get_args().push_back( new VariableExpr( newObj ) );
    746                                         assign->get_args().push_back( arg );
    747                                         stmtsToAddBefore.push_back( new ExprStmt( noLabels, assign ) );
    748                                         arg = new AddressExpr( new VariableExpr( newObj ) );
    749                                 } // if
     736                                ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, newType, 0 );
     737                                newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
     738                                stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
     739                                UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); // TODO: why doesn't this just use initialization syntax?
     740                                assign->get_args().push_back( new VariableExpr( newObj ) );
     741                                assign->get_args().push_back( arg );
     742                                stmtsToAddBefore.push_back( new ExprStmt( noLabels, assign ) );
     743                                arg = new AddressExpr( new VariableExpr( newObj ) );
    750744                        } // if
    751745                }
     
    965959                                if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
    966960                                        if ( varExpr->get_var()->get_name() == "?[?]" ) {
    967                                                 assert( appExpr->has_result() );
     961                                                assert( appExpr->result );
    968962                                                assert( appExpr->get_args().size() == 2 );
    969963                                                Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env );
     
    999993                                                } // if
    1000994                                        } else if ( varExpr->get_var()->get_name() == "*?" ) {
    1001                                                 assert( appExpr->has_result() );
     995                                                assert( appExpr->result );
    1002996                                                assert( ! appExpr->get_args().empty() );
    1003997                                                if ( isPolyType( appExpr->get_result(), scopeTyVars, env ) ) {
     
    10161010                                                } // if
    10171011                                        } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) {
    1018                                                 assert( appExpr->has_result() );
     1012                                                assert( appExpr->result );
    10191013                                                assert( appExpr->get_args().size() == 1 );
    10201014                                                if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) {
     
    10361030                                                } // if
    10371031                                        } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
    1038                                                 assert( appExpr->has_result() );
     1032                                                assert( appExpr->result );
    10391033                                                assert( appExpr->get_args().size() == 1 );
    10401034                                                if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) {
     
    10421036                                                } // if
    10431037                                        } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
    1044                                                 assert( appExpr->has_result() );
     1038                                                assert( appExpr->result );
    10451039                                                assert( appExpr->get_args().size() == 2 );
    10461040                                                Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env );
     
    10681062                                                } // if
    10691063                                        } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
    1070                                                 assert( appExpr->has_result() );
     1064                                                assert( appExpr->result );
    10711065                                                assert( appExpr->get_args().size() == 2 );
    10721066                                                Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env );
     
    11621156                void Pass1::premutate( AddressExpr * ) { visit_children = false; }
    11631157                Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
    1164                         assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() );
     1158                        assert( addrExpr->get_arg()->result && ! addrExpr->get_arg()->get_result()->isVoid() );
    11651159
    11661160                        bool needs = false;
    11671161                        if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
    1168                                 if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) {
     1162                                if ( expr->result && isPolyType( expr->get_result(), scopeTyVars, env ) ) {
    11691163                                        if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
    11701164                                                if ( name->get_name() == "*?" ) {
    11711165                                                        if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) {
    1172                                                                 assert( appExpr->get_function()->has_result() );
     1166                                                                assert( appExpr->get_function()->result );
    11731167                                                                FunctionType *function = getFunctionType( appExpr->get_function()->get_result() );
    11741168                                                                assert( function );
  • src/GenPoly/FindFunction.cc

    rb96ec83 r6840e7c  
    1818#include <utility>                      // for pair
    1919
     20#include "Common/PassVisitor.h"         // for PassVisitor
    2021#include "Common/SemanticError.h"       // for SemanticError
    2122#include "GenPoly/ErasableScopedMap.h"  // for ErasableScopedMap<>::iterator
     
    2728
    2829namespace GenPoly {
    29         class FindFunction : public Mutator {
     30        class FindFunction : public WithGuards, public WithVisitorRef<FindFunction>, public WithShortCircuiting {
    3031          public:
    3132                FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
    3233
    33                 virtual Type *mutate( FunctionType *functionType );
    34                 virtual Type *mutate( PointerType *pointerType );
     34                void premutate( FunctionType * functionType );
     35                Type * postmutate( FunctionType * functionType );
     36                void premutate( PointerType * pointerType );
    3537          private:
    3638                void handleForall( const Type::ForallList &forall );
     
    4345
    4446        void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
    45                 FindFunction finder( functions, tyVars, false, predicate );
     47                PassVisitor<FindFunction> finder( functions, tyVars, false, predicate );
    4648                type->acceptMutator( finder );
    4749        }
    4850
    4951        void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
    50                 FindFunction finder( functions, tyVars, true, predicate );
     52                PassVisitor<FindFunction> finder( functions, tyVars, true, predicate );
    5153                type = type->acceptMutator( finder );
    5254        }
     
    5759
    5860        void FindFunction::handleForall( const Type::ForallList &forall ) {
    59                 for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
    60                         TyVarMap::iterator var = tyVars.find( (*i)->get_name() );
     61                for ( const Declaration * td : forall ) {
     62                        TyVarMap::iterator var = tyVars.find( td->name );
    6163                        if ( var != tyVars.end() ) {
    6264                                tyVars.erase( var->first );
     
    6567        }
    6668
    67         Type * FindFunction::mutate( FunctionType *functionType ) {
    68                 tyVars.beginScope();
     69        void FindFunction::premutate( FunctionType * functionType ) {
     70                visit_children = false;
     71                GuardScope( tyVars );
    6972                handleForall( functionType->get_forall() );
    70                 mutateAll( functionType->get_returnVals(), *this );
     73                mutateAll( functionType->get_returnVals(), *visitor );
     74        }
     75
     76        Type * FindFunction::postmutate( FunctionType * functionType ) {
    7177                Type *ret = functionType;
    7278                if ( predicate( functionType, tyVars ) ) {
     
    7783                        } // if
    7884                } // if
    79                 tyVars.endScope();
    8085                return ret;
    8186        }
    8287
    83         Type * FindFunction::mutate( PointerType *pointerType ) {
    84                 tyVars.beginScope();
     88        void FindFunction::premutate( PointerType * pointerType ) {
     89                GuardScope( tyVars );
    8590                handleForall( pointerType->get_forall() );
    86                 Type *ret = Mutator::mutate( pointerType );
    87                 tyVars.endScope();
    88                 return ret;
    8991        }
    9092} // namespace GenPoly
  • src/GenPoly/GenPoly.cc

    rb96ec83 r6840e7c  
    432432        }
    433433
     434        bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, TypeSubstitution * env ) {
     435                // is parameter is not polymorphic, don't need to box
     436                if ( ! isPolyType( param, exprTyVars ) ) return false;
     437                Type * newType = arg->clone();
     438                if ( env ) env->apply( newType );
     439                std::unique_ptr<Type> manager( newType );
     440                // if the argument's type is polymorphic, we don't need to box again!
     441                return ! isPolyType( newType );
     442        }
     443
     444        bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, TypeSubstitution * env ) {
     445                FunctionType * function = getFunctionType( appExpr->function->result );
     446                assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() );
     447                TyVarMap exprTyVars( TypeDecl::Data{} );
     448                makeTyVarMap( function, exprTyVars );
     449                return needsBoxing( param, arg, exprTyVars, env );
     450        }
     451
    434452        void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) {
    435453                // xxx - should this actually be insert?
  • src/GenPoly/GenPoly.h

    rb96ec83 r6840e7c  
    8080        bool typesPolyCompatible( Type *aty, Type *bty );
    8181
     82        /// true if arg requires boxing given exprTyVars
     83        bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, TypeSubstitution * env );
     84
     85        /// true if arg requires boxing in the call to appExpr
     86        bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, TypeSubstitution * env );
     87
    8288        /// Adds the type variable `tyVar` to `tyVarMap`
    8389        void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap );
  • src/GenPoly/Specialize.cc

    rb96ec83 r6840e7c  
    147147
    148148        Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) {
    149                 assertf( actual->has_result(), "attempting to specialize an untyped expression" );
     149                assertf( actual->result, "attempting to specialize an untyped expression" );
    150150                if ( needsSpecialization( formalType, actual->get_result(), env ) ) {
    151151                        if ( FunctionType *funType = getFunctionType( formalType ) ) {
  • src/GenPoly/module.mk

    rb96ec83 r6840e7c  
    2020       GenPoly/Lvalue.cc \
    2121       GenPoly/Specialize.cc \
    22        GenPoly/CopyParams.cc \
    2322       GenPoly/FindFunction.cc \
    2423       GenPoly/InstantiateGeneric.cc
Note: See TracChangeset for help on using the changeset viewer.