aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/naming/MemberNaming.java
blob: c494c0881b760f738becfffd81bdd3f2f55e01d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.naming;

import static com.android.tools.r8.utils.DescriptorUtils.javaTypeToDescriptor;

import com.android.tools.r8.dex.Constants;
import com.android.tools.r8.graph.DexField;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.naming.MemberNaming.Signature.SignatureKind;
import com.android.tools.r8.utils.DescriptorUtils;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.objectweb.asm.Type;

/**
 * Stores renaming information for a member.
 * <p>
 * This includes the signature, the original name and inlining range information.
 */
public class MemberNaming {

  private static final int UNDEFINED_START_NUMBER = -1;

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof MemberNaming)) {
      return false;
    }

    MemberNaming that = (MemberNaming) o;
    return signature.equals(that.signature)
        && renamedSignature.equals(that.renamedSignature)
        && topLevelRange.equals(that.topLevelRange)
        && inlineInformation.equals(that.inlineInformation);
  }

  @Override
  public int hashCode() {
    int result = signature.hashCode();
    result = 31 * result + renamedSignature.hashCode();
    result = 31 * result + inlineInformation.hashCode();
    result = 31 * result + topLevelRange.hashCode();
    return result;
  }

  /**
   * Original signature of the member
   */
  final Signature signature;
  /**
   * Renamed signature where the name (but not the types) have been renamed.
   */
  final Signature renamedSignature;
  public final List<InlineInformation> inlineInformation = new LinkedList<>();
  public final Range topLevelRange;

  private int collapsedStartLineNumber = UNDEFINED_START_NUMBER;
  private int originalStartLineNumber = UNDEFINED_START_NUMBER;

  MemberNaming(Signature signature, String renamedName, Range inlinedLineRange) {
    this.signature = signature;
    this.renamedSignature = signature.asRenamed(renamedName);
    topLevelRange = inlinedLineRange == null ? fakeZeroRange : inlinedLineRange;
  }

  public void addInliningRange(Range inlinedRange, Signature signature, Range originalRange) {
    inlineInformation.add(new InlineInformation(inlinedRange, originalRange, signature));
  }

  public List<Range> getInlineRanges() {
    List<Range> inlineRanges = new ArrayList<>();
    for (InlineInformation information : inlineInformation) {
      if (information.isActualInlining()) {
        inlineRanges.add(information.inlinedRange);
      }
    }
    return inlineRanges;
  }

  public Signature getOriginalSignature() {
    return signature;
  }

  public String getOriginalName() {
    return signature.name;
  }

  public Signature getRenamedSignature() {
    return renamedSignature;
  }

  public String getRenamedName() {
    return renamedSignature.name;
  }

  public void setCollapsedStartLineNumber(int value) {
    assert collapsedStartLineNumber == UNDEFINED_START_NUMBER;
    collapsedStartLineNumber = value;
  }

  public boolean isMethodNaming() {
    return signature.kind() == SignatureKind.METHOD;
  }

  private int getCollapsedStartLineNumber() {
    return collapsedStartLineNumber;
  }

  protected void write(Writer writer, boolean collapseRanges, boolean indent) throws IOException {
    if (indent) {
      writer.append("    ");
    }
    int rangeCounter =
        collapseRanges ? getCollapsedStartLineNumber() : InlineInformation.DO_NOT_COLLAPSE;
    // Avoid printing the range information if there was none in the original file.
    if (topLevelRange != fakeZeroRange || rangeCounter != UNDEFINED_START_NUMBER) {
      if (collapseRanges) {
        // Skip ranges for methods that are used only once, as they do not have debug information.
        if (rangeCounter != UNDEFINED_START_NUMBER) {
          String rangeString = Integer.toString(rangeCounter);
          writer.append(rangeString).append(":").append(rangeString).append(":");
        } else {
          rangeCounter = 0;
        }
      } else {
        writer.append(topLevelRange.toString());
        writer.append(':');
      }
    } else {
      // We might end up in a case where we have no line information for the top entry but still
      // have inline ranges. Just to be sure, set rangeCounter to a useful value.
      if (collapseRanges) {
        rangeCounter = 0;
      }
    }
    signature.write(writer);
    if (originalStartLineNumber != UNDEFINED_START_NUMBER) {
      // If we have original line number information, print it here.
      String originalSourceLineString = Integer.toString(originalStartLineNumber);
      writer.append(':')
          .append(originalSourceLineString)
          .append(':')
          .append(originalSourceLineString);
    }
    writer.append(" -> ");
    writer.append(renamedSignature.name);
    writer.append("\n");
    for (InlineInformation information : inlineInformation) {
      assert !collapseRanges || rangeCounter >= 0;
      if (collapseRanges && information.isActualInlining()) {
        rangeCounter++;
      }
      information.write(writer, rangeCounter, indent);
    }
  }

  @Override
  public String toString() {
    try {
      StringWriter writer = new StringWriter();
      write(writer, false, false);
      return writer.toString();
    } catch (IOException e) {
      return e.toString();
    }
  }

  public void setOriginalStartLineNumber(int originalStartLineNumber) {
    assert this.originalStartLineNumber == UNDEFINED_START_NUMBER;
    this.originalStartLineNumber = originalStartLineNumber;
  }

  public abstract static class Signature {

    public final String name;

    protected Signature(String name) {
      this.name = name;
    }

    abstract Signature asRenamed(String renamedName);

    abstract public SignatureKind kind();

    @Override
    abstract public boolean equals(Object o);

    @Override
    abstract public int hashCode();

    abstract void write(Writer builder) throws IOException;

    @Override
    public String toString() {
      try {
        StringWriter writer = new StringWriter();
        write(writer);
        return writer.toString();
      } catch (IOException e) {
        return e.toString();
      }
    }

    enum SignatureKind {
      METHOD,
      FIELD
    }
  }

  public static class FieldSignature extends Signature {

    public final String type;

    public FieldSignature(String name, String type) {
      super(name);
      this.type = type;
    }

    public static FieldSignature fromDexField(DexField field) {
      return new FieldSignature(field.name.toSourceString(),
          field.type.toSourceString());
    }

    DexField toDexField(DexItemFactory factory, DexType clazz) {
      return factory.createField(
          clazz,
          factory.createType(javaTypeToDescriptor(type)),
          factory.createString(name));
    }

    @Override
    Signature asRenamed(String renamedName) {
      return new FieldSignature(renamedName, type);
    }

    @Override
    public SignatureKind kind() {
      return SignatureKind.FIELD;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof FieldSignature)) {
        return false;
      }
      FieldSignature that = (FieldSignature) o;
      return name.equals(that.name) && type.equals(that.type);
    }

    @Override
    public int hashCode() {
      return name.hashCode() * 31 + type.hashCode();
    }

    @Override
    public String toString() {
      return type + " " + name;
    }

    @Override
    void write(Writer writer) throws IOException {
      writer.append(type);
      writer.append(' ');
      writer.append(name);
    }
  }

  public static class MethodSignature extends Signature {

    public final String type;
    public final String[] parameters;

    public MethodSignature(String name, String type, String[] parameters) {
      super(name);
      this.type = type;
      this.parameters = parameters;
    }

    public static MethodSignature fromDexMethod(DexMethod method) {
      String[] paramNames = new String[method.getArity()];
      DexType[] values = method.proto.parameters.values;
      for (int i = 0; i < values.length; i++) {
        paramNames[i] = values[i].toSourceString();
      }
      return new MethodSignature(method.name.toSourceString(),
          method.proto.returnType.toSourceString(), paramNames);
    }

    public static MethodSignature fromSignature(String name, String signature) {
      Type[] parameterDescriptors = Type.getArgumentTypes(signature);
      Type returnDescriptor = Type.getReturnType(signature);
      String[] parameterTypes = new String[parameterDescriptors.length];
      for (int i = 0; i < parameterDescriptors.length; i++) {
        parameterTypes[i] =
            DescriptorUtils.descriptorToJavaType(parameterDescriptors[i].getDescriptor());
      }
      return new MethodSignature(
          name,
          DescriptorUtils.descriptorToJavaType(returnDescriptor.getDescriptor()),
          parameterTypes);
    }

    DexMethod toDexMethod(DexItemFactory factory, DexType clazz) {
      DexType[] paramTypes = new DexType[parameters.length];
      for (int i = 0; i < parameters.length; i++) {
        paramTypes[i] = factory.createType(javaTypeToDescriptor(parameters[i]));
      }
      DexType returnType = factory.createType(javaTypeToDescriptor(type));
      return factory.createMethod(
          clazz,
          factory.createProto(returnType, paramTypes),
          factory.createString(name));
    }

    public static MethodSignature initializer(String[] parameters) {
      return new MethodSignature(Constants.INSTANCE_INITIALIZER_NAME, "void", parameters);
    }

    @Override
    Signature asRenamed(String renamedName) {
      return new MethodSignature(renamedName, type, parameters);
    }

    @Override
    public SignatureKind kind() {
      return SignatureKind.METHOD;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof MethodSignature)) {
        return false;
      }

      MethodSignature that = (MethodSignature) o;
      return type.equals(that.type)
          && name.equals(that.name)
          && Arrays.equals(parameters, that.parameters);
    }

    @Override
    public int hashCode() {
      return (type.hashCode() * 17
          + name.hashCode()) * 31
          + Arrays.hashCode(parameters);
    }

    @Override
    void write(Writer writer) throws IOException {
      writer.append(type)
          .append(' ')
          .append(name)
          .append('(');
      for (int i = 0; i < parameters.length; i++) {
        writer.append(parameters[i]);
        if (i < parameters.length - 1) {
          writer.append(',');
        }
      }
      writer.append(')');
    }
  }

  public class InlineInformation {
    static final int DO_NOT_COLLAPSE = -1;

    public final Range inlinedRange;
    public final Range originalRange;
    public final Signature signature;

    public InlineInformation(Range inlinedRange, Range originalRange, Signature signature) {
      this.inlinedRange = inlinedRange;
      this.originalRange = originalRange;
      this.signature = signature;
    }

    public boolean isActualInlining() {
      return !(originalRange instanceof SingleLineRange);
    }

    public void write(Writer writer, int collapsedRange, boolean indent) throws IOException {
      if (indent) {
        writer.append("    ");
      }
      if (collapsedRange == DO_NOT_COLLAPSE) {
        writer.append(inlinedRange.toString());
      } else {
        writer.append(Range.toCollapsedString(collapsedRange));
      }
      writer.append(":");
      signature.write(writer);
      if (originalRange != null) {
        writer.append(':')
            .append(originalRange.toString());
      }
      writer.append(" -> ")
          .append(renamedSignature.name);
      writer.append("\n");
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof InlineInformation)) {
        return false;
      }

      InlineInformation that = (InlineInformation) o;

      return inlinedRange.equals(that.inlinedRange)
          && ((originalRange == null && that.originalRange == null)
              || originalRange.equals(that.originalRange))
          && signature.equals(that.signature);

    }

    @Override
    public int hashCode() {
      int result = inlinedRange.hashCode();
      result = 31 * result + originalRange.hashCode();
      result = 31 * result + signature.hashCode();
      return result;
    }
  }

  /**
   * Represents a linenumber range.
   */
  public static class Range {

    public final int from;
    public final int to;

    Range(int from, int to) {
      this.from = from;
      this.to = to;
    }

    public boolean contains(int value) {
      return value >= from && value <= to;
    }

    public boolean isSingle() {
      return false;
    }

    @Override
    public String toString() {
      return from + ":" + to;
    }

    public static String toCollapsedString(int value) {
      return value + ":" + value;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof Range)) {
        return false;
      }

      Range range = (Range) o;
      return from == range.from && to == range.to;
    }

    @Override
    public int hashCode() {
      int result = from;
      result = 31 * result + to;
      return result;
    }

  }

  /**
   * Represents a single linenumber range (':' followed by a signle number), which
   * is different semantically from a normal range that has the same from and to.
   */
  public static class SingleLineRange extends Range {
    public SingleLineRange(int fromAndTo) {
      super(fromAndTo, fromAndTo);
    }

    @Override
    public boolean isSingle() {
      return true;
    }

    @Override
    public String toString() {
      return Integer.toString(from);
    }
  }

  public final static Range fakeZeroRange = new Range(0, 0);
}