Showing the changes to java/pkg/sun/tools/tree/SwitchStatement.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,264 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.tools.tree; | |
| 27 | + | |
| 28 | +import sun.tools.java.*; | |
| 29 | +import sun.tools.asm.Assembler; | |
| 30 | +import sun.tools.asm.Label; | |
| 31 | +import sun.tools.asm.SwitchData; | |
| 32 | +import java.io.PrintStream; | |
| 33 | +import java.util.Hashtable; | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * WARNING: The contents of this source file are not part of any | |
| 37 | + * supported API. Code that depends on them does so at its own risk: | |
| 38 | + * they are subject to change or removal without notice. | |
| 39 | + */ | |
| 40 | +public | |
| 41 | +class SwitchStatement extends Statement { | |
| 42 | + Expression expr; | |
| 43 | + Statement args[]; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Constructor | |
| 47 | + */ | |
| 48 | + public SwitchStatement(long where, Expression expr, Statement args[]) { | |
| 49 | + super(SWITCH, where); | |
| 50 | + this.expr = expr; | |
| 51 | + this.args = args; | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Check statement | |
| 56 | + */ | |
| 57 | + Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) { | |
| 58 | + checkLabel(env, ctx); | |
| 59 | + CheckContext newctx = new CheckContext(ctx, this); | |
| 60 | + vset = expr.checkValue(env, newctx, reach(env, vset), exp); | |
| 61 | + Type switchType = expr.type; | |
| 62 | + | |
| 63 | + expr = convert(env, newctx, Type.tInt, expr); | |
| 64 | + | |
| 65 | + Hashtable tab = new Hashtable(); | |
| 66 | + boolean hasDefault = false; | |
| 67 | + // Note that vs is reset to vset.copy() on every case label. | |
| 68 | + // If the first substatement is not a case label, it is unreached. | |
| 69 | + Vset vs = DEAD_END; | |
| 70 | + | |
| 71 | + for (int i = 0 ; i < args.length ; i++) { | |
| 72 | + Statement s = args[i]; | |
| 73 | + | |
| 74 | + if (s.op == CASE) { | |
| 75 | + | |
| 76 | + vs = s.check(env, newctx, vs.join(vset.copy()), exp); | |
| 77 | + | |
| 78 | + Expression lbl = ((CaseStatement)s).expr; | |
| 79 | + if (lbl != null) { | |
| 80 | + if (lbl instanceof IntegerExpression) { | |
| 81 | + Integer Ivalue = | |
| 82 | + (Integer)(((IntegerExpression)lbl).getValue()); | |
| 83 | + int ivalue = Ivalue.intValue(); | |
| 84 | + if (tab.get(lbl) != null) { | |
| 85 | + env.error(s.where, "duplicate.label", Ivalue); | |
| 86 | + } else { | |
| 87 | + tab.put(lbl, s); | |
| 88 | + boolean overflow; | |
| 89 | + switch (switchType.getTypeCode()) { | |
| 90 | + case TC_BYTE: | |
| 91 | + overflow = (ivalue != (byte)ivalue); break; | |
| 92 | + case TC_SHORT: | |
| 93 | + overflow = (ivalue != (short)ivalue); break; | |
| 94 | + case TC_CHAR: | |
| 95 | + overflow = (ivalue != (char)ivalue); break; | |
| 96 | + default: | |
| 97 | + overflow = false; | |
| 98 | + } | |
| 99 | + if (overflow) { | |
| 100 | + env.error(s.where, "switch.overflow", | |
| 101 | + Ivalue, switchType); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + } else { | |
| 105 | + // Suppose a class got an error early on during | |
| 106 | + // checking. It will set all of its members to | |
| 107 | + // have the status "ERROR". Now suppose that a | |
| 108 | + // case label refers to one of this class's | |
| 109 | + // fields. When we check the case label, the | |
| 110 | + // compiler will try to inline the FieldExpression. | |
| 111 | + // Since the expression has ERROR status, it doesn't | |
| 112 | + // inline. This means that instead of the case | |
| 113 | + // label being an IntegerExpression, it will still | |
| 114 | + // be a FieldExpression, and we will end up in this | |
| 115 | + // else block. So, before we just assume that | |
| 116 | + // the expression isn't constant, do a check to | |
| 117 | + // see if it was constant but unable to inline. | |
| 118 | + // This eliminates some spurious error messages. | |
| 119 | + // (Bug id 4067498). | |
| 120 | + if (!lbl.isConstant() || | |
| 121 | + lbl.getType() != Type.tInt) { | |
| 122 | + env.error(s.where, "const.expr.required"); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + } else { | |
| 126 | + if (hasDefault) { | |
| 127 | + env.error(s.where, "duplicate.default"); | |
| 128 | + } | |
| 129 | + hasDefault = true; | |
| 130 | + } | |
| 131 | + } else { | |
| 132 | + vs = s.checkBlockStatement(env, newctx, vs, exp); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + if (!vs.isDeadEnd()) { | |
| 136 | + newctx.vsBreak = newctx.vsBreak.join(vs); | |
| 137 | + } | |
| 138 | + if (hasDefault) | |
| 139 | + vset = newctx.vsBreak; | |
| 140 | + return ctx.removeAdditionalVars(vset); | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Inline | |
| 145 | + */ | |
| 146 | + public Statement inline(Environment env, Context ctx) { | |
| 147 | + ctx = new Context(ctx, this); | |
| 148 | + expr = expr.inlineValue(env, ctx); | |
| 149 | + for (int i = 0 ; i < args.length ; i++) { | |
| 150 | + if (args[i] != null) { | |
| 151 | + args[i] = args[i].inline(env, ctx); | |
| 152 | + } | |
| 153 | + } | |
| 154 | + return this; | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Create a copy of the statement for method inlining | |
| 159 | + */ | |
| 160 | + public Statement copyInline(Context ctx, boolean valNeeded) { | |
| 161 | + SwitchStatement s = (SwitchStatement)clone(); | |
| 162 | + s.expr = expr.copyInline(ctx); | |
| 163 | + s.args = new Statement[args.length]; | |
| 164 | + for (int i = 0 ; i < args.length ; i++) { | |
| 165 | + if (args[i] != null) { | |
| 166 | + s.args[i] = args[i].copyInline(ctx, valNeeded); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + return s; | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * The cost of inlining this statement | |
| 174 | + */ | |
| 175 | + public int costInline(int thresh, Environment env, Context ctx) { | |
| 176 | + int cost = expr.costInline(thresh, env, ctx); | |
| 177 | + for (int i = 0 ; (i < args.length) && (cost < thresh) ; i++) { | |
| 178 | + if (args[i] != null) { | |
| 179 | + cost += args[i].costInline(thresh, env, ctx); | |
| 180 | + } | |
| 181 | + } | |
| 182 | + return cost; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Code | |
| 187 | + */ | |
| 188 | + public void code(Environment env, Context ctx, Assembler asm) { | |
| 189 | + CodeContext newctx = new CodeContext(ctx, this); | |
| 190 | + | |
| 191 | + expr.codeValue(env, newctx, asm); | |
| 192 | + | |
| 193 | + SwitchData sw = new SwitchData(); | |
| 194 | + boolean hasDefault = false; | |
| 195 | + | |
| 196 | + for (int i = 0 ; i < args.length ; i++) { | |
| 197 | + Statement s = args[i]; | |
| 198 | + if ((s != null) && (s.op == CASE)) { | |
| 199 | + Expression e = ((CaseStatement)s).expr; | |
| 200 | + if (e != null) { | |
| 201 | + sw.add(((IntegerExpression)e).value, new Label()); | |
| 202 | + } | |
| 203 | +// JCOV | |
| 204 | + else { | |
| 205 | + hasDefault = true; | |
| 206 | + } | |
| 207 | +// end JCOV | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | +// JCOV | |
| 212 | + if (env.coverage()) | |
| 213 | + sw.initTableCase(); | |
| 214 | +// end JCOV | |
| 215 | + asm.add(where, opc_tableswitch, sw); | |
| 216 | + | |
| 217 | + for (int i = 0 ; i < args.length ; i++) { | |
| 218 | + Statement s = args[i]; | |
| 219 | + if (s != null) { | |
| 220 | + if (s.op == CASE) { | |
| 221 | + Expression e = ((CaseStatement)s).expr; | |
| 222 | + if (e != null) { | |
| 223 | + asm.add(sw.get(((IntegerExpression)e).value)); | |
| 224 | +// JCOV | |
| 225 | + sw.addTableCase(((IntegerExpression)e).value, s.where); | |
| 226 | +// end JCOV | |
| 227 | + } else { | |
| 228 | + asm.add(sw.getDefaultLabel()); | |
| 229 | +// JCOV | |
| 230 | + sw.addTableDefault(s.where); | |
| 231 | +// end JCOV | |
| 232 | +/* JCOV hasDefault = true; end JCOV */ | |
| 233 | + } | |
| 234 | + } else { | |
| 235 | + s.code(env, newctx, asm); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + } | |
| 239 | + | |
| 240 | + if (!hasDefault) { | |
| 241 | + asm.add(sw.getDefaultLabel()); | |
| 242 | + } | |
| 243 | + asm.add(newctx.breakLabel); | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | ||
| 248 | + */ | |
| 249 | + public void print(PrintStream out, int indent) { | |
| 250 | + super.print(out, indent); | |
| 251 | + out.print("switch ("); | |
| 252 | + expr.print(out); | |
| 253 | + out.print(") {\n"); | |
| 254 | + for (int i = 0 ; i < args.length ; i++) { | |
| 255 | + if (args[i] != null) { | |
| 256 | + printIndent(out, indent + 1); | |
| 257 | + args[i].print(out, indent + 1); | |
| 258 | + out.print("\n"); | |
| 259 | + } | |
| 260 | + } | |
| 261 | + printIndent(out, indent); | |
| 262 | + out.print("}"); | |
| 263 | + } | |
| 264 | +} | |