comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/LineLocationToProbeCollectionMap.java @ 16856:7833417c8172

Changes to Instrumentation
author David Piorkowski <david.piorkowski@oracle.com>
date Mon, 18 Aug 2014 14:36:12 -0700
parents
children 7bfbad29d331
comparison
equal deleted inserted replaced
16855:226552569e34 16856:7833417c8172
1 /*
2 * Copyright (c) 2013, 2014, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.instrument.impl;
26
27 import java.util.*;
28
29 import com.oracle.truffle.api.instrument.*;
30 import com.oracle.truffle.api.source.*;
31
32 /**
33 * This class maintains a mapping from {@link LineLocation} to a collection of {@link Probe}s to be
34 * used by debugging tools.
35 *
36 */
37 public class LineLocationToProbeCollectionMap implements ProbeListener {
38 /**
39 * Map: Source line ==> probes associated with source sections starting on the line.
40 */
41 private final Map<LineLocation, Collection<Probe>> lineToProbesMap = new HashMap<>();
42
43 public LineLocationToProbeCollectionMap() {
44 }
45
46 /**
47 * Adds the current wrapper's child's line location and probe to this map.
48 */
49 public void newProbeInserted(SourceSection source, Probe probe) {
50 final LineLocation line = source.getLineLocation();
51 this.addProbeToLine(line, probe);
52 }
53
54 /**
55 * Does nothing.
56 */
57 public void probeTaggedAs(Probe probe, SyntaxTag tag) {
58
59 }
60
61 /**
62 * Returns the {@link Probe}, if any, associated with source that starts on a specified line; if
63 * there are more than one, return the one with the first character location.
64 */
65 public Probe findLineProbe(LineLocation lineLocation) {
66 Probe probe = null;
67 final Collection<Probe> probes = getProbesAtLine(lineLocation);
68 for (Probe probeOnLine : probes) {
69 if (probe == null) {
70 probe = probeOnLine;
71 } else if (probeOnLine.getSourceLocation().getCharIndex() < probe.getSourceLocation().getCharIndex()) {
72 probe = probeOnLine;
73 }
74 }
75 return probe;
76 }
77
78 /**
79 * Adds a probe to the given line.
80 * <p>
81 * If the line already exists in the internal {@link #lineToProbesMap}, this probe will be added
82 * to the existing collection. If no line already exists in the internal map, then a new key is
83 * added along with a new collection containing the probe.
84 * <p>
85 * This class requires that each added line/probe pair hasn't been previously added. However,
86 * attaching the same probe to a new line location is allowed.
87 *
88 * @param line The {@link LineLocation} to attach the probe to.
89 * @param probe The {@link Probe} to attach for that line location.
90 */
91 private void addProbeToLine(LineLocation line, Probe probe) {
92
93 if (!lineToProbesMap.containsKey(line)) {
94 // Key does not exist, add new probe list
95 final ArrayList<Probe> newProbeList = new ArrayList<>(2);
96 newProbeList.add(probe);
97 lineToProbesMap.put(line, newProbeList);
98 } else {
99 // Probe list exists, add to existing
100 final Collection<Probe> existingProbeList = lineToProbesMap.get(line);
101 assert !existingProbeList.contains(probe);
102 existingProbeList.add(probe);
103 }
104 }
105
106 /**
107 * Returns a collection of {@link Probe}s at the given {@link LineLocation}. If there are no
108 * probes at that line, an empty list is returned.
109 *
110 * @param line The line to check.
111 * @return A iterable collection of probes at the given line.
112 */
113 private Collection<Probe> getProbesAtLine(LineLocation line) {
114 Collection<Probe> probeList = lineToProbesMap.get(line);
115
116 if (probeList == null)
117 probeList = new ArrayList<>(2);
118
119 return probeList;
120 }
121 }