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.cst.Constant;
20import com.android.dx.rop.cst.CstString;
21import com.android.dx.rop.type.Type;
22import com.android.dx.rop.type.TypeList;
23
24/**
25 * Instruction which contains an explicit reference to a constant
26 * and which might throw an exception.
27 */
28public final class ThrowingCstInsn
29        extends CstInsn {
30    /** {@code non-null;} list of exceptions caught */
31    private final TypeList catches;
32
33    /**
34     * Constructs an instance.
35     *
36     * @param opcode {@code non-null;} the opcode
37     * @param position {@code non-null;} source position
38     * @param sources {@code non-null;} specs for all the sources
39     * @param catches {@code non-null;} list of exceptions caught
40     * @param cst {@code non-null;} the constant
41     */
42    public ThrowingCstInsn(Rop opcode, SourcePosition position,
43                           RegisterSpecList sources,
44                           TypeList catches, Constant cst) {
45        super(opcode, position, null, sources, cst);
46
47        if (opcode.getBranchingness() != Rop.BRANCH_THROW) {
48            throw new IllegalArgumentException("bogus branchingness");
49        }
50
51        if (catches == null) {
52            throw new NullPointerException("catches == null");
53        }
54
55        this.catches = catches;
56    }
57
58    /** {@inheritDoc} */
59    @Override
60    public String getInlineString() {
61        Constant cst = getConstant();
62        String constantString = cst.toHuman();
63        if (cst instanceof CstString) {
64            constantString = ((CstString) cst).toQuoted();
65        }
66        return constantString + " " + ThrowingInsn.toCatchString(catches);
67    }
68
69    /** {@inheritDoc} */
70    @Override
71    public TypeList getCatches() {
72        return catches;
73    }
74
75    /** {@inheritDoc} */
76    @Override
77    public void accept(Visitor visitor) {
78        visitor.visitThrowingCstInsn(this);
79    }
80
81    /** {@inheritDoc} */
82    @Override
83    public Insn withAddedCatch(Type type) {
84        return new ThrowingCstInsn(getOpcode(), getPosition(),
85                                   getSources(), catches.withAddedType(type),
86                                   getConstant());
87    }
88
89    /** {@inheritDoc} */
90    @Override
91    public Insn withRegisterOffset(int delta) {
92        return new ThrowingCstInsn(getOpcode(), getPosition(),
93                                   getSources().withOffset(delta),
94                                   catches,
95                                   getConstant());
96    }
97
98    /** {@inheritDoc} */
99    @Override
100    public Insn withNewRegisters(RegisterSpec result,
101            RegisterSpecList sources) {
102
103        return new ThrowingCstInsn(getOpcode(), getPosition(),
104                                   sources,
105                                   catches,
106                                   getConstant());
107    }
108
109
110}
111