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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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}