Apply the given pg-map via graph lense.

While visiting types in a subtyping order, mappings in the pg-map will
be converted to a custom graph lense. One caveat is to detect mapping
conflicts, e.g., so-called diamond problem.

Another caveat is to apply class mappings on-the-fly. We should rename
all the occurrences of renamed types while applying member mappings.

Yet another caveat is uses of renamed lib classes inside pgr classes.
We have a separate step that fixes trees by explicitly substitute those
type appearances with applied names.

------

Note that, instead of way too general support (http://go/r8g/4880),
we decided to support only the simple use case: compile a new piece
of code (e.g., test) against an obfuscated app.
See http://b/64802420#comment7 for more details.

Bug: 64802420
Change-Id: Ib3cb35811061c046e94a4fc9a021a783f15050dc
diff --git a/src/test/examples/applymapping044/AsubB.java b/src/test/examples/applymapping044/AsubB.java
new file mode 100644
index 0000000..2c5fd0c
--- /dev/null
+++ b/src/test/examples/applymapping044/AsubB.java
@@ -0,0 +1,13 @@
+// Copyright (c) 2017, 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 applymapping044;
+
+import naming044.A;
+import naming044.sub.SubB;
+
+public class AsubB extends SubB {
+  public int boo(A a) {
+    return f(a) * 3;
+  }
+}
diff --git a/src/test/examples/applymapping044/Main.java b/src/test/examples/applymapping044/Main.java
new file mode 100644
index 0000000..9d5f577
--- /dev/null
+++ b/src/test/examples/applymapping044/Main.java
@@ -0,0 +1,20 @@
+// Copyright (c) 2017, 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 applymapping044;
+
+import naming044.A;
+import naming044.B;
+import naming044.sub.SubB;
+
+public class Main {
+  public static void main(String[] args) {
+    B.m();
+    SubB.n();
+    A a = new A();
+    B b = new B();
+    b.f(a);
+    AsubB subB = new AsubB();
+    subB.f(a);
+  }
+}
diff --git a/src/test/examples/applymapping044/keep-rules-apply-mapping.txt b/src/test/examples/applymapping044/keep-rules-apply-mapping.txt
new file mode 100644
index 0000000..56f43b2
--- /dev/null
+++ b/src/test/examples/applymapping044/keep-rules-apply-mapping.txt
@@ -0,0 +1,13 @@
+# Copyright (c) 2017, 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.
+
+-keep public class applymapping044.Main {
+  public static void main(...);
+}
+
+-keep,allowobfuscation class * {
+  *;
+}
+
+-applymapping test-mapping.txt
diff --git a/src/test/examples/applymapping044/keep-rules.txt b/src/test/examples/applymapping044/keep-rules.txt
new file mode 100644
index 0000000..efed0ec
--- /dev/null
+++ b/src/test/examples/applymapping044/keep-rules.txt
@@ -0,0 +1,11 @@
+# Copyright (c) 2017, 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.
+
+-keep public class applymapping044.Main {
+  public static void main(...);
+}
+
+-keep,allowobfuscation class * {
+  *;
+}
diff --git a/src/test/examples/applymapping044/test-mapping.txt b/src/test/examples/applymapping044/test-mapping.txt
new file mode 100644
index 0000000..41c2c5c
--- /dev/null
+++ b/src/test/examples/applymapping044/test-mapping.txt
@@ -0,0 +1,9 @@
+naming044.A -> naming044.x:
+    int f -> o
+naming044.B -> naming044.y:
+    int m() -> n
+    int f(naming044.A) -> p
+naming044.sub.SubA -> naming044.z.x:
+    int f -> q
+naming044.sub.SubB -> naming044.z.y:
+    int n() -> m
diff --git a/src/test/examples/minification/conflict-mapping.txt b/src/test/examples/minification/conflict-mapping.txt
new file mode 100644
index 0000000..a4453da
--- /dev/null
+++ b/src/test/examples/minification/conflict-mapping.txt
@@ -0,0 +1,4 @@
+minification.InterfaceA -> ItfA:
+    int functionFromIntToInt(int) -> foo
+minification.InterfaceB -> ItfB:
+    int functionFromIntToInt(int) -> bar
diff --git a/src/test/examples/minification/keep-rules-apply-conflict-mapping.txt b/src/test/examples/minification/keep-rules-apply-conflict-mapping.txt
new file mode 100644
index 0000000..41f2261
--- /dev/null
+++ b/src/test/examples/minification/keep-rules-apply-conflict-mapping.txt
@@ -0,0 +1,14 @@
+# Copyright (c) 2017, 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.
+
+-applymapping conflict-mapping.txt
+
+# Keep the application entry point. Get rid of everything that is not
+# reachable from there.
+-keep public class minification.Minification {
+  public static void main(...);
+}
+
+# allow access modification to enable minification
+-allowaccessmodification
diff --git a/src/test/examples/naming001/keep-rules-105.txt b/src/test/examples/naming001/keep-rules-105.txt
new file mode 100644
index 0000000..f3bf7f6
--- /dev/null
+++ b/src/test/examples/naming001/keep-rules-105.txt
@@ -0,0 +1,11 @@
+# Copyright (c) 2017, 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.
+
+-allowaccessmodification
+
+-keep class naming001.D {
+  public static void main(...);
+}
+
+-applymapping mapping-105.txt
diff --git a/src/test/examples/naming001/mapping-105.txt b/src/test/examples/naming001/mapping-105.txt
new file mode 100644
index 0000000..3437a93
--- /dev/null
+++ b/src/test/examples/naming001/mapping-105.txt
@@ -0,0 +1,2 @@
+naming001.D -> naming001.D:
+    void keep() -> peek
diff --git a/src/test/examples/naming044/B.java b/src/test/examples/naming044/B.java
index 7580945..f723423 100644
--- a/src/test/examples/naming044/B.java
+++ b/src/test/examples/naming044/B.java
@@ -7,4 +7,7 @@
   public static int m() {
     return A.f;
   }
+  public int f(A a) {
+    return a.f;
+  }
 }
diff --git a/src/test/examples/naming044/sub/SubB.java b/src/test/examples/naming044/sub/SubB.java
index badc5f2..823de8c 100644
--- a/src/test/examples/naming044/sub/SubB.java
+++ b/src/test/examples/naming044/sub/SubB.java
@@ -3,7 +3,9 @@
 // BSD-style license that can be found in the LICENSE file.
 package naming044.sub;
 
-public class SubB {
+import naming044.B;
+
+public class SubB extends B {
   public static int n() {
     return SubA.f;
   }