01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2001 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.lang.inline;
12:
13: /**
14: Computes the length of an array.
15:
16: @version $Date: 2001/11/22 19:31:54 $
17: @author Daniel Bonniot (bonnio@users.sourceforge.net)
18: */
19:
20: import gnu.bytecode.*;
21: import gnu.mapping.*;
22: import gnu.expr.*;
23:
24: public class ArrayLength extends Procedure1 implements Inlineable {
25: public static ArrayLength create(String param) {
26: return instance;
27: }
28:
29: public static final ArrayLength instance = new ArrayLength();
30:
31: public void compile(ApplyExp exp, Compilation comp, Target target) {
32: CodeAttr code = comp.getCode();
33: exp.getArgs()[0].compile(comp, Target.pushObject);
34:
35: if (Tools.monomorphicArray(code.topType()))
36: code.emitArrayLength();
37: else
38: code.emitInvokeStatic(reflectGetLength);
39:
40: target.compileFromStack(comp, Type.int_type);
41: }
42:
43: private static Method reflectGetLength = ClassType.make(
44: "java.lang.reflect.Array")
45: .getDeclaredMethod("getLength", 1);
46:
47: public gnu.bytecode.Type getReturnType(Expression[] args) {
48: return Type.int_type;
49: }
50:
51: /****************************************************************
52: * Interpretation
53: ****************************************************************/
54:
55: public Object apply1(Object array) {
56: return new Integer(java.lang.reflect.Array.getLength(array));
57: }
58: }
|