Changeset 3145

Show
Ignore:
Timestamp:
12/03/08 21:39:21 (12 months ago)
Author:
sukyoungryu
Message:

[ast refactoring] Merged AbstractArrowType?, ArrowType?, and _RewriteGenericArrowType.

Location:
trunk/ProjectFortress
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/astgen/Fortress.ast

    r3144 r3145  
    13471347                /** 
    13481348                 * arrow type 
    1349                  */ 
    1350                 abstract AbstractArrowType(Type domain, Type range, 
    1351                                            Effect effect = FortressUtil.emptyEffect()); 
    1352                     /** 
    1353                      * arrow type 
    1354                      * Type ::= Type -> Type Throws? 
    1355                      * e.g.) (String, NN..., p = Printer) -> NN throws IOException 
    1356                      */ 
    1357                     ArrowType(); 
    1358                     /** 
    1359                      * type of a generic function, used during static checking 
    1360                      */ 
    1361                     _RewriteGenericArrowType(List<StaticParam> staticParams 
    1362                                               = Collections.<StaticParam>emptyList(), 
    1363                                              Option<WhereClause> whereClause 
    1364                                               = Option.<WhereClause>none()); 
     1349                 * Type ::= Type -> Type Throws? 
     1350                 * e.g.) (String, NN..., p = Printer) -> NN throws IOException 
     1351                 * 
     1352                 * <staticParams> 
     1353                 * <whereClause> 
     1354                 * type of a generic function, used during static checking 
     1355                 */ 
     1356                ArrowType(Type domain, Type range, 
     1357                          Effect effect = FortressUtil.emptyEffect(), 
     1358                          List<StaticParam> staticParams 
     1359                              = Collections.<StaticParam>emptyList(), 
     1360                          Option<WhereClause> whereClause 
     1361                              = Option.<WhereClause>none()); 
    13651362                /** 
    13661363                 * inferred type 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/disambiguator/TypeDisambiguator.java

    r3144 r3145  
    197197        Type rangeResult = (Type) that.getRange().accept(this); 
    198198        Effect effectResult = (Effect) that.getEffect().accept(this); 
    199         return forArrowTypeOnly(that, domainResult, rangeResult, effectResult); 
     199        return forArrowTypeOnly(that, domainResult, rangeResult, effectResult, 
     200                                that.getStaticParams(), that.getWhereClause()); 
    200201    } 
    201202 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/FnTypeEnv.java

    r3123 r3145  
    4949import com.sun.fortress.nodes.VarType; 
    5050import com.sun.fortress.nodes._InferenceVarType; 
    51 import com.sun.fortress.nodes._RewriteGenericArrowType; 
     51import com.sun.fortress.nodes.ArrowType; 
    5252import com.sun.fortress.nodes_util.NodeFactory; 
    5353import com.sun.fortress.nodes_util.Span; 
     
    148148                // Invariant: _fn.params().isSome() 
    149149                // Otherwise, _fn should not have been in entries. 
    150                 overloadedTypes.add(new _RewriteGenericArrowType(loc, 
    151                                                                  domainFromParams(_fn.parameters()), 
    152                                                                  selfType, 
    153                                                                  makeEffect(loc.getEnd(), CollectUtil.makeList(_fn.thrownTypes())), 
    154                                                                  _fn.staticParameters(), 
    155                                                                  _fn.where())); 
     150                overloadedTypes.add(new ArrowType(loc, 
     151                                                  domainFromParams(_fn.parameters()), 
     152                                                  selfType, 
     153                                                  makeEffect(loc.getEnd(), CollectUtil.makeList(_fn.thrownTypes())), 
     154                                                  _fn.staticParameters(), 
     155                                                  _fn.where())); 
    156156            } 
    157157        } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/ObjectTypeEnv.java

    r3122 r3145  
    3333import com.sun.fortress.nodes.Type; 
    3434import com.sun.fortress.nodes._InferenceVarType; 
    35 import com.sun.fortress.nodes._RewriteGenericArrowType; 
     35import com.sun.fortress.nodes.ArrowType; 
    3636import com.sun.fortress.nodes_util.NodeFactory; 
    3737 
     
    8989                // Some static params, some normal params 
    9090                // TODO: handle type variables bound in where clause 
    91                 type = 
    92                         new _RewriteGenericArrowType(decl.getSpan(), 
    93                                                      domainFromParams(decl.getParams().unwrap()), 
    94                                                      NodeFactory.makeTraitType(_var, TypeEnv.staticParamsToArgs(decl.getStaticParams())), 
    95                                                      decl.getStaticParams(), 
    96                                                      decl.getWhereClause()); 
     91                type = new ArrowType(decl.getSpan(), 
     92                                     domainFromParams(decl.getParams().unwrap()), 
     93                                     NodeFactory.makeTraitType(_var, TypeEnv.staticParamsToArgs(decl.getStaticParams())), 
     94                                     decl.getStaticParams(), 
     95                                     decl.getWhereClause()); 
    9796            } 
    9897        } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/SubtypeChecker.java

    r3123 r3145  
    376376    } 
    377377    private boolean isArrow(Type t) { 
    378         return (t instanceof AbstractArrowType); 
     378        return (t instanceof ArrowType); 
    379379    } 
    380380    private boolean isTuple(Type t) { 
     
    693693            if (isArrow(s) && isArrow(t)) { 
    694694                if (s instanceof ArrowType) { 
    695                     if (t instanceof ArrowType) { 
    696                         ArrowType ss = (ArrowType)s; 
    697                         ArrowType tt = (ArrowType)t; 
    698                         return (subdomain(tt.getDomain(), ss.getDomain(), h) && 
    699                                 subtype(ss.getRange(), tt.getRange(), h)); 
    700                     } else { // t instanceof _RewriteGenericArrowType 
    701                         return FALSE; 
    702                     } 
    703                 } else { // s instanceof _RewriteGenericArrowType 
    704                     if (t instanceof ArrowType) { 
    705                         return FALSE; 
    706                     } else { // t instanceof _RewriteGenericArrowType 
    707                         _RewriteGenericArrowType ss = (_RewriteGenericArrowType)s; 
    708                         _RewriteGenericArrowType tt = (_RewriteGenericArrowType)t; 
    709                         return (subdomain(tt.getDomain(), ss.getDomain(), h) && 
    710                                 subtype(ss.getRange(), tt.getRange(), h) && 
    711                                 equivalentStaticParams(ss.getStaticParams(), 
    712                                                        tt.getStaticParams(), h)); 
    713                     } 
     695                    ArrowType ss = (ArrowType)s; 
     696                    ArrowType tt = (ArrowType)t; 
     697                    return (subdomain(tt.getDomain(), ss.getDomain(), h) && 
     698                            subtype(ss.getRange(), tt.getRange(), h) && 
     699                            equivalentStaticParams(ss.getStaticParams(), 
     700                                                   tt.getStaticParams(), h)); 
    714701                } 
    715702            } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeAnalyzer.java

    r3144 r3145  
    319319 
    320320            @Override public Type forArrowTypeOnly(ArrowType t, Type normalDomain, Type normalRange, 
    321                                                    final Effect normalEffect) { 
     321                                                   final Effect normalEffect, 
     322                                                   List<StaticParam> staticParams, 
     323                                                   Option<WhereClause> whereClause) { 
    322324                Type domainArg = stripKeywords(normalDomain); 
    323325                final Map<Id, Type> domainKeys = extractKeywords(normalDomain); 
     
    560562                        else { return FALSE; } 
    561563                    } 
    562                     @Override public ConstraintFormula forAbstractArrowType(AbstractArrowType s) { 
    563                         if (t instanceof AbstractArrowType) { 
    564                             return arrowSubArrow(s, (AbstractArrowType) t, h); 
     564                    @Override public ConstraintFormula forArrowType(ArrowType s) { 
     565                        if (t instanceof ArrowType) { 
     566                            return arrowSubArrow(s, (ArrowType) t, h); 
    565567                        } 
    566568                        else { return FALSE; } 
     
    644646    } 
    645647 
    646     private ConstraintFormula arrowSubArrow(AbstractArrowType s, AbstractArrowType t, SubtypeHistory h) { 
     648    private ConstraintFormula arrowSubArrow(ArrowType s, ArrowType t, SubtypeHistory h) { 
    647649        ConstraintFormula f = sub(t.getDomain(), s.getDomain(), h); 
    648650        if (!f.isFalse()) { 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeChecker.java

    r3144 r3145  
    821821                                cur_type.accept(new TypeAbstractVisitor_void() { 
    822822                                        @Override 
    823                                         public void forAbstractArrowType(final AbstractArrowType cur_type) { 
     823                                        public void forArrowType(final ArrowType cur_type) { 
    824824                                                if( most_applicable.value().isNone() ) { 
    825825                                                        most_applicable.set(some(Pair.<FunctionalRef,Type>make(pruned_fn.first(), cur_type))); 
     
    829829                                                most_applicable.value().unwrap().second().accept(new TypeAbstractVisitor_void() { 
    830830                                                        @Override 
    831                                                         public void forAbstractArrowType(AbstractArrowType most_appl) { 
     831                                                        public void forArrowType(ArrowType most_appl) { 
    832832                                                                // Where is arg of cur_type <: arg of most_appl? 
    833833                                                                ConstraintFormula sub = 
     
    24602460                                                overloading.accept(new TypeAbstractVisitor<Option<Pair<Pair<Type,ConstraintFormula>,List<StaticArg>>>>() { 
    24612461                                                        @Override 
    2462                                                         public Option<Pair<Pair<Type,ConstraintFormula>, List<StaticArg>>> for_RewriteGenericArrowType( 
    2463                                                                         _RewriteGenericArrowType gen_arr_type) { 
     2462                                                        public Option<Pair<Pair<Type,ConstraintFormula>, List<StaticArg>>> forArrowType( 
     2463                                                                        ArrowType gen_arr_type) { 
    24642464                                                                // If the arrow type is generic, it needs static args, so make up inference variables 
    24652465                                                                List<StaticArg> new_args = 
     
    36693669                                                overloading.accept(new TypeAbstractVisitor<Option<Pair<Pair<Type,ConstraintFormula>,List<StaticArg>>>>() { 
    36703670                                                        @Override 
    3671                                                         public Option<Pair<Pair<Type,ConstraintFormula>, List<StaticArg>>> for_RewriteGenericArrowType( 
    3672                                                                         _RewriteGenericArrowType gen_arr_type) { 
     3671                                                        public Option<Pair<Pair<Type,ConstraintFormula>, List<StaticArg>>> forArrowType( 
     3672                                                                        ArrowType gen_arr_type) { 
    36733673                                                                // If the arrow type is generic, it needs static args, so make up inference variables 
    36743674                                                                List<StaticArg> new_args = 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeCheckerTestCase.java

    r3114 r3145  
    4646import com.sun.fortress.nodes.Type; 
    4747import com.sun.fortress.nodes.WhereClause; 
    48 import com.sun.fortress.nodes._RewriteGenericArrowType; 
    4948import com.sun.fortress.nodes_util.NodeFactory; 
    5049import com.sun.fortress.nodes_util.Span; 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypeEnv.java

    r3132 r3145  
    5353import com.sun.fortress.nodes.IntParam; 
    5454import com.sun.fortress.nodes.IntRef; 
     55import com.sun.fortress.nodes.ArrowType; 
    5556import com.sun.fortress.nodes.IntersectionType; 
    5657import com.sun.fortress.nodes.KeywordType; 
     
    7778import com.sun.fortress.nodes.UnitRef; 
    7879import com.sun.fortress.nodes._InferenceVarType; 
    79 import com.sun.fortress.nodes._RewriteGenericArrowType; 
    8080import com.sun.fortress.nodes_util.ExprFactory; 
    8181import com.sun.fortress.nodes_util.NodeFactory; 
     
    139139    } 
    140140 
    141     protected static _RewriteGenericArrowType genericArrowFromDecl(FnDecl decl) { 
    142         return new _RewriteGenericArrowType(decl.getSpan(), 
    143                                             domainFromParams(decl.getParams()), 
    144                                             // all types have been filled in at this point 
    145                                             decl.getReturnType().unwrap(), 
    146                                             makeEffect(decl.getSpan().getEnd(), 
    147                                                        decl.getThrowsClause()), 
    148                                             decl.getStaticParams(), 
    149                                             decl.getWhereClause()); 
     141    protected static ArrowType genericArrowFromDecl(FnDecl decl) { 
     142        return new ArrowType(decl.getSpan(), 
     143                             domainFromParams(decl.getParams()), 
     144                             // all types have been filled in at this point 
     145                             decl.getReturnType().unwrap(), 
     146                             makeEffect(decl.getSpan().getEnd(), 
     147                                        decl.getThrowsClause()), 
     148                             decl.getStaticParams(), 
     149                             decl.getWhereClause()); 
    150150    } 
    151151 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/TypesUtil.java

    r3123 r3145  
    2929 
    3030import com.sun.fortress.compiler.Types; 
    31 import com.sun.fortress.nodes.AbstractArrowType; 
    3231import com.sun.fortress.nodes.AnyType; 
    3332import com.sun.fortress.nodes.ArrowType; 
     
    4039import com.sun.fortress.nodes.NodeDepthFirstVisitor_void; 
    4140import com.sun.fortress.nodes.ObjectExpr; 
     41import com.sun.fortress.nodes.WhereClause; 
    4242import com.sun.fortress.nodes.StaticArg; 
    4343import com.sun.fortress.nodes.StaticParam; 
     
    5050import com.sun.fortress.nodes.UnionType; 
    5151import com.sun.fortress.nodes._InferenceVarType; 
    52 import com.sun.fortress.nodes._RewriteGenericArrowType; 
    5352import com.sun.fortress.nodes_util.NodeFactory; 
    5453import com.sun.fortress.useful.NI; 
     
    137136            final ConstraintFormula existingConstraint) { 
    138137        // List of arrow types that statically match 
    139         List<AbstractArrowType> matching_types = new ArrayList<AbstractArrowType>(); 
     138        List<ArrowType> matching_types = new ArrayList<ArrowType>(); 
    140139        // The constraint formed from all matching arrows 
    141140        ConstraintFormula result_constraint = ConstraintFormula.TRUE; 
    142141        for( Type arrow : conjuncts(fn_type) ) { 
    143142            // create instantiated arrow types using visitor 
    144             Pair<Option<AbstractArrowType>,ConstraintFormula> pair = 
    145                 arrow.accept(new NodeDepthFirstVisitor<Pair<Option<AbstractArrowType>,ConstraintFormula>>() { 
     143            Pair<Option<ArrowType>,ConstraintFormula> pair = 
     144                arrow.accept(new NodeDepthFirstVisitor<Pair<Option<ArrowType>,ConstraintFormula>>() { 
    146145                    @Override 
    147                     public Pair<Option<AbstractArrowType>, ConstraintFormula> defaultCase( 
     146                    public Pair<Option<ArrowType>, ConstraintFormula> defaultCase( 
    148147                            Node that) { 
    149                         return Pair.make(Option.<AbstractArrowType>none(), ConstraintFormula.FALSE); 
     148                        return Pair.make(Option.<ArrowType>none(), ConstraintFormula.FALSE); 
    150149                    } 
    151150 
    152151                    // apply (inferring if necessary) static arguments and checking sub-typing 
    153                     private Pair<Option<AbstractArrowType>,ConstraintFormula> 
    154                     arrowTypeHelper(AbstractArrowType that, List<StaticParam> static_params) { 
     152                    private Pair<Option<ArrowType>,ConstraintFormula> 
     153                    arrowTypeHelper(ArrowType that, List<StaticParam> static_params) { 
    155154                        int num_static_params = static_params.size(); 
    156155                        int num_static_args = staticArgs.size(); 
     
    163162                            // how to infer it yet. 
    164163                            for( StaticParam p : static_params ) 
    165                                 if( !(p instanceof TypeParam) ) return Pair.make(Option.<AbstractArrowType>none(), ConstraintFormula.FALSE); 
     164                                if( !(p instanceof TypeParam) ) return Pair.make(Option.<ArrowType>none(), ConstraintFormula.FALSE); 
    166165 
    167166                            static_args_to_apply = 
     
    176175                        else if( num_static_params != num_static_args ) { 
    177176                            // just not the right method 
    178                             return Pair.make(Option.<AbstractArrowType>none(), ConstraintFormula.FALSE); 
     177                            return Pair.make(Option.<ArrowType>none(), ConstraintFormula.FALSE); 
    179178                        } 
    180179                        // now apply the static arguments, 
    181                         that = (AbstractArrowType) 
     180                        that = (ArrowType) 
    182181                        that.accept(new StaticTypeReplacer(static_params,static_args_to_apply)); 
    183182                        // and then check parameter sub-typing 
     
    186185                    } 
    187186                    @Override 
    188                     public Pair<Option<AbstractArrowType>, ConstraintFormula> for_RewriteGenericArrowType( 
    189                             _RewriteGenericArrowType that) { 
     187                    public Pair<Option<ArrowType>, ConstraintFormula> forArrowType( 
     188                            ArrowType that) { 
    190189                        return this.arrowTypeHelper(that, that.getStaticParams()); 
    191                     } 
    192                     @Override 
    193                     public Pair<Option<AbstractArrowType>, ConstraintFormula> forArrowType( 
    194                             ArrowType that) { 
    195                         return this.arrowTypeHelper(that, Collections.<StaticParam>emptyList()); 
    196190                    } 
    197191                }); 
     
    210204            // Now, take all the matching ones and join their ranges to be the result range type. 
    211205            Iterable<Type> ranges = 
    212                 IterUtil.map(matching_types, new Lambda<AbstractArrowType, Type>(){ 
    213                     public Type value(AbstractArrowType arg0) { 
     206                IterUtil.map(matching_types, new Lambda<ArrowType, Type>(){ 
     207                    public Type value(ArrowType arg0) { 
    214208                        return arg0.getRange(); 
    215209                    }}); 
     
    230224                valid&=t.accept(new NodeDepthFirstVisitor<Boolean>(){ 
    231225                        @Override public Boolean defaultCase(Node that) {return false;    } 
    232                         @Override public Boolean for_RewriteGenericArrowType(_RewriteGenericArrowType that) {return true;} 
    233226                        @Override public Boolean forArrowType(ArrowType that) {return true;} 
    234227                }); 
     
    244237     * return a ConstraintFormula instead of a type. 
    245238     * @param checker the SubtypeChecker to use for any type comparisons 
    246      * @param fn the type of the function, which can be some AbstractArrowType, 
     239     * @param fn the type of the function, which can be some ArrowType, 
    247240     *           or an intersection of such (in the case of an overloaded 
    248241     *           function) 
     
    265258     * Figure out the static type of a non-generic function application. 
    266259     * @param checker the SubtypeChecker to use for any type comparisons 
    267      * @param fn the type of the function, which can be some AbstractArrowType, 
     260     * @param fn the type of the function, which can be some ArrowType, 
    268261     *           or an intersection of such (in the case of an overloaded 
    269262     *           function) 
     
    299292                    return valid ? some(that) : Option.<ArrowType>none(); 
    300293                } 
    301                 @Override public Option<ArrowType> for_RewriteGenericArrowType(_RewriteGenericArrowType that) { 
    302                     return NI.nyi(); 
    303                 } 
    304294                @Override public Option<ArrowType> defaultCase(Node that) { 
    305295                    return none(); 
     
    434424 
    435425                        @Override 
    436                         public Option<Pair<Type,ConstraintFormula>> for_RewriteGenericArrowType( 
    437                                         _RewriteGenericArrowType that) { 
     426                        public Option<Pair<Type,ConstraintFormula>> forArrowType(ArrowType that) { 
    438427 
    439428                                Option<ConstraintFormula> constraints = StaticTypeReplacer.argsMatchParams(static_args,that.getStaticParams(), subtype_checker); 
    440429 
    441430                                if(constraints.isSome()) { 
    442                                         _RewriteGenericArrowType temp = (_RewriteGenericArrowType) that.accept(new StaticTypeReplacer(that.getStaticParams(),static_args)); 
    443                                         Type new_type = new ArrowType(temp.getSpan(),temp.isParenthesized(),temp.getDomain(),temp.getRange(), temp.getEffect()); 
     431                                        ArrowType temp = (ArrowType) that.accept(new StaticTypeReplacer(that.getStaticParams(),static_args)); 
     432                                        Type new_type = new ArrowType(temp.getSpan(),temp.isParenthesized(), 
     433                                                                      temp.getDomain(),temp.getRange(), temp.getEffect(), 
     434                                                                      Collections.<StaticParam>emptyList(), 
     435                                                                      Option.<WhereClause>none()); 
    444436                                        return Option.some(Pair.make(new_type,constraints.unwrap())); 
    445437                                } 
     
    447439                                        return Option.none(); 
    448440                                } 
    449                         } 
    450                         @Override 
    451                         public Option<Pair<Type,ConstraintFormula>> forArrowType(ArrowType that) { 
    452                                 return Option.none(); 
    453441                        } 
    454442                }); 
     
    511499            for( Type conj : conjuncts(overloaded_type) ) { 
    512500                Boolean b = conj.accept(new TypeAbstractVisitor<Boolean>(){ 
    513                     @Override public Boolean for_RewriteGenericArrowType(_RewriteGenericArrowType that) { return !that.getStaticParams().isEmpty(); } 
     501                    @Override public Boolean forArrowType(ArrowType that) { return !that.getStaticParams().isEmpty(); } 
    514502                    @Override public Boolean forType(Type that) { return Boolean.FALSE; } 
    515503                }); 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java

    r3141 r3145  
    5858import com.sun.fortress.interpreter.glue.WellKnownNames; 
    5959import com.sun.fortress.nodes.APIName; 
    60 import com.sun.fortress.nodes.AbstractArrowType; 
    6160import com.sun.fortress.nodes.AliasedAPIName; 
    6261import com.sun.fortress.nodes.AliasedSimpleName; 
  • trunk/ProjectFortress/src/com/sun/fortress/tools/FortressAstToConcrete.java

    r3144 r3145  
    21662166                                             String domain_result, 
    21672167                                             String range_result, 
    2168                                              String effect_result) { 
     2168                                             String effect_result, 
     2169                                             List<String> staticParams_result, 
     2170                                             Option<String> whereClause_result) { 
    21692171        StringBuilder s = new StringBuilder(); 
    21702172 
     
    21762178        return handleParen( s.toString(), 
    21772179                            that.isParenthesized() ); 
    2178     } 
    2179  
    2180     @Override public String for_RewriteGenericArrowTypeOnly(_RewriteGenericArrowType that, 
    2181                                                             String domain_result, 
    2182                                                             String range_result, 
    2183                                                             String effect_result, 
    2184                                                             List<String> staticParams_result, 
    2185                                                             Option<String> where_result) { 
    2186         StringBuilder s = new StringBuilder(); 
    2187  
    2188         inOxfordBrackets(s, staticParams_result ); 
    2189         s.append( domain_result ); 
    2190         s.append( " -> " ); 
    2191         s.append( range_result ); 
    2192         s.append( effect_result ); 
    2193  
    2194         return "(* " + handleParen( s.toString(), 
    2195                                     that.isParenthesized() ) + " *)"; 
    21962180    } 
    21972181