root/trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/values/FGenericFunction.java

Revision 4002, 7.9 KB (checked in by sukyoungryu, 4 months ago)

[copyright] Fixed copyright notices.

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.interpreter.evaluator.values;
19
20import com.sun.fortress.exceptions.ProgramError;
21import static com.sun.fortress.exceptions.ProgramError.error;
22import static com.sun.fortress.exceptions.ProgramError.errorMsg;
23import com.sun.fortress.interpreter.evaluator.Environment;
24import com.sun.fortress.interpreter.evaluator.EvalType;
25import com.sun.fortress.interpreter.evaluator.types.FType;
26import com.sun.fortress.nodes.*;
27import com.sun.fortress.nodes_util.NodeComparator;
28import com.sun.fortress.nodes_util.NodeUtil;
29import com.sun.fortress.useful.Factory1P;
30import com.sun.fortress.useful.HasAt;
31import com.sun.fortress.useful.Memo1P;
32import edu.rice.cs.plt.tuple.Option;
33
34import java.util.ArrayList;
35import java.util.Comparator;
36import java.util.List;
37
38public class FGenericFunction extends GenericFunctionOrConstructor
39        implements GenericFunctionOrMethod, Factory1P<List<FType>, Simple_fcn, HasAt> {
40
41    FnDecl fndef;
42
43
44    /* (non-Javadoc)
45    * @see com.sun.fortress.interpreter.evaluator.values.SingleFcn#getDomain()
46    */
47
48    protected Simple_fcn getSymbolic() throws Error, ProgramError {
49        if (symbolicInstantiation == null) {
50            synchronized (this) {
51                if (symbolicInstantiation == null) {
52                    List<FType> symbolic_static_args = FunctionsAndState.symbolicStaticsByPartition.get(this);
53                    if (symbolic_static_args == null) {
54                        /* TODO This is not quite right, because we risk
55                        * identifying two functions whose where clauses are
56                        * interpreted differently in two different environments.
57                        */
58                        symbolic_static_args = FunctionsAndState.symbolicStaticsByPartition.syncPutIfMissing(this,
59                                                                                                             createSymbolicInstantiation(
60                                                                                                                     getEnv(),
61                                                                                                                     getStaticParams(),
62                                                                                                                     getWhere(),
63                                                                                                                     fndef));
64                    }
65                    symbolicInstantiation = typeApply(symbolic_static_args, fndef);
66                }
67            }
68        }
69        return symbolicInstantiation;
70    }
71
72    /* (non-Javadoc)
73    * @see com.sun.fortress.interpreter.evaluator.values.SingleFcn#at()
74    */
75    @Override
76    public String at() {
77        return fndef.at();
78    }
79
80    public String stringName() {
81        return fndef.stringName();
82    }
83
84    /* (non-Javadoc)
85    * @see com.sun.fortress.interpreter.evaluator.values.FValue#getString()
86    */
87    @Override
88    public String getString() {
89        return s(fndef);
90    }
91
92    @Override
93    public String toString() {
94        return getString() + fndef.at();
95    }
96
97    public boolean seqv(FValue v) {
98        return false;
99    }
100
101    protected Simple_fcn newClosure(Environment clenv, List<FType> args) {
102        FunctionClosure cl = FType.anyAreSymbolic(args) ?
103                             new ClosureInstance(clenv, fndef, args, this) :
104                             new FunctionClosure(clenv, fndef, args);
105        cl.finishInitializing();
106        return cl;
107    }
108
109    //    public BATreeEC<List<FValue>, List<FType>, Simple_fcn> cache =
110    //        new BATreeEC<List<FValue>, List<FType>, Simple_fcn>(FValue.asTypesList);
111
112    private class Factory implements Factory1P<List<FType>, Simple_fcn, HasAt> {
113
114        public Simple_fcn make(List<FType> args, HasAt location) {
115            Environment clenv = getEnv().extendAt(location);
116            List<StaticParam> params = getStaticParams();
117            EvalType.bindGenericParameters(params, args, clenv, location, fndef);
118
119            return newClosure(clenv, args);
120        }
121
122
123    }
124
125    Memo1P<List<FType>, Simple_fcn, HasAt> memo = new Memo1P<List<FType>, Simple_fcn, HasAt>(new Factory());
126
127    public Simple_fcn make(List<FType> l, HasAt location) {
128        return memo.make(l, location);
129    }
130
131    public FnDecl getFnDecl() {
132        return fndef;
133    }
134
135    public Environment getEnv() {
136        return getWithin();
137    }
138
139    public FGenericFunction(Environment e, FnDecl fndef) {
140        super(e);
141        this.fndef = fndef;
142    }
143
144    public Simple_fcn typeApply(List<StaticArg> args, Environment e, HasAt location) {
145        EvalType et = new EvalType(e);
146        // TODO Can combine these two functions if we enhance the memo and factory
147        // to pass two parameters instead of one.
148        ArrayList<FType> argValues = et.forStaticArgList(args);
149
150        return typeApply(argValues, location);
151    }
152
153    /**
154     * Same as typeApply, but with the types evaliated already.
155     *
156     * @param args
157     * @param e
158     * @param location
159     * @param argValues
160     * @return
161     * @throws ProgramError
162     */
163    public Simple_fcn typeApply(List<FType> argValues, HasAt location) throws ProgramError {
164        List<StaticParam> params = getStaticParams();
165
166        if (argValues.size() != params.size()) {
167            error(location, errorMsg("Generic instantiation (size) mismatch, expected ", params, " got ", argValues));
168        }
169        return make(argValues, location);
170    }
171
172    public Simple_fcn typeApply(List<FType> argValues) throws ProgramError {
173        return typeApply(argValues, fndef);
174    }
175
176    public List<StaticParam> getStaticParams() {
177        return NodeUtil.getStaticParams(fndef);
178    }
179
180    public List<Param> getParams() {
181        return NodeUtil.getParams(fndef);
182    }
183
184
185    protected Option<WhereClause> getWhere() {
186        return NodeUtil.getWhereClause(fndef);
187    }
188
189    @Override
190    public IdOrOpOrAnonymousName getFnName() {
191        return NodeUtil.getName(fndef);
192    }
193
194    public IdOrOpOrAnonymousName getName() {
195        return NodeUtil.getName(fndef);
196    }
197
198    public Applicable getDef() {
199        return fndef;
200    }
201
202    static class GenericComparer implements Comparator<GenericFunctionOrMethod> {
203
204        public int compare(GenericFunctionOrMethod a0, GenericFunctionOrMethod a1) {
205
206            IdOrOpOrAnonymousName fn0 = a0.getName();
207            IdOrOpOrAnonymousName fn1 = a1.getName();
208            int x = NodeComparator.compare(fn0, fn1);
209            if (x != 0) return x;
210
211            List<StaticParam> oltp0 = a0.getStaticParams();
212            List<StaticParam> oltp1 = a1.getStaticParams();
213
214            return NodeComparator.compare(oltp0, oltp1);
215
216        }
217
218    }
219
220    // static final GenericComparer genComparer = new GenericComparer();
221
222    // static class GenericFullComparer implements Comparator<FGenericFunction> {
223
224    //     public int compare(FGenericFunction arg0, FGenericFunction arg1) {
225    //         return NodeComparator.compare(arg0.fndef, arg1.fndef);
226
227    //     }
228    // }
229    // static final GenericFullComparer genFullComparer = new GenericFullComparer();
230
231    public Option<Type> getReturnType() {
232        return NodeUtil.getReturnType(fndef);
233    }
234
235
236}
Note: See TracBrowser for help on using the browser.