| 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 static com.sun.fortress.exceptions.ProgramError.error; |
|---|
| 21 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 22 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 23 | import com.sun.fortress.interpreter.evaluator.EvalType; |
|---|
| 24 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 25 | import com.sun.fortress.interpreter.evaluator.types.FTypeTop; |
|---|
| 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 GenericMethod extends MethodClosure |
|---|
| 39 | implements GenericFunctionOrMethod, Factory1P<List<FType>, MethodClosure, HasAt> { |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | @Override |
|---|
| 45 | public String getString() { |
|---|
| 46 | return s(getDef()); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | boolean isTraitMethod; |
|---|
| 50 | |
|---|
| 51 | Environment evaluationEnv; |
|---|
| 52 | |
|---|
| 53 | protected MethodClosure newClosure(Environment clenv, List<FType> args) { |
|---|
| 54 | MethodClosure cl; |
|---|
| 55 | if (!isTraitMethod) { |
|---|
| 56 | cl = new MethodClosureInstance(getEnv(), clenv, getDef(), getDefiner(), args, this); |
|---|
| 57 | } else if (FType.anyAreSymbolic(args)) { |
|---|
| 58 | cl = new TraitMethodInstance(getEnv(), clenv, getDef(), getDefiner(), args, this); |
|---|
| 59 | } else { |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | cl = new TraitMethod(getEnv(), clenv, getDef(), getDefiner(), args); |
|---|
| 66 | } |
|---|
| 67 | cl.finishInitializing(); |
|---|
| 68 | return (MethodClosure) cl; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | private class Factory implements Factory1P<List<FType>, MethodClosure, HasAt> { |
|---|
| 72 | |
|---|
| 73 | public MethodClosure make(List<FType> args, HasAt location) { |
|---|
| 74 | Environment clenv = evaluationEnv.extendAt(location); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | List<StaticParam> params = NodeUtil.getStaticParams(getDef()); |
|---|
| 79 | EvalType.bindGenericParameters(params, args, clenv, location, getDef()); |
|---|
| 80 | clenv.bless(); |
|---|
| 81 | return newClosure(clenv, args); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | Memo1P<List<FType>, MethodClosure, HasAt> memo = new Memo1P<List<FType>, MethodClosure, HasAt>(new Factory()); |
|---|
| 86 | |
|---|
| 87 | public MethodClosure make(List<FType> l, HasAt location) { |
|---|
| 88 | return memo.make(l, location); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public GenericMethod(Environment declarationEnv, |
|---|
| 92 | Environment evaluationEnv, |
|---|
| 93 | FnDecl fndef, |
|---|
| 94 | FType definer, |
|---|
| 95 | boolean isTraitMethod) { |
|---|
| 96 | super( |
|---|
| 97 | declarationEnv, |
|---|
| 98 | fndef, definer); |
|---|
| 99 | this.isTraitMethod = isTraitMethod; |
|---|
| 100 | this.evaluationEnv = evaluationEnv; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | public MethodClosure typeApply(List<StaticArg> args, Environment e, HasAt location) { |
|---|
| 109 | List<StaticParam> params = NodeUtil.getStaticParams(getDef()); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | if (args.size() != params.size()) { |
|---|
| 113 | error(location, e, errorMsg("Generic instantiation (size) mismatch, expected ", params, " got ", args)); |
|---|
| 114 | } |
|---|
| 115 | EvalType et = new EvalType(e); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | ArrayList<FType> argValues = et.forStaticArgList(args); |
|---|
| 120 | return make(argValues, location); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public Simple_fcn typeApply(List<FType> argValues, HasAt location) { |
|---|
| 124 | return make(argValues, location); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | public Simple_fcn typeApply(List<FType> argValues) { |
|---|
| 128 | return make(argValues, getDef()); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | public void finishInitializing() { |
|---|
| 132 | Applicable x = getDef(); |
|---|
| 133 | List<Param> params = NodeUtil.getParams(x); |
|---|
| 134 | Option<Type> rt = NodeUtil.getReturnType(x); |
|---|
| 135 | Environment env = getEnv(); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | List<StaticParam> tparams = NodeUtil.getStaticParams(getDef()); |
|---|
| 140 | |
|---|
| 141 | FType ft = EvalType.getFTypeFromOption(rt, env, FTypeTop.ONLY); |
|---|
| 142 | List<Parameter> fparams = EvalType.paramsToParameters(env, params); |
|---|
| 143 | |
|---|
| 144 | setParamsAndReturnType(fparams, ft); |
|---|
| 145 | return; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static class GenericComparer implements Comparator<GenericMethod> { |
|---|
| 149 | |
|---|
| 150 | public int compare(GenericMethod arg0, GenericMethod arg1) { |
|---|
| 151 | Applicable a0 = arg0.getDef(); |
|---|
| 152 | Applicable a1 = arg1.getDef(); |
|---|
| 153 | |
|---|
| 154 | IdOrOpOrAnonymousName fn0 = NodeUtil.getName(a0); |
|---|
| 155 | IdOrOpOrAnonymousName fn1 = NodeUtil.getName(a1); |
|---|
| 156 | int x = NodeComparator.compare(fn0, fn1); |
|---|
| 157 | if (x != 0) return x; |
|---|
| 158 | |
|---|
| 159 | List<StaticParam> oltp0 = NodeUtil.getStaticParams(a0); |
|---|
| 160 | List<StaticParam> oltp1 = NodeUtil.getStaticParams(a1); |
|---|
| 161 | |
|---|
| 162 | return NodeComparator.compare(oltp0, oltp1); |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | static final GenericComparer genComparer = new GenericComparer(); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | public IdOrOpOrAnonymousName getName() { |
|---|
| 198 | return NodeUtil.getName(getDef()); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | public List<StaticParam> getStaticParams() { |
|---|
| 202 | return NodeUtil.getStaticParams(getDef()); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public List<Param> getParams() { |
|---|
| 206 | return NodeUtil.getParams(getDef()); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | public Option<Type> getReturnType() { |
|---|
| 210 | return NodeUtil.getReturnType(getDef()); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | } |
|---|