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

Revision 4002, 4.2 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 static com.sun.fortress.exceptions.InterpreterBug.bug;
21import static com.sun.fortress.exceptions.ProgramError.errorMsg;
22import com.sun.fortress.interpreter.evaluator.Environment;
23import com.sun.fortress.interpreter.evaluator.types.BottomType;
24import com.sun.fortress.interpreter.evaluator.types.FType;
25import com.sun.fortress.interpreter.evaluator.types.FTypeArrow;
26import com.sun.fortress.useful.Hasher;
27import com.sun.fortress.useful.MagicNumbers;
28import com.sun.fortress.useful.Useful;
29
30import java.util.List;
31
32
33abstract public class Simple_fcn extends SingleFcn {
34    Simple_fcn(Environment within) {
35        super(within);
36    }
37
38    FType ret_type;
39
40    public void setFtypeUnconditionally(FType ftype) {
41        super.setFtypeUnconditionally(ftype);
42        FType t = ((FTypeArrow) ftype).getRange();
43        // Hack around lack of type inference
44        if (t == BottomType.ONLY) t = com.sun.fortress.interpreter.evaluator.types.FTypeTop.ONLY;
45        ret_type = t;
46    }
47
48    protected FValue check(FValue x) {
49        FType t = ret_type;
50        // Jam on, for now.
51        if (true || t.typeMatch(x)) return x;
52        return bug(errorMsg("Function ",
53                            this,
54                            " returned ",
55                            x,
56                            " but signature required ",
57                            t,
58                            ", supers are ",
59                            x.type().getTransitiveExtends()));
60    }
61
62    public String getString() {
63        return at() + ": " + getFnName().toString() + Useful.listInParens(getDomain()) + ":" + getRange();
64    }
65
66    public FType getRange() {
67        return ret_type;
68    }
69
70    /**
71     * Returns can-this-function-be-called-now.
72     * That is, does the function have all its types assigned?
73     *
74     * @return
75     */
76    abstract boolean getFinished();
77
78    /**
79     * @return A string suitable for identifying the source of the function.
80     */
81    abstract public String at();
82
83    // NOTE: I believe it is ok for functions to use object identity for
84    // equals and hashCode().
85
86    static class SignatureEquivalence extends Hasher<Simple_fcn> {
87        @Override
88        public long hash(Simple_fcn x) {
89            long a = (long) x.getFnName().hashCode() * MagicNumbers.s;
90            long b = (long) x.getDomain().hashCode() * MagicNumbers.l;
91            // System.err.println("Hash of " + x + " yields " + a + " and " + b);
92
93            return a + b;
94        }
95
96        @Override
97        public boolean equiv(Simple_fcn x, Simple_fcn y) {
98            List<FType> dx = x.getDomain();
99            List<FType> dy = y.getDomain();
100            if (dx.size() != dy.size()) return false;
101            if (!x.getFnName().equals(y.getFnName())) return false;
102            for (int i = 0; i < dx.size(); i++) {
103                if (!dx.get(i).equals(dy.get(i))) return false;
104            }
105            return true;
106        }
107
108    }
109
110    public static Hasher<Simple_fcn> signatureEquivalence = new SignatureEquivalence();
111
112    static class NameEquivalence extends Hasher<Simple_fcn> {
113        @Override
114        public long hash(Simple_fcn x) {
115            long a = (long) x.getFnName().hashCode() * MagicNumbers.N;
116            return a;
117        }
118
119        @Override
120        public boolean equiv(Simple_fcn x, Simple_fcn y) {
121            return x.getFnName().equals(y.getFnName());
122        }
123
124    }
125
126    public static Hasher<Simple_fcn> nameEquivalence = new NameEquivalence();
127
128
129}
Note: See TracBrowser for help on using the browser.