Changeset 3291

Show
Ignore:
Timestamp:
01/06/09 11:28:57 (11 months ago)
Author:
dr2chase
Message:

[repository, ffi] Generated APIs now use proper import syntax, seem to pass static checking

Location:
trunk/ProjectFortress/src/com/sun/fortress
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/src/com/sun/fortress/interpreter/Driver.java

    r3252 r3291  
    11/******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
     2    Copyright 2009 Sun Microsystems, Inc., 
    33    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    44    All rights reserved. 
  • trunk/ProjectFortress/src/com/sun/fortress/repository/ForeignJava.java

    r3289 r3291  
    3636import com.sun.fortress.nodes.Api; 
    3737import com.sun.fortress.nodes.BaseType; 
     38import com.sun.fortress.nodes.CompilationUnit; 
    3839import com.sun.fortress.nodes.Decl; 
    3940import com.sun.fortress.nodes.Expr; 
     
    5657import com.sun.fortress.nodes_util.Span; 
    5758import com.sun.fortress.useful.Bijection; 
     59import com.sun.fortress.useful.F; 
    5860import com.sun.fortress.useful.HashBijection; 
     61import com.sun.fortress.useful.IMultiMap; 
     62import com.sun.fortress.useful.MapOfMapOfSet; 
    5963import com.sun.fortress.useful.MultiMap; 
    6064import com.sun.fortress.useful.Useful; 
     
    7276     * Given a foreign API Name, what other (foreign) APIs does it import? 
    7377     */ 
    74     MultiMap<APIName, APIName> generatedImports = new MultiMap<APIName, APIName>(); 
     78    MapOfMapOfSet<APIName, APIName, Id> generatedImports = new MapOfMapOfSet<APIName, APIName, Id>(); 
    7579     
    7680    /** 
     
    126130     
    127131    static void s(Class cl, APIName api, String str) { 
    128         specialCases.put(cl, NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, api, str))); 
     132        specialCases.put(cl, NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, str))); 
    129133    } 
    130134     
     
    140144        s(Double.class, fortLib, "RR64"); 
    141145        s(Double.TYPE, fortLib, "RR64"); 
    142         s(String.class, fortLib, "String"); 
     146        s(Object.class, fortLib, "Any"); 
     147        s(String.class, fortLib, "FlatString"); 
    143148        s(BigInteger.class, fortLib, "ZZ"); 
    144149        specialCases.put(Void.TYPE, NodeFactory.makeVoidType(span)); 
     
    256261        Package p = imported_class.getPackage(); 
    257262        APIName api_name = packageToAPIName(p); 
    258          
     263        Id name = NodeFactory.makeId(span, imported_class.getSimpleName()); 
     264 
    259265        /* Though the class may have been previously referenced, the import 
    260266         * may still need to be recorded.  Re-recording an existing import 
    261267         * is harmless. 
    262268         */ 
    263         generatedImports.putItem(importing_package, api_name); 
     269        generatedImports.putItem(importing_package, api_name, name); 
    264270         
    265271        /* 
     
    268274        javaImplementedAPIs.putItem(api_name,imported_class); 
    269275         
    270         Id name = NodeFactory.makeId(span, imported_class.getSimpleName()); 
    271276         
    272277        /* 
     
    282287        }; 
    283288         
    284         name = NodeFactory.makeId(api_name, name); 
     289        // LOSE THE DOTTED REFERENCE, at least for now. 
     290        // name = NodeFactory.makeId(api_name, name); 
    285291        // Note: a TraitType is a reference to a trait. 
    286292        return NodeFactory.makeTraitType(span, false, name); 
     
    365371        for (Class pt : pts) { 
    366372            Type type = recurOnOpaqueClass(importing_package, pt); 
    367             Id id = NodeFactory.makeId(span, "p"+(i++)); 
     373            Span param_span = NodeFactory.makeSpan(m.toString() + " p#" + i); 
     374            Id id = NodeFactory.makeId(param_span, "p"+(i++)); 
    368375            Param p = NodeFactory.makeParam(id, type); 
    369376            params.add(p); 
    370377        } 
     378         
     379        Span fn_span = NodeFactory.makeSpan(m.toString()); 
    371380        Id id = is_static ? 
    372                 NodeFactory.makeId(span, cl.getSimpleName()+"."+ m.getName()) : 
    373             NodeFactory.makeId(span, m.getName()); 
    374         FnDecl fndecl = NodeFactory.makeFnDecl(span, Modifiers.None, 
     381                NodeFactory.makeId(fn_span, cl.getSimpleName()+"."+ m.getName()) : 
     382            NodeFactory.makeId(fn_span, m.getName()); 
     383        FnDecl fndecl = NodeFactory.makeFnDecl(fn_span, Modifiers.None, 
    375384                id, params,Option.some(return_type), Option.<Expr>none()); 
    376385        methodToDecl.put(m, fndecl); 
     
    399408 
    400409            List<Import> imports = new ArrayList<Import>(); 
    401             Set<APIName> gi = generatedImports.get(name); 
     410            IMultiMap<APIName, Id> gi = generatedImports.get(name); 
    402411            if (gi != null) 
    403                 for (APIName a : gi) { 
    404                     importAnApi(imports, a); 
     412                for (APIName a : gi.keySet()) { 
     413                    importAnApi(imports, a, gi.get(a)); 
    405414                } 
    406             // Implicitly import. 
    407             importAnApi(imports, NodeFactory.makeAPIName(span, "FortressLibrary")); 
    408415             
    409416            List<Decl> decls = new ArrayList<Decl>(); 
     
    420427    } 
    421428 
    422     private void importAnApi(List<Import> imports, APIName a) { 
    423         AliasedAPIName aan = NodeFactory.makeAliasedAPIName(a); 
    424         /* 
    425          * Hoping to lie, slightly, to static analysis. This is 
    426          * technically speaking a "foreign" import, but the import 
    427          * is already known to the ForeignJava data structures, and 
    428          * this allows use of fully qualified (hence unambiguous) 
    429          * references to classes from other packages in the 
    430          * generated API. 
    431          *  
    432          * So, one lie -- no foreign annotation. 
    433          */ 
    434         ImportApi iapi = NodeFactory.makeImportApi(span, Option 
    435                 .<String> none(), Useful.list(aan)); 
    436         imports.add(iapi); 
     429    private void importAnApi(List<Import> imports, APIName a, Set<Id> items) { 
     430         
     431        List<AliasedSimpleName> lasn = new ArrayList<AliasedSimpleName>(); 
     432        lasn = Useful.applyToAllAppending(items, new F<Id, AliasedSimpleName>() { 
     433            @Override 
     434            public AliasedSimpleName apply(Id x) { 
     435                return NodeFactory.makeAliasedSimpleName(x); 
     436            } }, lasn); 
     437         
     438        ImportNames imp_names = NodeFactory.makeImportNames(span, Option.some("java"), a, lasn); 
     439        
     440        imports.add(imp_names); 
    437441    } 
    438442     
     
    441445            map.put(a, fakeApi(a)); 
    442446        } 
     447         
     448        dumpGenerated(); 
     449         
    443450        return map; 
    444451    } 
     452     
     453    public void dumpGenerated() { 
     454        for (APIName a : javaImplementedAPIs.keySet()) { 
     455            System.out.println(a); 
     456            CompilationUnit cu = fakeApi(a).ast(); 
     457            System.out.println(cu.toStringVerbose()); 
     458        } 
     459    } 
    445460 
    446461} 
  • trunk/ProjectFortress/src/com/sun/fortress/useful/IMultiMap.java

    r2725 r3291  
    11/******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
     2    Copyright 2009 Sun Microsystems, Inc., 
    33    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    44    All rights reserved. 
     
    2929    public Set<V> putItem(K k, V v); 
    3030 
    31     public Set<V> putItems(K k, Set<V> vs); 
     31    public Set<V> putItems(K k, Collection<V> vs); 
    3232 
    3333    public Set<V> removeItem(K k, V v); 
     34     
     35    public Set<V> putKey(K k); 
    3436 
    3537    public final static IMultiMap EMPTY_MULTIMAP = new IMultiMap() { 
    3638        private <T> T error() { throw new IllegalStateException("Empty IMultiMap is immutable."); } 
    3739        public void addInverse(Map m) { error(); } 
     40        public Set putKey(Object k) { return error(); } 
    3841        public Set putItem(Object k, Object v) { return error(); } 
    39         public Set putItems(Object k, Set vs) { return error(); } 
     42        public Set putItems(Object k, Collection vs) { return error(); } 
    4043        public Set removeItem(Object k, Object v) { return error(); } 
    4144        public void clear() { error(); } 
  • trunk/ProjectFortress/src/com/sun/fortress/useful/MultiMap.java

    r3259 r3291  
    11/******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
     2    Copyright 2009 Sun Microsystems, Inc., 
    33    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    44    All rights reserved. 
     
    1818package com.sun.fortress.useful; 
    1919 
     20import java.util.Collection; 
    2021import java.util.HashMap; 
    2122import java.util.HashSet; 
     
    7778        return s; 
    7879    } 
    79     public Set<V> putItems(K k, Set<V> vs) { 
     80    public Set<V> putItems(K k, Collection<V> vs) { 
    8081        Set<V> s = get(k); 
    8182        if (s == null) { 
  • trunk/ProjectFortress/src/com/sun/fortress/useful/UsefulJUTest.java

    r3259 r3291  
    11/******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
     2    Copyright 2009 Sun Microsystems, Inc., 
    33    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    44    All rights reserved. 
     
    227227        map.remove("too"); 
    228228        assertEquals(true, map.validate()); 
    229  
    230          
    231     } 
     229    } 
     230     
     231    public void testMOMOS() { 
     232        MapOfMapOfSet<String, String, String> reln = new MapOfMapOfSet<String, String, String>(); 
     233         
     234        reln.putItem("eats", "cow", "grass"); 
     235        reln.putItem("eats", "cow", "corn"); 
     236        reln.putItem("eats", "rat", "cheese"); 
     237        reln.putItem("chases", "dog", "cat"); 
     238        reln.putItem("chases", "cat", "rat"); 
     239         
     240        IMultiMap<String, String> eats = reln.get("eats"); 
     241        IMultiMap<String, String> chases = reln.get("chases"); 
     242         
     243        assertEquals(eats.get("cow"), Useful.set("grass", "corn")); 
     244        assertEquals(eats.get("rat"), Useful.set("cheese")); 
     245        assertEquals(chases.get("dog"), Useful.set("cat"));              
     246        assertEquals(chases.get("cat"), Useful.set("rat"));              
     247    } 
     248     
    232249} 
  • trunk/ProjectFortress/src/com/sun/fortress/useful/UsefulPLT.java

    r3134 r3291  
    11/******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
     2    Copyright 2009 Sun Microsystems, Inc., 
    33    4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    44    All rights reserved. 
     
    109109                public void addInverse(Map<V, K> m) { error(); } 
    110110                public Set<V> putItem(K k, V v) { return error(); } 
    111                 public Set<V> putItems(K k, Set<V> vs) { return error(); } 
     111                public Set<V> putItems(K k, Collection<V> vs) { return error(); } 
    112112                public Set<V> removeItem(K k, V v) { return error(); } 
     113                public Set<V> putKey(K k) {return error();} 
    113114                }; 
    114115        }