NodeSourcePosition.java revision 12657:6ef01bd40ce2
1/*
2 * Copyright (c) 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.graph;
24
25import java.util.Objects;
26
27import jdk.vm.ci.code.BytecodePosition;
28import jdk.vm.ci.code.CodeUtil;
29import jdk.vm.ci.meta.JavaConstant;
30import jdk.vm.ci.meta.MetaUtil;
31import jdk.vm.ci.meta.ResolvedJavaMethod;
32
33public class NodeSourcePosition extends BytecodePosition {
34
35    /**
36     * The receiver of the method this frame refers to.
37     */
38    private final JavaConstant receiver;
39
40    public NodeSourcePosition(JavaConstant receiver, NodeSourcePosition caller, ResolvedJavaMethod method, int bci) {
41        super(caller, method, bci);
42        this.receiver = receiver;
43        assert receiver == null || method.getDeclaringClass().isInstance(receiver);
44    }
45
46    @Override
47    public boolean equals(Object obj) {
48        if (obj == this) {
49            return true;
50        }
51        if (obj != null && getClass() == obj.getClass()) {
52            NodeSourcePosition that = (NodeSourcePosition) obj;
53            if (this.getBCI() == that.getBCI() && Objects.equals(this.getMethod(), that.getMethod()) && Objects.equals(this.getCaller(), that.getCaller()) &&
54                            Objects.equals(this.receiver, that.receiver)) {
55                return true;
56            }
57        }
58        return false;
59    }
60
61    public JavaConstant getReceiver() {
62        return receiver;
63    }
64
65    @Override
66    public NodeSourcePosition getCaller() {
67        return (NodeSourcePosition) super.getCaller();
68    }
69
70    public NodeSourcePosition addCaller(JavaConstant newCallerReceiver, NodeSourcePosition link) {
71        if (getCaller() == null) {
72            assert newCallerReceiver == null || receiver == null : "replacing receiver";
73            return new NodeSourcePosition(newCallerReceiver, link, getMethod(), getBCI());
74        } else {
75            return new NodeSourcePosition(receiver, getCaller().addCaller(newCallerReceiver, link), getMethod(), getBCI());
76        }
77    }
78
79    public NodeSourcePosition addCaller(NodeSourcePosition link) {
80        return addCaller(null, link);
81    }
82
83    @Override
84    public String toString() {
85        StringBuilder sb = new StringBuilder(100);
86        NodeSourcePosition pos = this;
87        while (pos != null) {
88            MetaUtil.appendLocation(sb.append("at "), pos.getMethod(), pos.getBCI());
89            if (pos.receiver != null) {
90                sb.append("receiver=" + pos.receiver + " ");
91            }
92            pos = pos.getCaller();
93            if (pos != null) {
94                sb.append(CodeUtil.NEW_LINE);
95            }
96        }
97        return sb.toString();
98    }
99}
100