1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dexgen.rop.code;
18
19import com.android.dexgen.rop.type.StdTypeList;
20import com.android.dexgen.rop.type.Type;
21import com.android.dexgen.rop.type.TypeList;
22import com.android.dexgen.util.IntList;
23
24/**
25 * Instruction which contains switch cases.
26 */
27public final class SwitchInsn
28        extends Insn {
29    /** {@code non-null;} list of switch cases */
30    private final IntList cases;
31
32    /**
33     * Constructs an instance.
34     *
35     * @param opcode {@code non-null;} the opcode
36     * @param position {@code non-null;} source position
37     * @param result {@code null-ok;} spec for the result, if any
38     * @param sources {@code non-null;} specs for all the sources
39     * @param cases {@code non-null;} list of switch cases
40     */
41    public SwitchInsn(Rop opcode, SourcePosition position, RegisterSpec result,
42                      RegisterSpecList sources, IntList cases) {
43        super(opcode, position, result, sources);
44
45        if (opcode.getBranchingness() != Rop.BRANCH_SWITCH) {
46            throw new IllegalArgumentException("bogus branchingness");
47        }
48
49        if (cases == null) {
50            throw new NullPointerException("cases == null");
51        }
52
53        this.cases = cases;
54    }
55
56    /** {@inheritDoc} */
57    @Override
58    public String getInlineString() {
59        return cases.toString();
60    }
61
62    /** {@inheritDoc} */
63    @Override
64    public TypeList getCatches() {
65        return StdTypeList.EMPTY;
66    }
67
68    /** {@inheritDoc} */
69    @Override
70    public void accept(Visitor visitor) {
71        visitor.visitSwitchInsn(this);
72    }
73
74    /** {@inheritDoc} */
75    @Override
76    public Insn withAddedCatch(Type type) {
77        throw new UnsupportedOperationException("unsupported");
78    }
79
80    /** {@inheritDoc} */
81    @Override
82    public Insn withRegisterOffset(int delta) {
83        return new SwitchInsn(getOpcode(), getPosition(),
84                              getResult().withOffset(delta),
85                              getSources().withOffset(delta),
86                              cases);
87    }
88
89    /**
90     * {@inheritDoc}
91     *
92     * <p> SwitchInsn always compares false. The current use for this method
93     * never encounters {@code SwitchInsn}s
94     */
95    @Override
96    public boolean contentEquals(Insn b) {
97        return false;
98    }
99
100    /** {@inheritDoc} */
101    @Override
102    public Insn withNewRegisters(RegisterSpec result,
103            RegisterSpecList sources) {
104
105        return new SwitchInsn(getOpcode(), getPosition(),
106                              result,
107                              sources,
108                              cases);
109    }
110
111    /**
112     * Gets the list of switch cases.
113     *
114     * @return {@code non-null;} the case list
115     */
116    public IntList getCases() {
117        return cases;
118    }
119}
120