Changeset 2228

Show
Ignore:
Timestamp:
07/10/08 11:01:26 (17 months ago)
Author:
dr2chase
Message:

Working towards integration with static front end and compiled top level environments; trying to separate top-level refs from others.

Location:
trunk
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        77testFile.txt 
        88papers 
         9tmp 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java

    r2227 r2228  
    4949import com.sun.fortress.interpreter.evaluator.types.FType; 
    5050import com.sun.fortress.interpreter.evaluator.values.Closure; 
     51import com.sun.fortress.interpreter.evaluator.values.FGenericFunction; 
    5152import com.sun.fortress.interpreter.evaluator.values.FString; 
    5253import com.sun.fortress.interpreter.evaluator.values.FValue; 
     54import com.sun.fortress.interpreter.evaluator.values.OverloadedFunction; 
    5355import com.sun.fortress.nodes.APIName; 
    5456import com.sun.fortress.nodes.AbsDecl; 
     
    302304        } 
    303305 
    304         importers = null; 
     306         
    305307 
    306308        for (ComponentWrapper cw : components) { 
     
    313315            cw.initFuncs(); 
    314316        } 
     317         
    315318        for (ComponentWrapper cw : components) { 
    316319            finishAllFunctionalMethods(cw.getEnvironment()); 
    317320        } 
    318321 
     322       /* 
     323         * Special case -- finish (perhaps redundantly) any overloads that result 
     324         * only from imports (with no function definition in the importing 
     325         * component).  Lack of a function definition means that BuildEnvironments 
     326         * does not do the finish step.  See bug #147.   
     327         */ 
     328        for (Importer imp : importers) { 
     329            imp.finishOverloads(); 
     330        } 
     331         
     332        importers = null; 
     333         
    319334        for (ComponentWrapper cw : components) { 
     335            cw.reset(); 
     336        } 
     337         
     338         for (ComponentWrapper cw : components) { 
    320339            cw.initVars(); 
    321340        } 
     
    576595    static abstract class Importer { 
    577596        abstract boolean runImports(); 
     597        abstract public void finishOverloads(); 
    578598        abstract void reportErrors(); 
    579599    } 
     
    611631 
    612632        collectImportedValueAndTypeNames(fromApi, vnames, tnames); 
    613  
     633         
     634        Set<String> actually_used = importer.desugarer.topLevelUses; 
     635         
     636        // HACK, types referenced from native code, I think. 
     637        actually_used.add("Any"); 
     638        actually_used.add("__immutableFactory1"); 
     639         
     640        // vnames.retainAll(actually_used); 
     641        // tnames.retainAll(actually_used); 
     642         
     643         
    614644       final Importer imp = new Importer() { 
    615645           public String toString() { 
     
    620650 
    621651           final Set<String> added = new HashSet<String>(); 
     652           final Set<String> overloaded = new HashSet<String>(); 
     653           final public void finishOverloads() { 
     654               for (String s: overloaded) { 
     655                   OverloadedFunction fv = (OverloadedFunction) into_e.getValue(s); 
     656                   fv.finishInitializing(); 
     657               } 
     658           } 
    622659        @Override 
    623660        synchronized void reportErrors() { 
     
    714751                    } 
    715752                    if (do_import) { 
    716                         into_e.putValue(s, NI.cnnf(from_e.getValueRaw(s))); 
     753                        FValue fv = NI.cnnf(from_e.getValueRaw(s)); 
     754                        into_e.putValue(s, fv); 
     755                        FValue fv2 = into_e.getValueRaw(s); 
     756                        if (  
     757                            fv2 instanceof OverloadedFunction) { 
     758                         // hack, leave this empty temporarily. 
     759                          //  overloaded.add(s); 
     760                        } 
    717761                        added.add(s); 
    718762                    } else { 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/env/ComponentWrapper.java

    r2181 r2228  
    5454    private final static int UNVISITED=0, IMPORTED=1, POPULATED=2, TYPED=3, FUNCTIONED=4, FINISHED=5; 
    5555 
     56    public void reset() { 
     57        ownNonFunctionNames = null; 
     58        ownNames = null; 
     59        ownTypeNames = null; 
     60        excludedImportNames = null; 
     61        importedNames = null; 
     62        HashMap<String, ComponentWrapper> exports = this.exports; 
     63        this.exports = null; 
     64        if (exports != null) 
     65            for (ComponentWrapper cw : exports.values()) { 
     66                cw.reset(); 
     67            } 
     68    } 
     69     
    5670    Visitor2<String, Object> nameCollector = new Visitor2<String, Object>() { 
    5771 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/BaseEnv.java

    r2190 r2228  
    3939import com.sun.fortress.nodes.APIName; 
    4040import com.sun.fortress.nodes.Id; 
     41import com.sun.fortress.nodes.NamedType; 
    4142import com.sun.fortress.nodes.OpName; 
    4243import com.sun.fortress.nodes.OpRef; 
     
    313314    } 
    314315 
     316    final public  FType getType(NamedType q)  { 
     317        FType x = getTypeNull(q.getName()); 
     318        if (x == null) 
     319            { 
     320                // System.err.println(this.toString()); 
     321                return error(errorMsg("Missing type ", q)); 
     322            } 
     323        else 
     324            return x; 
     325    } 
     326     
    315327    final public  FType getType(Id q)  { 
    316328        FType x = getTypeNull(q); 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/Environment.java

    r2190 r2228  
    2626import com.sun.fortress.nodes.APIName; 
    2727import com.sun.fortress.nodes.Id; 
     28import com.sun.fortress.nodes.NamedType; 
    2829import com.sun.fortress.nodes.OpRef; 
    2930import com.sun.fortress.nodes.VarRef; 
     
    8081    /* Type names take the form ID or Api.ID */ 
    8182    public abstract FType getType(Id d); 
     83     
     84    public abstract FType getType(NamedType q); 
    8285 
    8386    public abstract FType getType(String str); 
     
    182185    /** 
    183186     * Put a value in the top-most scope. 
    184      * Return true if successful, false if already defined. 
    185187     * @param str 
    186188     * @param f2 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/EvalType.java

    r2196 r2228  
    371371    public FType forVarType(VarType i) { 
    372372        try { 
    373             FType result = env.getType(i.getName()); 
     373            FType result = env.getType(i); 
    374374            return result; 
    375375        } catch (FortressException p) { 
     
    504504 
    505505    public FType forTraitType(TraitType x) { 
    506        FType ft1 = forId(x.getName()); 
     506        FType ft1; 
     507        try { 
     508            ft1 = env.getType(x); 
     509        } catch (FortressException p) { 
     510            throw p.setContext(x,env); 
     511        } 
     512               
    507513        if (ft1 instanceof  FTypeGeneric) { 
    508514            FTypeGeneric ftg = (FTypeGeneric) ft1; 
     
    513519            // TraitTypes (possibly with zero arguments). EricAllen 11/5/2007 
    514520            return ft1; 
    515 //            return error(x, env, errorMsg("Expected generic type, got ", ft1, 
    516 //                                          " instead")); 
    517521        } 
    518522    } 
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/rewrite/Desugarer.java

    r2190 r2228  
    162162            lexicalNestedness = lexicalNestingDepth; 
    163163        } 
     164         
    164165        /** May assume {@code original} has a non-zero length. */ 
    165166        Expr replacement(VarRef original) { 
    166167            return NodeFactory.makeVarRef(original, lexicalNestedness); 
    167168        } 
     169         
    168170        Expr replacement(FnRef original) { 
    169171            return NodeFactory.makeFnRef(original, lexicalNestedness); 
     
    171173        Expr replacement(OpRef original) { 
    172174            return NodeFactory.makeOpRef(original, lexicalNestedness); 
     175        } 
     176        VarType replacement(VarType original) { 
     177             return NodeFactory.makeVarType(original, lexicalNestedness); 
     178        } 
     179        TraitType replacement(TraitType original) { 
     180            return NodeFactory.makeTraitType(original, lexicalNestedness); 
    173181        } 
    174182        public String toString() { return "Thing@"+objectNestedness+"/"+lexicalNestedness; } 
     
    204212            if (_transient) isTransient = true; 
    205213        } 
     214        @Override 
    206215        Expr replacement(VarRef original) { 
    207216            if (isTransient) { 
     
    238247 
    239248 
    240     private static class IsAnArrowName extends NodeAbstractVisitor<Boolean> { 
     249    private static class ArrowOrFunctional {}; 
     250    private final static ArrowOrFunctional NEITHER = new ArrowOrFunctional(); 
     251    private final static ArrowOrFunctional ARROW = new ArrowOrFunctional(); 
     252    private final static ArrowOrFunctional FUNCTIONAL = new ArrowOrFunctional(); 
     253     
     254    private static class IsAnArrowName extends NodeAbstractVisitor<ArrowOrFunctional> { 
    241255 
    242256        /* (non-Javadoc) 
     
    244258         */ 
    245259        @Override 
    246         public Boolean forLValueBind(LValueBind that) { 
     260        public ArrowOrFunctional forLValueBind(LValueBind that) { 
    247261            return optionTypeIsArrow(that.getType()); 
    248262        } 
     
    270284         */ 
    271285        @Override 
    272         public Boolean forAbsFnDecl(AbsFnDecl that) { 
     286        public ArrowOrFunctional forAbsFnDecl(AbsFnDecl that) { 
    273287            // Return "is a self method" 
    274             return NodeUtil.selfParameterIndex(that) >= 0; 
     288            return NodeUtil.selfParameterIndex(that) >= 0  ? FUNCTIONAL : NEITHER; 
    275289        } 
    276290 
     
    279293         */ 
    280294        @Override 
    281         public Boolean forFnDef(FnDef that) { 
     295        public ArrowOrFunctional forFnDef(FnDef that) { 
    282296         // Return "is a self method" 
    283             return NodeUtil.selfParameterIndex(that) >= 0; 
     297            return NodeUtil.selfParameterIndex(that) >= 0 ? FUNCTIONAL : NEITHER; 
    284298        } 
    285299 
     
    288302         */ 
    289303        @Override 
    290         public Boolean forTestDecl(TestDecl that) { 
     304        public ArrowOrFunctional forTestDecl(TestDecl that) { 
    291305            // FALSE 
    292             return Boolean.FALSE; 
     306            return NEITHER; 
    293307        } 
    294308 
     
    299313         */ 
    300314        @Override 
    301         public Boolean forNormalParam(NormalParam that) { 
     315        public ArrowOrFunctional forNormalParam(NormalParam that) { 
    302316            return optionTypeIsArrow(that.getType()); 
    303317        } 
     
    307321         */ 
    308322        @Override 
    309         public Boolean forVarargsParam(VarargsParam that) { 
    310             return Boolean.FALSE; 
    311         } 
    312  
    313         private Boolean optionTypeIsArrow(Option<Type> ot) { 
    314             return ot.unwrap(null) instanceof ArrowType; 
     323        public ArrowOrFunctional forVarargsParam(VarargsParam that) { 
     324            return NEITHER; 
     325        } 
     326 
     327        private ArrowOrFunctional optionTypeIsArrow(Option<Type> ot) { 
     328            return ot.unwrap(null) instanceof ArrowType ? ARROW : NEITHER; 
    315329        } 
    316330 
     
    342356    private BATree<String, StaticParam> visibleGenericParameters; 
    343357    private BATree<String, StaticParam> usedGenericParameters; 
    344  
     358    public BASet<String> topLevelUses; 
     359    public BASet<String> functionals; 
     360 
     361    protected void noteUse(Thing t, String s) { 
     362        if (t.lexicalNestedness == 0 || functionals.contains(s)) 
     363            topLevelUses.add(s); 
     364    } 
    345365    /** 
    346366     * All the object exprs (this may generalize to nested functions as well) 
     
    353373    Desugarer(BATree<String, Thing> initial, 
    354374              BATree<String, StaticParam> initialGenericScope, 
    355               BASet<String> initialArrows) { 
     375              BASet<String> initialArrows, 
     376              BASet<String> initialTopLevelUses, 
     377              BASet<String> initialFunctionals) { 
    356378        rewrites = initial; 
    357379        arrows = initialArrows; 
    358380        visibleGenericParameters = initialGenericScope; 
    359381        usedGenericParameters = new BATree<String, StaticParam>(StringComparer.V); 
     382        topLevelUses = initialTopLevelUses; 
     383        functionals = initialFunctionals; 
    360384        // packages = new BASet<String>(StringComparer.V); 
    361385    } 
     
    364388        this(new BATree<String, Thing>(StringComparer.V), 
    365389             new BATree<String, StaticParam>(StringComparer.V), 
    366              new BASet<String>(StringComparer.V)); 
     390             new BASet<String>(StringComparer.V), 
     391             new BASet<String>(StringComparer.V), 
     392             new BASet<String>(StringComparer.V) 
     393             ); 
    367394        this.isLibrary = isLibrary; 
    368395    } 
     
    449476            return vre; 
    450477        } else { 
     478            noteUse(t,s); 
    451479            return t.replacement(vre); 
    452480        } 
     
    459487            return vre; 
    460488        } else { 
     489            noteUse(t,s); 
    461490            return t.replacement(vre); 
    462491        } 
    463492 
    464493    } 
     494     
     495   NamedType newType(VarType nt, String s) { 
     496         
     497        Thing t = rewrites.get(s); 
     498        if (t == null) { 
     499            return nt; 
     500        } else { 
     501            noteUse(t,s); 
     502            return t.replacement(nt); 
     503        } 
     504    } 
     505    
     506   NamedType newType(TraitType nt, String s) { 
     507        
     508       Thing t = rewrites.get(s); 
     509       if (t == null) { 
     510           return nt; 
     511       } else { 
     512           noteUse(t,s); 
     513           return t.replacement(nt); 
     514       } 
     515   } 
    465516 
    466517//    Iterable<Id> newName(Iterable<Id> ids, String s) { 
     
    647698                } else if (node instanceof OpExpr) { 
    648699                    node = cleanupOpExpr((OpExpr)node); 
     700                     
    649701                } else if (node instanceof _RewriteFnRef) { 
    650702 
    651703                    _RewriteFnRef fr = (_RewriteFnRef) node; 
     704 
     705                } else if (node instanceof VarType) { 
     706                    VarType vre = (VarType) node; 
     707                    String s = NodeUtil.nameString(vre.getName()); 
     708                    StaticParam tp = visibleGenericParameters.get(s); 
     709                    if (tp != null) { 
     710                        usedGenericParameters.put(s, tp); 
     711                    } 
     712                    node = newType(vre, s); 
     713 
     714                } else if (node instanceof TraitType) { 
     715                    TraitType vre = (TraitType) node; 
     716                    String s = NodeUtil.nameString(vre.getName()); 
     717                    node = newType(vre, s); 
    652718 
    653719                } else if (node instanceof TightJuxt&& looksLikeMethodInvocation((Juxt) node)) { 
     
    673739                        return NodeFactory.makeLValue(lvb, newId); 
    674740                    } 
    675                 } else if (node instanceof VarType) { 
    676                     VarType vre = (VarType) node; 
    677                     String s = NodeUtil.nameString(vre.getName()); 
    678                     StaticParam tp = visibleGenericParameters.get(s); 
    679                     if (tp != null) { 
    680                         usedGenericParameters.put(s, tp); 
    681                     } 
    682  
    683741                } else if (node instanceof FieldRef) { 
    684742                    atTopLevelInsideTraitOrObject = false; 
     
    902960                } else if (node instanceof Accumulator) { 
    903961                    Accumulator ac = (Accumulator)node; 
    904                     return visitAccumulator(ac.getSpan(), ac.getGens(), 
     962                    node = visitAccumulator(ac.getSpan(), ac.getGens(), 
    905963                                            ac.getOpr(), ac.getBody(), 
    906964                                            ac.getStaticArgs()); 
     965                    return node; 
    907966                } else if (node instanceof Spawn) { 
    908967                    return translateSpawn((Spawn)node); 
     
    14581517                String s = d.getName().getText(); 
    14591518                rewrites_put(s, new Member(NodeUtil.isTransient(d))); 
    1460                 if (d.accept(isAnArrowName)) 
     1519                ArrowOrFunctional aof = d.accept(isAnArrowName); 
     1520                if (aof != NEITHER) { 
    14611521                    arrows.add(s); 
    1462                 else 
     1522                    if (aof == FUNCTIONAL) 
     1523                        functionals.add(s); 
     1524                } else 
    14631525                    arrows.remove(s); 
    14641526            } 
     
    15421604                                String sdd = dd.stringName(); 
    15431605                                if (dd instanceof VarDecl) { 
    1544                                     visited.add(tdod); 
     1606                                    //visited.add(tdod); 
    15451607                                } else if (dd instanceof AbsVarDecl) { 
    1546                                     visited.add(tdod); 
     1608                                    //visited.add(tdod); 
    15471609                                } else { 
    1548                                     if (dd.accept(isAnArrowName)) { 
     1610                                    ArrowOrFunctional aof = dd.accept(isAnArrowName); 
     1611                                     
     1612                                    if (aof != NEITHER) { 
    15491613                                        arrow_names.add(sdd); 
     1614                                        if (aof == FUNCTIONAL) 
     1615                                            functionals.add(sdd); 
    15501616                                    } else { 
    15511617                                        not_arrow_names.add(sdd); 
  • trunk/ProjectFortress/src/com/sun/fortress/nodes_util/NodeFactory.java

    r2214 r2228  
    11571157        } 
    11581158 
     1159 
    11591160        public static Expr makeFnRef(Span span, Id name, 
    11601161                        List<StaticArg> staticArgs) { 
     
    12021203        } 
    12031204 
     1205        public static VarType makeVarType(VarType original, int lexicalNestedness) { 
     1206            return new VarType(original.getSpan(), original.isParenthesized(), original.getName(), lexicalNestedness); 
     1207        } 
     1208 
     1209        public static TraitType makeTraitType(TraitType original, int lexicalNestedness) { 
     1210            return new TraitType(original.getSpan(), original.isParenthesized(), original.getName(), lexicalNestedness, original.getArgs()); 
     1211 
     1212        } 
     1213         
    12041214        public static Import makeImportStar(String apiName) { 
    12051215                return NodeFactory.makeImportStar(NodeFactory.makeAPIName(apiName), new LinkedList<IdOrOpOrAnonymousName>()); 
     
    12161226        } 
    12171227 
     1228 
     1229 
     1230 
     1231 
    12181232} 
  • trunk/ProjectFortress/src/com/sun/fortress/nodes_util/NodeUtil.java

    r2183 r2228  
    143143 
    144144    public static String nameString(APIName n) { 
    145         Iterable<String> ns = IterUtil.map(n.getIds(), IdToStringFn); 
    146         return IterUtil.toString(ns, "", ".", ""); 
     145        return n.getText(); 
     146//        Iterable<String> ns = IterUtil.map(n.getIds(), IdToStringFn); 
     147//      return IterUtil.toString(ns, "", ".", ""); 
    147148    } 
    148149 
  • trunk/ProjectFortress/src/com/sun/fortress/useful/BATreeNode.java

    r1800 r2228  
    2626 
    2727        public String toString() { 
    28             return toStringBuffer(new StringBuffer()).toString(); 
     28            return recursiveToStringLines(); 
    2929        } 
    3030