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.dx.rop.code;
18
19import com.android.dx.rop.type.Type;
20import com.android.dx.rop.type.TypeList;
21
22/**
23 * Instruction which possibly throws. The {@code successors} list in the
24 * basic block an instance of this class is inside corresponds in-order to
25 * the list of exceptions handled by this instruction, with the
26 * no-exception case appended as the final target.
27 */
28public final class ThrowingInsn
29        extends Insn {
30    /** {@code non-null;} list of exceptions caught */
31    private final TypeList catches;
32
33    /**
34     * Gets the string form of a register spec list to be used as a catches
35     * list.
36     *
37     * @param catches {@code non-null;} the catches list
38     * @return {@code non-null;} the string form
39     */
40    public static String toCatchString(TypeList catches) {
41        StringBuffer sb = new StringBuffer(100);
42
43        sb.append("catch");
44
45        int sz = catches.size();
46        for (int i = 0; i < sz; i++) {
47            sb.append(" ");
48            sb.append(catches.getType(i).toHuman());
49        }
50
51        return sb.toString();
52    }
53
54    /**
55     * Constructs an instance.
56     *
57     * @param opcode {@code non-null;} the opcode
58     * @param position {@code non-null;} source position
59     * @param sources {@code non-null;} specs for all the sources
60     * @param catches {@code non-null;} list of exceptions caught
61     */
62    public ThrowingInsn(Rop opcode, SourcePosition position,
63                        RegisterSpecList sources,
64                        TypeList catches) {
65        super(opcode, position, null, sources);
66
67        if (opcode.getBranchingness() != Rop.BRANCH_THROW) {
68            throw new IllegalArgumentException("bogus branchingness");
69        }
70
71        if (catches == null) {
72            throw new NullPointerException("catches == null");
73        }
74
75        this.catches = catches;
76    }
77
78    /** {@inheritDoc} */
79    @Override
80    public String getInlineString() {
81        return toCatchString(catches);
82    }
83
84    /** {@inheritDoc} */
85    @Override
86    public TypeList getCatches() {
87        return catches;
88    }
89
90    /** {@inheritDoc} */
91    @Override
92    public void accept(Visitor visitor) {
93        visitor.visitThrowingInsn(this);
94    }
95
96    /** {@inheritDoc} */
97    @Override
98    public Insn withAddedCatch(Type type) {
99        return new ThrowingInsn(getOpcode(), getPosition(),
100                                getSources(), catches.withAddedType(type));
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public Insn withRegisterOffset(int delta) {
106        return new ThrowingInsn(getOpcode(), getPosition(),
107                                getSources().withOffset(delta),
108                                catches);
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public Insn withNewRegisters(RegisterSpec result,
114            RegisterSpecList sources) {
115
116        return new ThrowingInsn(getOpcode(), getPosition(),
117                                sources,
118                                catches);
119    }
120}
121