root/trunk/ProjectFortress/src/com/sun/fortress/compiler/phases/CodeGenerationPhase.java @ 3849

Revision 3849, 6.2 KB (checked in by dr2chase, 5 months ago)

Fixed top-level overload naming

Line 
1/*******************************************************************************
2    Copyright 2009 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16******************************************************************************/
17
18package com.sun.fortress.compiler.phases;
19
20import java.util.Comparator;
21import java.util.Map;
22import java.util.Set;
23
24import edu.rice.cs.plt.collect.PredicateSet;
25import edu.rice.cs.plt.collect.Relation;
26import edu.rice.cs.plt.iter.IterUtil;
27
28import com.sun.fortress.compiler.AnalyzeResult;
29import com.sun.fortress.compiler.codegen.*;
30import com.sun.fortress.compiler.environments.TopLevelEnvGen;
31import com.sun.fortress.compiler.index.ApiIndex;
32import com.sun.fortress.compiler.index.ComponentIndex;
33import com.sun.fortress.compiler.index.Function;
34import com.sun.fortress.compiler.typechecker.TypeAnalyzer;
35import com.sun.fortress.exceptions.MultipleStaticError;
36import com.sun.fortress.exceptions.StaticError;
37import com.sun.fortress.nodes.APIName;
38import com.sun.fortress.nodes.Component;
39import com.sun.fortress.nodes.IdOrOpOrAnonymousName;
40import com.sun.fortress.repository.ForeignJava;
41import com.sun.fortress.repository.FortressRepository;
42import com.sun.fortress.scala_src.typechecker.TraitTable;
43import com.sun.fortress.useful.BASet;
44import com.sun.fortress.useful.Debug;
45import com.sun.fortress.useful.DefaultComparator;
46import com.sun.fortress.useful.MultiMap;
47import com.sun.fortress.useful.Useful;
48
49public class CodeGenerationPhase extends Phase {
50
51    public static Symbols symbolTable = new Symbols();
52    public static final boolean debugOverloading = false;
53
54    public CodeGenerationPhase(Phase parentPhase) {
55        super(parentPhase);
56    }
57
58
59    @Override
60    public AnalyzeResult execute() throws StaticError {
61        Debug.debug(Debug.Type.FORTRESS, 1, "Start phase CodeGeneration");
62        AnalyzeResult previous = parentPhase.getResult();
63        FortressRepository repository = getRepository();
64
65
66
67        Debug.debug(Debug.Type.CODEGEN, 1,
68                    "CodeGenerationPhase: components " + previous.components() +
69                    " apis = " + previous.apis().keySet());
70
71//        for ( APIName api : previous.apis().keySet() )
72//            symbolTable.addApi(api, previous.apis().get(api));
73
74        for (Map.Entry<APIName, ApiIndex> entry : repository.apis().entrySet()) {
75            symbolTable.addApi(entry.getKey(), entry.getValue());
76        }
77
78        for (Component component : previous.componentIterator()) {
79            APIName api = component.getName();
80            symbolTable.addComponent(api, previous.components().get(api));
81        }
82
83        Debug.debug(Debug.Type.CODEGEN, 1,
84                    "SymbolTable=" + symbolTable.toString());
85
86        for ( APIName api : previous.apis().keySet() ) {
87                if (ForeignJava.only.foreignApiNeedingCompilation(api)) {
88                    ApiIndex ai = previous.apis().get(api);
89                    TypeAnalyzer ta = new TypeAnalyzer(new TraitTable(ai, getEnv()));
90
91                   Relation<IdOrOpOrAnonymousName, Function>  fns = ai.functions();
92                   
93                   Set<OverloadSet> overloads =
94                       new BASet<OverloadSet>(DefaultComparator.<OverloadSet>normal());
95
96                    for (IdOrOpOrAnonymousName name : fns.firstSet()) {
97                        PredicateSet<Function> defs = fns.matchFirst(name);
98                        if (defs.size() > 1) {
99                            foundAnOverLoadedForeignFunction(ai, ta, name, defs, overloads);
100                        }
101                    }
102                    ForeignJava.only.generateWrappersForApi(api, overloads);
103                    // Need to generate overloaded functions -- where?
104                }
105        }
106
107        for (Component component : previous.componentIterator()) {
108            Debug.debug(Debug.Type.CODEGEN, 1,
109                        "CodeGenerationPhase: Compile(" + component.getName() + ")");
110            ComponentIndex ci = previous.components().get(component.getName());
111            Relation<IdOrOpOrAnonymousName, Function>  fns = ci.functions();
112            TypeAnalyzer ta = new TypeAnalyzer(new TraitTable(ci, getEnv()));
113
114            CodeGen c = new CodeGen(component, symbolTable, ta, ci);
115            component.accept(c);
116        }
117
118        return new AnalyzeResult(previous.apis(), previous.components(),
119                                 IterUtil.<StaticError> empty(),
120                                 previous.typeCheckerOutput());
121
122    }
123
124
125    /**
126     * @param ai
127     * @param name
128     * @param defs
129     * @param overloads
130     */
131    private void foundAnOverLoadedForeignFunction(ApiIndex ai, TypeAnalyzer ta,
132            IdOrOpOrAnonymousName name, PredicateSet<Function> defs, Set<OverloadSet> overloads) {
133        // Woo-hoo, an overloaded function.
134        if (debugOverloading)
135            System.err.println("Found an overloaded function " + name);
136       
137        MultiMap<Integer, Function> partitionedByArgCount = new MultiMap<Integer, Function> ();
138       
139        for (Function d : defs) {
140            partitionedByArgCount.putItem(d.parameters().size(), d);
141        }
142       
143        for(Map.Entry<Integer, Set<Function>> entry : partitionedByArgCount.entrySet()) {
144            int i = entry.getKey();
145            Set<Function> fs = entry.getValue();
146            if (fs.size() > 1) {
147                OverloadSet os = new OverloadSet.Local(ai.ast().getName(), name, ta, fs,
148                        i);
149               
150                os.split(true);
151                String s = os.toString();
152                overloads.add(os);
153            }
154        }
155     
156     }
157
158}
Note: See TracBrowser for help on using the browser.