author | jlahoda |
Wed, 27 Nov 2019 09:00:01 +0100 | |
changeset 59285 | 7799a51dbe30 |
parent 49541 | 4f6887eade94 |
permissions | -rw-r--r-- |
49429 | 1 |
/* |
2 |
* Copyright (c) 2018, Google LLC. 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 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
28 |
import com.sun.tools.javac.code.Symbol; |
|
29 |
import com.sun.tools.javac.tree.JCTree; |
|
30 |
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; |
|
31 |
import com.sun.tools.javac.tree.JCTree.JCIdent; |
|
32 |
import com.sun.tools.javac.tree.JCTree.JCLiteral; |
|
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
33 |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
49429 | 34 |
import com.sun.tools.javac.tree.TreeInfo; |
35 |
import com.sun.tools.javac.tree.TreeScanner; |
|
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
36 |
import java.util.Collection; |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
37 |
import java.util.HashMap; |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
38 |
import java.util.Map; |
49429 | 39 |
import java.util.Objects; |
40 |
||
41 |
/** A tree visitor that computes a hash code. */ |
|
42 |
public class TreeHasher extends TreeScanner { |
|
43 |
||
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
44 |
private final Map<Symbol, Integer> symbolHashes; |
49429 | 45 |
private int result = 17; |
46 |
||
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
47 |
public TreeHasher(Map<Symbol, Integer> symbolHashes) { |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
48 |
this.symbolHashes = Objects.requireNonNull(symbolHashes); |
49429 | 49 |
} |
50 |
||
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
51 |
public static int hash(JCTree tree, Collection<? extends Symbol> symbols) { |
49429 | 52 |
if (tree == null) { |
53 |
return 0; |
|
54 |
} |
|
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
55 |
Map<Symbol, Integer> symbolHashes = new HashMap<>(); |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
56 |
symbols.forEach(s -> symbolHashes.put(s, symbolHashes.size())); |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
57 |
TreeHasher hasher = new TreeHasher(symbolHashes); |
49429 | 58 |
tree.accept(hasher); |
59 |
return hasher.result; |
|
60 |
} |
|
61 |
||
62 |
private void hash(Object object) { |
|
63 |
result = 31 * result + Objects.hashCode(object); |
|
64 |
} |
|
65 |
||
66 |
@Override |
|
67 |
public void scan(JCTree tree) { |
|
68 |
if (tree == null) { |
|
69 |
return; |
|
70 |
} |
|
71 |
tree = TreeInfo.skipParens(tree); |
|
72 |
if (tree.type != null) { |
|
73 |
Object value = tree.type.constValue(); |
|
74 |
if (value != null) { |
|
75 |
hash(value); |
|
76 |
return; |
|
77 |
} |
|
78 |
} |
|
79 |
hash(tree.getTag()); |
|
80 |
tree.accept(this); |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
84 |
public void visitLiteral(JCLiteral tree) { |
|
85 |
hash(tree.value); |
|
86 |
super.visitLiteral(tree); |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
public void visitIdent(JCIdent tree) { |
|
91 |
Symbol sym = tree.sym; |
|
92 |
if (sym != null) { |
|
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
93 |
Integer hash = symbolHashes.get(sym); |
49429 | 94 |
if (hash != null) { |
95 |
hash(hash); |
|
96 |
return; |
|
97 |
} |
|
98 |
} |
|
99 |
hash(sym); |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public void visitSelect(JCFieldAccess tree) { |
|
104 |
hash(tree.sym); |
|
105 |
super.visitSelect(tree); |
|
106 |
} |
|
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
107 |
|
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
108 |
@Override |
59285
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
109 |
public void visitBindingPattern(JCTree.JCBindingPattern tree) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
110 |
symbolHashes.computeIfAbsent(tree.symbol, k -> symbolHashes.size()); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
111 |
super.visitBindingPattern(tree); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
112 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
113 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
49541
diff
changeset
|
114 |
@Override |
49541
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
115 |
public void visitVarDef(JCVariableDecl tree) { |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
116 |
symbolHashes.computeIfAbsent(tree.sym, k -> symbolHashes.size()); |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
117 |
super.visitVarDef(tree); |
4f6887eade94
8201194: Handle local variable declarations in lambda deduplication
cushon
parents:
49429
diff
changeset
|
118 |
} |
49429 | 119 |
} |