1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/*
2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project
3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License");
5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License.
6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at
7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *      http://www.apache.org/licenses/LICENSE-2.0
9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software
11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS,
12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and
14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License.
15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.rop.code;
18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.CstUtf8;
20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Hex;
21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/**
23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Information about a source position for code, which includes both a
24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * line number and original bytecode address.
25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class SourcePosition {
27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@code non-null;} convenient "no information known" instance */
28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public static final SourcePosition NO_INFO =
29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        new SourcePosition(null, -1, -1);
30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@code null-ok;} name of the file of origin or {@code null} if unknown */
32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    private final CstUtf8 sourceFile;
33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * {@code >= -1;} the bytecode address, or {@code -1} if that
36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * information is unknown
37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    private final int address;
39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * {@code >= -1;} the line number, or {@code -1} if that
42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * information is unknown
43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    private final int line;
45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Constructs an instance.
48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param sourceFile {@code null-ok;} name of the file of origin or
50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * {@code null} if unknown
51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param address {@code >= -1;} original bytecode address or {@code -1}
52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * if unknown
53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param line {@code >= -1;} original line number or {@code -1} if
54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * unknown
55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public SourcePosition(CstUtf8 sourceFile, int address, int line) {
57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (address < -1) {
58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            throw new IllegalArgumentException("address < -1");
59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (line < -1) {
62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            throw new IllegalArgumentException("line < -1");
63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        this.sourceFile = sourceFile;
66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        this.address = address;
67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        this.line = line;
68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@inheritDoc} */
71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    @Override
72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public String toString() {
73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        StringBuffer sb = new StringBuffer(50);
74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (sourceFile != null) {
76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            sb.append(sourceFile.toHuman());
77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            sb.append(":");
78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (line >= 0) {
81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            sb.append(line);
82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        sb.append('@');
85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (address < 0) {
87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            sb.append("????");
88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        } else {
89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            sb.append(Hex.u2(address));
90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return sb.toString();
93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@inheritDoc} */
96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    @Override
97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public boolean equals(Object other) {
98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (!(other instanceof SourcePosition)) {
99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            return false;
100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (this == other) {
103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            return true;
104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        SourcePosition pos = (SourcePosition) other;
107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return (address == pos.address) && sameLineAndFile(pos);
109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@inheritDoc} */
112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    @Override
113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public int hashCode() {
114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return sourceFile.hashCode() + address + line;
115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Returns whether the lines match between this instance and
119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * the one given.
120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param other {@code non-null;} the instance to compare to
122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code true} iff the lines match
123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public boolean sameLine(SourcePosition other) {
125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return (line == other.line);
126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Returns whether the lines and files match between this instance and
130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * the one given.
131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param other {@code non-null;} the instance to compare to
133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code true} iff the lines and files match
134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public boolean sameLineAndFile(SourcePosition other) {
136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return (line == other.line) &&
137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            ((sourceFile == other.sourceFile) ||
138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul             ((sourceFile != null) && sourceFile.equals(other.sourceFile)));
139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Gets the source file, if known.
143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code null-ok;} the source file or {@code null} if unknown
145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
146917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public CstUtf8 getSourceFile() {
147917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return sourceFile;
148917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
149917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
150917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
151917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Gets the original bytecode address.
152917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
153917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code >= -1;} the address or {@code -1} if unknown
154917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
155917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public int getAddress() {
156917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return address;
157917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
158917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
159917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
160917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Gets the original line number.
161917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
162917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code >= -1;} the original line number or {@code -1} if
163917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * unknown
164917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
165917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public int getLine() {
166917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return line;
167917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
168917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul}
169