| 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.compiler.WellKnownNames; |
|---|
| 21 | import com.sun.fortress.exceptions.FortressException; |
|---|
| 22 | import static com.sun.fortress.exceptions.InterpreterBug.bug; |
|---|
| 23 | import static com.sun.fortress.exceptions.ProgramError.error; |
|---|
| 24 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 25 | import static com.sun.fortress.exceptions.UnificationError.unificationError; |
|---|
| 26 | import com.sun.fortress.interpreter.Driver; |
|---|
| 27 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 28 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 29 | import com.sun.fortress.interpreter.evaluator.types.FTypeRest; |
|---|
| 30 | import com.sun.fortress.interpreter.glue.Glue; |
|---|
| 31 | import com.sun.fortress.interpreter.glue.IndexedArrayWrapper; |
|---|
| 32 | import com.sun.fortress.useful.HasAt; |
|---|
| 33 | import com.sun.fortress.useful.Useful; |
|---|
| 34 | |
|---|
| 35 | import java.util.ArrayList; |
|---|
| 36 | import java.util.Collections; |
|---|
| 37 | import java.util.Iterator; |
|---|
| 38 | import java.util.List; |
|---|
| 39 | |
|---|
| 40 | public abstract class NonPrimitive extends Simple_fcn { |
|---|
| 41 | |
|---|
| 42 | static final List<FValue> VOID_ARG = Collections.singletonList((FValue) FVoid.V); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | @Override |
|---|
| 50 | public String at() { |
|---|
| 51 | return getAt().at(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | protected abstract HasAt getAt(); |
|---|
| 55 | |
|---|
| 56 | NonPrimitive(Environment within) { |
|---|
| 57 | super(within); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | private List<Parameter> params; |
|---|
| 61 | |
|---|
| 62 | private boolean lastParamIsRest; |
|---|
| 63 | |
|---|
| 64 | private volatile List<FType> cachedDomain; |
|---|
| 65 | |
|---|
| 66 | protected boolean hasRest() { |
|---|
| 67 | return lastParamIsRest; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | public final void setParams(List<Parameter> original_params) { |
|---|
| 74 | |
|---|
| 75 | List<Parameter> params = adjustParameterList(original_params); |
|---|
| 76 | |
|---|
| 77 | if (this.params != null) { |
|---|
| 78 | if (!this.params.equals(params)) bug(this.getAt(), errorMsg( |
|---|
| 79 | "Attempted second set of constructor/function/method params of ", |
|---|
| 80 | this, |
|---|
| 81 | " to ", |
|---|
| 82 | Useful.listInParens(original_params))); |
|---|
| 83 | |
|---|
| 84 | return; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | this.params = params; |
|---|
| 88 | lastParamIsRest = params.size() > 0 && (params.get(params.size() - 1).getType() instanceof FTypeRest); |
|---|
| 89 | setValueType(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | protected List<Parameter> adjustParameterList(List<Parameter> params2) { |
|---|
| 93 | return params2; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | abstract protected void setValueType(); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | public List<Parameter> getParameters() { |
|---|
| 102 | if (params == null) return bug(getAt(), errorMsg("getParams of NonPrimitive ", this)); |
|---|
| 103 | return params; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | @Override |
|---|
| 107 | public List<FType> getDomain() { |
|---|
| 108 | if (cachedDomain == null) { |
|---|
| 109 | synchronized (this) { |
|---|
| 110 | if (cachedDomain == null) { |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | List<FType> l = typeListFromParameters(getParameters()); |
|---|
| 116 | cachedDomain = l; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | return cachedDomain; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public static List<FValue> stripAsIf(List<FValue> args) { |
|---|
| 124 | List<FValue> res = new ArrayList<FValue>(args.size()); |
|---|
| 125 | for (FValue v : args) { |
|---|
| 126 | if (v instanceof FAsIf) { |
|---|
| 127 | res.add(((FAsIf) v).getValue()); |
|---|
| 128 | } else { |
|---|
| 129 | res.add(v); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | return res; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | public List<FValue> typecheckParams(List<FValue> args) { |
|---|
| 141 | args = fixupArgCount(args); |
|---|
| 142 | Iterator<FValue> argsIter = args.iterator(); |
|---|
| 143 | Iterator<Parameter> paramsIter = params.iterator(); |
|---|
| 144 | boolean asif = false; |
|---|
| 145 | for (int i = 1; paramsIter.hasNext(); i++) { |
|---|
| 146 | Parameter param = paramsIter.next(); |
|---|
| 147 | FType paramType = param.getType(); |
|---|
| 148 | if (paramType instanceof FTypeRest) { |
|---|
| 149 | FType restType = ((FTypeRest) paramType).getType(); |
|---|
| 150 | for (; argsIter.hasNext(); i++) { |
|---|
| 151 | FValue arg = argsIter.next(); |
|---|
| 152 | if (arg instanceof FAsIf) asif = true; |
|---|
| 153 | if (!restType.typeMatch(arg)) { |
|---|
| 154 | error(errorMsg("Closure/Constructor for ", |
|---|
| 155 | getAt().stringName(), |
|---|
| 156 | " rest parameter ", |
|---|
| 157 | i, |
|---|
| 158 | " (", |
|---|
| 159 | param.getName(), |
|---|
| 160 | ":", |
|---|
| 161 | restType, |
|---|
| 162 | "...) got type ", |
|---|
| 163 | arg.type())); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } else { |
|---|
| 167 | |
|---|
| 168 | FValue arg = argsIter.next(); |
|---|
| 169 | if (arg instanceof FAsIf) asif = true; |
|---|
| 170 | if (!paramType.typeMatch(arg)) { |
|---|
| 171 | error(errorMsg("Closure/Constructor for ", |
|---|
| 172 | getAt().stringName(), |
|---|
| 173 | " parameter ", |
|---|
| 174 | i, |
|---|
| 175 | " (", |
|---|
| 176 | param.getName(), |
|---|
| 177 | ":", |
|---|
| 178 | param.getType(), |
|---|
| 179 | ") got type ", |
|---|
| 180 | arg.type())); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | if (asif) return stripAsIf(args); |
|---|
| 185 | else return args; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | public Environment buildEnvFromParams(List<FValue> args) throws Error { |
|---|
| 195 | Environment env = within.extendAt(getAt()); |
|---|
| 196 | return buildEnvFromParams(args, env); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public Environment buildEnvFromEnvAndParams(Environment env, List<FValue> args) throws Error { |
|---|
| 200 | env = env.extendAt(getAt()); |
|---|
| 201 | return buildEnvFromParams(args, env); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | private Environment buildEnvFromParams(List<FValue> args, Environment env) throws Error { |
|---|
| 205 | |
|---|
| 206 | List<FValue> argsTemp = fixupArgCount(args); |
|---|
| 207 | if (argsTemp == null) return bug( |
|---|
| 208 | "The number of parameters (" + params.size() + ") does not match with the number of arguments (" + |
|---|
| 209 | args.size() + ")."); |
|---|
| 210 | args = argsTemp; |
|---|
| 211 | Iterator<FValue> argsIter = args.iterator(); |
|---|
| 212 | FValue arg = null; |
|---|
| 213 | int i = 0; |
|---|
| 214 | for (Parameter param : params) { |
|---|
| 215 | FType paramType = param.getType(); |
|---|
| 216 | if (paramType instanceof FTypeRest) { |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | String genericName = WellKnownNames.varargsFactoryName; |
|---|
| 222 | int[] natParams = new int[1]; |
|---|
| 223 | natParams[0] = args.size() - i; |
|---|
| 224 | |
|---|
| 225 | Environment wknInstantiationEnv = Driver.getFortressLibrary(); |
|---|
| 226 | |
|---|
| 227 | Simple_fcn f = Glue.instantiateGenericConstructor(wknInstantiationEnv, |
|---|
| 228 | genericName, |
|---|
| 229 | ((FTypeRest) paramType).getType(), |
|---|
| 230 | natParams); |
|---|
| 231 | |
|---|
| 232 | FValue theArray = f.applyToArgs(); |
|---|
| 233 | if (!(theArray instanceof FObject)) return bug(errorMsg(f, " returned non-FObject ", theArray)); |
|---|
| 234 | |
|---|
| 235 | IndexedArrayWrapper iaw = new IndexedArrayWrapper(theArray); |
|---|
| 236 | int j = 0; |
|---|
| 237 | while (argsIter.hasNext()) { |
|---|
| 238 | arg = argsIter.next(); |
|---|
| 239 | iaw.put(arg.getValue(), j); |
|---|
| 240 | j++; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | env.putValue(param.getName(), theArray); |
|---|
| 244 | } else { |
|---|
| 245 | |
|---|
| 246 | arg = argsIter.next(); |
|---|
| 247 | i++; |
|---|
| 248 | if (!paramType.typeMatch(arg)) { |
|---|
| 249 | unificationError(errorMsg("Closure/Constructor for ", |
|---|
| 250 | getAt().stringName(), |
|---|
| 251 | " param ", |
|---|
| 252 | i, |
|---|
| 253 | " (", |
|---|
| 254 | param.getName(), |
|---|
| 255 | ":", |
|---|
| 256 | paramType, |
|---|
| 257 | ") got arg ", |
|---|
| 258 | arg, |
|---|
| 259 | " of type ", |
|---|
| 260 | arg.type())); |
|---|
| 261 | } |
|---|
| 262 | arg = arg.getValue(); |
|---|
| 263 | try { |
|---|
| 264 | if (param.getMutable()) { |
|---|
| 265 | env.putValueRaw(param.getName(), arg, param.getType()); |
|---|
| 266 | } else { |
|---|
| 267 | env.putValueRaw(param.getName(), arg); |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | catch (FortressException ex) { |
|---|
| 271 | throw ex.setWithin(env); |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | } |
|---|
| 276 | return env; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | public List<FValue> fixupArgCount(List<FValue> args0, HasAt loc) { |
|---|
| 283 | List<FValue> args = fixupArgCount(args0); |
|---|
| 284 | if (args == null) { |
|---|
| 285 | error(loc, errorMsg("Incorrect number of arguments, expected ", |
|---|
| 286 | Useful.listInParens(params), |
|---|
| 287 | ", got ", |
|---|
| 288 | Useful.listInParens(args0))); |
|---|
| 289 | } |
|---|
| 290 | return args; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | public List<FValue> fixupArgCount(List<FValue> args) { |
|---|
| 298 | if (this.params == null) { |
|---|
| 299 | bug(this.getAt(), "Calling fixupArgCount on " + getAt().stringName() + " with null params"); |
|---|
| 300 | } |
|---|
| 301 | if (args.size() == params.size()) return args; |
|---|
| 302 | if (hasRest() && args.size() + 1 >= params.size()) { |
|---|
| 303 | return args; |
|---|
| 304 | } |
|---|
| 305 | if (params.size() == 1) { |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | if (args.size() == 0) return VOID_ARG; |
|---|
| 313 | return Collections.singletonList((FValue) FTuple.make(args)); |
|---|
| 314 | } |
|---|
| 315 | return null; |
|---|
| 316 | } |
|---|
| 317 | } |
|---|