Ignore:
Timestamp:
Jun 4, 2019, 4:45:04 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
de8dfac2
Parents:
4ae2364
Message:

Port CommonType? to new AST

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Unify.cc

    r4ae2364 ree574a2  
    136136
    137137                return unifyExact(
    138                         newFirst, newSecond, newEnv, need, have, open, WidenMode{ false, false }, symtab );
     138                        newFirst, newSecond, newEnv, need, have, open, noWiden(), symtab );
    139139        }
    140140
     
    173173                env.apply( newFirst );
    174174                env.apply( newSecond );
    175                 clear_qualifiers( newFirst );
    176                 clear_qualifiers( newSecond );
     175                reset_qualifiers( newFirst );
     176                reset_qualifiers( newSecond );
    177177
    178178                return unifyExact(
    179                         newFirst, newSecond, newEnv, need, have, open, WidenMode{ false, false }, symtab );
     179                        newFirst, newSecond, newEnv, need, have, open, noWiden(), symtab );
    180180        }
    181181
     
    700700        }
    701701
    702         class Unify_new : public ast::WithShortCircuiting {
     702        class Unify_new final : public ast::WithShortCircuiting {
    703703                const ast::Type * type2;
    704704                ast::TypeEnvironment & tenv;
     
    720720                void previsit( const ast::Node * ) { visit_children = false; }
    721721               
    722                 void previsit( const ast::VoidType * ) {
    723                         visit_children = false;
     722                void postvisit( const ast::VoidType * ) {
    724723                        result = dynamic_cast< const ast::VoidType * >( type2 );
    725724                }
    726725
    727                 void previsit( const ast::BasicType * basic ) {
    728                         visit_children = false;
     726                void postvisit( const ast::BasicType * basic ) {
    729727                        if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
    730728                                result = basic->kind == basic2->kind;
     
    732730                }
    733731
    734                 void previsit( const ast::PointerType * pointer ) {
    735                         visit_children = false;
     732                void postvisit( const ast::PointerType * pointer ) {
    736733                        if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
    737734                                result = unifyExact(
    738735                                        pointer->base, pointer2->base, tenv, need, have, open,
    739                                         WidenMode{ false, false }, symtab );
    740                         }
    741                 }
    742 
    743                 void previsit( const ast::ArrayType * array ) {
    744                         visit_children = false;
     736                                        noWiden(), symtab );
     737                        }
     738                }
     739
     740                void postvisit( const ast::ArrayType * array ) {
    745741                        auto array2 = dynamic_cast< const ast::ArrayType * >( type2 );
    746742                        if ( ! array2 ) return;
     
    761757
    762758                        result = unifyExact(
    763                                 array->base, array2->base, tenv, need, have, open, WidenMode{ false, false },
     759                                array->base, array2->base, tenv, need, have, open, noWiden(),
    764760                                symtab );
    765761                }
    766762
    767                 void previsit( const ast::ReferenceType * ref ) {
    768                         visit_children = false;
     763                void postvisit( const ast::ReferenceType * ref ) {
    769764                        if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
    770765                                result = unifyExact(
    771                                         ref->base, ref2->base, tenv, need, have, open, WidenMode{ false, false },
     766                                        ref->base, ref2->base, tenv, need, have, open, noWiden(),
    772767                                        symtab );
    773768                        }
     
    783778                        TtypeExpander_new( ast::TypeEnvironment & env ) : tenv( env ) {}
    784779
    785                         const ast::Type * postmutate( const ast::TypeInstType * typeInst ) {
     780                        const ast::Type * postvisit( const ast::TypeInstType * typeInst ) {
    786781                                if ( const ast::EqvClass * clz = tenv.lookup( typeInst->name ) ) {
    787782                                        // expand ttype parameter into its actual type
     
    811806                                        // overloaded on outermost mutex and a mutex function has different
    812807                                        // requirements than a non-mutex function
    813                                         t.get_and_mutate()->qualifiers
    814                                                 -= ast::CV::Const | ast::CV::Volatile | ast::CV::Atomic;
     808                                        remove_qualifiers( t, ast::CV::Const | ast::CV::Volatile | ast::CV::Atomic );
    815809                                        dst.emplace_back( new ast::ObjectDecl{ d->location, "", t } );
    816810                                }
     
    851845                                        return unifyExact(
    852846                                                t1, tupleFromDecls( crnt2, end2 ), env, need, have, open,
    853                                                 WidenMode{ false, false }, symtab );
     847                                                noWiden(), symtab );
    854848                                } else if ( ! isTuple1 && isTuple2 ) {
    855849                                        // combine remainder of list1, then unify
    856850                                        return unifyExact(
    857851                                                tupleFromDecls( crnt1, end1 ), t2, env, need, have, open,
    858                                                 WidenMode{ false, false }, symtab );
     852                                                noWiden(), symtab );
    859853                                }
    860854
    861855                                if ( ! unifyExact(
    862                                         t1, t2, env, need, have, open, WidenMode{ false, false }, symtab )
     856                                        t1, t2, env, need, have, open, noWiden(), symtab )
    863857                                ) return false;
    864858
     
    874868                                return unifyExact(
    875869                                        t1, tupleFromDecls( crnt2, end2 ), env, need, have, open,
    876                                         WidenMode{ false, false }, symtab );
     870                                        noWiden(), symtab );
    877871                        } else if ( crnt2 != end2 ) {
    878872                                // try unifying empty tuple with ttype
     
    881875                                return unifyExact(
    882876                                        tupleFromDecls( crnt1, end1 ), t2, env, need, have, open,
    883                                         WidenMode{ false, false }, symtab );
     877                                        noWiden(), symtab );
    884878                        }
    885879
     
    919913
    920914        public:
    921                 void previsit( const ast::FunctionType * func ) {
    922                         visit_children = false;
     915                void postvisit( const ast::FunctionType * func ) {
    923916                        auto func2 = dynamic_cast< const ast::FunctionType * >( type2 );
    924917                        if ( ! func2 ) return;
     
    952945                template< typename RefType >
    953946                const RefType * handleRefType( const RefType * inst, const ast::Type * other ) {
    954                         visit_children = false;
    955947                        // check that the other type is compatible and named the same
    956948                        auto otherInst = dynamic_cast< const RefType * >( other );
     
    10111003
    10121004                                if ( ! unifyExact(
    1013                                                 pty, pty2, tenv, need, have, open, WidenMode{ false, false }, symtab ) ) {
     1005                                                pty, pty2, tenv, need, have, open, noWiden(), symtab ) ) {
    10141006                                        result = false;
    10151007                                        return;
     
    10231015
    10241016        public:
    1025                 void previsit( const ast::StructInstType * aggrType ) {
     1017                void postvisit( const ast::StructInstType * aggrType ) {
    10261018                        handleGenericRefType( aggrType, type2 );
    10271019                }
    10281020
    1029                 void previsit( const ast::UnionInstType * aggrType ) {
     1021                void postvisit( const ast::UnionInstType * aggrType ) {
    10301022                        handleGenericRefType( aggrType, type2 );
    10311023                }
    10321024
    1033                 void previsit( const ast::EnumInstType * aggrType ) {
     1025                void postvisit( const ast::EnumInstType * aggrType ) {
    10341026                        handleRefType( aggrType, type2 );
    10351027                }
    10361028
    1037                 void previsit( const ast::TraitInstType * aggrType ) {
     1029                void postvisit( const ast::TraitInstType * aggrType ) {
    10381030                        handleRefType( aggrType, type2 );
    10391031                }
    10401032
    1041                 void previsit( const ast::TypeInstType * typeInst ) {
     1033                void postvisit( const ast::TypeInstType * typeInst ) {
    10421034                        assert( open.find( typeInst->name ) == open.end() );
    10431035                        handleRefType( typeInst, type2 );
     
    10781070                                        return unifyExact(
    10791071                                                t1, tupleFromTypes( list2 ), env, need, have, open,
    1080                                                 WidenMode{ false, false }, symtab );
     1072                                                noWiden(), symtab );
    10811073                                } else if ( ! isTuple1 && isTuple2 ) {
    10821074                                        // combine entirety of list1, then unify
    10831075                                        return unifyExact(
    10841076                                                tupleFromTypes( list1 ), t2, env, need, have, open,
    1085                                                 WidenMode{ false, false }, symtab );
     1077                                                noWiden(), symtab );
    10861078                                }
    10871079
    10881080                                if ( ! unifyExact(
    1089                                         t1, t2, env, need, have, open, WidenMode{ false, false }, symtab )
     1081                                        t1, t2, env, need, have, open, noWiden(), symtab )
    10901082                                ) return false;
    10911083
     
    11011093                                return unifyExact(
    11021094                                                t1, tupleFromTypes( list2 ), env, need, have, open,
    1103                                                 WidenMode{ false, false }, symtab );
     1095                                                noWiden(), symtab );
    11041096                        } else if ( crnt2 != list2.end() ) {
    11051097                                // try unifying empty tuple with ttype
     
    11101102                                return unifyExact(
    11111103                                                tupleFromTypes( list1 ), t2, env, need, have, open,
    1112                                                 WidenMode{ false, false }, symtab );
     1104                                                noWiden(), symtab );
    11131105                        }
    11141106
     
    11171109
    11181110        public:
    1119                 void previsit( const ast::TupleType * tuple ) {
    1120                         visit_children = false;
     1111                void postvisit( const ast::TupleType * tuple ) {
    11211112                        auto tuple2 = dynamic_cast< const ast::TupleType * >( type2 );
    11221113                        if ( ! tuple2 ) return;
     
    11321123                }
    11331124
    1134                 void previsit( const ast::VarArgsType * ) {
    1135                         visit_children = false;
     1125                void postvisit( const ast::VarArgsType * ) {
    11361126                        result = dynamic_cast< const ast::VarArgsType * >( type2 );
    11371127                }
    11381128
    1139                 void previsit( const ast::ZeroType * ) {
    1140                         visit_children = false;
     1129                void postvisit( const ast::ZeroType * ) {
    11411130                        result = dynamic_cast< const ast::ZeroType * >( type2 );
    11421131                }
    11431132
    1144                 void previsit( const ast::OneType * ) {
    1145                         visit_children = false;
     1133                void postvisit( const ast::OneType * ) {
    11461134                        result = dynamic_cast< const ast::OneType * >( type2 );
    11471135                }       
     
    11511139                template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
    11521140        };
     1141
     1142        bool unify(
     1143                        const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
     1144                        ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
     1145                        ast::OpenVarSet & open, const ast::SymbolTable & symtab, ast::ptr<ast::Type> & common
     1146        ) {
     1147                ast::OpenVarSet closed;
     1148                findOpenVars( type1, open, closed, need, have, FirstClosed );
     1149                findOpenVars( type2, open, closed, need, have, FirstOpen );
     1150                return unifyInexact(
     1151                        type1, type2, env, need, have, open, WidenMode{ true, true }, symtab, common );
     1152        }
    11531153
    11541154        bool unifyExact(
     
    11841184
    11851185        bool unifyInexact(
    1186                         ast::ptr<ast::Type> & type1, ast::ptr<ast::Type> & type2, ast::TypeEnvironment & env,
    1187                         ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
    1188                         WidenMode widen, const ast::SymbolTable & symtab, ast::ptr<ast::Type> & common
     1186                        const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
     1187                        ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
     1188                        const ast::OpenVarSet & open, WidenMode widen, const ast::SymbolTable & symtab,
     1189                        ast::ptr<ast::Type> & common
    11891190        ) {
    11901191                ast::CV::Qualifiers q1 = type1->qualifiers, q2 = type2->qualifiers;
     
    11931194                // type2 are left unchanged; calling convention forces type{1,2}->strong_ref >= 1
    11941195                ast::ptr<ast::Type> t1{ type1 }, t2{ type2 };
    1195                 clear_qualifiers( t1 );
    1196                 clear_qualifiers( t2 );
     1196                reset_qualifiers( t1 );
     1197                reset_qualifiers( t2 );
    11971198               
    11981199                if ( unifyExact( t1, t2, env, need, have, open, widen, symtab ) ) {
     
    12011202                        // if exact unification on unqualified types, try to merge qualifiers
    12021203                        if ( q1 == q2 || ( ( q1 > q2 || widen.first ) && ( q2 > q1 || widen.second ) ) ) {
    1203                                 common.set_and_mutate( type1 )->qualifiers = q1 | q2;
     1204                                common = type1;
     1205                                reset_qualifiers( common, q1 | q2 );
    12041206                                return true;
    12051207                        } else {
     
    12111213
    12121214                        // no exact unification, but common type
    1213                         common.get_and_mutate()->qualifiers = q1 | q2;
     1215                        reset_qualifiers( common, q1 | q2 );
    12141216                        return true;
    12151217                } else {
Note: See TracChangeset for help on using the changeset viewer.