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