1// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package com.android.tools.r8.graph;
5
6import com.android.tools.r8.dex.IndexedItemCollection;
7import com.android.tools.r8.dex.MixedSectionCollection;
8import java.util.Arrays;
9import java.util.List;
10
11public class DexDebugInfo extends CanonicalizedDexItem {
12
13  public final int startLine;
14  public final DexString[] parameters;
15  public final DexDebugEvent[] events;
16
17  public DexDebugInfo(int startLine, DexString[] parameters, DexDebugEvent[] events) {
18    assert startLine >= 0;
19    this.startLine = startLine;
20    this.parameters = parameters;
21    this.events = events;
22    // This call to hashCode is just an optimization to speedup equality when
23    // canonicalizing DexDebugInfo objects inside a synchronize method.
24    hashCode();
25  }
26
27  public List<DexDebugEntry> computeEntries() {
28    DexDebugEntryBuilder builder = new DexDebugEntryBuilder(startLine);
29    for (DexDebugEvent event : events) {
30      event.addToBuilder(builder);
31    }
32    return builder.build();
33  }
34
35  public int computeHashCode() {
36    return startLine
37        + Arrays.hashCode(parameters) * 7
38        + Arrays.hashCode(events) * 13;
39  }
40
41  public boolean computeEquals(Object other) {
42    if (other instanceof DexDebugInfo) {
43      DexDebugInfo o = (DexDebugInfo) other;
44      if (startLine != o.startLine) {
45        return false;
46      }
47      if (!Arrays.equals(parameters, o.parameters)) {
48        return false;
49      }
50      return Arrays.equals(events, o.events);
51    }
52    return false;
53  }
54
55  @Override
56  public void collectIndexedItems(IndexedItemCollection collection) {
57    collectAll(collection, parameters);
58    collectAll(collection, events);
59  }
60
61  @Override
62  void collectMixedSectionItems(MixedSectionCollection collection) {
63    collection.add(this);
64  }
65
66  public String toString() {
67    StringBuilder builder = new StringBuilder();
68    builder.append("DebugInfo (line " + startLine + ") events: [\n");
69    for (DexDebugEvent event : events) {
70      builder.append("  ").append(event).append("\n");
71    }
72    builder.append("  END_SEQUENCE\n");
73    builder.append("]\n");
74    return builder.toString();
75  }
76}
77