1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
11
12 class JInstanceOfOp extends JExpression {
13
14
15 private JExpression expr;
16
17
18 private Type typeSpec;
19
20
32
33 public JInstanceOfOp(int line, JExpression expr, Type typeSpec) {
34 super(line);
35 this.expr = expr;
36 this.typeSpec = typeSpec;
37 }
38
39
48
49 public JInstanceOfOp analyze(Context context) {
50 expr = (JExpression) expr.analyze(context);
51 typeSpec = typeSpec.resolve(context);
52 if (!typeSpec.isReference()) {
53 JAST.compilationUnit.reportSemanticError(line(),
54 "Type argument to instanceof "
55 + "operator must be a reference type");
56 } else if (!(expr.type() == Type.NULLTYPE || expr.type() == Type.ANY || expr
57 .type().isReference())) {
58 JAST.compilationUnit.reportSemanticError(line(),
59 "operand to instanceof "
60 + "operator must be a reference type");
61 } else if (expr.type().isReference()
62 && !typeSpec.isJavaAssignableFrom(expr.type())) {
63 JAST.compilationUnit.reportSemanticError(line(),
64 "It is impossible for the expression "
65 + "to be an instance of this type");
66 }
67 type = Type.BOOLEAN;
68 return this;
69 }
70
71
78
79 public void codegen(CLEmitter output) {
80 expr.codegen(output);
81 output.addReferenceInstruction(INSTANCEOF, typeSpec.toDescriptor());
82 }
83
84
87
88 public void writeToStdOut(PrettyPrinter p) {
89 p.printf("<JInstanceOfOp line=\"%d\" type=\"%s\">\n", line(),
90 ((type == null) ? "" : type.toString()));
91 p.indentRight();
92 p.printf("<RelationalExpression>\n");
93 p.indentRight();
94 expr.writeToStdOut(p);
95 p.indentLeft();
96 p.printf("</RelationalExpression>\n");
97 p.printf("<ReferenceType value=\"%s\"/>\n", ((typeSpec == null) ? ""
98 : typeSpec.toString()));
99 p.indentLeft();
100 p.printf("</JInstanceOfOp>\n");
101 }
102
103 }
104