Changeset 3288

Show
Ignore:
Timestamp:
01/05/09 10:11:11 (10 months ago)
Author:
dr2chase
Message:

[repository] native imports generate fake apis, sort of

Location:
trunk/ProjectFortress/src/com/sun/fortress
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/src/com/sun/fortress/nodes_util/NodeComparator.java

    r3286 r3288  
    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

    r3284 r3288  
    4646import com.sun.fortress.nodes.Param; 
    4747import com.sun.fortress.nodes.StaticParam; 
     48import com.sun.fortress.nodes.TraitDecl; 
    4849import com.sun.fortress.nodes.TraitObjectDecl; 
    4950import com.sun.fortress.nodes.TraitTypeWhere; 
     
    6364public class ForeignJava { 
    6465 
    65     /** given an API Name, what Java class does it import?  (Name of existing 
     66    /** given an API Name, what Java classes does it import?  (Name of existing 
    6667     * Java class, not its wrapper.) 
    6768     */ 
    6869    MultiMap<APIName, Class> javaImplementedAPIs = new MultiMap<APIName, Class>(); 
     70     
     71    /** 
     72     * Given a foreign API Name, what other (foreign) APIs does it import? 
     73     */ 
    6974    MultiMap<APIName, APIName> generatedImports = new MultiMap<APIName, APIName>(); 
    7075     
     
    8489    MultiMap<Class, String> itemsFromClasses = new MultiMap<Class, String>(); 
    8590     
     91    Map<APIName, ApiIndex> cachedFakeApis = new HashMap<APIName, ApiIndex>(); 
     92     
     93    /** 
     94     * Stores the FnDecl for a particular method -- also acts as an is-visited 
     95     * marker. 
     96     */ 
    8697    Bijection<Method, FnDecl> methodToDecl = new HashBijection<Method, FnDecl>(); 
    87     Bijection<Class, TraitObjectDecl> classToTraitDecl = new HashBijection<Class, TraitObjectDecl>(); 
     98     
     99    /** 
     100     * Stores the TraitDecl for a trait. 
     101     */ 
     102    Bijection<Class, TraitDecl> classToTraitDecl = new HashBijection<Class, TraitDecl>(); 
    88103 
    89104    /* Special cases 
     
    275290        List<BaseType> excludesC = Collections.emptyList(); 
    276291        Option<List<BaseType>> comprisesC = Option.none(); 
    277         TraitObjectDecl tod = NodeFactory.makeTraitDecl (span, mods, name, sparams, extendsC, whereC, decls, excludesC, comprisesC); 
     292        TraitDecl td = NodeFactory.makeTraitDecl (span, mods, name, sparams, extendsC, whereC, decls, excludesC, comprisesC); 
    278293        // Need to fake up an API for this class, too. 
    279         classToTraitDecl.put(imported_class, tod); 
    280         apiToStaticDecls.putItem(api_name, tod); 
     294        classToTraitDecl.put(imported_class, td); 
     295        apiToStaticDecls.putItem(api_name, td); 
    281296        javaImplementedAPIs.putItem(api_name, imported_class); 
     297         
     298        /* Invalidate any old entry. 
     299         * Let's hope this does not cause problems down the line. 
     300         */ 
     301        cachedFakeApis.remove(api_name); 
    282302    } 
    283303     
     
    366386 
    367387    public ApiIndex fakeApi(APIName name) { 
    368         Set<Class> classes = javaImplementedAPIs.get(name); 
    369         // Need to generate wrappers for all these classes, 
    370         //  if they do not already exist. 
    371  
    372         List<Import> imports = new ArrayList<Import>(); 
    373         for (APIName a : generatedImports.get(name)) { 
    374             AliasedAPIName aan = NodeFactory.makeAliasedAPIName(a); 
    375             /* 
    376              * Hoping to lie, slightly, to static analysis. 
    377              * This is technically speaking a "foreign" import, 
    378              * but the import is already known to the ForeignJava 
    379              * data structures, and this allows use of fully qualified 
    380              * (hence unambiguous) references to classes from other packages 
    381              * in the generated API. 
    382              *  
    383              * So, one lie -- no foreign annotation. 
    384              */ 
    385             ImportApi iapi = NodeFactory.makeImportApi(span, Option.<String>none(), Useful.list(aan)); 
    386             imports.add(iapi); 
     388        ApiIndex result = cachedFakeApis.get(name); 
     389        if (result == null) { 
     390 
     391            Set<Class> classes = javaImplementedAPIs.get(name); 
     392            // Need to generate wrappers for all these classes, 
     393            // if they do not already exist. 
     394 
     395            List<Import> imports = new ArrayList<Import>(); 
     396            Set<APIName> gi = generatedImports.get(name); 
     397            if (gi != null) 
     398            for (APIName a : gi) { 
     399                AliasedAPIName aan = NodeFactory.makeAliasedAPIName(a); 
     400                /* 
     401                 * Hoping to lie, slightly, to static analysis. This is 
     402                 * technically speaking a "foreign" import, but the import is 
     403                 * already known to the ForeignJava data structures, and this 
     404                 * allows use of fully qualified (hence unambiguous) references 
     405                 * to classes from other packages in the generated API. 
     406                 *  
     407                 * So, one lie -- no foreign annotation. 
     408                 */ 
     409                ImportApi iapi = NodeFactory.makeImportApi(span, Option 
     410                        .<String> none(), Useful.list(aan)); 
     411                imports.add(iapi); 
     412            } 
     413 
     414            List<Decl> decls = new ArrayList<Decl>(); 
     415            for (Decl d : apiToStaticDecls.get(name)) { 
     416                decls.add(d); 
     417            } 
     418            Api a = NodeFactory.makeApi(span, name, imports, decls); 
     419 
     420            result = IndexBuilder.builder.buildApiIndex(a, Long.MIN_VALUE + 2); 
     421            cachedFakeApis.put(name, result); 
    387422        } 
    388          
    389         List<Decl> decls = new ArrayList<Decl>(); 
    390         for (Decl d : apiToStaticDecls.get(name)) { 
    391             decls.add(d); 
     423        return result; 
     424         
     425    } 
     426     
     427    public Map<APIName, ApiIndex> augmentApiMap(Map<APIName, ApiIndex> map) { 
     428        for (APIName a : javaImplementedAPIs.keySet()) { 
     429            map.put(a, fakeApi(a)); 
    392430        } 
    393         Api a =  NodeFactory.makeApi( span,  name, 
    394                     imports, 
    395                     decls); 
    396          
    397         return IndexBuilder.builder.buildApiIndex(a, Long.MIN_VALUE+2); 
    398          
     431        return map; 
    399432    } 
    400433 
  • trunk/ProjectFortress/src/com/sun/fortress/repository/GraphRepository.java

    r3283 r3288  
    158158    @Override 
    159159    public Map<APIName, ApiIndex> apis() { 
    160         return cache.apis(); 
     160        return foreignJava.augmentApiMap(cache.apis.copy()); 
    161161    } 
    162162