| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.interpreter.evaluator.values; |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.exceptions.ProgramError; |
|---|
| 21 | import static com.sun.fortress.exceptions.ProgramError.error; |
|---|
| 22 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 23 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 24 | import com.sun.fortress.interpreter.evaluator.EvalType; |
|---|
| 25 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 26 | import com.sun.fortress.nodes.*; |
|---|
| 27 | import com.sun.fortress.nodes_util.NodeComparator; |
|---|
| 28 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 29 | import com.sun.fortress.useful.Factory1P; |
|---|
| 30 | import com.sun.fortress.useful.HasAt; |
|---|
| 31 | import com.sun.fortress.useful.Memo1P; |
|---|
| 32 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 33 | |
|---|
| 34 | import java.util.ArrayList; |
|---|
| 35 | import java.util.Comparator; |
|---|
| 36 | import java.util.List; |
|---|
| 37 | |
|---|
| 38 | public class FGenericFunction extends GenericFunctionOrConstructor |
|---|
| 39 | implements GenericFunctionOrMethod, Factory1P<List<FType>, Simple_fcn, HasAt> { |
|---|
| 40 | |
|---|
| 41 | FnDecl fndef; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 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 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 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 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | @Override |
|---|
| 76 | public String at() { |
|---|
| 77 | return fndef.at(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public String stringName() { |
|---|
| 81 | return fndef.stringName(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 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 | |
|---|
| 110 | |
|---|
| 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 | |
|---|
| 147 | |
|---|
| 148 | ArrayList<FType> argValues = et.forStaticArgList(args); |
|---|
| 149 | |
|---|
| 150 | return typeApply(argValues, location); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 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 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | public Option<Type> getReturnType() { |
|---|
| 232 | return NodeUtil.getReturnType(fndef); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | } |
|---|