01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2000 */
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.tools.code;
12:
13: import gnu.bytecode.*;
14: import gnu.mapping.*;
15: import gnu.expr.*;
16:
17: public class IsOfClassProc extends Procedure1 implements Inlineable {
18: public IsOfClassProc(Type type, boolean possiblyNull) {
19: this .type = type;
20: this .possiblyNull = possiblyNull;
21: }
22:
23: private Type type;
24: private boolean possiblyNull;
25:
26: public void compile(ApplyExp exp, Compilation comp, Target target) {
27: Expression[] args = exp.getArgs();
28: CodeAttr code = comp.getCode();
29:
30: args[0].compile(comp, Target.pushObject);
31:
32: if (possiblyNull) {
33: //first a check is needed for the case that the argument is null
34: code.emitDup();
35: code.emitIfNotNull();
36: }
37:
38: code.emitInvokeVirtual(getClassMethod);
39: code.emitInvokeVirtual(getNameMethod);
40: code.emitPushString(type.getName());
41: code.emitInvokeVirtual(equalsMethod);
42:
43: if (possiblyNull) {
44: code.emitElse();
45: code.emitPop(1);
46: code.emitPushBoolean(false);
47: code.emitFi();
48: }
49:
50: target.compileFromStack(comp, Type.boolean_type);
51: }
52:
53: private static final Method getClassMethod = Type.pointer_type
54: .getDeclaredMethod("getClass", 0);
55:
56: private static final Method getNameMethod = ClassType.make(
57: "java.lang.Class").getDeclaredMethod("getName", 0);
58:
59: private static final Method equalsMethod = Type.pointer_type
60: .getDeclaredMethod("equals", 1);
61:
62: public Type getReturnType(Expression[] args) {
63: return Type.boolean_type;
64: }
65:
66: public Object apply1(Object arg) {
67: throw new Error("Not implemented");
68: }
69: }
|