author | jlahoda |
Mon, 10 Jun 2019 05:09:52 +0200 | |
changeset 55306 | ea43db53de91 |
parent 54568 | b2ed96c35687 |
child 58713 | ad69fd32778e |
permissions | -rw-r--r-- |
10 | 1 |
/* |
55306 | 2 |
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
28 |
import java.util.*; |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
29 |
import java.util.Map.Entry; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
30 |
import java.util.function.Function; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
31 |
import java.util.stream.Stream; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
32 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
33 |
import com.sun.source.tree.CaseTree.CaseKind; |
10 | 34 |
import com.sun.tools.javac.code.*; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26781
diff
changeset
|
35 |
import com.sun.tools.javac.code.Kinds.KindSelector; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
36 |
import com.sun.tools.javac.code.Scope.WriteableScope; |
10 | 37 |
import com.sun.tools.javac.jvm.*; |
11314 | 38 |
import com.sun.tools.javac.main.Option.PkgInfo; |
50213
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
39 |
import com.sun.tools.javac.resources.CompilerProperties.Fragments; |
10 | 40 |
import com.sun.tools.javac.tree.*; |
41 |
import com.sun.tools.javac.util.*; |
|
42 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
43 |
import com.sun.tools.javac.util.List; |
|
44 |
||
45 |
import com.sun.tools.javac.code.Symbol.*; |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
46 |
import com.sun.tools.javac.code.Symbol.OperatorSymbol.AccessCode; |
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44230
diff
changeset
|
47 |
import com.sun.tools.javac.resources.CompilerProperties.Errors; |
10 | 48 |
import com.sun.tools.javac.tree.JCTree.*; |
49 |
import com.sun.tools.javac.code.Type.*; |
|
50 |
||
51 |
import com.sun.tools.javac.jvm.Target; |
|
13077 | 52 |
import com.sun.tools.javac.tree.EndPosTable; |
10 | 53 |
|
54 |
import static com.sun.tools.javac.code.Flags.*; |
|
10950 | 55 |
import static com.sun.tools.javac.code.Flags.BLOCK; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
56 |
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
57 |
import static com.sun.tools.javac.code.TypeTag.*; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26781
diff
changeset
|
58 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
59 |
import static com.sun.tools.javac.code.Symbol.OperatorSymbol.AccessCode.DEREF; |
10 | 60 |
import static com.sun.tools.javac.jvm.ByteCodes.*; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
61 |
import com.sun.tools.javac.tree.JCTree.JCBreak; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
62 |
import com.sun.tools.javac.tree.JCTree.JCCase; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
63 |
import com.sun.tools.javac.tree.JCTree.JCExpression; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
64 |
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; |
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
39920
diff
changeset
|
65 |
import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.LEFT; |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
66 |
import com.sun.tools.javac.tree.JCTree.JCSwitchExpression; |
10950 | 67 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
10 | 68 |
|
69 |
/** This pass translates away some syntactic sugar: inner classes, |
|
70 |
* class literals, assertions, foreach loops, etc. |
|
71 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
72 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
73 |
* If you write code that depends on this, you do so at your own risk. |
10 | 74 |
* This code and its internal interfaces are subject to change or |
75 |
* deletion without notice.</b> |
|
76 |
*/ |
|
77 |
public class Lower extends TreeTranslator { |
|
22163 | 78 |
protected static final Context.Key<Lower> lowerKey = new Context.Key<>(); |
10 | 79 |
|
80 |
public static Lower instance(Context context) { |
|
81 |
Lower instance = context.get(lowerKey); |
|
82 |
if (instance == null) |
|
83 |
instance = new Lower(context); |
|
84 |
return instance; |
|
85 |
} |
|
86 |
||
24604
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
87 |
private final Names names; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
88 |
private final Log log; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
89 |
private final Symtab syms; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
90 |
private final Resolve rs; |
29051
7244db2ab176
8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
29050
diff
changeset
|
91 |
private final Operators operators; |
24604
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
92 |
private final Check chk; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
93 |
private final Attr attr; |
10 | 94 |
private TreeMaker make; |
95 |
private DiagnosticPosition make_pos; |
|
24604
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
96 |
private final ConstFold cfolder; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
97 |
private final Target target; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
98 |
private final Source source; |
25007 | 99 |
private final TypeEnvs typeEnvs; |
10 | 100 |
private final Name dollarAssertionsDisabled; |
101 |
private final Name classDollar; |
|
37638
aac00923d48e
7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
36526
diff
changeset
|
102 |
private final Name dollarCloseResource; |
24604
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
103 |
private final Types types; |
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
104 |
private final boolean debugLower; |
50735
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
105 |
private final boolean disableProtectedAccessors; // experimental |
24604
7f68545b5128
8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents:
24396
diff
changeset
|
106 |
private final PkgInfo pkginfoOpt; |
10 | 107 |
|
108 |
protected Lower(Context context) { |
|
109 |
context.put(lowerKey, this); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1037
diff
changeset
|
110 |
names = Names.instance(context); |
10 | 111 |
log = Log.instance(context); |
112 |
syms = Symtab.instance(context); |
|
113 |
rs = Resolve.instance(context); |
|
29051
7244db2ab176
8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
29050
diff
changeset
|
114 |
operators = Operators.instance(context); |
10 | 115 |
chk = Check.instance(context); |
116 |
attr = Attr.instance(context); |
|
117 |
make = TreeMaker.instance(context); |
|
118 |
cfolder = ConstFold.instance(context); |
|
119 |
target = Target.instance(context); |
|
120 |
source = Source.instance(context); |
|
25007 | 121 |
typeEnvs = TypeEnvs.instance(context); |
10 | 122 |
dollarAssertionsDisabled = names. |
123 |
fromString(target.syntheticNameChar() + "assertionsDisabled"); |
|
124 |
classDollar = names. |
|
125 |
fromString("class" + target.syntheticNameChar()); |
|
37638
aac00923d48e
7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
36526
diff
changeset
|
126 |
dollarCloseResource = names. |
aac00923d48e
7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
36526
diff
changeset
|
127 |
fromString(target.syntheticNameChar() + "closeResource"); |
10 | 128 |
|
129 |
types = Types.instance(context); |
|
130 |
Options options = Options.instance(context); |
|
6721 | 131 |
debugLower = options.isSet("debuglower"); |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
132 |
pkginfoOpt = PkgInfo.get(options); |
50735
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
133 |
disableProtectedAccessors = options.isSet("disableProtectedAccessors"); |
10 | 134 |
} |
135 |
||
136 |
/** The currently enclosing class. |
|
137 |
*/ |
|
138 |
ClassSymbol currentClass; |
|
139 |
||
140 |
/** A queue of all translated classes. |
|
141 |
*/ |
|
142 |
ListBuffer<JCTree> translated; |
|
143 |
||
144 |
/** Environment for symbol lookup, set by translateTopLevelClass. |
|
145 |
*/ |
|
146 |
Env<AttrContext> attrEnv; |
|
147 |
||
148 |
/** A hash table mapping syntax trees to their ending source positions. |
|
149 |
*/ |
|
11055 | 150 |
EndPosTable endPosTable; |
10 | 151 |
|
152 |
/************************************************************************** |
|
153 |
* Global mappings |
|
154 |
*************************************************************************/ |
|
155 |
||
156 |
/** A hash table mapping local classes to their definitions. |
|
157 |
*/ |
|
158 |
Map<ClassSymbol, JCClassDecl> classdefs; |
|
159 |
||
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
160 |
/** A hash table mapping local classes to a list of pruned trees. |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
161 |
*/ |
22163 | 162 |
public Map<ClassSymbol, List<JCTree>> prunedTree = new WeakHashMap<>(); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
163 |
|
10 | 164 |
/** A hash table mapping virtual accessed symbols in outer subclasses |
165 |
* to the actually referred symbol in superclasses. |
|
166 |
*/ |
|
167 |
Map<Symbol,Symbol> actualSymbols; |
|
168 |
||
169 |
/** The current method definition. |
|
170 |
*/ |
|
171 |
JCMethodDecl currentMethodDef; |
|
172 |
||
173 |
/** The current method symbol. |
|
174 |
*/ |
|
175 |
MethodSymbol currentMethodSym; |
|
176 |
||
177 |
/** The currently enclosing outermost class definition. |
|
178 |
*/ |
|
179 |
JCClassDecl outermostClassDef; |
|
180 |
||
181 |
/** The currently enclosing outermost member definition. |
|
182 |
*/ |
|
183 |
JCTree outermostMemberDef; |
|
184 |
||
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
185 |
/** A map from local variable symbols to their translation (as per LambdaToMethod). |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
186 |
* This is required when a capturing local class is created from a lambda (in which |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
187 |
* case the captured symbols should be replaced with the translated lambda symbols). |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
188 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
189 |
Map<Symbol, Symbol> lambdaTranslationMap = null; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
190 |
|
10 | 191 |
/** A navigator class for assembling a mapping from local class symbols |
192 |
* to class definition trees. |
|
193 |
* There is only one case; all other cases simply traverse down the tree. |
|
194 |
*/ |
|
195 |
class ClassMap extends TreeScanner { |
|
196 |
||
197 |
/** All encountered class defs are entered into classdefs table. |
|
198 |
*/ |
|
199 |
public void visitClassDef(JCClassDecl tree) { |
|
200 |
classdefs.put(tree.sym, tree); |
|
201 |
super.visitClassDef(tree); |
|
202 |
} |
|
203 |
} |
|
204 |
ClassMap classMap = new ClassMap(); |
|
205 |
||
206 |
/** Map a class symbol to its definition. |
|
207 |
* @param c The class symbol of which we want to determine the definition. |
|
208 |
*/ |
|
209 |
JCClassDecl classDef(ClassSymbol c) { |
|
210 |
// First lookup the class in the classdefs table. |
|
211 |
JCClassDecl def = classdefs.get(c); |
|
212 |
if (def == null && outermostMemberDef != null) { |
|
213 |
// If this fails, traverse outermost member definition, entering all |
|
214 |
// local classes into classdefs, and try again. |
|
215 |
classMap.scan(outermostMemberDef); |
|
216 |
def = classdefs.get(c); |
|
217 |
} |
|
218 |
if (def == null) { |
|
219 |
// If this fails, traverse outermost class definition, entering all |
|
220 |
// local classes into classdefs, and try again. |
|
221 |
classMap.scan(outermostClassDef); |
|
222 |
def = classdefs.get(c); |
|
223 |
} |
|
224 |
return def; |
|
225 |
} |
|
226 |
||
227 |
/** A hash table mapping class symbols to lists of free variables. |
|
228 |
* accessed by them. Only free variables of the method immediately containing |
|
229 |
* a class are associated with that class. |
|
230 |
*/ |
|
231 |
Map<ClassSymbol,List<VarSymbol>> freevarCache; |
|
232 |
||
233 |
/** A navigator class for collecting the free variables accessed |
|
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
234 |
* from a local class. There is only one case; all other cases simply |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
235 |
* traverse down the tree. This class doesn't deal with the specific |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
236 |
* of Lower - it's an abstract visitor that is meant to be reused in |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
237 |
* order to share the local variable capture logic. |
10 | 238 |
*/ |
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
239 |
abstract class BasicFreeVarCollector extends TreeScanner { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
240 |
|
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
241 |
/** Add all free variables of class c to fvs list |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
242 |
* unless they are already there. |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
243 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
244 |
abstract void addFreeVars(ClassSymbol c); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
245 |
|
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
246 |
/** If tree refers to a variable in owner of local class, add it to |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
247 |
* free variables list. |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
248 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
249 |
public void visitIdent(JCIdent tree) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
250 |
visitSymbol(tree.sym); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
251 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
252 |
// where |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
253 |
abstract void visitSymbol(Symbol _sym); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
254 |
|
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
255 |
/** If tree refers to a class instance creation expression |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
256 |
* add all free variables of the freshly created class. |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
257 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
258 |
public void visitNewClass(JCNewClass tree) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
259 |
ClassSymbol c = (ClassSymbol)tree.constructor.owner; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
260 |
addFreeVars(c); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
261 |
super.visitNewClass(tree); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
262 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
263 |
|
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
264 |
/** If tree refers to a superclass constructor call, |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
265 |
* add all free variables of the superclass. |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
266 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
267 |
public void visitApply(JCMethodInvocation tree) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
268 |
if (TreeInfo.name(tree.meth) == names._super) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
269 |
addFreeVars((ClassSymbol) TreeInfo.symbol(tree.meth).owner); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
270 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
271 |
super.visitApply(tree); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
272 |
} |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
273 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
274 |
@Override |
55306 | 275 |
public void visitYield(JCYield tree) { |
276 |
scan(tree.value); |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
277 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
278 |
|
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
279 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
280 |
|
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
281 |
/** |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
282 |
* Lower-specific subclass of {@code BasicFreeVarCollector}. |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
283 |
*/ |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
284 |
class FreeVarCollector extends BasicFreeVarCollector { |
10 | 285 |
|
286 |
/** The owner of the local class. |
|
287 |
*/ |
|
288 |
Symbol owner; |
|
289 |
||
290 |
/** The local class. |
|
291 |
*/ |
|
292 |
ClassSymbol clazz; |
|
293 |
||
294 |
/** The list of owner's variables accessed from within the local class, |
|
295 |
* without any duplicates. |
|
296 |
*/ |
|
297 |
List<VarSymbol> fvs; |
|
298 |
||
299 |
FreeVarCollector(ClassSymbol clazz) { |
|
300 |
this.clazz = clazz; |
|
301 |
this.owner = clazz.owner; |
|
302 |
this.fvs = List.nil(); |
|
303 |
} |
|
304 |
||
305 |
/** Add free variable to fvs list unless it is already there. |
|
306 |
*/ |
|
307 |
private void addFreeVar(VarSymbol v) { |
|
308 |
for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) |
|
309 |
if (l.head == v) return; |
|
310 |
fvs = fvs.prepend(v); |
|
311 |
} |
|
312 |
||
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
313 |
@Override |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
314 |
void addFreeVars(ClassSymbol c) { |
10 | 315 |
List<VarSymbol> fvs = freevarCache.get(c); |
316 |
if (fvs != null) { |
|
317 |
for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) { |
|
318 |
addFreeVar(l.head); |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
||
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
323 |
@Override |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
324 |
void visitSymbol(Symbol _sym) { |
10 | 325 |
Symbol sym = _sym; |
326 |
if (sym.kind == VAR || sym.kind == MTH) { |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
327 |
if (sym != null && sym.owner != owner) |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
328 |
sym = proxies.get(sym); |
10 | 329 |
if (sym != null && sym.owner == owner) { |
330 |
VarSymbol v = (VarSymbol)sym; |
|
331 |
if (v.getConstValue() == null) { |
|
332 |
addFreeVar(v); |
|
333 |
} |
|
334 |
} else { |
|
335 |
if (outerThisStack.head != null && |
|
336 |
outerThisStack.head != _sym) |
|
337 |
visitSymbol(outerThisStack.head); |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/** If tree refers to a class instance creation expression |
|
343 |
* add all free variables of the freshly created class. |
|
344 |
*/ |
|
345 |
public void visitNewClass(JCNewClass tree) { |
|
346 |
ClassSymbol c = (ClassSymbol)tree.constructor.owner; |
|
347 |
if (tree.encl == null && |
|
348 |
c.hasOuterInstance() && |
|
349 |
outerThisStack.head != null) |
|
350 |
visitSymbol(outerThisStack.head); |
|
351 |
super.visitNewClass(tree); |
|
352 |
} |
|
353 |
||
354 |
/** If tree refers to a qualified this or super expression |
|
355 |
* for anything but the current class, add the outer this |
|
356 |
* stack as a free variable. |
|
357 |
*/ |
|
358 |
public void visitSelect(JCFieldAccess tree) { |
|
359 |
if ((tree.name == names._this || tree.name == names._super) && |
|
360 |
tree.selected.type.tsym != clazz && |
|
361 |
outerThisStack.head != null) |
|
362 |
visitSymbol(outerThisStack.head); |
|
363 |
super.visitSelect(tree); |
|
364 |
} |
|
365 |
||
366 |
/** If tree refers to a superclass constructor call, |
|
367 |
* add all free variables of the superclass. |
|
368 |
*/ |
|
369 |
public void visitApply(JCMethodInvocation tree) { |
|
370 |
if (TreeInfo.name(tree.meth) == names._super) { |
|
371 |
Symbol constructor = TreeInfo.symbol(tree.meth); |
|
372 |
ClassSymbol c = (ClassSymbol)constructor.owner; |
|
373 |
if (c.hasOuterInstance() && |
|
10950 | 374 |
!tree.meth.hasTag(SELECT) && |
10 | 375 |
outerThisStack.head != null) |
376 |
visitSymbol(outerThisStack.head); |
|
377 |
} |
|
378 |
super.visitApply(tree); |
|
379 |
} |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
380 |
|
10 | 381 |
} |
382 |
||
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
383 |
ClassSymbol ownerToCopyFreeVarsFrom(ClassSymbol c) { |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
384 |
if (!c.isLocal()) { |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
385 |
return null; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
386 |
} |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
387 |
Symbol currentOwner = c.owner; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26781
diff
changeset
|
388 |
while (currentOwner.owner.kind.matches(KindSelector.TYP) && currentOwner.isLocal()) { |
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
389 |
currentOwner = currentOwner.owner; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
390 |
} |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26781
diff
changeset
|
391 |
if (currentOwner.owner.kind.matches(KindSelector.VAL_MTH) && c.isSubClass(currentOwner, types)) { |
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
392 |
return (ClassSymbol)currentOwner; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
393 |
} |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
394 |
return null; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
395 |
} |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
396 |
|
10 | 397 |
/** Return the variables accessed from within a local class, which |
398 |
* are declared in the local class' owner. |
|
399 |
* (in reverse order of first access). |
|
400 |
*/ |
|
401 |
List<VarSymbol> freevars(ClassSymbol c) { |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
402 |
List<VarSymbol> fvs = freevarCache.get(c); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
403 |
if (fvs != null) { |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
404 |
return fvs; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
405 |
} |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26781
diff
changeset
|
406 |
if (c.owner.kind.matches(KindSelector.VAL_MTH)) { |
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
407 |
FreeVarCollector collector = new FreeVarCollector(c); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
408 |
collector.scan(classDef(c)); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
409 |
fvs = collector.fvs; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
410 |
freevarCache.put(c, fvs); |
10 | 411 |
return fvs; |
412 |
} else { |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
413 |
ClassSymbol owner = ownerToCopyFreeVarsFrom(c); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
414 |
if (owner != null) { |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
415 |
fvs = freevarCache.get(owner); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
416 |
freevarCache.put(c, fvs); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
417 |
return fvs; |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
418 |
} else { |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
419 |
return List.nil(); |
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
420 |
} |
10 | 421 |
} |
422 |
} |
|
423 |
||
22163 | 424 |
Map<TypeSymbol,EnumMapping> enumSwitchMap = new LinkedHashMap<>(); |
10 | 425 |
|
426 |
EnumMapping mapForEnum(DiagnosticPosition pos, TypeSymbol enumClass) { |
|
427 |
EnumMapping map = enumSwitchMap.get(enumClass); |
|
428 |
if (map == null) |
|
429 |
enumSwitchMap.put(enumClass, map = new EnumMapping(pos, enumClass)); |
|
430 |
return map; |
|
431 |
} |
|
432 |
||
433 |
/** This map gives a translation table to be used for enum |
|
434 |
* switches. |
|
435 |
* |
|
436 |
* <p>For each enum that appears as the type of a switch |
|
437 |
* expression, we maintain an EnumMapping to assist in the |
|
438 |
* translation, as exemplified by the following example: |
|
439 |
* |
|
440 |
* <p>we translate |
|
441 |
* <pre> |
|
442 |
* switch(colorExpression) { |
|
443 |
* case red: stmt1; |
|
444 |
* case green: stmt2; |
|
445 |
* } |
|
446 |
* </pre> |
|
447 |
* into |
|
448 |
* <pre> |
|
449 |
* switch(Outer$0.$EnumMap$Color[colorExpression.ordinal()]) { |
|
450 |
* case 1: stmt1; |
|
451 |
* case 2: stmt2 |
|
452 |
* } |
|
453 |
* </pre> |
|
4142 | 454 |
* with the auxiliary table initialized as follows: |
10 | 455 |
* <pre> |
456 |
* class Outer$0 { |
|
457 |
* synthetic final int[] $EnumMap$Color = new int[Color.values().length]; |
|
458 |
* static { |
|
459 |
* try { $EnumMap$Color[red.ordinal()] = 1; } catch (NoSuchFieldError ex) {} |
|
460 |
* try { $EnumMap$Color[green.ordinal()] = 2; } catch (NoSuchFieldError ex) {} |
|
461 |
* } |
|
462 |
* } |
|
463 |
* </pre> |
|
464 |
* class EnumMapping provides mapping data and support methods for this translation. |
|
465 |
*/ |
|
466 |
class EnumMapping { |
|
467 |
EnumMapping(DiagnosticPosition pos, TypeSymbol forEnum) { |
|
468 |
this.forEnum = forEnum; |
|
22163 | 469 |
this.values = new LinkedHashMap<>(); |
10 | 470 |
this.pos = pos; |
471 |
Name varName = names |
|
472 |
.fromString(target.syntheticNameChar() + |
|
473 |
"SwitchMap" + |
|
474 |
target.syntheticNameChar() + |
|
54568
b2ed96c35687
8222289: Overhaul logic for reading/writing constant pool entries
mcimadamore
parents:
52794
diff
changeset
|
475 |
names.fromUtf(ClassWriter.externalize(forEnum.type.tsym.flatName())).toString() |
10 | 476 |
.replace('/', '.') |
477 |
.replace('.', target.syntheticNameChar())); |
|
478 |
ClassSymbol outerCacheClass = outerCacheClass(); |
|
479 |
this.mapVar = new VarSymbol(STATIC | SYNTHETIC | FINAL, |
|
480 |
varName, |
|
26781
a786b07c7b91
8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents:
25874
diff
changeset
|
481 |
new ArrayType(syms.intType, syms.arrayClass), |
10 | 482 |
outerCacheClass); |
483 |
enterSynthetic(pos, mapVar, outerCacheClass.members()); |
|
484 |
} |
|
485 |
||
486 |
DiagnosticPosition pos = null; |
|
487 |
||
488 |
// the next value to use |
|
489 |
int next = 1; // 0 (unused map elements) go to the default label |
|
490 |
||
491 |
// the enum for which this is a map |
|
492 |
final TypeSymbol forEnum; |
|
493 |
||
494 |
// the field containing the map |
|
495 |
final VarSymbol mapVar; |
|
496 |
||
497 |
// the mapped values |
|
498 |
final Map<VarSymbol,Integer> values; |
|
499 |
||
500 |
JCLiteral forConstant(VarSymbol v) { |
|
501 |
Integer result = values.get(v); |
|
502 |
if (result == null) |
|
503 |
values.put(v, result = next++); |
|
504 |
return make.Literal(result); |
|
505 |
} |
|
506 |
||
507 |
// generate the field initializer for the map |
|
508 |
void translate() { |
|
509 |
make.at(pos.getStartPosition()); |
|
510 |
JCClassDecl owner = classDef((ClassSymbol)mapVar.owner); |
|
511 |
||
512 |
// synthetic static final int[] $SwitchMap$Color = new int[Color.values().length]; |
|
513 |
MethodSymbol valuesMethod = lookupMethod(pos, |
|
514 |
names.values, |
|
515 |
forEnum.type, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
516 |
List.nil()); |
10 | 517 |
JCExpression size = make // Color.values().length |
518 |
.Select(make.App(make.QualIdent(valuesMethod)), |
|
519 |
syms.lengthVar); |
|
520 |
JCExpression mapVarInit = make |
|
521 |
.NewArray(make.Type(syms.intType), List.of(size), null) |
|
26781
a786b07c7b91
8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents:
25874
diff
changeset
|
522 |
.setType(new ArrayType(syms.intType, syms.arrayClass)); |
10 | 523 |
|
524 |
// try { $SwitchMap$Color[red.ordinal()] = 1; } catch (java.lang.NoSuchFieldError ex) {} |
|
22163 | 525 |
ListBuffer<JCStatement> stmts = new ListBuffer<>(); |
10 | 526 |
Symbol ordinalMethod = lookupMethod(pos, |
527 |
names.ordinal, |
|
528 |
forEnum.type, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
529 |
List.nil()); |
10 | 530 |
List<JCCatch> catcher = List.<JCCatch>nil() |
531 |
.prepend(make.Catch(make.VarDef(new VarSymbol(PARAMETER, names.ex, |
|
532 |
syms.noSuchFieldErrorType, |
|
533 |
syms.noSymbol), |
|
534 |
null), |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
535 |
make.Block(0, List.nil()))); |
10 | 536 |
for (Map.Entry<VarSymbol,Integer> e : values.entrySet()) { |
537 |
VarSymbol enumerator = e.getKey(); |
|
538 |
Integer mappedValue = e.getValue(); |
|
539 |
JCExpression assign = make |
|
540 |
.Assign(make.Indexed(mapVar, |
|
541 |
make.App(make.Select(make.QualIdent(enumerator), |
|
542 |
ordinalMethod))), |
|
543 |
make.Literal(mappedValue)) |
|
544 |
.setType(syms.intType); |
|
545 |
JCStatement exec = make.Exec(assign); |
|
546 |
JCStatement _try = make.Try(make.Block(0, List.of(exec)), catcher, null); |
|
547 |
stmts.append(_try); |
|
548 |
} |
|
549 |
||
550 |
owner.defs = owner.defs |
|
551 |
.prepend(make.Block(STATIC, stmts.toList())) |
|
552 |
.prepend(make.VarDef(mapVar, mapVarInit)); |
|
553 |
} |
|
554 |
} |
|
555 |
||
556 |
||
557 |
/************************************************************************** |
|
558 |
* Tree building blocks |
|
559 |
*************************************************************************/ |
|
560 |
||
561 |
/** Equivalent to make.at(pos.getStartPosition()) with side effect of caching |
|
562 |
* pos as make_pos, for use in diagnostics. |
|
563 |
**/ |
|
564 |
TreeMaker make_at(DiagnosticPosition pos) { |
|
565 |
make_pos = pos; |
|
566 |
return make.at(pos); |
|
567 |
} |
|
568 |
||
569 |
/** Make an attributed tree representing a literal. This will be an |
|
570 |
* Ident node in the case of boolean literals, a Literal node in all |
|
571 |
* other cases. |
|
572 |
* @param type The literal's type. |
|
573 |
* @param value The literal's value. |
|
574 |
*/ |
|
575 |
JCExpression makeLit(Type type, Object value) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
576 |
return make.Literal(type.getTag(), value).setType(type.constType(value)); |
10 | 577 |
} |
578 |
||
579 |
/** Make an attributed tree representing null. |
|
580 |
*/ |
|
581 |
JCExpression makeNull() { |
|
582 |
return makeLit(syms.botType, null); |
|
583 |
} |
|
584 |
||
585 |
/** Make an attributed class instance creation expression. |
|
586 |
* @param ctype The class type. |
|
587 |
* @param args The constructor arguments. |
|
588 |
*/ |
|
589 |
JCNewClass makeNewClass(Type ctype, List<JCExpression> args) { |
|
590 |
JCNewClass tree = make.NewClass(null, |
|
591 |
null, make.QualIdent(ctype.tsym), args, null); |
|
592 |
tree.constructor = rs.resolveConstructor( |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
593 |
make_pos, attrEnv, ctype, TreeInfo.types(args), List.nil()); |
10 | 594 |
tree.type = ctype; |
595 |
return tree; |
|
596 |
} |
|
597 |
||
598 |
/** Make an attributed unary expression. |
|
599 |
* @param optag The operators tree tag. |
|
600 |
* @param arg The operator's argument. |
|
601 |
*/ |
|
10950 | 602 |
JCUnary makeUnary(JCTree.Tag optag, JCExpression arg) { |
10 | 603 |
JCUnary tree = make.Unary(optag, arg); |
29051
7244db2ab176
8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
29050
diff
changeset
|
604 |
tree.operator = operators.resolveUnary(tree, optag, arg.type); |
10 | 605 |
tree.type = tree.operator.type.getReturnType(); |
606 |
return tree; |
|
607 |
} |
|
608 |
||
609 |
/** Make an attributed binary expression. |
|
610 |
* @param optag The operators tree tag. |
|
611 |
* @param lhs The operator's left argument. |
|
612 |
* @param rhs The operator's right argument. |
|
613 |
*/ |
|
10950 | 614 |
JCBinary makeBinary(JCTree.Tag optag, JCExpression lhs, JCExpression rhs) { |
10 | 615 |
JCBinary tree = make.Binary(optag, lhs, rhs); |
29051
7244db2ab176
8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
29050
diff
changeset
|
616 |
tree.operator = operators.resolveBinary(tree, optag, lhs.type, rhs.type); |
10 | 617 |
tree.type = tree.operator.type.getReturnType(); |
618 |
return tree; |
|
619 |
} |
|
620 |
||
621 |
/** Make an attributed assignop expression. |
|
622 |
* @param optag The operators tree tag. |
|
623 |
* @param lhs The operator's left argument. |
|
624 |
* @param rhs The operator's right argument. |
|
625 |
*/ |
|
10950 | 626 |
JCAssignOp makeAssignop(JCTree.Tag optag, JCTree lhs, JCTree rhs) { |
10 | 627 |
JCAssignOp tree = make.Assignop(optag, lhs, rhs); |
29051
7244db2ab176
8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
29050
diff
changeset
|
628 |
tree.operator = operators.resolveBinary(tree, tree.getTag().noAssignOp(), lhs.type, rhs.type); |
10 | 629 |
tree.type = lhs.type; |
630 |
return tree; |
|
631 |
} |
|
632 |
||
633 |
/** Convert tree into string object, unless it has already a |
|
634 |
* reference type.. |
|
635 |
*/ |
|
636 |
JCExpression makeString(JCExpression tree) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
637 |
if (!tree.type.isPrimitiveOrVoid()) { |
10 | 638 |
return tree; |
639 |
} else { |
|
640 |
Symbol valueOfSym = lookupMethod(tree.pos(), |
|
641 |
names.valueOf, |
|
642 |
syms.stringType, |
|
643 |
List.of(tree.type)); |
|
644 |
return make.App(make.QualIdent(valueOfSym), List.of(tree)); |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
/** Create an empty anonymous class definition and enter and complete |
|
649 |
* its symbol. Return the class definition's symbol. |
|
650 |
* and create |
|
651 |
* @param flags The class symbol's flags |
|
652 |
* @param owner The class symbol's owner |
|
653 |
*/ |
|
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
654 |
JCClassDecl makeEmptyClass(long flags, ClassSymbol owner) { |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
655 |
return makeEmptyClass(flags, owner, null, true); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
656 |
} |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
657 |
|
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
658 |
JCClassDecl makeEmptyClass(long flags, ClassSymbol owner, Name flatname, |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
659 |
boolean addToDefs) { |
10 | 660 |
// Create class symbol. |
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
661 |
ClassSymbol c = syms.defineClass(names.empty, owner); |
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
662 |
if (flatname != null) { |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
663 |
c.flatname = flatname; |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
664 |
} else { |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
665 |
c.flatname = chk.localClassName(c); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
666 |
} |
10 | 667 |
c.sourcefile = owner.sourcefile; |
30066
d74c06a92bd8
8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents:
29842
diff
changeset
|
668 |
c.completer = Completer.NULL_COMPLETER; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
669 |
c.members_field = WriteableScope.create(c); |
10 | 670 |
c.flags_field = flags; |
671 |
ClassType ctype = (ClassType) c.type; |
|
672 |
ctype.supertype_field = syms.objectType; |
|
673 |
ctype.interfaces_field = List.nil(); |
|
674 |
||
675 |
JCClassDecl odef = classDef(owner); |
|
676 |
||
677 |
// Enter class symbol in owner scope and compiled table. |
|
678 |
enterSynthetic(odef.pos(), c, owner.members()); |
|
36526 | 679 |
chk.putCompiled(c); |
10 | 680 |
|
681 |
// Create class definition tree. |
|
682 |
JCClassDecl cdef = make.ClassDef( |
|
683 |
make.Modifiers(flags), names.empty, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
684 |
List.nil(), |
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
685 |
null, List.nil(), List.nil()); |
10 | 686 |
cdef.sym = c; |
687 |
cdef.type = c.type; |
|
688 |
||
689 |
// Append class definition tree to owner's definitions. |
|
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
690 |
if (addToDefs) odef.defs = odef.defs.prepend(cdef); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
691 |
return cdef; |
10 | 692 |
} |
693 |
||
694 |
/************************************************************************** |
|
695 |
* Symbol manipulation utilities |
|
696 |
*************************************************************************/ |
|
697 |
||
698 |
/** Enter a synthetic symbol in a given scope, but complain if there was already one there. |
|
699 |
* @param pos Position for error reporting. |
|
700 |
* @param sym The symbol. |
|
701 |
* @param s The scope. |
|
702 |
*/ |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
703 |
private void enterSynthetic(DiagnosticPosition pos, Symbol sym, WriteableScope s) { |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
704 |
s.enter(sym); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
705 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
706 |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
707 |
/** Create a fresh synthetic name within a given scope - the unique name is |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
708 |
* obtained by appending '$' chars at the end of the name until no match |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
709 |
* is found. |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
710 |
* |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
711 |
* @param name base name |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
712 |
* @param s scope in which the name has to be unique |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
713 |
* @return fresh synthetic name |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
714 |
*/ |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
715 |
private Name makeSyntheticName(Name name, Scope s) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
716 |
do { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
717 |
name = name.append( |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
718 |
target.syntheticNameChar(), |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
719 |
names.empty); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
720 |
} while (lookupSynthetic(name, s) != null); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
721 |
return name; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
722 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
723 |
|
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
724 |
/** Check whether synthetic symbols generated during lowering conflict |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
725 |
* with user-defined symbols. |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
726 |
* |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
727 |
* @param translatedTrees lowered class trees |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
728 |
*/ |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
729 |
void checkConflicts(List<JCTree> translatedTrees) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
730 |
for (JCTree t : translatedTrees) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
731 |
t.accept(conflictsChecker); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
732 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
733 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
734 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
735 |
JCTree.Visitor conflictsChecker = new TreeScanner() { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
736 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
737 |
TypeSymbol currentClass; |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
738 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
739 |
@Override |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
740 |
public void visitMethodDef(JCMethodDecl that) { |
50213
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
741 |
checkConflicts(that.pos(), that.sym, currentClass); |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
742 |
super.visitMethodDef(that); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
743 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
744 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
745 |
@Override |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
746 |
public void visitVarDef(JCVariableDecl that) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
747 |
if (that.sym.owner.kind == TYP) { |
50213
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
748 |
checkConflicts(that.pos(), that.sym, currentClass); |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
749 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
750 |
super.visitVarDef(that); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
751 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
752 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
753 |
@Override |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
754 |
public void visitClassDef(JCClassDecl that) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
755 |
TypeSymbol prevCurrentClass = currentClass; |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
756 |
currentClass = that.sym; |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
757 |
try { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
758 |
super.visitClassDef(that); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
759 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
760 |
finally { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
761 |
currentClass = prevCurrentClass; |
10 | 762 |
} |
763 |
} |
|
50213
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
764 |
|
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
765 |
void checkConflicts(DiagnosticPosition pos, Symbol sym, TypeSymbol c) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
766 |
for (Type ct = c.type; ct != Type.noType ; ct = types.supertype(ct)) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
767 |
for (Symbol sym2 : ct.tsym.members().getSymbolsByName(sym.name, NON_RECURSIVE)) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
768 |
// VM allows methods and variables with differing types |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
769 |
if (sym.kind == sym2.kind && |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
770 |
types.isSameType(types.erasure(sym.type), types.erasure(sym2.type)) && |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
771 |
sym != sym2 && |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
772 |
(sym.flags() & Flags.SYNTHETIC) != (sym2.flags() & Flags.SYNTHETIC) && |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
773 |
(sym.flags() & BRIDGE) == 0 && (sym2.flags() & BRIDGE) == 0) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
774 |
syntheticError(pos, (sym2.flags() & SYNTHETIC) == 0 ? sym2 : sym); |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
775 |
return; |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
776 |
} |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
777 |
} |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
778 |
} |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
779 |
} |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
780 |
|
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
781 |
/** Report a conflict between a user symbol and a synthetic symbol. |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
782 |
*/ |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
783 |
private void syntheticError(DiagnosticPosition pos, Symbol sym) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
784 |
if (!sym.type.isErroneous()) { |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
785 |
log.error(pos, Errors.CannotGenerateClass(sym.location(), Fragments.SyntheticNameConflict(sym, sym.location()))); |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
786 |
} |
3085969bdf91
8203436: javac should fail early when emitting illegal signature attributes
mcimadamore
parents:
49280
diff
changeset
|
787 |
} |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
788 |
}; |
10 | 789 |
|
790 |
/** Look up a synthetic name in a given scope. |
|
14259 | 791 |
* @param s The scope. |
10 | 792 |
* @param name The name. |
793 |
*/ |
|
794 |
private Symbol lookupSynthetic(Name name, Scope s) { |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
795 |
Symbol sym = s.findFirst(name); |
10 | 796 |
return (sym==null || (sym.flags()&SYNTHETIC)==0) ? null : sym; |
797 |
} |
|
798 |
||
799 |
/** Look up a method in a given scope. |
|
800 |
*/ |
|
801 |
private MethodSymbol lookupMethod(DiagnosticPosition pos, Name name, Type qual, List<Type> args) { |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
802 |
return rs.resolveInternalMethod(pos, attrEnv, qual, name, args, List.nil()); |
10 | 803 |
} |
804 |
||
6029 | 805 |
/** Anon inner classes are used as access constructor tags. |
806 |
* accessConstructorTag will use an existing anon class if one is available, |
|
807 |
* and synthethise a class (with makeEmptyClass) if one is not available. |
|
808 |
* However, there is a small possibility that an existing class will not |
|
809 |
* be generated as expected if it is inside a conditional with a constant |
|
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
810 |
* expression. If that is found to be the case, create an empty class tree here. |
6029 | 811 |
*/ |
812 |
private void checkAccessConstructorTags() { |
|
813 |
for (List<ClassSymbol> l = accessConstrTags; l.nonEmpty(); l = l.tail) { |
|
814 |
ClassSymbol c = l.head; |
|
815 |
if (isTranslatedClassAvailable(c)) |
|
816 |
continue; |
|
817 |
// Create class definition tree. |
|
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
818 |
JCClassDecl cdec = makeEmptyClass(STATIC | SYNTHETIC, |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
819 |
c.outermostClass(), c.flatname, false); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
820 |
swapAccessConstructorTag(c, cdec.sym); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
821 |
translated.append(cdec); |
6029 | 822 |
} |
823 |
} |
|
824 |
// where |
|
825 |
private boolean isTranslatedClassAvailable(ClassSymbol c) { |
|
826 |
for (JCTree tree: translated) { |
|
10950 | 827 |
if (tree.hasTag(CLASSDEF) |
6029 | 828 |
&& ((JCClassDecl) tree).sym == c) { |
829 |
return true; |
|
830 |
} |
|
831 |
} |
|
832 |
return false; |
|
833 |
} |
|
834 |
||
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
835 |
void swapAccessConstructorTag(ClassSymbol oldCTag, ClassSymbol newCTag) { |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
836 |
for (MethodSymbol methodSymbol : accessConstrs.values()) { |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
837 |
Assert.check(methodSymbol.type.hasTag(METHOD)); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
838 |
MethodType oldMethodType = |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
839 |
(MethodType)methodSymbol.type; |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
840 |
if (oldMethodType.argtypes.head.tsym == oldCTag) |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
841 |
methodSymbol.type = |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
842 |
types.createMethodTypeWithParameters(oldMethodType, |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
843 |
oldMethodType.getParameterTypes().tail |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
844 |
.prepend(newCTag.erasure(types))); |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
845 |
} |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
846 |
} |
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
847 |
|
10 | 848 |
/************************************************************************** |
849 |
* Access methods |
|
850 |
*************************************************************************/ |
|
851 |
||
852 |
/** A mapping from symbols to their access numbers. |
|
853 |
*/ |
|
854 |
private Map<Symbol,Integer> accessNums; |
|
855 |
||
856 |
/** A mapping from symbols to an array of access symbols, indexed by |
|
857 |
* access code. |
|
858 |
*/ |
|
859 |
private Map<Symbol,MethodSymbol[]> accessSyms; |
|
860 |
||
861 |
/** A mapping from (constructor) symbols to access constructor symbols. |
|
862 |
*/ |
|
863 |
private Map<Symbol,MethodSymbol> accessConstrs; |
|
864 |
||
6029 | 865 |
/** A list of all class symbols used for access constructor tags. |
866 |
*/ |
|
867 |
private List<ClassSymbol> accessConstrTags; |
|
868 |
||
10 | 869 |
/** A queue for all accessed symbols. |
870 |
*/ |
|
871 |
private ListBuffer<Symbol> accessed; |
|
872 |
||
873 |
/** return access code for identifier, |
|
874 |
* @param tree The tree representing the identifier use. |
|
875 |
* @param enclOp The closest enclosing operation node of tree, |
|
876 |
* null if tree is not a subtree of an operation. |
|
877 |
*/ |
|
878 |
private static int accessCode(JCTree tree, JCTree enclOp) { |
|
879 |
if (enclOp == null) |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
880 |
return AccessCode.DEREF.code; |
10950 | 881 |
else if (enclOp.hasTag(ASSIGN) && |
10 | 882 |
tree == TreeInfo.skipParens(((JCAssign) enclOp).lhs)) |
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
883 |
return AccessCode.ASSIGN.code; |
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
39920
diff
changeset
|
884 |
else if ((enclOp.getTag().isIncOrDecUnaryOp() || enclOp.getTag().isAssignop()) && |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
39920
diff
changeset
|
885 |
tree == TreeInfo.skipParens(((JCOperatorExpression) enclOp).getOperand(LEFT))) |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
39920
diff
changeset
|
886 |
return (((JCOperatorExpression) enclOp).operator).getAccessCode(enclOp.getTag()); |
10 | 887 |
else |
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
888 |
return AccessCode.DEREF.code; |
10 | 889 |
} |
890 |
||
891 |
/** Return binary operator that corresponds to given access code. |
|
892 |
*/ |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
893 |
private OperatorSymbol binaryAccessOperator(int acode, Tag tag) { |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
894 |
return operators.lookupBinaryOp(op -> op.getAccessCode(tag) == acode); |
10 | 895 |
} |
896 |
||
897 |
/** Return tree tag for assignment operation corresponding |
|
898 |
* to given binary operator. |
|
899 |
*/ |
|
10950 | 900 |
private static JCTree.Tag treeTag(OperatorSymbol operator) { |
10 | 901 |
switch (operator.opcode) { |
902 |
case ByteCodes.ior: case ByteCodes.lor: |
|
10950 | 903 |
return BITOR_ASG; |
10 | 904 |
case ByteCodes.ixor: case ByteCodes.lxor: |
10950 | 905 |
return BITXOR_ASG; |
10 | 906 |
case ByteCodes.iand: case ByteCodes.land: |
10950 | 907 |
return BITAND_ASG; |
10 | 908 |
case ByteCodes.ishl: case ByteCodes.lshl: |
909 |
case ByteCodes.ishll: case ByteCodes.lshll: |
|
10950 | 910 |
return SL_ASG; |
10 | 911 |
case ByteCodes.ishr: case ByteCodes.lshr: |
912 |
case ByteCodes.ishrl: case ByteCodes.lshrl: |
|
10950 | 913 |
return SR_ASG; |
10 | 914 |
case ByteCodes.iushr: case ByteCodes.lushr: |
915 |
case ByteCodes.iushrl: case ByteCodes.lushrl: |
|
10950 | 916 |
return USR_ASG; |
10 | 917 |
case ByteCodes.iadd: case ByteCodes.ladd: |
918 |
case ByteCodes.fadd: case ByteCodes.dadd: |
|
919 |
case ByteCodes.string_add: |
|
10950 | 920 |
return PLUS_ASG; |
10 | 921 |
case ByteCodes.isub: case ByteCodes.lsub: |
922 |
case ByteCodes.fsub: case ByteCodes.dsub: |
|
10950 | 923 |
return MINUS_ASG; |
10 | 924 |
case ByteCodes.imul: case ByteCodes.lmul: |
925 |
case ByteCodes.fmul: case ByteCodes.dmul: |
|
10950 | 926 |
return MUL_ASG; |
10 | 927 |
case ByteCodes.idiv: case ByteCodes.ldiv: |
928 |
case ByteCodes.fdiv: case ByteCodes.ddiv: |
|
10950 | 929 |
return DIV_ASG; |
10 | 930 |
case ByteCodes.imod: case ByteCodes.lmod: |
931 |
case ByteCodes.fmod: case ByteCodes.dmod: |
|
10950 | 932 |
return MOD_ASG; |
10 | 933 |
default: |
934 |
throw new AssertionError(); |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
/** The name of the access method with number `anum' and access code `acode'. |
|
939 |
*/ |
|
940 |
Name accessName(int anum, int acode) { |
|
941 |
return names.fromString( |
|
942 |
"access" + target.syntheticNameChar() + anum + acode / 10 + acode % 10); |
|
943 |
} |
|
944 |
||
945 |
/** Return access symbol for a private or protected symbol from an inner class. |
|
946 |
* @param sym The accessed private symbol. |
|
947 |
* @param tree The accessing tree. |
|
948 |
* @param enclOp The closest enclosing operation node of tree, |
|
949 |
* null if tree is not a subtree of an operation. |
|
950 |
* @param protAccess Is access to a protected symbol in another |
|
951 |
* package? |
|
952 |
* @param refSuper Is access via a (qualified) C.super? |
|
953 |
*/ |
|
954 |
MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp, |
|
955 |
boolean protAccess, boolean refSuper) { |
|
956 |
ClassSymbol accOwner = refSuper && protAccess |
|
957 |
// For access via qualified super (T.super.x), place the |
|
958 |
// access symbol on T. |
|
959 |
? (ClassSymbol)((JCFieldAccess) tree).selected.type.tsym |
|
960 |
// Otherwise pretend that the owner of an accessed |
|
961 |
// protected symbol is the enclosing class of the current |
|
962 |
// class which is a subclass of the symbol's owner. |
|
963 |
: accessClass(sym, protAccess, tree); |
|
964 |
||
965 |
Symbol vsym = sym; |
|
966 |
if (sym.owner != accOwner) { |
|
967 |
vsym = sym.clone(accOwner); |
|
968 |
actualSymbols.put(vsym, sym); |
|
969 |
} |
|
970 |
||
971 |
Integer anum // The access number of the access method. |
|
972 |
= accessNums.get(vsym); |
|
973 |
if (anum == null) { |
|
974 |
anum = accessed.length(); |
|
975 |
accessNums.put(vsym, anum); |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
976 |
accessSyms.put(vsym, new MethodSymbol[AccessCode.numberOfAccessCodes]); |
10 | 977 |
accessed.append(vsym); |
978 |
// System.out.println("accessing " + vsym + " in " + vsym.location()); |
|
979 |
} |
|
980 |
||
981 |
int acode; // The access code of the access method. |
|
982 |
List<Type> argtypes; // The argument types of the access method. |
|
983 |
Type restype; // The result type of the access method. |
|
4142 | 984 |
List<Type> thrown; // The thrown exceptions of the access method. |
10 | 985 |
switch (vsym.kind) { |
986 |
case VAR: |
|
987 |
acode = accessCode(tree, enclOp); |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
988 |
if (acode >= AccessCode.FIRSTASGOP.code) { |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
989 |
OperatorSymbol operator = binaryAccessOperator(acode, enclOp.getTag()); |
10 | 990 |
if (operator.opcode == string_add) |
991 |
argtypes = List.of(syms.objectType); |
|
992 |
else |
|
993 |
argtypes = operator.type.getParameterTypes().tail; |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
994 |
} else if (acode == AccessCode.ASSIGN.code) |
10 | 995 |
argtypes = List.of(vsym.erasure(types)); |
996 |
else |
|
997 |
argtypes = List.nil(); |
|
998 |
restype = vsym.erasure(types); |
|
999 |
thrown = List.nil(); |
|
1000 |
break; |
|
1001 |
case MTH: |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1002 |
acode = AccessCode.DEREF.code; |
10 | 1003 |
argtypes = vsym.erasure(types).getParameterTypes(); |
1004 |
restype = vsym.erasure(types).getReturnType(); |
|
1005 |
thrown = vsym.type.getThrownTypes(); |
|
1006 |
break; |
|
1007 |
default: |
|
1008 |
throw new AssertionError(); |
|
1009 |
} |
|
1010 |
||
1011 |
// For references via qualified super, increment acode by one, |
|
1012 |
// making it odd. |
|
1013 |
if (protAccess && refSuper) acode++; |
|
1014 |
||
1015 |
// Instance access methods get instance as first parameter. |
|
1016 |
// For protected symbols this needs to be the instance as a member |
|
1017 |
// of the type containing the accessed symbol, not the class |
|
1018 |
// containing the access method. |
|
1019 |
if ((vsym.flags() & STATIC) == 0) { |
|
1020 |
argtypes = argtypes.prepend(vsym.owner.erasure(types)); |
|
1021 |
} |
|
1022 |
MethodSymbol[] accessors = accessSyms.get(vsym); |
|
1023 |
MethodSymbol accessor = accessors[acode]; |
|
1024 |
if (accessor == null) { |
|
1025 |
accessor = new MethodSymbol( |
|
29293
1583c6dd6df7
8071453: Allow interface methods to be private
sadayapalam
parents:
29292
diff
changeset
|
1026 |
STATIC | SYNTHETIC | (accOwner.isInterface() ? PUBLIC : 0), |
10 | 1027 |
accessName(anum.intValue(), acode), |
1028 |
new MethodType(argtypes, restype, thrown, syms.methodClass), |
|
1029 |
accOwner); |
|
1030 |
enterSynthetic(tree.pos(), accessor, accOwner.members()); |
|
1031 |
accessors[acode] = accessor; |
|
1032 |
} |
|
1033 |
return accessor; |
|
1034 |
} |
|
1035 |
||
1036 |
/** The qualifier to be used for accessing a symbol in an outer class. |
|
1037 |
* This is either C.sym or C.this.sym, depending on whether or not |
|
1038 |
* sym is static. |
|
1039 |
* @param sym The accessed symbol. |
|
1040 |
*/ |
|
1041 |
JCExpression accessBase(DiagnosticPosition pos, Symbol sym) { |
|
1042 |
return (sym.flags() & STATIC) != 0 |
|
1043 |
? access(make.at(pos.getStartPosition()).QualIdent(sym.owner)) |
|
1044 |
: makeOwnerThis(pos, sym, true); |
|
1045 |
} |
|
1046 |
||
1047 |
/** Do we need an access method to reference private symbol? |
|
1048 |
*/ |
|
1049 |
boolean needsPrivateAccess(Symbol sym) { |
|
50735
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
1050 |
if (target.hasNestmateAccess()) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
1051 |
return false; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
1052 |
} |
10 | 1053 |
if ((sym.flags() & PRIVATE) == 0 || sym.owner == currentClass) { |
1054 |
return false; |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
1055 |
} else if (sym.name == names.init && sym.owner.isLocal()) { |
10 | 1056 |
// private constructor in local class: relax protection |
1057 |
sym.flags_field &= ~PRIVATE; |
|
1058 |
return false; |
|
1059 |
} else { |
|
1060 |
return true; |
|
1061 |
} |
|
1062 |
} |
|
1063 |
||
1064 |
/** Do we need an access method to reference symbol in other package? |
|
1065 |
*/ |
|
1066 |
boolean needsProtectedAccess(Symbol sym, JCTree tree) { |
|
50735
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
50384
diff
changeset
|
1067 |
if (disableProtectedAccessors) return false; |
10 | 1068 |
if ((sym.flags() & PROTECTED) == 0 || |
1069 |
sym.owner.owner == currentClass.owner || // fast special case |
|
1070 |
sym.packge() == currentClass.packge()) |
|
1071 |
return false; |
|
1072 |
if (!currentClass.isSubClass(sym.owner, types)) |
|
1073 |
return true; |
|
1074 |
if ((sym.flags() & STATIC) != 0 || |
|
10950 | 1075 |
!tree.hasTag(SELECT) || |
10 | 1076 |
TreeInfo.name(((JCFieldAccess) tree).selected) == names._super) |
1077 |
return false; |
|
1078 |
return !((JCFieldAccess) tree).selected.type.tsym.isSubClass(currentClass, types); |
|
1079 |
} |
|
1080 |
||
1081 |
/** The class in which an access method for given symbol goes. |
|
1082 |
* @param sym The access symbol |
|
1083 |
* @param protAccess Is access to a protected symbol in another |
|
1084 |
* package? |
|
1085 |
*/ |
|
1086 |
ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) { |
|
1087 |
if (protAccess) { |
|
1088 |
Symbol qualifier = null; |
|
1089 |
ClassSymbol c = currentClass; |
|
10950 | 1090 |
if (tree.hasTag(SELECT) && (sym.flags() & STATIC) == 0) { |
10 | 1091 |
qualifier = ((JCFieldAccess) tree).selected.type.tsym; |
1092 |
while (!qualifier.isSubClass(c, types)) { |
|
1093 |
c = c.owner.enclClass(); |
|
1094 |
} |
|
1095 |
return c; |
|
1096 |
} else { |
|
1097 |
while (!c.isSubClass(sym.owner, types)) { |
|
1098 |
c = c.owner.enclClass(); |
|
1099 |
} |
|
1100 |
} |
|
1101 |
return c; |
|
1102 |
} else { |
|
1103 |
// the symbol is private |
|
1104 |
return sym.owner.enclClass(); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
||
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1108 |
private void addPrunedInfo(JCTree tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1109 |
List<JCTree> infoList = prunedTree.get(currentClass); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1110 |
infoList = (infoList == null) ? List.of(tree) : infoList.prepend(tree); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1111 |
prunedTree.put(currentClass, infoList); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1112 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1113 |
|
10 | 1114 |
/** Ensure that identifier is accessible, return tree accessing the identifier. |
1115 |
* @param sym The accessed symbol. |
|
1116 |
* @param tree The tree referring to the symbol. |
|
1117 |
* @param enclOp The closest enclosing operation node of tree, |
|
1118 |
* null if tree is not a subtree of an operation. |
|
1119 |
* @param refSuper Is access via a (qualified) C.super? |
|
1120 |
*/ |
|
1121 |
JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean refSuper) { |
|
1122 |
// Access a free variable via its proxy, or its proxy's proxy |
|
1123 |
while (sym.kind == VAR && sym.owner.kind == MTH && |
|
1124 |
sym.owner.enclClass() != currentClass) { |
|
1125 |
// A constant is replaced by its constant value. |
|
1126 |
Object cv = ((VarSymbol)sym).getConstValue(); |
|
1127 |
if (cv != null) { |
|
1128 |
make.at(tree.pos); |
|
1129 |
return makeLit(sym.type, cv); |
|
1130 |
} |
|
52038
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1131 |
if (lambdaTranslationMap != null && lambdaTranslationMap.get(sym) != null) { |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1132 |
return make.at(tree.pos).Ident(lambdaTranslationMap.get(sym)); |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1133 |
} else { |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1134 |
// Otherwise replace the variable by its proxy. |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1135 |
sym = proxies.get(sym); |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1136 |
Assert.check(sym != null && (sym.flags_field & FINAL) != 0); |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1137 |
tree = make.at(tree.pos).Ident(sym); |
957de5be48bc
8209407: VerifyError is thrown for inner class with lambda
vromero
parents:
51757
diff
changeset
|
1138 |
} |
10 | 1139 |
} |
10950 | 1140 |
JCExpression base = (tree.hasTag(SELECT)) ? ((JCFieldAccess) tree).selected : null; |
10 | 1141 |
switch (sym.kind) { |
1142 |
case TYP: |
|
1143 |
if (sym.owner.kind != PCK) { |
|
1144 |
// Convert type idents to |
|
1145 |
// <flat name> or <package name> . <flat name> |
|
1146 |
Name flatname = Convert.shortName(sym.flatName()); |
|
1147 |
while (base != null && |
|
1148 |
TreeInfo.symbol(base) != null && |
|
1149 |
TreeInfo.symbol(base).kind != PCK) { |
|
10950 | 1150 |
base = (base.hasTag(SELECT)) |
10 | 1151 |
? ((JCFieldAccess) base).selected |
1152 |
: null; |
|
1153 |
} |
|
10950 | 1154 |
if (tree.hasTag(IDENT)) { |
10 | 1155 |
((JCIdent) tree).name = flatname; |
1156 |
} else if (base == null) { |
|
1157 |
tree = make.at(tree.pos).Ident(sym); |
|
1158 |
((JCIdent) tree).name = flatname; |
|
1159 |
} else { |
|
1160 |
((JCFieldAccess) tree).selected = base; |
|
1161 |
((JCFieldAccess) tree).name = flatname; |
|
1162 |
} |
|
1163 |
} |
|
1164 |
break; |
|
1165 |
case MTH: case VAR: |
|
1166 |
if (sym.owner.kind == TYP) { |
|
1167 |
||
1168 |
// Access methods are required for |
|
1169 |
// - private members, |
|
1170 |
// - protected members in a superclass of an |
|
1171 |
// enclosing class contained in another package. |
|
1172 |
// - all non-private members accessed via a qualified super. |
|
1173 |
boolean protAccess = refSuper && !needsPrivateAccess(sym) |
|
1174 |
|| needsProtectedAccess(sym, tree); |
|
1175 |
boolean accReq = protAccess || needsPrivateAccess(sym); |
|
1176 |
||
1177 |
// A base has to be supplied for |
|
1178 |
// - simple identifiers accessing variables in outer classes. |
|
1179 |
boolean baseReq = |
|
1180 |
base == null && |
|
1181 |
sym.owner != syms.predefClass && |
|
1182 |
!sym.isMemberOf(currentClass, types); |
|
1183 |
||
1184 |
if (accReq || baseReq) { |
|
1185 |
make.at(tree.pos); |
|
1186 |
||
1187 |
// Constants are replaced by their constant value. |
|
1188 |
if (sym.kind == VAR) { |
|
1189 |
Object cv = ((VarSymbol)sym).getConstValue(); |
|
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1190 |
if (cv != null) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1191 |
addPrunedInfo(tree); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1192 |
return makeLit(sym.type, cv); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
1193 |
} |
10 | 1194 |
} |
1195 |
||
1196 |
// Private variables and methods are replaced by calls |
|
1197 |
// to their access methods. |
|
1198 |
if (accReq) { |
|
1199 |
List<JCExpression> args = List.nil(); |
|
1200 |
if ((sym.flags() & STATIC) == 0) { |
|
1201 |
// Instance access methods get instance |
|
1202 |
// as first parameter. |
|
1203 |
if (base == null) |
|
1204 |
base = makeOwnerThis(tree.pos(), sym, true); |
|
1205 |
args = args.prepend(base); |
|
1206 |
base = null; // so we don't duplicate code |
|
1207 |
} |
|
1208 |
Symbol access = accessSymbol(sym, tree, |
|
1209 |
enclOp, protAccess, |
|
1210 |
refSuper); |
|
1211 |
JCExpression receiver = make.Select( |
|
1212 |
base != null ? base : make.QualIdent(access.owner), |
|
1213 |
access); |
|
1214 |
return make.App(receiver, args); |
|
1215 |
||
1216 |
// Other accesses to members of outer classes get a |
|
1217 |
// qualifier. |
|
1218 |
} else if (baseReq) { |
|
1219 |
return make.at(tree.pos).Select( |
|
1220 |
accessBase(tree.pos(), sym), sym).setType(tree.type); |
|
1221 |
} |
|
1222 |
} |
|
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1223 |
} else if (sym.owner.kind == MTH && lambdaTranslationMap != null) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1224 |
//sym is a local variable - check the lambda translation map to |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1225 |
//see if sym has been translated to something else in the current |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1226 |
//scope (by LambdaToMethod) |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1227 |
Symbol translatedSym = lambdaTranslationMap.get(sym); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1228 |
if (translatedSym != null) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1229 |
tree = make.at(tree.pos).Ident(translatedSym); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
1230 |
} |
10 | 1231 |
} |
1232 |
} |
|
1233 |
return tree; |
|
1234 |
} |
|
1235 |
||
1236 |
/** Ensure that identifier is accessible, return tree accessing the identifier. |
|
1237 |
* @param tree The identifier tree. |
|
1238 |
*/ |
|
1239 |
JCExpression access(JCExpression tree) { |
|
1240 |
Symbol sym = TreeInfo.symbol(tree); |
|
1241 |
return sym == null ? tree : access(sym, tree, null, false); |
|
1242 |
} |
|
1243 |
||
1244 |
/** Return access constructor for a private constructor, |
|
1245 |
* or the constructor itself, if no access constructor is needed. |
|
1246 |
* @param pos The position to report diagnostics, if any. |
|
1247 |
* @param constr The private constructor. |
|
1248 |
*/ |
|
1249 |
Symbol accessConstructor(DiagnosticPosition pos, Symbol constr) { |
|
1250 |
if (needsPrivateAccess(constr)) { |
|
1251 |
ClassSymbol accOwner = constr.owner.enclClass(); |
|
1252 |
MethodSymbol aconstr = accessConstrs.get(constr); |
|
1253 |
if (aconstr == null) { |
|
1254 |
List<Type> argtypes = constr.type.getParameterTypes(); |
|
1255 |
if ((accOwner.flags_field & ENUM) != 0) |
|
1256 |
argtypes = argtypes |
|
1257 |
.prepend(syms.intType) |
|
1258 |
.prepend(syms.stringType); |
|
1259 |
aconstr = new MethodSymbol( |
|
1260 |
SYNTHETIC, |
|
1261 |
names.init, |
|
1262 |
new MethodType( |
|
1263 |
argtypes.append( |
|
1264 |
accessConstructorTag().erasure(types)), |
|
1265 |
constr.type.getReturnType(), |
|
1266 |
constr.type.getThrownTypes(), |
|
1267 |
syms.methodClass), |
|
1268 |
accOwner); |
|
1269 |
enterSynthetic(pos, aconstr, accOwner.members()); |
|
1270 |
accessConstrs.put(constr, aconstr); |
|
1271 |
accessed.append(constr); |
|
1272 |
} |
|
1273 |
return aconstr; |
|
1274 |
} else { |
|
1275 |
return constr; |
|
1276 |
} |
|
1277 |
} |
|
1278 |
||
1279 |
/** Return an anonymous class nested in this toplevel class. |
|
1280 |
*/ |
|
1281 |
ClassSymbol accessConstructorTag() { |
|
1282 |
ClassSymbol topClass = currentClass.outermostClass(); |
|
36526 | 1283 |
ModuleSymbol topModle = topClass.packge().modle; |
50384
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1284 |
for (int i = 1; ; i++) { |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1285 |
Name flatname = names.fromString("" + topClass.getQualifiedName() + |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1286 |
target.syntheticNameChar() + |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1287 |
i); |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1288 |
ClassSymbol ctag = chk.getCompiled(topModle, flatname); |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1289 |
if (ctag == null) |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1290 |
ctag = makeEmptyClass(STATIC | SYNTHETIC, topClass).sym; |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1291 |
else if (!ctag.isAnonymous()) |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1292 |
continue; |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1293 |
// keep a record of all tags, to verify that all are generated as required |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1294 |
accessConstrTags = accessConstrTags.prepend(ctag); |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1295 |
return ctag; |
4d03087dd257
8189335: javac exception when referencing private inner class with same name as normal class with dollar_1
bsrbnd
parents:
50213
diff
changeset
|
1296 |
} |
10 | 1297 |
} |
1298 |
||
1299 |
/** Add all required access methods for a private symbol to enclosing class. |
|
1300 |
* @param sym The symbol. |
|
1301 |
*/ |
|
1302 |
void makeAccessible(Symbol sym) { |
|
1303 |
JCClassDecl cdef = classDef(sym.owner.enclClass()); |
|
8032 | 1304 |
if (cdef == null) Assert.error("class def not found: " + sym + " in " + sym.owner); |
10 | 1305 |
if (sym.name == names.init) { |
1306 |
cdef.defs = cdef.defs.prepend( |
|
1307 |
accessConstructorDef(cdef.pos, sym, accessConstrs.get(sym))); |
|
1308 |
} else { |
|
1309 |
MethodSymbol[] accessors = accessSyms.get(sym); |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1310 |
for (int i = 0; i < AccessCode.numberOfAccessCodes; i++) { |
10 | 1311 |
if (accessors[i] != null) |
1312 |
cdef.defs = cdef.defs.prepend( |
|
1313 |
accessDef(cdef.pos, sym, accessors[i], i)); |
|
1314 |
} |
|
1315 |
} |
|
1316 |
} |
|
1317 |
||
1318 |
/** Construct definition of an access method. |
|
1319 |
* @param pos The source code position of the definition. |
|
1320 |
* @param vsym The private or protected symbol. |
|
1321 |
* @param accessor The access method for the symbol. |
|
1322 |
* @param acode The access code. |
|
1323 |
*/ |
|
1324 |
JCTree accessDef(int pos, Symbol vsym, MethodSymbol accessor, int acode) { |
|
1325 |
// System.err.println("access " + vsym + " with " + accessor);//DEBUG |
|
1326 |
currentClass = vsym.owner.enclClass(); |
|
1327 |
make.at(pos); |
|
1328 |
JCMethodDecl md = make.MethodDef(accessor, null); |
|
1329 |
||
1330 |
// Find actual symbol |
|
1331 |
Symbol sym = actualSymbols.get(vsym); |
|
1332 |
if (sym == null) sym = vsym; |
|
1333 |
||
1334 |
JCExpression ref; // The tree referencing the private symbol. |
|
1335 |
List<JCExpression> args; // Any additional arguments to be passed along. |
|
1336 |
if ((sym.flags() & STATIC) != 0) { |
|
1337 |
ref = make.Ident(sym); |
|
1338 |
args = make.Idents(md.params); |
|
1339 |
} else { |
|
21897
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1340 |
JCExpression site = make.Ident(md.params.head); |
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1341 |
if (acode % 2 != 0) { |
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1342 |
//odd access codes represent qualified super accesses - need to |
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1343 |
//emit reference to the direct superclass, even if the refered |
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1344 |
//member is from an indirect superclass (JLS 13.1) |
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1345 |
site.setType(types.erasure(types.supertype(vsym.owner.enclClass().type))); |
21708
9642be503906
8027281: Incorrect invokespecial generated for JCK lang EXPR/expr636/expr63602m* tests
jlahoda
parents:
21041
diff
changeset
|
1346 |
} |
21897
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21708
diff
changeset
|
1347 |
ref = make.Select(site, sym); |
10 | 1348 |
args = make.Idents(md.params.tail); |
1349 |
} |
|
1350 |
JCStatement stat; // The statement accessing the private symbol. |
|
1351 |
if (sym.kind == VAR) { |
|
1352 |
// Normalize out all odd access codes by taking floor modulo 2: |
|
1353 |
int acode1 = acode - (acode & 1); |
|
1354 |
||
1355 |
JCExpression expr; // The access method's return value. |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1356 |
AccessCode aCode = AccessCode.getFromCode(acode1); |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1357 |
switch (aCode) { |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1358 |
case DEREF: |
10 | 1359 |
expr = ref; |
1360 |
break; |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1361 |
case ASSIGN: |
10 | 1362 |
expr = make.Assign(ref, args.head); |
1363 |
break; |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1364 |
case PREINC: case POSTINC: case PREDEC: case POSTDEC: |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1365 |
expr = makeUnary(aCode.tag, ref); |
10 | 1366 |
break; |
1367 |
default: |
|
1368 |
expr = make.Assignop( |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1369 |
treeTag(binaryAccessOperator(acode1, JCTree.Tag.NO_TAG)), ref, args.head); |
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
1370 |
((JCAssignOp) expr).operator = binaryAccessOperator(acode1, JCTree.Tag.NO_TAG); |
10 | 1371 |
} |
1372 |
stat = make.Return(expr.setType(sym.type)); |
|
1373 |
} else { |
|
1374 |
stat = make.Call(make.App(ref, args)); |
|
1375 |
} |
|
1376 |
md.body = make.Block(0, List.of(stat)); |
|
1377 |
||
1378 |
// Make sure all parameters, result types and thrown exceptions |
|
1379 |
// are accessible. |
|
1380 |
for (List<JCVariableDecl> l = md.params; l.nonEmpty(); l = l.tail) |
|
1381 |
l.head.vartype = access(l.head.vartype); |
|
1382 |
md.restype = access(md.restype); |
|
1383 |
for (List<JCExpression> l = md.thrown; l.nonEmpty(); l = l.tail) |
|
1384 |
l.head = access(l.head); |
|
1385 |
||
1386 |
return md; |
|
1387 |
} |
|
1388 |
||
1389 |
/** Construct definition of an access constructor. |
|
1390 |
* @param pos The source code position of the definition. |
|
1391 |
* @param constr The private constructor. |
|
1392 |
* @param accessor The access method for the constructor. |
|
1393 |
*/ |
|
1394 |
JCTree accessConstructorDef(int pos, Symbol constr, MethodSymbol accessor) { |
|
1395 |
make.at(pos); |
|
1396 |
JCMethodDecl md = make.MethodDef(accessor, |
|
1397 |
accessor.externalType(types), |
|
1398 |
null); |
|
1399 |
JCIdent callee = make.Ident(names._this); |
|
1400 |
callee.sym = constr; |
|
1401 |
callee.type = constr.type; |
|
1402 |
md.body = |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
1403 |
make.Block(0, List.of( |
10 | 1404 |
make.Call( |
1405 |
make.App( |
|
1406 |
callee, |
|
1407 |
make.Idents(md.params.reverse().tail.reverse()))))); |
|
1408 |
return md; |
|
1409 |
} |
|
1410 |
||
1411 |
/************************************************************************** |
|
1412 |
* Free variables proxies and this$n |
|
1413 |
*************************************************************************/ |
|
1414 |
||
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1415 |
/** A map which allows to retrieve the translated proxy variable for any given symbol of an |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1416 |
* enclosing scope that is accessed (the accessed symbol could be the synthetic 'this$n' symbol). |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1417 |
* Inside a constructor, the map temporarily overrides entries corresponding to proxies and any |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1418 |
* 'this$n' symbols, where they represent the constructor parameters. |
10 | 1419 |
*/ |
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1420 |
Map<Symbol, Symbol> proxies; |
10 | 1421 |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1422 |
/** A scope containing all unnamed resource variables/saved |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1423 |
* exception variables for translated TWR blocks |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1424 |
*/ |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1425 |
WriteableScope twrVars; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1426 |
|
10 | 1427 |
/** A stack containing the this$n field of the currently translated |
1428 |
* classes (if needed) in innermost first order. |
|
1429 |
* Inside a constructor, proxies and any this$n symbol are duplicated |
|
1430 |
* in an additional innermost scope, where they represent the constructor |
|
1431 |
* parameters. |
|
1432 |
*/ |
|
1433 |
List<VarSymbol> outerThisStack; |
|
1434 |
||
1435 |
/** The name of a free variable proxy. |
|
1436 |
*/ |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1437 |
Name proxyName(Name name, int index) { |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1438 |
Name proxyName = names.fromString("val" + target.syntheticNameChar() + name); |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1439 |
if (index > 0) { |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1440 |
proxyName = proxyName.append(names.fromString("" + target.syntheticNameChar() + index)); |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1441 |
} |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1442 |
return proxyName; |
10 | 1443 |
} |
1444 |
||
1445 |
/** Proxy definitions for all free variables in given list, in reverse order. |
|
1446 |
* @param pos The source code position of the definition. |
|
1447 |
* @param freevars The free variables. |
|
1448 |
* @param owner The class in which the definitions go. |
|
1449 |
*/ |
|
1450 |
List<JCVariableDecl> freevarDefs(int pos, List<VarSymbol> freevars, Symbol owner) { |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1451 |
return freevarDefs(pos, freevars, owner, 0); |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1452 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1453 |
|
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1454 |
List<JCVariableDecl> freevarDefs(int pos, List<VarSymbol> freevars, Symbol owner, |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1455 |
long additionalFlags) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1456 |
long flags = FINAL | SYNTHETIC | additionalFlags; |
10 | 1457 |
List<JCVariableDecl> defs = List.nil(); |
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1458 |
Set<Name> proxyNames = new HashSet<>(); |
10 | 1459 |
for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail) { |
1460 |
VarSymbol v = l.head; |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1461 |
int index = 0; |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1462 |
Name proxyName; |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1463 |
do { |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1464 |
proxyName = proxyName(v.name, index++); |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1465 |
} while (!proxyNames.add(proxyName)); |
10 | 1466 |
VarSymbol proxy = new VarSymbol( |
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1467 |
flags, proxyName, v.erasure(types), owner); |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1468 |
proxies.put(v, proxy); |
10 | 1469 |
JCVariableDecl vd = make.at(pos).VarDef(proxy, null); |
1470 |
vd.vartype = access(vd.vartype); |
|
1471 |
defs = defs.prepend(vd); |
|
1472 |
} |
|
1473 |
return defs; |
|
1474 |
} |
|
1475 |
||
1476 |
/** The name of a this$n field |
|
1477 |
* @param type The class referenced by the this$n field |
|
1478 |
*/ |
|
1479 |
Name outerThisName(Type type, Symbol owner) { |
|
1480 |
Type t = type.getEnclosingType(); |
|
1481 |
int nestingLevel = 0; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
1482 |
while (t.hasTag(CLASS)) { |
10 | 1483 |
t = t.getEnclosingType(); |
1484 |
nestingLevel++; |
|
1485 |
} |
|
1486 |
Name result = names.fromString("this" + target.syntheticNameChar() + nestingLevel); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1487 |
while (owner.kind == TYP && ((ClassSymbol)owner).members().findFirst(result) != null) |
10 | 1488 |
result = names.fromString(result.toString() + target.syntheticNameChar()); |
1489 |
return result; |
|
1490 |
} |
|
1491 |
||
15720
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1492 |
private VarSymbol makeOuterThisVarSymbol(Symbol owner, long flags) { |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1493 |
Type target = types.erasure(owner.enclClass().type.getEnclosingType()); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1494 |
VarSymbol outerThis = |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1495 |
new VarSymbol(flags, outerThisName(target, owner), target, owner); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1496 |
outerThisStack = outerThisStack.prepend(outerThis); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1497 |
return outerThis; |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1498 |
} |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1499 |
|
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1500 |
private JCVariableDecl makeOuterThisVarDecl(int pos, VarSymbol sym) { |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1501 |
JCVariableDecl vd = make.at(pos).VarDef(sym, null); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1502 |
vd.vartype = access(vd.vartype); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1503 |
return vd; |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1504 |
} |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1505 |
|
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1506 |
/** Definition for this$n field. |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1507 |
* @param pos The source code position of the definition. |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1508 |
* @param owner The method in which the definition goes. |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1509 |
*/ |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1510 |
JCVariableDecl outerThisDef(int pos, MethodSymbol owner) { |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1511 |
ClassSymbol c = owner.enclClass(); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1512 |
boolean isMandated = |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1513 |
// Anonymous constructors |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1514 |
(owner.isConstructor() && owner.isAnonymous()) || |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1515 |
// Constructors of non-private inner member classes |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1516 |
(owner.isConstructor() && c.isInner() && |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1517 |
!c.isPrivate() && !c.isStatic()); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1518 |
long flags = |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1519 |
FINAL | (isMandated ? MANDATED : SYNTHETIC) | PARAMETER; |
15720
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1520 |
VarSymbol outerThis = makeOuterThisVarSymbol(owner, flags); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1521 |
owner.extraParams = owner.extraParams.prepend(outerThis); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1522 |
return makeOuterThisVarDecl(pos, outerThis); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1523 |
} |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1524 |
|
10 | 1525 |
/** Definition for this$n field. |
1526 |
* @param pos The source code position of the definition. |
|
1527 |
* @param owner The class in which the definition goes. |
|
1528 |
*/ |
|
15720
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1529 |
JCVariableDecl outerThisDef(int pos, ClassSymbol owner) { |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1530 |
VarSymbol outerThis = makeOuterThisVarSymbol(owner, FINAL | SYNTHETIC); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
1531 |
return makeOuterThisVarDecl(pos, outerThis); |
10 | 1532 |
} |
1533 |
||
1534 |
/** Return a list of trees that load the free variables in given list, |
|
1535 |
* in reverse order. |
|
1536 |
* @param pos The source code position to be used for the trees. |
|
1537 |
* @param freevars The list of free variables. |
|
1538 |
*/ |
|
1539 |
List<JCExpression> loadFreevars(DiagnosticPosition pos, List<VarSymbol> freevars) { |
|
1540 |
List<JCExpression> args = List.nil(); |
|
1541 |
for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail) |
|
1542 |
args = args.prepend(loadFreevar(pos, l.head)); |
|
1543 |
return args; |
|
1544 |
} |
|
1545 |
//where |
|
1546 |
JCExpression loadFreevar(DiagnosticPosition pos, VarSymbol v) { |
|
1547 |
return access(v, make.at(pos).Ident(v), null, false); |
|
1548 |
} |
|
1549 |
||
13844 | 1550 |
/** Construct a tree simulating the expression {@code C.this}. |
10 | 1551 |
* @param pos The source code position to be used for the tree. |
1552 |
* @param c The qualifier class. |
|
1553 |
*/ |
|
1554 |
JCExpression makeThis(DiagnosticPosition pos, TypeSymbol c) { |
|
1555 |
if (currentClass == c) { |
|
1556 |
// in this case, `this' works fine |
|
1557 |
return make.at(pos).This(c.erasure(types)); |
|
1558 |
} else { |
|
1559 |
// need to go via this$n |
|
1560 |
return makeOuterThis(pos, c); |
|
1561 |
} |
|
1562 |
} |
|
1563 |
||
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1564 |
/** |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1565 |
* Optionally replace a try statement with the desugaring of a |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1566 |
* try-with-resources statement. The canonical desugaring of |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1567 |
* |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1568 |
* try ResourceSpecification |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1569 |
* Block |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1570 |
* |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1571 |
* is |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1572 |
* |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1573 |
* { |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1574 |
* final VariableModifiers_minus_final R #resource = Expression; |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1575 |
* |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1576 |
* try ResourceSpecificationtail |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1577 |
* Block |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1578 |
* } body-only-finally { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1579 |
* if (#resource != null) //nullcheck skipped if Expression is provably non-null |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1580 |
* #resource.close(); |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1581 |
* } catch (Throwable #primaryException) { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1582 |
* if (#resource != null) //nullcheck skipped if Expression is provably non-null |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1583 |
* try { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1584 |
* #resource.close(); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1585 |
* } catch (Throwable #suppressedException) { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1586 |
* #primaryException.addSuppressed(#suppressedException); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1587 |
* } |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1588 |
* throw #primaryException; |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1589 |
* } |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1590 |
* } |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1591 |
* |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1592 |
* @param tree The try statement to inspect. |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1593 |
* @return A a desugared try-with-resources tree, or the original |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1594 |
* try block if there are no resources to manage. |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1595 |
*/ |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1596 |
JCTree makeTwrTry(JCTry tree) { |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1597 |
make_at(tree.pos()); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1598 |
twrVars = twrVars.dup(); |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1599 |
JCBlock twrBlock = makeTwrBlock(tree.resources, tree.body, 0); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1600 |
if (tree.catchers.isEmpty() && tree.finalizer == null) |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1601 |
result = translate(twrBlock); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1602 |
else |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1603 |
result = translate(make.Try(twrBlock, tree.catchers, tree.finalizer)); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1604 |
twrVars = twrVars.leave(); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1605 |
return result; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1606 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1607 |
|
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1608 |
private JCBlock makeTwrBlock(List<JCTree> resources, JCBlock block, int depth) { |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1609 |
if (resources.isEmpty()) |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1610 |
return block; |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1611 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1612 |
// Add resource declaration or expression to block statements |
22163 | 1613 |
ListBuffer<JCStatement> stats = new ListBuffer<>(); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1614 |
JCTree resource = resources.head; |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1615 |
JCExpression resourceUse; |
37638
aac00923d48e
7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
36526
diff
changeset
|
1616 |
boolean resourceNonNull; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1617 |
if (resource instanceof JCVariableDecl) { |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1618 |
JCVariableDecl var = (JCVariableDecl) resource; |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1619 |
resourceUse = make.Ident(var.sym).setType(resource.type); |
37638
aac00923d48e
7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
36526
diff
changeset
|
1620 |
resourceNonNull = var.init != null && TreeInfo.skipParens(var.init).hasTag(NEWCLASS); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1621 |
stats.add(var); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1622 |
} else { |
8032 | 1623 |
Assert.check(resource instanceof JCExpression); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1624 |
VarSymbol syntheticTwrVar = |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1625 |
new VarSymbol(SYNTHETIC | FINAL, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1626 |
makeSyntheticName(names.fromString("twrVar" + |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1627 |
depth), twrVars), |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
1628 |
(resource.type.hasTag(BOT)) ? |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1629 |
syms.autoCloseableType : resource.type, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1630 |
currentMethodSym); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1631 |
twrVars.enter(syntheticTwrVar); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1632 |
JCVariableDecl syntheticTwrVarDecl = |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1633 |
make.VarDef(syntheticTwrVar, (JCExpression)resource); |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1634 |
resourceUse = (JCExpression)make.Ident(syntheticTwrVar); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1635 |
resourceNonNull = false; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1636 |
stats.add(syntheticTwrVarDecl); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1637 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1638 |
|
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1639 |
//create (semi-) finally block that will be copied into the main try body: |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1640 |
int oldPos = make.pos; |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1641 |
make.at(TreeInfo.endPos(block)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1642 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1643 |
// if (#resource != null) { #resource.close(); } |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1644 |
JCStatement bodyCloseStatement = makeResourceCloseInvocation(resourceUse); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1645 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1646 |
if (!resourceNonNull) { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1647 |
bodyCloseStatement = make.If(makeNonNullCheck(resourceUse), |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1648 |
bodyCloseStatement, |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1649 |
null); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1650 |
} |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1651 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1652 |
JCBlock finallyClause = make.Block(BODY_ONLY_FINALIZE, List.of(bodyCloseStatement)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1653 |
make.at(oldPos); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1654 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1655 |
// Create catch clause that saves exception, closes the resource and then rethrows the exception: |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1656 |
VarSymbol primaryException = |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1657 |
new VarSymbol(FINAL|SYNTHETIC, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1658 |
names.fromString("t" + |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1659 |
target.syntheticNameChar()), |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1660 |
syms.throwableType, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1661 |
currentMethodSym); |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1662 |
JCVariableDecl primaryExceptionDecl = make.VarDef(primaryException, null); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1663 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1664 |
// close resource: |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1665 |
// try { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1666 |
// #resource.close(); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1667 |
// } catch (Throwable #suppressedException) { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1668 |
// #primaryException.addSuppressed(#suppressedException); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1669 |
// } |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1670 |
VarSymbol suppressedException = |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
1671 |
new VarSymbol(SYNTHETIC, make.paramName(2), |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1672 |
syms.throwableType, |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1673 |
currentMethodSym); |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1674 |
JCStatement addSuppressedStatement = |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1675 |
make.Exec(makeCall(make.Ident(primaryException), |
7213
912fdd70fd2a
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6721
diff
changeset
|
1676 |
names.addSuppressed, |
49280
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1677 |
List.of(make.Ident(suppressedException)))); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1678 |
JCBlock closeResourceTryBlock = |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1679 |
make.Block(0L, List.of(makeResourceCloseInvocation(resourceUse))); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1680 |
JCVariableDecl catchSuppressedDecl = make.VarDef(suppressedException, null); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1681 |
JCBlock catchSuppressedBlock = make.Block(0L, List.of(addSuppressedStatement)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1682 |
List<JCCatch> catchSuppressedClauses = |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1683 |
List.of(make.Catch(catchSuppressedDecl, catchSuppressedBlock)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1684 |
JCTry closeResourceTry = make.Try(closeResourceTryBlock, catchSuppressedClauses, null); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1685 |
closeResourceTry.finallyCanCompleteNormally = true; |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1686 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1687 |
JCStatement exceptionalCloseStatement = closeResourceTry; |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1688 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1689 |
if (!resourceNonNull) { |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1690 |
// if (#resource != null) { } |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1691 |
exceptionalCloseStatement = make.If(makeNonNullCheck(resourceUse), |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1692 |
exceptionalCloseStatement, |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1693 |
null); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1694 |
} |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1695 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1696 |
JCStatement exceptionalRethrow = make.Throw(make.Ident(primaryException)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1697 |
JCBlock exceptionalCloseBlock = make.Block(0L, List.of(exceptionalCloseStatement, exceptionalRethrow)); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1698 |
JCCatch exceptionalCatchClause = make.Catch(primaryExceptionDecl, exceptionalCloseBlock); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1699 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1700 |
//create the main try statement with the close: |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1701 |
JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, depth + 1), |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1702 |
List.of(exceptionalCatchClause), |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1703 |
finallyClause); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1704 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1705 |
outerTry.finallyCanCompleteNormally = true; |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1706 |
stats.add(outerTry); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1707 |
|
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1708 |
JCBlock newBlock = make.Block(0L, stats.toList()); |
c2a3a2aa2475
8194978: Javac produces dead code for try-with-resource
jlahoda
parents:
48504
diff
changeset
|
1709 |
return newBlock; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1710 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1711 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1712 |
private JCStatement makeResourceCloseInvocation(JCExpression resource) { |
12716
5a41387068a8
7166990: java/compiler Inherited interfaces using generics sometimes looses the generic type
sundar
parents:
11314
diff
changeset
|
1713 |
// convert to AutoCloseable if needed |
5a41387068a8
7166990: java/compiler Inherited interfaces using generics sometimes looses the generic type
sundar
parents:
11314
diff
changeset
|
1714 |
if (types.asSuper(resource.type, syms.autoCloseableType.tsym) == null) { |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
1715 |
resource = convert(resource, syms.autoCloseableType); |
12716
5a41387068a8
7166990: java/compiler Inherited interfaces using generics sometimes looses the generic type
sundar
parents:
11314
diff
changeset
|
1716 |
} |
5a41387068a8
7166990: java/compiler Inherited interfaces using generics sometimes looses the generic type
sundar
parents:
11314
diff
changeset
|
1717 |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1718 |
// create resource.close() method invocation |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1719 |
JCExpression resourceClose = makeCall(resource, |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1720 |
names.close, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
1721 |
List.nil()); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1722 |
return make.Exec(resourceClose); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1723 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
1724 |
|
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1725 |
private JCExpression makeNonNullCheck(JCExpression expression) { |
10950 | 1726 |
return makeBinary(NE, expression, makeNull()); |
8434
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1727 |
} |
afb406fc66fe
7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
8242
diff
changeset
|
1728 |
|
10 | 1729 |
/** Construct a tree that represents the outer instance |
13844 | 1730 |
* {@code C.this}. Never pick the current `this'. |
10 | 1731 |
* @param pos The source code position to be used for the tree. |
1732 |
* @param c The qualifier class. |
|
1733 |
*/ |
|
1734 |
JCExpression makeOuterThis(DiagnosticPosition pos, TypeSymbol c) { |
|
1735 |
List<VarSymbol> ots = outerThisStack; |
|
1736 |
if (ots.isEmpty()) { |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44230
diff
changeset
|
1737 |
log.error(pos, Errors.NoEnclInstanceOfTypeInScope(c)); |
8032 | 1738 |
Assert.error(); |
10 | 1739 |
return makeNull(); |
1740 |
} |
|
1741 |
VarSymbol ot = ots.head; |
|
1742 |
JCExpression tree = access(make.at(pos).Ident(ot)); |
|
1743 |
TypeSymbol otc = ot.type.tsym; |
|
1744 |
while (otc != c) { |
|
1745 |
do { |
|
1746 |
ots = ots.tail; |
|
1747 |
if (ots.isEmpty()) { |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44230
diff
changeset
|
1748 |
log.error(pos, Errors.NoEnclInstanceOfTypeInScope(c)); |
8032 | 1749 |
Assert.error(); // should have been caught in Attr |
10 | 1750 |
return tree; |
1751 |
} |
|
1752 |
ot = ots.head; |
|
1753 |
} while (ot.owner != otc); |
|
1754 |
if (otc.owner.kind != PCK && !otc.hasOuterInstance()) { |
|
1755 |
chk.earlyRefError(pos, c); |
|
8032 | 1756 |
Assert.error(); // should have been caught in Attr |
10 | 1757 |
return makeNull(); |
1758 |
} |
|
1759 |
tree = access(make.at(pos).Select(tree, ot)); |
|
1760 |
otc = ot.type.tsym; |
|
1761 |
} |
|
1762 |
return tree; |
|
1763 |
} |
|
1764 |
||
1765 |
/** Construct a tree that represents the closest outer instance |
|
13844 | 1766 |
* {@code C.this} such that the given symbol is a member of C. |
10 | 1767 |
* @param pos The source code position to be used for the tree. |
1768 |
* @param sym The accessed symbol. |
|
1769 |
* @param preciseMatch should we accept a type that is a subtype of |
|
1770 |
* sym's owner, even if it doesn't contain sym |
|
1771 |
* due to hiding, overriding, or non-inheritance |
|
1772 |
* due to protection? |
|
1773 |
*/ |
|
1774 |
JCExpression makeOwnerThis(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) { |
|
1775 |
Symbol c = sym.owner; |
|
1776 |
if (preciseMatch ? sym.isMemberOf(currentClass, types) |
|
1777 |
: currentClass.isSubClass(sym.owner, types)) { |
|
1778 |
// in this case, `this' works fine |
|
1779 |
return make.at(pos).This(c.erasure(types)); |
|
1780 |
} else { |
|
1781 |
// need to go via this$n |
|
1782 |
return makeOwnerThisN(pos, sym, preciseMatch); |
|
1783 |
} |
|
1784 |
} |
|
1785 |
||
1786 |
/** |
|
1787 |
* Similar to makeOwnerThis but will never pick "this". |
|
1788 |
*/ |
|
1789 |
JCExpression makeOwnerThisN(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) { |
|
1790 |
Symbol c = sym.owner; |
|
1791 |
List<VarSymbol> ots = outerThisStack; |
|
1792 |
if (ots.isEmpty()) { |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44230
diff
changeset
|
1793 |
log.error(pos, Errors.NoEnclInstanceOfTypeInScope(c)); |
8032 | 1794 |
Assert.error(); |
10 | 1795 |
return makeNull(); |
1796 |
} |
|
1797 |
VarSymbol ot = ots.head; |
|
1798 |
JCExpression tree = access(make.at(pos).Ident(ot)); |
|
1799 |
TypeSymbol otc = ot.type.tsym; |
|
1800 |
while (!(preciseMatch ? sym.isMemberOf(otc, types) : otc.isSubClass(sym.owner, types))) { |
|
1801 |
do { |
|
1802 |
ots = ots.tail; |
|
1803 |
if (ots.isEmpty()) { |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44230
diff
changeset
|
1804 |
log.error(pos, Errors.NoEnclInstanceOfTypeInScope(c)); |
8032 | 1805 |
Assert.error(); |
10 | 1806 |
return tree; |
1807 |
} |
|
1808 |
ot = ots.head; |
|
1809 |
} while (ot.owner != otc); |
|
1810 |
tree = access(make.at(pos).Select(tree, ot)); |
|
1811 |
otc = ot.type.tsym; |
|
1812 |
} |
|
1813 |
return tree; |
|
1814 |
} |
|
1815 |
||
13844 | 1816 |
/** Return tree simulating the assignment {@code this.name = name}, where |
10 | 1817 |
* name is the name of a free variable. |
1818 |
*/ |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
1819 |
JCStatement initField(int pos, Symbol rhs, Symbol lhs) { |
8032 | 1820 |
Assert.check(rhs.owner.kind == MTH); |
1821 |
Assert.check(rhs.owner.owner == lhs.owner); |
|
10 | 1822 |
make.at(pos); |
1823 |
return |
|
1824 |
make.Exec( |
|
1825 |
make.Assign( |
|
1826 |
make.Select(make.This(lhs.owner.erasure(types)), lhs), |
|
1827 |
make.Ident(rhs)).setType(lhs.erasure(types))); |
|
1828 |
} |
|
1829 |
||
13844 | 1830 |
/** Return tree simulating the assignment {@code this.this$n = this$n}. |
10 | 1831 |
*/ |
1832 |
JCStatement initOuterThis(int pos) { |
|
1833 |
VarSymbol rhs = outerThisStack.head; |
|
8032 | 1834 |
Assert.check(rhs.owner.kind == MTH); |
10 | 1835 |
VarSymbol lhs = outerThisStack.tail.head; |
8032 | 1836 |
Assert.check(rhs.owner.owner == lhs.owner); |
10 | 1837 |
make.at(pos); |
1838 |
return |
|
1839 |
make.Exec( |
|
1840 |
make.Assign( |
|
1841 |
make.Select(make.This(lhs.owner.erasure(types)), lhs), |
|
1842 |
make.Ident(rhs)).setType(lhs.erasure(types))); |
|
1843 |
} |
|
1844 |
||
1845 |
/************************************************************************** |
|
1846 |
* Code for .class |
|
1847 |
*************************************************************************/ |
|
1848 |
||
1849 |
/** Return the symbol of a class to contain a cache of |
|
1850 |
* compiler-generated statics such as class$ and the |
|
1851 |
* $assertionsDisabled flag. We create an anonymous nested class |
|
1852 |
* (unless one already exists) and return its symbol. However, |
|
1853 |
* for backward compatibility in 1.4 and earlier we use the |
|
1854 |
* top-level class itself. |
|
1855 |
*/ |
|
1856 |
private ClassSymbol outerCacheClass() { |
|
1857 |
ClassSymbol clazz = outermostClassDef.sym; |
|
1858 |
Scope s = clazz.members(); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1859 |
for (Symbol sym : s.getSymbols(NON_RECURSIVE)) |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1860 |
if (sym.kind == TYP && |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1861 |
sym.name == names.empty && |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
1862 |
(sym.flags() & INTERFACE) == 0) return (ClassSymbol) sym; |
15565
040a54631aad
7199823: javac generates inner class that can't be verified
vromero
parents:
15385
diff
changeset
|
1863 |
return makeEmptyClass(STATIC | SYNTHETIC, clazz).sym; |
10 | 1864 |
} |
1865 |
||
44230
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1866 |
/** Create an attributed tree of the form left.name(). */ |
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1867 |
private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) { |
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1868 |
Assert.checkNonNull(left.type); |
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1869 |
Symbol funcsym = lookupMethod(make_pos, name, left.type, |
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1870 |
TreeInfo.types(args)); |
6f46f350a21b
8175191: dead code removal at package com.sun.tools.javac.comp
vromero
parents:
42828
diff
changeset
|
1871 |
return make.App(make.Select(left, funcsym), args); |
10 | 1872 |
} |
1873 |
||
1874 |
/** The tree simulating a T.class expression. |
|
1875 |
* @param clazz The tree identifying type T. |
|
1876 |
*/ |
|
1877 |
private JCExpression classOf(JCTree clazz) { |
|
1878 |
return classOfType(clazz.type, clazz.pos()); |
|
1879 |
} |
|
1880 |
||
1881 |
private JCExpression classOfType(Type type, DiagnosticPosition pos) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
1882 |
switch (type.getTag()) { |
10 | 1883 |
case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
1884 |
case DOUBLE: case BOOLEAN: case VOID: |
|
1885 |
// replace with <BoxedClass>.TYPE |
|
1886 |
ClassSymbol c = types.boxedClass(type); |
|
1887 |
Symbol typeSym = |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
13844
diff
changeset
|
1888 |
rs.accessBase( |
55306 | 1889 |
rs.findIdentInType(pos, attrEnv, c.type, names.TYPE, KindSelector.VAR), |
10 | 1890 |
pos, c.type, names.TYPE, true); |
1891 |
if (typeSym.kind == VAR) |
|
1892 |
((VarSymbol)typeSym).getConstValue(); // ensure initializer is evaluated |
|
1893 |
return make.QualIdent(typeSym); |
|
1894 |
case CLASS: case ARRAY: |
|
1895 |
VarSymbol sym = new VarSymbol( |
|
1896 |
STATIC | PUBLIC | FINAL, names._class, |
|
1897 |
syms.classType, type.tsym); |
|
1898 |
return make_at(pos).Select(make.Type(type), sym); |
|
1899 |
default: |
|
1900 |
throw new AssertionError(); |
|
1901 |
} |
|
1902 |
} |
|
1903 |
||
1904 |
/************************************************************************** |
|
1905 |
* Code for enabling/disabling assertions. |
|
1906 |
*************************************************************************/ |
|
1907 |
||
21005
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1908 |
private ClassSymbol assertionsDisabledClassCache; |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1909 |
|
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1910 |
/**Used to create an auxiliary class to hold $assertionsDisabled for interfaces. |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1911 |
*/ |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1912 |
private ClassSymbol assertionsDisabledClass() { |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1913 |
if (assertionsDisabledClassCache != null) return assertionsDisabledClassCache; |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1914 |
|
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1915 |
assertionsDisabledClassCache = makeEmptyClass(STATIC | SYNTHETIC, outermostClassDef.sym).sym; |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1916 |
|
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1917 |
return assertionsDisabledClassCache; |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1918 |
} |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1919 |
|
10 | 1920 |
// This code is not particularly robust if the user has |
1921 |
// previously declared a member named '$assertionsDisabled'. |
|
1922 |
// The same faulty idiom also appears in the translation of |
|
1923 |
// class literals above. We should report an error if a |
|
1924 |
// previous declaration is not synthetic. |
|
1925 |
||
1926 |
private JCExpression assertFlagTest(DiagnosticPosition pos) { |
|
1927 |
// Outermost class may be either true class or an interface. |
|
1928 |
ClassSymbol outermostClass = outermostClassDef.sym; |
|
1929 |
||
21005
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1930 |
//only classes can hold a non-public field, look for a usable one: |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1931 |
ClassSymbol container = !currentClass.isInterface() ? currentClass : |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1932 |
assertionsDisabledClass(); |
10 | 1933 |
|
1934 |
VarSymbol assertDisabledSym = |
|
1935 |
(VarSymbol)lookupSynthetic(dollarAssertionsDisabled, |
|
1936 |
container.members()); |
|
1937 |
if (assertDisabledSym == null) { |
|
1938 |
assertDisabledSym = |
|
1939 |
new VarSymbol(STATIC | FINAL | SYNTHETIC, |
|
1940 |
dollarAssertionsDisabled, |
|
1941 |
syms.booleanType, |
|
1942 |
container); |
|
1943 |
enterSynthetic(pos, assertDisabledSym, container.members()); |
|
1944 |
Symbol desiredAssertionStatusSym = lookupMethod(pos, |
|
1945 |
names.desiredAssertionStatus, |
|
1946 |
types.erasure(syms.classType), |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
1947 |
List.nil()); |
10 | 1948 |
JCClassDecl containerDef = classDef(container); |
1949 |
make_at(containerDef.pos()); |
|
10950 | 1950 |
JCExpression notStatus = makeUnary(NOT, make.App(make.Select( |
10 | 1951 |
classOfType(types.erasure(outermostClass.type), |
1952 |
containerDef.pos()), |
|
1953 |
desiredAssertionStatusSym))); |
|
1954 |
JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym, |
|
1955 |
notStatus); |
|
1956 |
containerDef.defs = containerDef.defs.prepend(assertDisabledDef); |
|
21005
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1957 |
|
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1958 |
if (currentClass.isInterface()) { |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1959 |
//need to load the assertions enabled/disabled state while |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1960 |
//initializing the interface: |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1961 |
JCClassDecl currentClassDef = classDef(currentClass); |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1962 |
make_at(currentClassDef.pos()); |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1963 |
JCStatement dummy = make.If(make.QualIdent(assertDisabledSym), make.Skip(), null); |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
1964 |
JCBlock clinit = make.Block(STATIC, List.of(dummy)); |
21005
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1965 |
currentClassDef.defs = currentClassDef.defs.prepend(clinit); |
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
1966 |
} |
10 | 1967 |
} |
1968 |
make_at(pos); |
|
10950 | 1969 |
return makeUnary(NOT, make.Ident(assertDisabledSym)); |
10 | 1970 |
} |
1971 |
||
1972 |
||
1973 |
/************************************************************************** |
|
1974 |
* Building blocks for let expressions |
|
1975 |
*************************************************************************/ |
|
1976 |
||
1977 |
interface TreeBuilder { |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
1978 |
JCExpression build(JCExpression arg); |
10 | 1979 |
} |
1980 |
||
1981 |
/** Construct an expression using the builder, with the given rval |
|
1982 |
* expression as an argument to the builder. However, the rval |
|
1983 |
* expression must be computed only once, even if used multiple |
|
1984 |
* times in the result of the builder. We do that by |
|
1985 |
* constructing a "let" expression that saves the rvalue into a |
|
1986 |
* temporary variable and then uses the temporary variable in |
|
1987 |
* place of the expression built by the builder. The complete |
|
1988 |
* resulting expression is of the form |
|
1989 |
* <pre> |
|
1990 |
* (let <b>TYPE</b> <b>TEMP</b> = <b>RVAL</b>; |
|
1991 |
* in (<b>BUILDER</b>(<b>TEMP</b>))) |
|
1992 |
* </pre> |
|
1993 |
* where <code><b>TEMP</b></code> is a newly declared variable |
|
1994 |
* in the let expression. |
|
1995 |
*/ |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
1996 |
JCExpression abstractRval(JCExpression rval, Type type, TreeBuilder builder) { |
10 | 1997 |
rval = TreeInfo.skipParens(rval); |
1998 |
switch (rval.getTag()) { |
|
10950 | 1999 |
case LITERAL: |
10 | 2000 |
return builder.build(rval); |
10950 | 2001 |
case IDENT: |
10 | 2002 |
JCIdent id = (JCIdent) rval; |
2003 |
if ((id.sym.flags() & FINAL) != 0 && id.sym.owner.kind == MTH) |
|
2004 |
return builder.build(rval); |
|
2005 |
} |
|
35355
817b3a285dbb
8143388: Compiler throws NullPointerException during compilation
jlahoda
parents:
34751
diff
changeset
|
2006 |
Name name = TreeInfo.name(rval); |
42499
49b675740462
8147527: Wrong code generated for postfix unary operators
jlahoda
parents:
42407
diff
changeset
|
2007 |
if (name == names._super || name == names._this) |
35355
817b3a285dbb
8143388: Compiler throws NullPointerException during compilation
jlahoda
parents:
34751
diff
changeset
|
2008 |
return builder.build(rval); |
10 | 2009 |
VarSymbol var = |
2010 |
new VarSymbol(FINAL|SYNTHETIC, |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1037
diff
changeset
|
2011 |
names.fromString( |
10 | 2012 |
target.syntheticNameChar() |
2013 |
+ "" + rval.hashCode()), |
|
2014 |
type, |
|
2015 |
currentMethodSym); |
|
161
bfe63856332f
6614974: javac successfully compiles code that throws java.lang.VerifyError when run
mcimadamore
parents:
10
diff
changeset
|
2016 |
rval = convert(rval,type); |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2017 |
JCVariableDecl def = make.VarDef(var, rval); // XXX cast |
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2018 |
JCExpression built = builder.build(make.Ident(var)); |
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2019 |
JCExpression res = make.LetExpr(def, built); |
10 | 2020 |
res.type = built.type; |
2021 |
return res; |
|
2022 |
} |
|
2023 |
||
2024 |
// same as above, with the type of the temporary variable computed |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2025 |
JCExpression abstractRval(JCExpression rval, TreeBuilder builder) { |
10 | 2026 |
return abstractRval(rval, rval.type, builder); |
2027 |
} |
|
2028 |
||
2029 |
// same as above, but for an expression that may be used as either |
|
2030 |
// an rvalue or an lvalue. This requires special handling for |
|
2031 |
// Select expressions, where we place the left-hand-side of the |
|
2032 |
// select in a temporary, and for Indexed expressions, where we |
|
2033 |
// place both the indexed expression and the index value in temps. |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2034 |
JCExpression abstractLval(JCExpression lval, final TreeBuilder builder) { |
10 | 2035 |
lval = TreeInfo.skipParens(lval); |
2036 |
switch (lval.getTag()) { |
|
10950 | 2037 |
case IDENT: |
10 | 2038 |
return builder.build(lval); |
10950 | 2039 |
case SELECT: { |
10 | 2040 |
final JCFieldAccess s = (JCFieldAccess)lval; |
2041 |
Symbol lid = TreeInfo.symbol(s.selected); |
|
2042 |
if (lid != null && lid.kind == TYP) return builder.build(lval); |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2043 |
return abstractRval(s.selected, selected -> builder.build(make.Select(selected, s.sym))); |
10 | 2044 |
} |
10950 | 2045 |
case INDEXED: { |
10 | 2046 |
final JCArrayAccess i = (JCArrayAccess)lval; |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2047 |
return abstractRval(i.indexed, indexed -> abstractRval(i.index, syms.intType, index -> { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2048 |
JCExpression newLval = make.Indexed(indexed, index); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2049 |
newLval.setType(i.type); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2050 |
return builder.build(newLval); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2051 |
})); |
10 | 2052 |
} |
10950 | 2053 |
case TYPECAST: { |
1468
19d266637353
6751514: Unary post-increment with type variables crash javac during lowering
mcimadamore
parents:
1260
diff
changeset
|
2054 |
return abstractLval(((JCTypeCast)lval).expr, builder); |
19d266637353
6751514: Unary post-increment with type variables crash javac during lowering
mcimadamore
parents:
1260
diff
changeset
|
2055 |
} |
10 | 2056 |
} |
2057 |
throw new AssertionError(lval); |
|
2058 |
} |
|
2059 |
||
2060 |
// evaluate and discard the first expression, then evaluate the second. |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2061 |
JCExpression makeComma(final JCExpression expr1, final JCExpression expr2) { |
51757
5b033f23aced
8183548: Comma-expressions shouldn't use any temporary variable
bsrbnd
parents:
51563
diff
changeset
|
2062 |
JCExpression res = make.LetExpr(List.of(make.Exec(expr1)), expr2); |
5b033f23aced
8183548: Comma-expressions shouldn't use any temporary variable
bsrbnd
parents:
51563
diff
changeset
|
2063 |
res.type = expr2.type; |
5b033f23aced
8183548: Comma-expressions shouldn't use any temporary variable
bsrbnd
parents:
51563
diff
changeset
|
2064 |
return res; |
10 | 2065 |
} |
2066 |
||
2067 |
/************************************************************************** |
|
2068 |
* Translation methods |
|
2069 |
*************************************************************************/ |
|
2070 |
||
2071 |
/** Visitor argument: enclosing operator node. |
|
2072 |
*/ |
|
2073 |
private JCExpression enclOp; |
|
2074 |
||
2075 |
/** Visitor method: Translate a single node. |
|
2076 |
* Attach the source position from the old tree to its replacement tree. |
|
2077 |
*/ |
|
24066
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
23791
diff
changeset
|
2078 |
@Override |
10 | 2079 |
public <T extends JCTree> T translate(T tree) { |
2080 |
if (tree == null) { |
|
2081 |
return null; |
|
2082 |
} else { |
|
2083 |
make_at(tree.pos()); |
|
2084 |
T result = super.translate(tree); |
|
11055 | 2085 |
if (endPosTable != null && result != tree) { |
2086 |
endPosTable.replaceTree(tree, result); |
|
10 | 2087 |
} |
2088 |
return result; |
|
2089 |
} |
|
2090 |
} |
|
2091 |
||
2092 |
/** Visitor method: Translate a single node, boxing or unboxing if needed. |
|
2093 |
*/ |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2094 |
public <T extends JCExpression> T translate(T tree, Type type) { |
10 | 2095 |
return (tree == null) ? null : boxIfNeeded(translate(tree), type); |
2096 |
} |
|
2097 |
||
2098 |
/** Visitor method: Translate tree. |
|
2099 |
*/ |
|
2100 |
public <T extends JCTree> T translate(T tree, JCExpression enclOp) { |
|
2101 |
JCExpression prevEnclOp = this.enclOp; |
|
2102 |
this.enclOp = enclOp; |
|
2103 |
T res = translate(tree); |
|
2104 |
this.enclOp = prevEnclOp; |
|
2105 |
return res; |
|
2106 |
} |
|
2107 |
||
2108 |
/** Visitor method: Translate list of trees. |
|
2109 |
*/ |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2110 |
public <T extends JCExpression> List<T> translate(List<T> trees, Type type) { |
10 | 2111 |
if (trees == null) return null; |
2112 |
for (List<T> l = trees; l.nonEmpty(); l = l.tail) |
|
2113 |
l.head = translate(l.head, type); |
|
2114 |
return trees; |
|
2115 |
} |
|
2116 |
||
24069 | 2117 |
public void visitPackageDef(JCPackageDecl tree) { |
2118 |
if (!needPackageInfoClass(tree)) |
|
36526 | 2119 |
return; |
2120 |
||
24069 | 2121 |
long flags = Flags.ABSTRACT | Flags.INTERFACE; |
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2122 |
// package-info is marked SYNTHETIC in JDK 1.6 and later releases |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2123 |
flags = flags | Flags.SYNTHETIC; |
24069 | 2124 |
ClassSymbol c = tree.packge.package_info; |
36526 | 2125 |
c.setAttributes(tree.packge); |
24069 | 2126 |
c.flags_field |= flags; |
2127 |
ClassType ctype = (ClassType) c.type; |
|
2128 |
ctype.supertype_field = syms.objectType; |
|
2129 |
ctype.interfaces_field = List.nil(); |
|
36526 | 2130 |
createInfoClass(tree.annotations, c); |
10 | 2131 |
} |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2132 |
// where |
24069 | 2133 |
private boolean needPackageInfoClass(JCPackageDecl pd) { |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2134 |
switch (pkginfoOpt) { |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2135 |
case ALWAYS: |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2136 |
return true; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2137 |
case LEGACY: |
24069 | 2138 |
return pd.getAnnotations().nonEmpty(); |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2139 |
case NONEMPTY: |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
2140 |
for (Attribute.Compound a : |
24069 | 2141 |
pd.packge.getDeclarationAttributes()) { |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2142 |
Attribute.RetentionPolicy p = types.getRetention(a); |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2143 |
if (p != Attribute.RetentionPolicy.SOURCE) |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2144 |
return true; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2145 |
} |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2146 |
return false; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2147 |
} |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2148 |
throw new AssertionError(); |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6148
diff
changeset
|
2149 |
} |
10 | 2150 |
|
36526 | 2151 |
public void visitModuleDef(JCModuleDecl tree) { |
2152 |
ModuleSymbol msym = tree.sym; |
|
2153 |
ClassSymbol c = msym.module_info; |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40306
diff
changeset
|
2154 |
c.setAttributes(msym); |
36526 | 2155 |
c.flags_field |= Flags.MODULE; |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2156 |
createInfoClass(List.nil(), tree.sym.module_info); |
36526 | 2157 |
} |
2158 |
||
2159 |
private void createInfoClass(List<JCAnnotation> annots, ClassSymbol c) { |
|
2160 |
long flags = Flags.ABSTRACT | Flags.INTERFACE; |
|
2161 |
JCClassDecl infoClass = |
|
2162 |
make.ClassDef(make.Modifiers(flags, annots), |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2163 |
c.name, List.nil(), |
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2164 |
null, List.nil(), List.nil()); |
36526 | 2165 |
infoClass.sym = c; |
2166 |
translated.append(infoClass); |
|
2167 |
} |
|
2168 |
||
10 | 2169 |
public void visitClassDef(JCClassDecl tree) { |
25007 | 2170 |
Env<AttrContext> prevEnv = attrEnv; |
10 | 2171 |
ClassSymbol currentClassPrev = currentClass; |
2172 |
MethodSymbol currentMethodSymPrev = currentMethodSym; |
|
25007 | 2173 |
|
10 | 2174 |
currentClass = tree.sym; |
2175 |
currentMethodSym = null; |
|
25007 | 2176 |
attrEnv = typeEnvs.remove(currentClass); |
2177 |
if (attrEnv == null) |
|
2178 |
attrEnv = prevEnv; |
|
2179 |
||
10 | 2180 |
classdefs.put(currentClass, tree); |
2181 |
||
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2182 |
Map<Symbol, Symbol> prevProxies = proxies; |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2183 |
proxies = new HashMap<>(proxies); |
10 | 2184 |
List<VarSymbol> prevOuterThisStack = outerThisStack; |
2185 |
||
2186 |
// If this is an enum definition |
|
2187 |
if ((tree.mods.flags & ENUM) != 0 && |
|
2188 |
(types.supertype(currentClass.type).tsym.flags() & ENUM) == 0) |
|
2189 |
visitEnumDef(tree); |
|
2190 |
||
2191 |
// If this is a nested class, define a this$n field for |
|
2192 |
// it and add to proxies. |
|
2193 |
JCVariableDecl otdef = null; |
|
2194 |
if (currentClass.hasOuterInstance()) |
|
2195 |
otdef = outerThisDef(tree.pos, currentClass); |
|
2196 |
||
2197 |
// If this is a local class, define proxies for all its free variables. |
|
2198 |
List<JCVariableDecl> fvdefs = freevarDefs( |
|
2199 |
tree.pos, freevars(currentClass), currentClass); |
|
2200 |
||
2201 |
// Recursively translate superclass, interfaces. |
|
2202 |
tree.extending = translate(tree.extending); |
|
2203 |
tree.implementing = translate(tree.implementing); |
|
2204 |
||
10627
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2205 |
if (currentClass.isLocal()) { |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2206 |
ClassSymbol encl = currentClass.owner.enclClass(); |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2207 |
if (encl.trans_local == null) { |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2208 |
encl.trans_local = List.nil(); |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2209 |
} |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2210 |
encl.trans_local = encl.trans_local.prepend(currentClass); |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2211 |
} |
d8e900b62e02
7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
10201
diff
changeset
|
2212 |
|
10 | 2213 |
// Recursively translate members, taking into account that new members |
2214 |
// might be created during the translation and prepended to the member |
|
2215 |
// list `tree.defs'. |
|
2216 |
List<JCTree> seen = List.nil(); |
|
2217 |
while (tree.defs != seen) { |
|
2218 |
List<JCTree> unseen = tree.defs; |
|
2219 |
for (List<JCTree> l = unseen; l.nonEmpty() && l != seen; l = l.tail) { |
|
2220 |
JCTree outermostMemberDefPrev = outermostMemberDef; |
|
2221 |
if (outermostMemberDefPrev == null) outermostMemberDef = l.head; |
|
2222 |
l.head = translate(l.head); |
|
2223 |
outermostMemberDef = outermostMemberDefPrev; |
|
2224 |
} |
|
2225 |
seen = unseen; |
|
2226 |
} |
|
2227 |
||
2228 |
// Convert a protected modifier to public, mask static modifier. |
|
2229 |
if ((tree.mods.flags & PROTECTED) != 0) tree.mods.flags |= PUBLIC; |
|
2230 |
tree.mods.flags &= ClassFlags; |
|
2231 |
||
2232 |
// Convert name to flat representation, replacing '.' by '$'. |
|
2233 |
tree.name = Convert.shortName(currentClass.flatName()); |
|
2234 |
||
2235 |
// Add this$n and free variables proxy definitions to class. |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2236 |
|
10 | 2237 |
for (List<JCVariableDecl> l = fvdefs; l.nonEmpty(); l = l.tail) { |
2238 |
tree.defs = tree.defs.prepend(l.head); |
|
2239 |
enterSynthetic(tree.pos(), l.head.sym, currentClass.members()); |
|
2240 |
} |
|
2241 |
if (currentClass.hasOuterInstance()) { |
|
2242 |
tree.defs = tree.defs.prepend(otdef); |
|
2243 |
enterSynthetic(tree.pos(), otdef.sym, currentClass.members()); |
|
2244 |
} |
|
2245 |
||
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2246 |
proxies = prevProxies; |
10 | 2247 |
outerThisStack = prevOuterThisStack; |
2248 |
||
2249 |
// Append translated tree to `translated' queue. |
|
2250 |
translated.append(tree); |
|
2251 |
||
25007 | 2252 |
attrEnv = prevEnv; |
10 | 2253 |
currentClass = currentClassPrev; |
2254 |
currentMethodSym = currentMethodSymPrev; |
|
2255 |
||
2256 |
// Return empty block {} as a placeholder for an inner class. |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2257 |
result = make_at(tree.pos()).Block(SYNTHETIC, List.nil()); |
10 | 2258 |
} |
2259 |
||
2260 |
/** Translate an enum class. */ |
|
2261 |
private void visitEnumDef(JCClassDecl tree) { |
|
2262 |
make_at(tree.pos()); |
|
2263 |
||
2264 |
// add the supertype, if needed |
|
2265 |
if (tree.extending == null) |
|
2266 |
tree.extending = make.Type(types.supertype(tree.type)); |
|
2267 |
||
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2268 |
// classOfType adds a cache field to tree.defs |
10 | 2269 |
JCExpression e_class = classOfType(tree.sym.type, tree.pos()). |
2270 |
setType(types.erasure(syms.classType)); |
|
2271 |
||
2272 |
// process each enumeration constant, adding implicit constructor parameters |
|
2273 |
int nextOrdinal = 0; |
|
22163 | 2274 |
ListBuffer<JCExpression> values = new ListBuffer<>(); |
2275 |
ListBuffer<JCTree> enumDefs = new ListBuffer<>(); |
|
2276 |
ListBuffer<JCTree> otherDefs = new ListBuffer<>(); |
|
10 | 2277 |
for (List<JCTree> defs = tree.defs; |
2278 |
defs.nonEmpty(); |
|
2279 |
defs=defs.tail) { |
|
10950 | 2280 |
if (defs.head.hasTag(VARDEF) && (((JCVariableDecl) defs.head).mods.flags & ENUM) != 0) { |
10 | 2281 |
JCVariableDecl var = (JCVariableDecl)defs.head; |
2282 |
visitEnumConstantDef(var, nextOrdinal++); |
|
2283 |
values.append(make.QualIdent(var.sym)); |
|
2284 |
enumDefs.append(var); |
|
2285 |
} else { |
|
2286 |
otherDefs.append(defs.head); |
|
2287 |
} |
|
2288 |
} |
|
2289 |
||
2290 |
// private static final T[] #VALUES = { a, b, c }; |
|
2291 |
Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES"); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
2292 |
while (tree.sym.members().findFirst(valuesName) != null) // avoid name clash |
10 | 2293 |
valuesName = names.fromString(valuesName + "" + target.syntheticNameChar()); |
26781
a786b07c7b91
8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents:
25874
diff
changeset
|
2294 |
Type arrayType = new ArrayType(types.erasure(tree.type), syms.arrayClass); |
10 | 2295 |
VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC, |
2296 |
valuesName, |
|
2297 |
arrayType, |
|
2298 |
tree.type.tsym); |
|
2299 |
JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)), |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2300 |
List.nil(), |
10 | 2301 |
values.toList()); |
2302 |
newArray.type = arrayType; |
|
2303 |
enumDefs.append(make.VarDef(valuesVar, newArray)); |
|
2304 |
tree.sym.members().enter(valuesVar); |
|
2305 |
||
2306 |
Symbol valuesSym = lookupMethod(tree.pos(), names.values, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2307 |
tree.type, List.nil()); |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2308 |
List<JCStatement> valuesBody; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2309 |
if (useClone()) { |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2310 |
// return (T[]) $VALUES.clone(); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2311 |
JCTypeCast valuesResult = |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2312 |
make.TypeCast(valuesSym.type.getReturnType(), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2313 |
make.App(make.Select(make.Ident(valuesVar), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2314 |
syms.arrayCloneMethod))); |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2315 |
valuesBody = List.of(make.Return(valuesResult)); |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2316 |
} else { |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2317 |
// template: T[] $result = new T[$values.length]; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2318 |
Name resultName = names.fromString(target.syntheticNameChar() + "result"); |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
2319 |
while (tree.sym.members().findFirst(resultName) != null) // avoid name clash |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2320 |
resultName = names.fromString(resultName + "" + target.syntheticNameChar()); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2321 |
VarSymbol resultVar = new VarSymbol(FINAL|SYNTHETIC, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2322 |
resultName, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2323 |
arrayType, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2324 |
valuesSym); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2325 |
JCNewArray resultArray = make.NewArray(make.Type(types.erasure(tree.type)), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2326 |
List.of(make.Select(make.Ident(valuesVar), syms.lengthVar)), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2327 |
null); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2328 |
resultArray.type = arrayType; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2329 |
JCVariableDecl decl = make.VarDef(resultVar, resultArray); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2330 |
|
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2331 |
// template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2332 |
if (systemArraycopyMethod == null) { |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2333 |
systemArraycopyMethod = |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2334 |
new MethodSymbol(PUBLIC | STATIC, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2335 |
names.fromString("arraycopy"), |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2336 |
new MethodType(List.of(syms.objectType, |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2337 |
syms.intType, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2338 |
syms.objectType, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2339 |
syms.intType, |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2340 |
syms.intType), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2341 |
syms.voidType, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2342 |
List.nil(), |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2343 |
syms.methodClass), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2344 |
syms.systemType.tsym); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2345 |
} |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2346 |
JCStatement copy = |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2347 |
make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2348 |
systemArraycopyMethod), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2349 |
List.of(make.Ident(valuesVar), make.Literal(0), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2350 |
make.Ident(resultVar), make.Literal(0), |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2351 |
make.Select(make.Ident(valuesVar), syms.lengthVar)))); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2352 |
|
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2353 |
// template: return $result; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2354 |
JCStatement ret = make.Return(make.Ident(resultVar)); |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2355 |
valuesBody = List.of(decl, copy, ret); |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2356 |
} |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2357 |
|
10 | 2358 |
JCMethodDecl valuesDef = |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2359 |
make.MethodDef((MethodSymbol)valuesSym, make.Block(0, valuesBody)); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2360 |
|
10 | 2361 |
enumDefs.append(valuesDef); |
2362 |
||
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2363 |
if (debugLower) |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2364 |
System.err.println(tree.sym + ".valuesDef = " + valuesDef); |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2365 |
|
10 | 2366 |
/** The template for the following code is: |
2367 |
* |
|
2368 |
* public static E valueOf(String name) { |
|
2369 |
* return (E)Enum.valueOf(E.class, name); |
|
2370 |
* } |
|
2371 |
* |
|
2372 |
* where E is tree.sym |
|
2373 |
*/ |
|
2374 |
MethodSymbol valueOfSym = lookupMethod(tree.pos(), |
|
2375 |
names.valueOf, |
|
2376 |
tree.sym.type, |
|
2377 |
List.of(syms.stringType)); |
|
8032 | 2378 |
Assert.check((valueOfSym.flags() & STATIC) != 0); |
10 | 2379 |
VarSymbol nameArgSym = valueOfSym.params.head; |
2380 |
JCIdent nameVal = make.Ident(nameArgSym); |
|
2381 |
JCStatement enum_ValueOf = |
|
2382 |
make.Return(make.TypeCast(tree.sym.type, |
|
2383 |
makeCall(make.Ident(syms.enumSym), |
|
2384 |
names.valueOf, |
|
2385 |
List.of(e_class, nameVal)))); |
|
2386 |
JCMethodDecl valueOf = make.MethodDef(valueOfSym, |
|
2387 |
make.Block(0, List.of(enum_ValueOf))); |
|
2388 |
nameVal.sym = valueOf.params.head.sym; |
|
2389 |
if (debugLower) |
|
2390 |
System.err.println(tree.sym + ".valueOf = " + valueOf); |
|
2391 |
enumDefs.append(valueOf); |
|
2392 |
||
2393 |
enumDefs.appendList(otherDefs.toList()); |
|
2394 |
tree.defs = enumDefs.toList(); |
|
2395 |
} |
|
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2396 |
// where |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2397 |
private MethodSymbol systemArraycopyMethod; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2398 |
private boolean useClone() { |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2399 |
try { |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
2400 |
return syms.objectType.tsym.members().findFirst(names.clone) != null; |
1037
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2401 |
} |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2402 |
catch (CompletionFailure e) { |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2403 |
return false; |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2404 |
} |
c6b315a34dc6
6627362: javac generates code that uses array.clone, which is not available on JavaCard
jjg
parents:
940
diff
changeset
|
2405 |
} |
10 | 2406 |
|
2407 |
/** Translate an enumeration constant and its initializer. */ |
|
2408 |
private void visitEnumConstantDef(JCVariableDecl var, int ordinal) { |
|
2409 |
JCNewClass varDef = (JCNewClass)var.init; |
|
2410 |
varDef.args = varDef.args. |
|
2411 |
prepend(makeLit(syms.intType, ordinal)). |
|
2412 |
prepend(makeLit(syms.stringType, var.name.toString())); |
|
2413 |
} |
|
2414 |
||
2415 |
public void visitMethodDef(JCMethodDecl tree) { |
|
2416 |
if (tree.name == names.init && (currentClass.flags_field&ENUM) != 0) { |
|
2417 |
// Add "String $enum$name, int $enum$ordinal" to the beginning of the |
|
2418 |
// argument list for each constructor of an enum. |
|
2419 |
JCVariableDecl nameParam = make_at(tree.pos()). |
|
2420 |
Param(names.fromString(target.syntheticNameChar() + |
|
2421 |
"enum" + target.syntheticNameChar() + "name"), |
|
2422 |
syms.stringType, tree.sym); |
|
2423 |
nameParam.mods.flags |= SYNTHETIC; nameParam.sym.flags_field |= SYNTHETIC; |
|
2424 |
JCVariableDecl ordParam = make. |
|
2425 |
Param(names.fromString(target.syntheticNameChar() + |
|
2426 |
"enum" + target.syntheticNameChar() + |
|
2427 |
"ordinal"), |
|
2428 |
syms.intType, tree.sym); |
|
2429 |
ordParam.mods.flags |= SYNTHETIC; ordParam.sym.flags_field |= SYNTHETIC; |
|
2430 |
||
27545
9b19c6e31489
8029012: parameter_index for type annotation not updated after outer.this added
emc
parents:
27230
diff
changeset
|
2431 |
MethodSymbol m = tree.sym; |
10 | 2432 |
tree.params = tree.params.prepend(ordParam).prepend(nameParam); |
27545
9b19c6e31489
8029012: parameter_index for type annotation not updated after outer.this added
emc
parents:
27230
diff
changeset
|
2433 |
|
15720
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
2434 |
m.extraParams = m.extraParams.prepend(ordParam.sym); |
e61b2f7a5148
8006345: Report Synthesized Parameters in java.lang.reflect.Parameter API
mcimadamore
parents:
15712
diff
changeset
|
2435 |
m.extraParams = m.extraParams.prepend(nameParam.sym); |
10 | 2436 |
Type olderasure = m.erasure(types); |
2437 |
m.erasure_field = new MethodType( |
|
2438 |
olderasure.getParameterTypes().prepend(syms.intType).prepend(syms.stringType), |
|
2439 |
olderasure.getReturnType(), |
|
2440 |
olderasure.getThrownTypes(), |
|
2441 |
syms.methodClass); |
|
2442 |
} |
|
2443 |
||
2444 |
JCMethodDecl prevMethodDef = currentMethodDef; |
|
2445 |
MethodSymbol prevMethodSym = currentMethodSym; |
|
2446 |
try { |
|
2447 |
currentMethodDef = tree; |
|
2448 |
currentMethodSym = tree.sym; |
|
2449 |
visitMethodDefInternal(tree); |
|
2450 |
} finally { |
|
2451 |
currentMethodDef = prevMethodDef; |
|
2452 |
currentMethodSym = prevMethodSym; |
|
2453 |
} |
|
2454 |
} |
|
27545
9b19c6e31489
8029012: parameter_index for type annotation not updated after outer.this added
emc
parents:
27230
diff
changeset
|
2455 |
|
10 | 2456 |
private void visitMethodDefInternal(JCMethodDecl tree) { |
2457 |
if (tree.name == names.init && |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2458 |
(currentClass.isInner() || currentClass.isLocal())) { |
10 | 2459 |
// We are seeing a constructor of an inner class. |
2460 |
MethodSymbol m = tree.sym; |
|
2461 |
||
2462 |
// Push a new proxy scope for constructor parameters. |
|
2463 |
// and create definitions for any this$n and proxy parameters. |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2464 |
Map<Symbol, Symbol> prevProxies = proxies; |
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2465 |
proxies = new HashMap<>(proxies); |
10 | 2466 |
List<VarSymbol> prevOuterThisStack = outerThisStack; |
2467 |
List<VarSymbol> fvs = freevars(currentClass); |
|
2468 |
JCVariableDecl otdef = null; |
|
2469 |
if (currentClass.hasOuterInstance()) |
|
2470 |
otdef = outerThisDef(tree.pos, m); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
2471 |
List<JCVariableDecl> fvdefs = freevarDefs(tree.pos, fvs, m, PARAMETER); |
10 | 2472 |
|
2473 |
// Recursively translate result type, parameters and thrown list. |
|
2474 |
tree.restype = translate(tree.restype); |
|
2475 |
tree.params = translateVarDefs(tree.params); |
|
2476 |
tree.thrown = translate(tree.thrown); |
|
2477 |
||
2478 |
// when compiling stubs, don't process body |
|
2479 |
if (tree.body == null) { |
|
2480 |
result = tree; |
|
2481 |
return; |
|
2482 |
} |
|
2483 |
||
2484 |
// Add this$n (if needed) in front of and free variables behind |
|
2485 |
// constructor parameter list. |
|
2486 |
tree.params = tree.params.appendList(fvdefs); |
|
27545
9b19c6e31489
8029012: parameter_index for type annotation not updated after outer.this added
emc
parents:
27230
diff
changeset
|
2487 |
if (currentClass.hasOuterInstance()) { |
10 | 2488 |
tree.params = tree.params.prepend(otdef); |
27545
9b19c6e31489
8029012: parameter_index for type annotation not updated after outer.this added
emc
parents:
27230
diff
changeset
|
2489 |
} |
10 | 2490 |
|
2491 |
// If this is an initial constructor, i.e., it does not start with |
|
2492 |
// this(...), insert initializers for this$n and proxies |
|
2493 |
// before (pre-1.4, after) the call to superclass constructor. |
|
2494 |
JCStatement selfCall = translate(tree.body.stats.head); |
|
2495 |
||
2496 |
List<JCStatement> added = List.nil(); |
|
2497 |
if (fvs.nonEmpty()) { |
|
2498 |
List<Type> addedargtypes = List.nil(); |
|
2499 |
for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) { |
|
34751
32ee3b176042
8143133: Wrong MethodParameters on capturing local class with multiple constructors
mcimadamore
parents:
33707
diff
changeset
|
2500 |
m.capturedLocals = |
32ee3b176042
8143133: Wrong MethodParameters on capturing local class with multiple constructors
mcimadamore
parents:
33707
diff
changeset
|
2501 |
m.capturedLocals.prepend((VarSymbol) |
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2502 |
(proxies.get(l.head))); |
18377
3f5b364f9d22
8015701: MethodParameters are not filled in for synthetic captured local variables
emc
parents:
18010
diff
changeset
|
2503 |
if (TreeInfo.isInitialConstructor(tree)) { |
10 | 2504 |
added = added.prepend( |
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2505 |
initField(tree.body.pos, proxies.get(l.head), prevProxies.get(l.head))); |
18377
3f5b364f9d22
8015701: MethodParameters are not filled in for synthetic captured local variables
emc
parents:
18010
diff
changeset
|
2506 |
} |
10 | 2507 |
addedargtypes = addedargtypes.prepend(l.head.erasure(types)); |
2508 |
} |
|
2509 |
Type olderasure = m.erasure(types); |
|
2510 |
m.erasure_field = new MethodType( |
|
2511 |
olderasure.getParameterTypes().appendList(addedargtypes), |
|
2512 |
olderasure.getReturnType(), |
|
2513 |
olderasure.getThrownTypes(), |
|
2514 |
syms.methodClass); |
|
2515 |
} |
|
2516 |
if (currentClass.hasOuterInstance() && |
|
2517 |
TreeInfo.isInitialConstructor(tree)) |
|
2518 |
{ |
|
2519 |
added = added.prepend(initOuterThis(tree.body.pos)); |
|
2520 |
} |
|
2521 |
||
2522 |
// pop local variables from proxy stack |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
2523 |
proxies = prevProxies; |
10 | 2524 |
|
2525 |
// recursively translate following local statements and |
|
2526 |
// combine with this- or super-call |
|
2527 |
List<JCStatement> stats = translate(tree.body.stats.tail); |
|
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2528 |
tree.body.stats = stats.prepend(selfCall).prependList(added); |
10 | 2529 |
outerThisStack = prevOuterThisStack; |
2530 |
} else { |
|
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2531 |
Map<Symbol, Symbol> prevLambdaTranslationMap = |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2532 |
lambdaTranslationMap; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2533 |
try { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2534 |
lambdaTranslationMap = (tree.sym.flags() & SYNTHETIC) != 0 && |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2535 |
tree.sym.name.startsWith(names.lambda) ? |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2536 |
makeTranslationMap(tree) : null; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2537 |
super.visitMethodDef(tree); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2538 |
} finally { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2539 |
lambdaTranslationMap = prevLambdaTranslationMap; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2540 |
} |
10 | 2541 |
} |
2542 |
result = tree; |
|
2543 |
} |
|
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2544 |
//where |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2545 |
private Map<Symbol, Symbol> makeTranslationMap(JCMethodDecl tree) { |
22163 | 2546 |
Map<Symbol, Symbol> translationMap = new HashMap<>(); |
16325
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2547 |
for (JCVariableDecl vd : tree.params) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2548 |
Symbol p = vd.sym; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2549 |
if (p != p.baseSymbol()) { |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2550 |
translationMap.put(p.baseSymbol(), p); |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2551 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2552 |
} |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2553 |
return translationMap; |
088a91896245
8004962: Code generation crash with lambda and local classes
mcimadamore
parents:
15720
diff
changeset
|
2554 |
} |
10 | 2555 |
|
2556 |
public void visitTypeCast(JCTypeCast tree) { |
|
2557 |
tree.clazz = translate(tree.clazz); |
|
2558 |
if (tree.type.isPrimitive() != tree.expr.type.isPrimitive()) |
|
2559 |
tree.expr = translate(tree.expr, tree.type); |
|
2560 |
else |
|
2561 |
tree.expr = translate(tree.expr); |
|
2562 |
result = tree; |
|
2563 |
} |
|
2564 |
||
2565 |
public void visitNewClass(JCNewClass tree) { |
|
2566 |
ClassSymbol c = (ClassSymbol)tree.constructor.owner; |
|
2567 |
||
2568 |
// Box arguments, if necessary |
|
2569 |
boolean isEnum = (tree.constructor.owner.flags() & ENUM) != 0; |
|
2570 |
List<Type> argTypes = tree.constructor.type.getParameterTypes(); |
|
2571 |
if (isEnum) argTypes = argTypes.prepend(syms.intType).prepend(syms.stringType); |
|
2572 |
tree.args = boxArgs(argTypes, tree.args, tree.varargsElement); |
|
2573 |
tree.varargsElement = null; |
|
2574 |
||
2575 |
// If created class is local, add free variables after |
|
2576 |
// explicit constructor arguments. |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2577 |
if (c.isLocal()) { |
10 | 2578 |
tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c))); |
2579 |
} |
|
2580 |
||
2581 |
// If an access constructor is used, append null as a last argument. |
|
2582 |
Symbol constructor = accessConstructor(tree.pos(), tree.constructor); |
|
2583 |
if (constructor != tree.constructor) { |
|
2584 |
tree.args = tree.args.append(makeNull()); |
|
2585 |
tree.constructor = constructor; |
|
2586 |
} |
|
2587 |
||
2588 |
// If created class has an outer instance, and new is qualified, pass |
|
2589 |
// qualifier as first argument. If new is not qualified, pass the |
|
2590 |
// correct outer instance as first argument. |
|
2591 |
if (c.hasOuterInstance()) { |
|
2592 |
JCExpression thisArg; |
|
2593 |
if (tree.encl != null) { |
|
2594 |
thisArg = attr.makeNullCheck(translate(tree.encl)); |
|
2595 |
thisArg.type = tree.encl.type; |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2596 |
} else if (c.isLocal()) { |
10 | 2597 |
// local class |
2598 |
thisArg = makeThis(tree.pos(), c.type.getEnclosingType().tsym); |
|
2599 |
} else { |
|
2600 |
// nested class |
|
2601 |
thisArg = makeOwnerThis(tree.pos(), c, false); |
|
2602 |
} |
|
2603 |
tree.args = tree.args.prepend(thisArg); |
|
2604 |
} |
|
2605 |
tree.encl = null; |
|
2606 |
||
2607 |
// If we have an anonymous class, create its flat version, rather |
|
2608 |
// than the class or interface following new. |
|
2609 |
if (tree.def != null) { |
|
2610 |
translate(tree.def); |
|
2611 |
tree.clazz = access(make_at(tree.clazz.pos()).Ident(tree.def.sym)); |
|
2612 |
tree.def = null; |
|
2613 |
} else { |
|
2614 |
tree.clazz = access(c, tree.clazz, enclOp, false); |
|
2615 |
} |
|
2616 |
result = tree; |
|
2617 |
} |
|
2618 |
||
2619 |
// Simplify conditionals with known constant controlling expressions. |
|
2620 |
// This allows us to avoid generating supporting declarations for |
|
2621 |
// the dead code, which will not be eliminated during code generation. |
|
2622 |
// Note that Flow.isFalse and Flow.isTrue only return true |
|
2623 |
// for constant expressions in the sense of JLS 15.27, which |
|
4142 | 2624 |
// are guaranteed to have no side-effects. More aggressive |
10 | 2625 |
// constant propagation would require that we take care to |
2626 |
// preserve possible side-effects in the condition expression. |
|
2627 |
||
23791 | 2628 |
// One common case is equality expressions involving a constant and null. |
2629 |
// Since null is not a constant expression (because null cannot be |
|
2630 |
// represented in the constant pool), equality checks involving null are |
|
2631 |
// not captured by Flow.isTrue/isFalse. |
|
2632 |
// Equality checks involving a constant and null, e.g. |
|
2633 |
// "" == null |
|
2634 |
// are safe to simplify as no side-effects can occur. |
|
2635 |
||
2636 |
private boolean isTrue(JCTree exp) { |
|
2637 |
if (exp.type.isTrue()) |
|
2638 |
return true; |
|
2639 |
Boolean b = expValue(exp); |
|
2640 |
return b == null ? false : b; |
|
2641 |
} |
|
2642 |
private boolean isFalse(JCTree exp) { |
|
2643 |
if (exp.type.isFalse()) |
|
2644 |
return true; |
|
2645 |
Boolean b = expValue(exp); |
|
2646 |
return b == null ? false : !b; |
|
2647 |
} |
|
2648 |
/* look for (in)equality relations involving null. |
|
2649 |
* return true - if expression is always true |
|
2650 |
* false - if expression is always false |
|
2651 |
* null - if expression cannot be eliminated |
|
2652 |
*/ |
|
2653 |
private Boolean expValue(JCTree exp) { |
|
2654 |
while (exp.hasTag(PARENS)) |
|
2655 |
exp = ((JCParens)exp).expr; |
|
2656 |
||
2657 |
boolean eq; |
|
2658 |
switch (exp.getTag()) { |
|
2659 |
case EQ: eq = true; break; |
|
2660 |
case NE: eq = false; break; |
|
2661 |
default: |
|
2662 |
return null; |
|
2663 |
} |
|
2664 |
||
2665 |
// we have a JCBinary(EQ|NE) |
|
2666 |
// check if we have two literals (constants or null) |
|
2667 |
JCBinary b = (JCBinary)exp; |
|
2668 |
if (b.lhs.type.hasTag(BOT)) return expValueIsNull(eq, b.rhs); |
|
2669 |
if (b.rhs.type.hasTag(BOT)) return expValueIsNull(eq, b.lhs); |
|
2670 |
return null; |
|
2671 |
} |
|
2672 |
private Boolean expValueIsNull(boolean eq, JCTree t) { |
|
2673 |
if (t.type.hasTag(BOT)) return Boolean.valueOf(eq); |
|
2674 |
if (t.hasTag(LITERAL)) return Boolean.valueOf(!eq); |
|
2675 |
return null; |
|
2676 |
} |
|
2677 |
||
10 | 2678 |
/** Visitor method for conditional expressions. |
2679 |
*/ |
|
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2680 |
@Override |
10 | 2681 |
public void visitConditional(JCConditional tree) { |
2682 |
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType); |
|
23791 | 2683 |
if (isTrue(cond)) { |
10 | 2684 |
result = convert(translate(tree.truepart, tree.type), tree.type); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2685 |
addPrunedInfo(cond); |
23791 | 2686 |
} else if (isFalse(cond)) { |
10 | 2687 |
result = convert(translate(tree.falsepart, tree.type), tree.type); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2688 |
addPrunedInfo(cond); |
10 | 2689 |
} else { |
2690 |
// Condition is not a compile-time constant. |
|
2691 |
tree.truepart = translate(tree.truepart, tree.type); |
|
2692 |
tree.falsepart = translate(tree.falsepart, tree.type); |
|
2693 |
result = tree; |
|
2694 |
} |
|
2695 |
} |
|
2696 |
//where |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2697 |
private JCExpression convert(JCExpression tree, Type pt) { |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2698 |
if (tree.type == pt || tree.type.hasTag(BOT)) |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2699 |
return tree; |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2700 |
JCExpression result = make_at(tree.pos()).TypeCast(make.Type(pt), tree); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2701 |
result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt) |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2702 |
: pt; |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2703 |
return result; |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2704 |
} |
10 | 2705 |
|
2706 |
/** Visitor method for if statements. |
|
2707 |
*/ |
|
2708 |
public void visitIf(JCIf tree) { |
|
2709 |
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType); |
|
23791 | 2710 |
if (isTrue(cond)) { |
10 | 2711 |
result = translate(tree.thenpart); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2712 |
addPrunedInfo(cond); |
23791 | 2713 |
} else if (isFalse(cond)) { |
10 | 2714 |
if (tree.elsepart != null) { |
2715 |
result = translate(tree.elsepart); |
|
2716 |
} else { |
|
2717 |
result = make.Skip(); |
|
2718 |
} |
|
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14547
diff
changeset
|
2719 |
addPrunedInfo(cond); |
10 | 2720 |
} else { |
2721 |
// Condition is not a compile-time constant. |
|
2722 |
tree.thenpart = translate(tree.thenpart); |
|
2723 |
tree.elsepart = translate(tree.elsepart); |
|
2724 |
result = tree; |
|
2725 |
} |
|
2726 |
} |
|
2727 |
||
2728 |
/** Visitor method for assert statements. Translate them away. |
|
2729 |
*/ |
|
2730 |
public void visitAssert(JCAssert tree) { |
|
2731 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
2732 |
if (!tree.cond.type.isTrue()) { |
|
2733 |
JCExpression cond = assertFlagTest(tree.pos()); |
|
2734 |
List<JCExpression> exnArgs = (tree.detail == null) ? |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2735 |
List.nil() : List.of(translate(tree.detail)); |
10 | 2736 |
if (!tree.cond.type.isFalse()) { |
2737 |
cond = makeBinary |
|
10950 | 2738 |
(AND, |
10 | 2739 |
cond, |
10950 | 2740 |
makeUnary(NOT, tree.cond)); |
10 | 2741 |
} |
2742 |
result = |
|
2743 |
make.If(cond, |
|
17808 | 2744 |
make_at(tree). |
10 | 2745 |
Throw(makeNewClass(syms.assertionErrorType, exnArgs)), |
2746 |
null); |
|
2747 |
} else { |
|
2748 |
result = make.Skip(); |
|
2749 |
} |
|
2750 |
} |
|
2751 |
||
2752 |
public void visitApply(JCMethodInvocation tree) { |
|
2753 |
Symbol meth = TreeInfo.symbol(tree.meth); |
|
2754 |
List<Type> argtypes = meth.type.getParameterTypes(); |
|
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2755 |
if (meth.name == names.init && meth.owner == syms.enumSym) |
10 | 2756 |
argtypes = argtypes.tail.tail; |
2757 |
tree.args = boxArgs(argtypes, tree.args, tree.varargsElement); |
|
2758 |
tree.varargsElement = null; |
|
2759 |
Name methName = TreeInfo.name(tree.meth); |
|
2760 |
if (meth.name==names.init) { |
|
2761 |
// We are seeing a this(...) or super(...) constructor call. |
|
2762 |
// If an access constructor is used, append null as a last argument. |
|
2763 |
Symbol constructor = accessConstructor(tree.pos(), meth); |
|
2764 |
if (constructor != meth) { |
|
2765 |
tree.args = tree.args.append(makeNull()); |
|
2766 |
TreeInfo.setSymbol(tree.meth, constructor); |
|
2767 |
} |
|
2768 |
||
2769 |
// If we are calling a constructor of a local class, add |
|
2770 |
// free variables after explicit constructor arguments. |
|
2771 |
ClassSymbol c = (ClassSymbol)constructor.owner; |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2772 |
if (c.isLocal()) { |
10 | 2773 |
tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c))); |
2774 |
} |
|
2775 |
||
2776 |
// If we are calling a constructor of an enum class, pass |
|
2777 |
// along the name and ordinal arguments |
|
2778 |
if ((c.flags_field&ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) { |
|
2779 |
List<JCVariableDecl> params = currentMethodDef.params; |
|
2780 |
if (currentMethodSym.owner.hasOuterInstance()) |
|
2781 |
params = params.tail; // drop this$n |
|
2782 |
tree.args = tree.args |
|
2783 |
.prepend(make_at(tree.pos()).Ident(params.tail.head.sym)) // ordinal |
|
2784 |
.prepend(make.Ident(params.head.sym)); // name |
|
2785 |
} |
|
2786 |
||
2787 |
// If we are calling a constructor of a class with an outer |
|
2788 |
// instance, and the call |
|
2789 |
// is qualified, pass qualifier as first argument in front of |
|
2790 |
// the explicit constructor arguments. If the call |
|
2791 |
// is not qualified, pass the correct outer instance as |
|
2792 |
// first argument. |
|
2793 |
if (c.hasOuterInstance()) { |
|
2794 |
JCExpression thisArg; |
|
10950 | 2795 |
if (tree.meth.hasTag(SELECT)) { |
10 | 2796 |
thisArg = attr. |
2797 |
makeNullCheck(translate(((JCFieldAccess) tree.meth).selected)); |
|
2798 |
tree.meth = make.Ident(constructor); |
|
2799 |
((JCIdent) tree.meth).name = methName; |
|
19501
9dac9369db2c
8022053: javac generates unverifiable initializer for nested subclass of local class
vromero
parents:
19495
diff
changeset
|
2800 |
} else if (c.isLocal() || methName == names._this){ |
10 | 2801 |
// local class or this() call |
2802 |
thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym); |
|
2803 |
} else { |
|
8622
4f032629a0fd
6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents:
8434
diff
changeset
|
2804 |
// super() call of nested class - never pick 'this' |
4f032629a0fd
6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents:
8434
diff
changeset
|
2805 |
thisArg = makeOwnerThisN(tree.meth.pos(), c, false); |
10 | 2806 |
} |
2807 |
tree.args = tree.args.prepend(thisArg); |
|
2808 |
} |
|
2809 |
} else { |
|
2810 |
// We are seeing a normal method invocation; translate this as usual. |
|
2811 |
tree.meth = translate(tree.meth); |
|
2812 |
||
2813 |
// If the translated method itself is an Apply tree, we are |
|
2814 |
// seeing an access method invocation. In this case, append |
|
2815 |
// the method arguments to the arguments of the access method. |
|
10950 | 2816 |
if (tree.meth.hasTag(APPLY)) { |
10 | 2817 |
JCMethodInvocation app = (JCMethodInvocation)tree.meth; |
2818 |
app.args = tree.args.prependList(app.args); |
|
2819 |
result = app; |
|
2820 |
return; |
|
2821 |
} |
|
2822 |
} |
|
2823 |
result = tree; |
|
2824 |
} |
|
2825 |
||
2826 |
List<JCExpression> boxArgs(List<Type> parameters, List<JCExpression> _args, Type varargsElement) { |
|
2827 |
List<JCExpression> args = _args; |
|
2828 |
if (parameters.isEmpty()) return args; |
|
2829 |
boolean anyChanges = false; |
|
22163 | 2830 |
ListBuffer<JCExpression> result = new ListBuffer<>(); |
10 | 2831 |
while (parameters.tail.nonEmpty()) { |
2832 |
JCExpression arg = translate(args.head, parameters.head); |
|
2833 |
anyChanges |= (arg != args.head); |
|
2834 |
result.append(arg); |
|
2835 |
args = args.tail; |
|
2836 |
parameters = parameters.tail; |
|
2837 |
} |
|
2838 |
Type parameter = parameters.head; |
|
2839 |
if (varargsElement != null) { |
|
2840 |
anyChanges = true; |
|
22163 | 2841 |
ListBuffer<JCExpression> elems = new ListBuffer<>(); |
10 | 2842 |
while (args.nonEmpty()) { |
2843 |
JCExpression arg = translate(args.head, varargsElement); |
|
2844 |
elems.append(arg); |
|
2845 |
args = args.tail; |
|
2846 |
} |
|
2847 |
JCNewArray boxedArgs = make.NewArray(make.Type(varargsElement), |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2848 |
List.nil(), |
10 | 2849 |
elems.toList()); |
26781
a786b07c7b91
8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents:
25874
diff
changeset
|
2850 |
boxedArgs.type = new ArrayType(varargsElement, syms.arrayClass); |
10 | 2851 |
result.append(boxedArgs); |
2852 |
} else { |
|
2853 |
if (args.length() != 1) throw new AssertionError(args); |
|
2854 |
JCExpression arg = translate(args.head, parameter); |
|
2855 |
anyChanges |= (arg != args.head); |
|
2856 |
result.append(arg); |
|
2857 |
if (!anyChanges) return _args; |
|
2858 |
} |
|
2859 |
return result.toList(); |
|
2860 |
} |
|
2861 |
||
2862 |
/** Expand a boxing or unboxing conversion if needed. */ |
|
2863 |
@SuppressWarnings("unchecked") // XXX unchecked |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2864 |
<T extends JCExpression> T boxIfNeeded(T tree, Type type) { |
10 | 2865 |
boolean havePrimitive = tree.type.isPrimitive(); |
2866 |
if (havePrimitive == type.isPrimitive()) |
|
2867 |
return tree; |
|
2868 |
if (havePrimitive) { |
|
2869 |
Type unboxedTarget = types.unboxedType(type); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
2870 |
if (!unboxedTarget.hasTag(NONE)) { |
2510
d7ba8e16a928
6816548: Uninitialized register when performing casting + auto(un)boxing
mcimadamore
parents:
2220
diff
changeset
|
2871 |
if (!types.isSubtype(tree.type, unboxedTarget)) //e.g. Character c = 89; |
d7ba8e16a928
6816548: Uninitialized register when performing casting + auto(un)boxing
mcimadamore
parents:
2220
diff
changeset
|
2872 |
tree.type = unboxedTarget.constType(tree.type.constValue()); |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2873 |
return (T)boxPrimitive(tree, types.erasure(type)); |
10 | 2874 |
} else { |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2875 |
tree = (T)boxPrimitive(tree); |
10 | 2876 |
} |
2877 |
} else { |
|
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
2878 |
tree = (T)unbox(tree, type); |
10 | 2879 |
} |
2880 |
return tree; |
|
2881 |
} |
|
2882 |
||
2883 |
/** Box up a single primitive expression. */ |
|
2884 |
JCExpression boxPrimitive(JCExpression tree) { |
|
2885 |
return boxPrimitive(tree, types.boxedClass(tree.type).type); |
|
2886 |
} |
|
2887 |
||
2888 |
/** Box up a single primitive expression. */ |
|
2889 |
JCExpression boxPrimitive(JCExpression tree, Type box) { |
|
2890 |
make_at(tree.pos()); |
|
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2891 |
Symbol valueOfSym = lookupMethod(tree.pos(), |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2892 |
names.valueOf, |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2893 |
box, |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2894 |
List.<Type>nil() |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2895 |
.prepend(tree.type)); |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
2896 |
return make.App(make.QualIdent(valueOfSym), List.of(tree)); |
10 | 2897 |
} |
2898 |
||
2899 |
/** Unbox an object to a primitive value. */ |
|
2900 |
JCExpression unbox(JCExpression tree, Type primitive) { |
|
2901 |
Type unboxedType = types.unboxedType(tree.type); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14263
diff
changeset
|
2902 |
if (unboxedType.hasTag(NONE)) { |
6583
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2903 |
unboxedType = primitive; |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2904 |
if (!unboxedType.isPrimitive()) |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2905 |
throw new AssertionError(unboxedType); |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2906 |
make_at(tree.pos()); |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2907 |
tree = make.TypeCast(types.boxedClass(unboxedType).type, tree); |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2908 |
} else { |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2909 |
// There must be a conversion from unboxedType to primitive. |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2910 |
if (!types.isSubtype(unboxedType, primitive)) |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2911 |
throw new AssertionError(tree); |
6410e8c9c848
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
6575
diff
changeset
|
2912 |
} |
10 | 2913 |
make_at(tree.pos()); |
2914 |
Symbol valueSym = lookupMethod(tree.pos(), |
|
2915 |
unboxedType.tsym.name.append(names.Value), // x.intValue() |
|
2916 |
tree.type, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
2917 |
List.nil()); |
10 | 2918 |
return make.App(make.Select(tree, valueSym)); |
2919 |
} |
|
2920 |
||
2921 |
/** Visitor method for parenthesized expressions. |
|
2922 |
* If the subexpression has changed, omit the parens. |
|
2923 |
*/ |
|
2924 |
public void visitParens(JCParens tree) { |
|
2925 |
JCTree expr = translate(tree.expr); |
|
2926 |
result = ((expr == tree.expr) ? tree : expr); |
|
2927 |
} |
|
2928 |
||
2929 |
public void visitIndexed(JCArrayAccess tree) { |
|
2930 |
tree.indexed = translate(tree.indexed); |
|
2931 |
tree.index = translate(tree.index, syms.intType); |
|
2932 |
result = tree; |
|
2933 |
} |
|
2934 |
||
2935 |
public void visitAssign(JCAssign tree) { |
|
2936 |
tree.lhs = translate(tree.lhs, tree); |
|
2937 |
tree.rhs = translate(tree.rhs, tree.lhs.type); |
|
2938 |
||
2939 |
// If translated left hand side is an Apply, we are |
|
2940 |
// seeing an access method invocation. In this case, append |
|
2941 |
// right hand side as last argument of the access method. |
|
10950 | 2942 |
if (tree.lhs.hasTag(APPLY)) { |
10 | 2943 |
JCMethodInvocation app = (JCMethodInvocation)tree.lhs; |
2944 |
app.args = List.of(tree.rhs).prependList(app.args); |
|
2945 |
result = app; |
|
2946 |
} else { |
|
2947 |
result = tree; |
|
2948 |
} |
|
2949 |
} |
|
2950 |
||
2951 |
public void visitAssignop(final JCAssignOp tree) { |
|
15712
fa256e8cfe33
7167125: Two variables after the same operation in a inner class return different results
vromero
parents:
15565
diff
changeset
|
2952 |
final boolean boxingReq = !tree.lhs.type.isPrimitive() && |
fa256e8cfe33
7167125: Two variables after the same operation in a inner class return different results
vromero
parents:
15565
diff
changeset
|
2953 |
tree.operator.type.getReturnType().isPrimitive(); |
fa256e8cfe33
7167125: Two variables after the same operation in a inner class return different results
vromero
parents:
15565
diff
changeset
|
2954 |
|
40306
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
2955 |
AssignopDependencyScanner depScanner = new AssignopDependencyScanner(tree); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
2956 |
depScanner.scan(tree.rhs); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
2957 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
2958 |
if (boxingReq || depScanner.dependencyFound) { |
16334
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2959 |
// boxing required; need to rewrite as x = (unbox typeof x)(x op y); |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2960 |
// or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y) |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2961 |
// (but without recomputing x) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2962 |
JCTree newTree = abstractLval(tree.lhs, lhs -> { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2963 |
Tag newTag = tree.getTag().noAssignOp(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2964 |
// Erasure (TransTypes) can change the type of |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2965 |
// tree.lhs. However, we can still get the |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2966 |
// unerased type of tree.lhs as it is stored |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2967 |
// in tree.type in Attr. |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2968 |
OperatorSymbol newOperator = operators.resolveBinary(tree, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2969 |
newTag, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2970 |
tree.type, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2971 |
tree.rhs.type); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2972 |
//Need to use the "lhs" at two places, once on the future left hand side |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2973 |
//and once in the future binary operator. But further processing may change |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2974 |
//the components of the tree in place (see visitSelect for e.g. <Class>.super.<ident>), |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2975 |
//so cloning the tree to avoid interference between the uses: |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2976 |
JCExpression expr = (JCExpression) lhs.clone(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2977 |
if (expr.type != tree.type) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2978 |
expr = make.TypeCast(tree.type, expr); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2979 |
JCBinary opResult = make.Binary(newTag, expr, tree.rhs); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2980 |
opResult.operator = newOperator; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2981 |
opResult.type = newOperator.type.getReturnType(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2982 |
JCExpression newRhs = boxingReq ? |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2983 |
make.TypeCast(types.unboxedType(tree.type), opResult) : |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2984 |
opResult; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2985 |
return make.Assign(lhs, newRhs).setType(tree.type); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
2986 |
}); |
16334
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2987 |
result = translate(newTree); |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2988 |
return; |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2989 |
} |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2990 |
tree.lhs = translate(tree.lhs, tree); |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2991 |
tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2992 |
|
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2993 |
// If translated left hand side is an Apply, we are |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2994 |
// seeing an access method invocation. In this case, append |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2995 |
// right hand side as last argument of the access method. |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2996 |
if (tree.lhs.hasTag(APPLY)) { |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2997 |
JCMethodInvocation app = (JCMethodInvocation)tree.lhs; |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2998 |
// if operation is a += on strings, |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
2999 |
// make sure to convert argument to string |
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39806
diff
changeset
|
3000 |
JCExpression rhs = tree.operator.opcode == string_add |
16334
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3001 |
? makeString(tree.rhs) |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3002 |
: tree.rhs; |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3003 |
app.args = List.of(rhs).prependList(app.args); |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3004 |
result = app; |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3005 |
} else { |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3006 |
result = tree; |
49b3f71982f6
8009170: Regression: javac generates redundant bytecode in assignop involving arrays
vromero
parents:
16325
diff
changeset
|
3007 |
} |
10 | 3008 |
} |
3009 |
||
40306
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3010 |
class AssignopDependencyScanner extends TreeScanner { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3011 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3012 |
Symbol sym; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3013 |
boolean dependencyFound = false; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3014 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3015 |
AssignopDependencyScanner(JCAssignOp tree) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3016 |
this.sym = TreeInfo.symbol(tree.lhs); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3017 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3018 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3019 |
@Override |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3020 |
public void scan(JCTree tree) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3021 |
if (tree != null && sym != null) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3022 |
tree.accept(this); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3023 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3024 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3025 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3026 |
@Override |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3027 |
public void visitAssignop(JCAssignOp tree) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3028 |
if (TreeInfo.symbol(tree.lhs) == sym) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3029 |
dependencyFound = true; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3030 |
return; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3031 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3032 |
super.visitAssignop(tree); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3033 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3034 |
|
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3035 |
@Override |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3036 |
public void visitUnary(JCUnary tree) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3037 |
if (TreeInfo.symbol(tree.arg) == sym) { |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3038 |
dependencyFound = true; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3039 |
return; |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3040 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3041 |
super.visitUnary(tree); |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3042 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3043 |
} |
1a0fcaf3f2ed
8160156: javac is generating let expressions unnecessarily
vromero
parents:
40236
diff
changeset
|
3044 |
|
10 | 3045 |
/** Lower a tree of the form e++ or e-- where e is an object type */ |
33707
d74fef6b01e0
8141639: Signatures in Lower could be made tighter by using JCExpression instead of JCTree
mcimadamore
parents:
31940
diff
changeset
|
3046 |
JCExpression lowerBoxedPostop(final JCUnary tree) { |
10 | 3047 |
// translate to tmp1=lval(e); tmp2=tmp1; tmp1 OP 1; tmp2 |
3048 |
// or |
|
3049 |
// translate to tmp1=lval(e); tmp2=tmp1; (typeof tree)tmp1 OP 1; tmp2 |
|
3050 |
// where OP is += or -= |
|
10950 | 3051 |
final boolean cast = TreeInfo.skipParens(tree.arg).hasTag(TYPECAST); |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3052 |
return abstractLval(tree.arg, tmp1 -> abstractRval(tmp1, tree.arg.type, tmp2 -> { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3053 |
Tag opcode = (tree.hasTag(POSTINC)) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3054 |
? PLUS_ASG : MINUS_ASG; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3055 |
//"tmp1" and "tmp2" may refer to the same instance |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3056 |
//(for e.g. <Class>.super.<ident>). But further processing may |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3057 |
//change the components of the tree in place (see visitSelect), |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3058 |
//so cloning the tree to avoid interference between the two uses: |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3059 |
JCExpression lhs = (JCExpression)tmp1.clone(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3060 |
lhs = cast |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3061 |
? make.TypeCast(tree.arg.type, lhs) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3062 |
: lhs; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3063 |
JCExpression update = makeAssignop(opcode, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3064 |
lhs, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3065 |
make.Literal(1)); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3066 |
return makeComma(update, tmp2); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42499
diff
changeset
|
3067 |
})); |
10 | 3068 |
} |
3069 |
||
3070 |
public void visitUnary(JCUnary tree) { |
|
10950 | 3071 |
boolean isUpdateOperator = tree.getTag().isIncOrDecUnaryOp(); |
10 | 3072 |
if (isUpdateOperator && !tree.arg.type.isPrimitive()) { |
3073 |
switch(tree.getTag()) { |
|
10950 | 3074 |
case PREINC: // ++ e |
10 | 3075 |
// translate to e += 1 |
10950 | 3076 |
case PREDEC: // -- e |
10 | 3077 |
// translate to e -= 1 |
3078 |
{ |
|
10950 | 3079 |
JCTree.Tag opcode = (tree.hasTag(PREINC)) |
3080 |
? PLUS_ASG : MINUS_ASG; |
|
10 | 3081 |
JCAssignOp newTree = makeAssignop(opcode, |
3082 |
tree.arg, |
|
3083 |
make.Literal(1)); |
|
3084 |
result = translate(newTree, tree.type); |
|
3085 |
return; |
|
3086 |
} |
|
10950 | 3087 |
case POSTINC: // e ++ |
3088 |
case POSTDEC: // e -- |
|
10 | 3089 |
{ |
3090 |
result = translate(lowerBoxedPostop(tree), tree.type); |
|
3091 |
return; |
|
3092 |
} |
|
3093 |
} |
|
3094 |
throw new AssertionError(tree); |
|
3095 |
} |
|
3096 |
||
3097 |
tree.arg = boxIfNeeded(translate(tree.arg, tree), tree.type); |
|
3098 |
||
10950 | 3099 |
if (tree.hasTag(NOT) && tree.arg.type.constValue() != null) { |
10 | 3100 |
tree.type = cfolder.fold1(bool_not, tree.arg.type); |
3101 |
} |
|
3102 |
||
3103 |
// If translated left hand side is an Apply, we are |
|
3104 |
// seeing an access method invocation. In this case, return |
|
4142 | 3105 |
// that access method invocation as result. |
10950 | 3106 |
if (isUpdateOperator && tree.arg.hasTag(APPLY)) { |
10 | 3107 |
result = tree.arg; |
3108 |
} else { |
|
3109 |
result = tree; |
|
3110 |
} |
|
3111 |
} |
|
3112 |
||
3113 |
public void visitBinary(JCBinary tree) { |
|
3114 |
List<Type> formals = tree.operator.type.getParameterTypes(); |
|
3115 |
JCTree lhs = tree.lhs = translate(tree.lhs, formals.head); |
|
3116 |
switch (tree.getTag()) { |
|
10950 | 3117 |
case OR: |
23791 | 3118 |
if (isTrue(lhs)) { |
10 | 3119 |
result = lhs; |
3120 |
return; |
|
3121 |
} |
|
23791 | 3122 |
if (isFalse(lhs)) { |
10 | 3123 |
result = translate(tree.rhs, formals.tail.head); |
3124 |
return; |
|
3125 |
} |
|
3126 |
break; |
|
10950 | 3127 |
case AND: |
23791 | 3128 |
if (isFalse(lhs)) { |
10 | 3129 |
result = lhs; |
3130 |
return; |
|
3131 |
} |
|
23791 | 3132 |
if (isTrue(lhs)) { |
10 | 3133 |
result = translate(tree.rhs, formals.tail.head); |
3134 |
return; |
|
3135 |
} |
|
3136 |
break; |
|
3137 |
} |
|
3138 |
tree.rhs = translate(tree.rhs, formals.tail.head); |
|
3139 |
result = tree; |
|
3140 |
} |
|
3141 |
||
3142 |
public void visitIdent(JCIdent tree) { |
|
3143 |
result = access(tree.sym, tree, enclOp, false); |
|
3144 |
} |
|
3145 |
||
3146 |
/** Translate away the foreach loop. */ |
|
3147 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
3148 |
if (types.elemtype(tree.expr.type) == null) |
|
3149 |
visitIterableForeachLoop(tree); |
|
3150 |
else |
|
3151 |
visitArrayForeachLoop(tree); |
|
3152 |
} |
|
3153 |
// where |
|
3154 |
/** |
|
4142 | 3155 |
* A statement of the form |
10 | 3156 |
* |
3157 |
* <pre> |
|
3158 |
* for ( T v : arrayexpr ) stmt; |
|
3159 |
* </pre> |
|
3160 |
* |
|
3161 |
* (where arrayexpr is of an array type) gets translated to |
|
3162 |
* |
|
13844 | 3163 |
* <pre>{@code |
10 | 3164 |
* for ( { arraytype #arr = arrayexpr; |
3165 |
* int #len = array.length; |
|
3166 |
* int #i = 0; }; |
|
3167 |
* #i < #len; i$++ ) { |
|
3168 |
* T v = arr$[#i]; |
|
3169 |
* stmt; |
|
3170 |
* } |
|
13844 | 3171 |
* }</pre> |
10 | 3172 |
* |
3173 |
* where #arr, #len, and #i are freshly named synthetic local variables. |
|
3174 |
*/ |
|
3175 |
private void visitArrayForeachLoop(JCEnhancedForLoop tree) { |
|
3176 |
make_at(tree.expr.pos()); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
3177 |
VarSymbol arraycache = new VarSymbol(SYNTHETIC, |
10 | 3178 |
names.fromString("arr" + target.syntheticNameChar()), |
3179 |
tree.expr.type, |
|
3180 |
currentMethodSym); |
|
3181 |
JCStatement arraycachedef = make.VarDef(arraycache, tree.expr); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
3182 |
VarSymbol lencache = new VarSymbol(SYNTHETIC, |
10 | 3183 |
names.fromString("len" + target.syntheticNameChar()), |
3184 |
syms.intType, |
|
3185 |
currentMethodSym); |
|
3186 |
JCStatement lencachedef = make. |
|
3187 |
VarDef(lencache, make.Select(make.Ident(arraycache), syms.lengthVar)); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
3188 |
VarSymbol index = new VarSymbol(SYNTHETIC, |
10 | 3189 |
names.fromString("i" + target.syntheticNameChar()), |
3190 |
syms.intType, |
|
3191 |
currentMethodSym); |
|
3192 |
||
3193 |
JCVariableDecl indexdef = make.VarDef(index, make.Literal(INT, 0)); |
|
3194 |
indexdef.init.type = indexdef.type = syms.intType.constType(0); |
|
3195 |
||
48504
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3196 |
List<JCStatement> loopinit = List.of(arraycachedef, lencachedef, indexdef); |
10950 | 3197 |
JCBinary cond = makeBinary(LT, make.Ident(index), make.Ident(lencache)); |
3198 |
||
3199 |
JCExpressionStatement step = make.Exec(makeUnary(PREINC, make.Ident(index))); |
|
10 | 3200 |
|
3201 |
Type elemtype = types.elemtype(tree.expr.type); |
|
517
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3202 |
JCExpression loopvarinit = make.Indexed(make.Ident(arraycache), |
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3203 |
make.Ident(index)).setType(elemtype); |
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3204 |
JCVariableDecl loopvardef = (JCVariableDecl)make.VarDef(tree.var.mods, |
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3205 |
tree.var.name, |
48504
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3206 |
tree.var.vartype, |
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3207 |
loopvarinit).setType(tree.var.type); |
517
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3208 |
loopvardef.sym = tree.var.sym; |
48504
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3209 |
JCBlock body = make. |
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3210 |
Block(0, List.of(loopvardef, tree.body)); |
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3211 |
|
10 | 3212 |
result = translate(make. |
3213 |
ForLoop(loopinit, |
|
3214 |
cond, |
|
3215 |
List.of(step), |
|
3216 |
body)); |
|
3217 |
patchTargets(body, tree, result); |
|
3218 |
} |
|
3219 |
/** Patch up break and continue targets. */ |
|
3220 |
private void patchTargets(JCTree body, final JCTree src, final JCTree dest) { |
|
3221 |
class Patcher extends TreeScanner { |
|
3222 |
public void visitBreak(JCBreak tree) { |
|
3223 |
if (tree.target == src) |
|
3224 |
tree.target = dest; |
|
3225 |
} |
|
55306 | 3226 |
public void visitYield(JCYield tree) { |
3227 |
if (tree.target == src) |
|
3228 |
tree.target = dest; |
|
3229 |
scan(tree.value); |
|
3230 |
} |
|
10 | 3231 |
public void visitContinue(JCContinue tree) { |
3232 |
if (tree.target == src) |
|
3233 |
tree.target = dest; |
|
3234 |
} |
|
3235 |
public void visitClassDef(JCClassDecl tree) {} |
|
3236 |
} |
|
3237 |
new Patcher().scan(body); |
|
3238 |
} |
|
3239 |
/** |
|
3240 |
* A statement of the form |
|
3241 |
* |
|
3242 |
* <pre> |
|
3243 |
* for ( T v : coll ) stmt ; |
|
3244 |
* </pre> |
|
3245 |
* |
|
13844 | 3246 |
* (where coll implements {@code Iterable<? extends T>}) gets translated to |
10 | 3247 |
* |
13844 | 3248 |
* <pre>{@code |
10 | 3249 |
* for ( Iterator<? extends T> #i = coll.iterator(); #i.hasNext(); ) { |
3250 |
* T v = (T) #i.next(); |
|
3251 |
* stmt; |
|
3252 |
* } |
|
13844 | 3253 |
* }</pre> |
10 | 3254 |
* |
3255 |
* where #i is a freshly named synthetic local variable. |
|
3256 |
*/ |
|
3257 |
private void visitIterableForeachLoop(JCEnhancedForLoop tree) { |
|
3258 |
make_at(tree.expr.pos()); |
|
3259 |
Type iteratorTarget = syms.objectType; |
|
24612
75dc732b45af
8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents:
24604
diff
changeset
|
3260 |
Type iterableType = types.asSuper(types.cvarUpperBound(tree.expr.type), |
10 | 3261 |
syms.iterableType.tsym); |
3262 |
if (iterableType.getTypeArguments().nonEmpty()) |
|
3263 |
iteratorTarget = types.erasure(iterableType.getTypeArguments().head); |
|
29292
c10d63c667cd
8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents:
29051
diff
changeset
|
3264 |
Type eType = types.skipTypeVars(tree.expr.type, false); |
10 | 3265 |
tree.expr.type = types.erasure(eType); |
16965
e00f419042b7
8011432: javac, compiler regression iterable + captured type
vromero
parents:
16558
diff
changeset
|
3266 |
if (eType.isCompound()) |
10 | 3267 |
tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr); |
3268 |
Symbol iterator = lookupMethod(tree.expr.pos(), |
|
3269 |
names.iterator, |
|
16552
59519c4e7603
5053846: javac: MethodRef entries are duplicated in the constant pool
vromero
parents:
16336
diff
changeset
|
3270 |
eType, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3271 |
List.nil()); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19927
diff
changeset
|
3272 |
VarSymbol itvar = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()), |
19495
0a4337ec0592
8013394: compile of iterator use fails with error \"defined in an inaccessible class or interface\"
vromero
parents:
18377
diff
changeset
|
3273 |
types.erasure(types.asSuper(iterator.type.getReturnType(), syms.iteratorType.tsym)), |
10 | 3274 |
currentMethodSym); |
16552
59519c4e7603
5053846: javac: MethodRef entries are duplicated in the constant pool
vromero
parents:
16336
diff
changeset
|
3275 |
|
48504
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3276 |
JCStatement init = make. |
16552
59519c4e7603
5053846: javac: MethodRef entries are duplicated in the constant pool
vromero
parents:
16336
diff
changeset
|
3277 |
VarDef(itvar, make.App(make.Select(tree.expr, iterator) |
59519c4e7603
5053846: javac: MethodRef entries are duplicated in the constant pool
vromero
parents:
16336
diff
changeset
|
3278 |
.setType(types.erasure(iterator.type)))); |
59519c4e7603
5053846: javac: MethodRef entries are duplicated in the constant pool
vromero
parents:
16336
diff
changeset
|
3279 |
|
10 | 3280 |
Symbol hasNext = lookupMethod(tree.expr.pos(), |
3281 |
names.hasNext, |
|
3282 |
itvar.type, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3283 |
List.nil()); |
10 | 3284 |
JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext)); |
3285 |
Symbol next = lookupMethod(tree.expr.pos(), |
|
3286 |
names.next, |
|
3287 |
itvar.type, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3288 |
List.nil()); |
10 | 3289 |
JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next)); |
940
0d5cabfaffde
6500701: Enhanced for loop with generics generates faulty bytecode
mcimadamore
parents:
735
diff
changeset
|
3290 |
if (tree.var.type.isPrimitive()) |
24612
75dc732b45af
8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents:
24604
diff
changeset
|
3291 |
vardefinit = make.TypeCast(types.cvarUpperBound(iteratorTarget), vardefinit); |
940
0d5cabfaffde
6500701: Enhanced for loop with generics generates faulty bytecode
mcimadamore
parents:
735
diff
changeset
|
3292 |
else |
0d5cabfaffde
6500701: Enhanced for loop with generics generates faulty bytecode
mcimadamore
parents:
735
diff
changeset
|
3293 |
vardefinit = make.TypeCast(tree.var.type, vardefinit); |
517
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3294 |
JCVariableDecl indexDef = (JCVariableDecl)make.VarDef(tree.var.mods, |
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3295 |
tree.var.name, |
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3296 |
tree.var.vartype, |
48082
d8c634b016c6
8192885: Compiler in JDK 10-ea+33 misses to include entry in LineNumberTable for goto instruction of foreach loop
vromero
parents:
47864
diff
changeset
|
3297 |
vardefinit).setType(tree.var.type); |
517
ea4702edd512
6682380: Foreach loop with generics inside finally block crashes javac with -target 1.5
mcimadamore
parents:
161
diff
changeset
|
3298 |
indexDef.sym = tree.var.sym; |
48082
d8c634b016c6
8192885: Compiler in JDK 10-ea+33 misses to include entry in LineNumberTable for goto instruction of foreach loop
vromero
parents:
47864
diff
changeset
|
3299 |
JCBlock body = make.Block(0, List.of(indexDef, tree.body)); |
2220
dfae517d8078
6807255: LineNumberTable wrong if enhanced-for-loops are used
mcimadamore
parents:
1468
diff
changeset
|
3300 |
body.endpos = TreeInfo.endPos(tree.body); |
10 | 3301 |
result = translate(make. |
48504
25732365355c
8194836: delta apply changesets for JDK-8192885 and JDK-8175883
vromero
parents:
48082
diff
changeset
|
3302 |
ForLoop(List.of(init), |
10 | 3303 |
cond, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3304 |
List.nil(), |
10 | 3305 |
body)); |
3306 |
patchTargets(body, tree, result); |
|
3307 |
} |
|
3308 |
||
3309 |
public void visitVarDef(JCVariableDecl tree) { |
|
3310 |
MethodSymbol oldMethodSym = currentMethodSym; |
|
3311 |
tree.mods = translate(tree.mods); |
|
3312 |
tree.vartype = translate(tree.vartype); |
|
3313 |
if (currentMethodSym == null) { |
|
3314 |
// A class or instance field initializer. |
|
3315 |
currentMethodSym = |
|
3316 |
new MethodSymbol((tree.mods.flags&STATIC) | BLOCK, |
|
3317 |
names.empty, null, |
|
3318 |
currentClass); |
|
3319 |
} |
|
3320 |
if (tree.init != null) tree.init = translate(tree.init, tree.type); |
|
3321 |
result = tree; |
|
3322 |
currentMethodSym = oldMethodSym; |
|
3323 |
} |
|
3324 |
||
3325 |
public void visitBlock(JCBlock tree) { |
|
3326 |
MethodSymbol oldMethodSym = currentMethodSym; |
|
3327 |
if (currentMethodSym == null) { |
|
3328 |
// Block is a static or instance initializer. |
|
3329 |
currentMethodSym = |
|
3330 |
new MethodSymbol(tree.flags | BLOCK, |
|
3331 |
names.empty, null, |
|
3332 |
currentClass); |
|
3333 |
} |
|
3334 |
super.visitBlock(tree); |
|
3335 |
currentMethodSym = oldMethodSym; |
|
3336 |
} |
|
3337 |
||
3338 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
3339 |
tree.body = translate(tree.body); |
|
3340 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
3341 |
result = tree; |
|
3342 |
} |
|
3343 |
||
3344 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
3345 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
3346 |
tree.body = translate(tree.body); |
|
3347 |
result = tree; |
|
3348 |
} |
|
3349 |
||
3350 |
public void visitForLoop(JCForLoop tree) { |
|
3351 |
tree.init = translate(tree.init); |
|
3352 |
if (tree.cond != null) |
|
3353 |
tree.cond = translate(tree.cond, syms.booleanType); |
|
3354 |
tree.step = translate(tree.step); |
|
3355 |
tree.body = translate(tree.body); |
|
3356 |
result = tree; |
|
3357 |
} |
|
3358 |
||
3359 |
public void visitReturn(JCReturn tree) { |
|
3360 |
if (tree.expr != null) |
|
3361 |
tree.expr = translate(tree.expr, |
|
3362 |
types.erasure(currentMethodDef |
|
3363 |
.restype.type)); |
|
3364 |
result = tree; |
|
3365 |
} |
|
3366 |
||
3367 |
public void visitSwitch(JCSwitch tree) { |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3368 |
handleSwitch(tree, tree.selector, tree.cases); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3369 |
} |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3370 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3371 |
@Override |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3372 |
public void visitSwitchExpression(JCSwitchExpression tree) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3373 |
if (tree.cases.stream().noneMatch(c -> c.pats.isEmpty())) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3374 |
JCThrow thr = make.Throw(makeNewClass(syms.incompatibleClassChangeErrorType, |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3375 |
List.nil())); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3376 |
JCCase c = make.Case(JCCase.STATEMENT, List.nil(), List.of(thr), null); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3377 |
tree.cases = tree.cases.append(c); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3378 |
} |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3379 |
handleSwitch(tree, tree.selector, tree.cases); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3380 |
} |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3381 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3382 |
private void handleSwitch(JCTree tree, JCExpression selector, List<JCCase> cases) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3383 |
//expand multiple label cases: |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3384 |
ListBuffer<JCCase> convertedCases = new ListBuffer<>(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3385 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3386 |
for (JCCase c : cases) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3387 |
switch (c.pats.size()) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3388 |
case 0: //default |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3389 |
case 1: //single label |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3390 |
convertedCases.append(c); |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3391 |
break; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3392 |
default: //multiple labels, expand: |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3393 |
//case C1, C2, C3: ... |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3394 |
//=> |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3395 |
//case C1: |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3396 |
//case C2: |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3397 |
//case C3: ... |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3398 |
List<JCExpression> patterns = c.pats; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3399 |
while (patterns.tail.nonEmpty()) { |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3400 |
convertedCases.append(make_at(c.pos()).Case(JCCase.STATEMENT, |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3401 |
List.of(patterns.head), |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3402 |
List.nil(), |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3403 |
null)); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3404 |
patterns = patterns.tail; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3405 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3406 |
c.pats = patterns; |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3407 |
convertedCases.append(c); |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3408 |
break; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3409 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3410 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3411 |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3412 |
for (JCCase c : convertedCases) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3413 |
if (c.caseKind == JCCase.RULE && c.completesNormally) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3414 |
JCBreak b = make_at(c.pos()).Break(null); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3415 |
b.target = tree; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3416 |
c.stats = c.stats.append(b); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3417 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3418 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3419 |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3420 |
cases = convertedCases.toList(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3421 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3422 |
Type selsuper = types.supertype(selector.type); |
10 | 3423 |
boolean enumSwitch = selsuper != null && |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3424 |
(selector.type.tsym.flags() & ENUM) != 0; |
4142 | 3425 |
boolean stringSwitch = selsuper != null && |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3426 |
types.isSameType(selector.type, syms.stringType); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3427 |
Type target = enumSwitch ? selector.type : |
4142 | 3428 |
(stringSwitch? syms.stringType : syms.intType); |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3429 |
selector = translate(selector, target); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3430 |
cases = translateCases(cases); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3431 |
if (tree.hasTag(SWITCH)) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3432 |
((JCSwitch) tree).selector = selector; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3433 |
((JCSwitch) tree).cases = cases; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3434 |
} else if (tree.hasTag(SWITCH_EXPRESSION)) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3435 |
((JCSwitchExpression) tree).selector = selector; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3436 |
((JCSwitchExpression) tree).cases = cases; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3437 |
} else { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3438 |
Assert.error(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3439 |
} |
10 | 3440 |
if (enumSwitch) { |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3441 |
result = visitEnumSwitch(tree, selector, cases); |
4142 | 3442 |
} else if (stringSwitch) { |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3443 |
result = visitStringSwitch(tree, selector, cases); |
10 | 3444 |
} else { |
3445 |
result = tree; |
|
3446 |
} |
|
3447 |
} |
|
3448 |
||
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3449 |
public JCTree visitEnumSwitch(JCTree tree, JCExpression selector, List<JCCase> cases) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3450 |
TypeSymbol enumSym = selector.type.tsym; |
10 | 3451 |
EnumMapping map = mapForEnum(tree.pos(), enumSym); |
3452 |
make_at(tree.pos()); |
|
3453 |
Symbol ordinalMethod = lookupMethod(tree.pos(), |
|
3454 |
names.ordinal, |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3455 |
selector.type, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3456 |
List.nil()); |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3457 |
JCArrayAccess newSelector = make.Indexed(map.mapVar, |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3458 |
make.App(make.Select(selector, |
10 | 3459 |
ordinalMethod))); |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3460 |
ListBuffer<JCCase> newCases = new ListBuffer<>(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3461 |
for (JCCase c : cases) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3462 |
if (c.pats.nonEmpty()) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3463 |
VarSymbol label = (VarSymbol)TreeInfo.symbol(c.pats.head); |
10 | 3464 |
JCLiteral pat = map.forConstant(label); |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3465 |
newCases.append(make.Case(JCCase.STATEMENT, List.of(pat), c.stats, null)); |
10 | 3466 |
} else { |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3467 |
newCases.append(c); |
10 | 3468 |
} |
3469 |
} |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3470 |
JCTree enumSwitch; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3471 |
if (tree.hasTag(SWITCH)) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3472 |
enumSwitch = make.Switch(newSelector, newCases.toList()); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3473 |
} else if (tree.hasTag(SWITCH_EXPRESSION)) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3474 |
enumSwitch = make.SwitchExpression(newSelector, newCases.toList()); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3475 |
enumSwitch.setType(tree.type); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3476 |
} else { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3477 |
Assert.error(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3478 |
throw new AssertionError(); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3479 |
} |
4417
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3480 |
patchTargets(enumSwitch, tree, enumSwitch); |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3481 |
return enumSwitch; |
10 | 3482 |
} |
3483 |
||
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3484 |
public JCTree visitStringSwitch(JCTree tree, JCExpression selector, List<JCCase> caseList) { |
4142 | 3485 |
int alternatives = caseList.size(); |
3486 |
||
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3487 |
if (alternatives == 0) { // Strange but legal possibility (only legal for switch statement) |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3488 |
return make.at(tree.pos()).Exec(attr.makeNullCheck(selector)); |
4142 | 3489 |
} else { |
3490 |
/* |
|
3491 |
* The general approach used is to translate a single |
|
3492 |
* string switch statement into a series of two chained |
|
3493 |
* switch statements: the first a synthesized statement |
|
3494 |
* switching on the argument string's hash value and |
|
3495 |
* computing a string's position in the list of original |
|
3496 |
* case labels, if any, followed by a second switch on the |
|
3497 |
* computed integer value. The second switch has the same |
|
3498 |
* code structure as the original string switch statement |
|
3499 |
* except that the string case labels are replaced with |
|
3500 |
* positional integer constants starting at 0. |
|
3501 |
* |
|
3502 |
* The first switch statement can be thought of as an |
|
3503 |
* inlined map from strings to their position in the case |
|
3504 |
* label list. An alternate implementation would use an |
|
3505 |
* actual Map for this purpose, as done for enum switches. |
|
3506 |
* |
|
3507 |
* With some additional effort, it would be possible to |
|
3508 |
* use a single switch statement on the hash code of the |
|
3509 |
* argument, but care would need to be taken to preserve |
|
3510 |
* the proper control flow in the presence of hash |
|
3511 |
* collisions and other complications, such as |
|
3512 |
* fallthroughs. Switch statements with one or two |
|
3513 |
* alternatives could also be specially translated into |
|
3514 |
* if-then statements to omit the computation of the hash |
|
3515 |
* code. |
|
3516 |
* |
|
3517 |
* The generated code assumes that the hashing algorithm |
|
3518 |
* of String is the same in the compilation environment as |
|
3519 |
* in the environment the code will run in. The string |
|
3520 |
* hashing algorithm in the SE JDK has been unchanged |
|
4417
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3521 |
* since at least JDK 1.2. Since the algorithm has been |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3522 |
* specified since that release as well, it is very |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3523 |
* unlikely to be changed in the future. |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3524 |
* |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3525 |
* Different hashing algorithms, such as the length of the |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3526 |
* strings or a perfect hashing algorithm over the |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3527 |
* particular set of case labels, could potentially be |
fc5cc811d2dd
6906748: Project Coin: Minor strings in switch cleanup
darcy
parents:
4142
diff
changeset
|
3528 |
* used instead of String.hashCode. |
4142 | 3529 |
*/ |
3530 |
||
22163 | 3531 |
ListBuffer<JCStatement> stmtList = new ListBuffer<>(); |
4142 | 3532 |
|
3533 |
// Map from String case labels to their original position in |
|
3534 |
// the list of case labels. |
|
22163 | 3535 |
Map<String, Integer> caseLabelToPosition = new LinkedHashMap<>(alternatives + 1, 1.0f); |
4142 | 3536 |
|
3537 |
// Map of hash codes to the string case labels having that hashCode. |
|
22163 | 3538 |
Map<Integer, Set<String>> hashToString = new LinkedHashMap<>(alternatives + 1, 1.0f); |
4142 | 3539 |
|
3540 |
int casePosition = 0; |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3541 |
|
4142 | 3542 |
for(JCCase oneCase : caseList) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3543 |
if (oneCase.pats.nonEmpty()) { // pats is empty for a "default" case |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3544 |
JCExpression expression = oneCase.pats.head; |
4142 | 3545 |
String labelExpr = (String) expression.type.constValue(); |
3546 |
Integer mapping = caseLabelToPosition.put(labelExpr, casePosition); |
|
8032 | 3547 |
Assert.checkNull(mapping); |
4142 | 3548 |
int hashCode = labelExpr.hashCode(); |
3549 |
||
3550 |
Set<String> stringSet = hashToString.get(hashCode); |
|
3551 |
if (stringSet == null) { |
|
22163 | 3552 |
stringSet = new LinkedHashSet<>(1, 1.0f); |
4142 | 3553 |
stringSet.add(labelExpr); |
3554 |
hashToString.put(hashCode, stringSet); |
|
3555 |
} else { |
|
3556 |
boolean added = stringSet.add(labelExpr); |
|
8032 | 3557 |
Assert.check(added); |
4142 | 3558 |
} |
3559 |
} |
|
3560 |
casePosition++; |
|
3561 |
} |
|
3562 |
||
3563 |
// Synthesize a switch statement that has the effect of |
|
3564 |
// mapping from a string to the integer position of that |
|
3565 |
// string in the list of case labels. This is done by |
|
3566 |
// switching on the hashCode of the string followed by an |
|
3567 |
// if-then-else chain comparing the input for equality |
|
3568 |
// with all the case labels having that hash value. |
|
3569 |
||
3570 |
/* |
|
3571 |
* s$ = top of stack; |
|
3572 |
* tmp$ = -1; |
|
3573 |
* switch($s.hashCode()) { |
|
3574 |
* case caseLabel.hashCode: |
|
3575 |
* if (s$.equals("caseLabel_1") |
|
3576 |
* tmp$ = caseLabelToPosition("caseLabel_1"); |
|
3577 |
* else if (s$.equals("caseLabel_2")) |
|
3578 |
* tmp$ = caseLabelToPosition("caseLabel_2"); |
|
3579 |
* ... |
|
3580 |
* break; |
|
3581 |
* ... |
|
3582 |
* } |
|
3583 |
*/ |
|
3584 |
||
3585 |
VarSymbol dollar_s = new VarSymbol(FINAL|SYNTHETIC, |
|
3586 |
names.fromString("s" + tree.pos + target.syntheticNameChar()), |
|
3587 |
syms.stringType, |
|
3588 |
currentMethodSym); |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3589 |
stmtList.append(make.at(tree.pos()).VarDef(dollar_s, selector).setType(dollar_s.type)); |
4142 | 3590 |
|
3591 |
VarSymbol dollar_tmp = new VarSymbol(SYNTHETIC, |
|
3592 |
names.fromString("tmp" + tree.pos + target.syntheticNameChar()), |
|
3593 |
syms.intType, |
|
3594 |
currentMethodSym); |
|
3595 |
JCVariableDecl dollar_tmp_def = |
|
3596 |
(JCVariableDecl)make.VarDef(dollar_tmp, make.Literal(INT, -1)).setType(dollar_tmp.type); |
|
3597 |
dollar_tmp_def.init.type = dollar_tmp.type = syms.intType; |
|
3598 |
stmtList.append(dollar_tmp_def); |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19941
diff
changeset
|
3599 |
ListBuffer<JCCase> caseBuffer = new ListBuffer<>(); |
4142 | 3600 |
// hashCode will trigger nullcheck on original switch expression |
3601 |
JCMethodInvocation hashCodeCall = makeCall(make.Ident(dollar_s), |
|
3602 |
names.hashCode, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3603 |
List.nil()).setType(syms.intType); |
4142 | 3604 |
JCSwitch switch1 = make.Switch(hashCodeCall, |
3605 |
caseBuffer.toList()); |
|
3606 |
for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) { |
|
3607 |
int hashCode = entry.getKey(); |
|
3608 |
Set<String> stringsWithHashCode = entry.getValue(); |
|
8032 | 3609 |
Assert.check(stringsWithHashCode.size() >= 1); |
4142 | 3610 |
|
3611 |
JCStatement elsepart = null; |
|
3612 |
for(String caseLabel : stringsWithHashCode ) { |
|
3613 |
JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s), |
|
3614 |
names.equals, |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42827
diff
changeset
|
3615 |
List.of(make.Literal(caseLabel))); |
4142 | 3616 |
elsepart = make.If(stringEqualsCall, |
3617 |
make.Exec(make.Assign(make.Ident(dollar_tmp), |
|
3618 |
make.Literal(caseLabelToPosition.get(caseLabel))). |
|
3619 |
setType(dollar_tmp.type)), |
|
3620 |
elsepart); |
|
3621 |
} |
|
3622 |
||
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19941
diff
changeset
|
3623 |
ListBuffer<JCStatement> lb = new ListBuffer<>(); |
4142 | 3624 |
JCBreak breakStmt = make.Break(null); |
3625 |
breakStmt.target = switch1; |
|
3626 |
lb.append(elsepart).append(breakStmt); |
|
3627 |
||
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3628 |
caseBuffer.append(make.Case(JCCase.STATEMENT, List.of(make.Literal(hashCode)), lb.toList(), null)); |
4142 | 3629 |
} |
3630 |
||
3631 |
switch1.cases = caseBuffer.toList(); |
|
3632 |
stmtList.append(switch1); |
|
3633 |
||
3634 |
// Make isomorphic switch tree replacing string labels |
|
3635 |
// with corresponding integer ones from the label to |
|
3636 |
// position map. |
|
3637 |
||
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19941
diff
changeset
|
3638 |
ListBuffer<JCCase> lb = new ListBuffer<>(); |
4142 | 3639 |
for(JCCase oneCase : caseList ) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3640 |
boolean isDefault = (oneCase.pats.isEmpty()); |
4142 | 3641 |
JCExpression caseExpr; |
3642 |
if (isDefault) |
|
3643 |
caseExpr = null; |
|
3644 |
else { |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3645 |
caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.pats.head). |
4142 | 3646 |
type.constValue())); |
3647 |
} |
|
3648 |
||
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3649 |
lb.append(make.Case(JCCase.STATEMENT, caseExpr == null ? List.nil() : List.of(caseExpr), |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3650 |
oneCase.stats, null)); |
4142 | 3651 |
} |
3652 |
||
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3653 |
if (tree.hasTag(SWITCH)) { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3654 |
JCSwitch switch2 = make.Switch(make.Ident(dollar_tmp), lb.toList()); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3655 |
// Rewire up old unlabeled break statements to the |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3656 |
// replacement switch being created. |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3657 |
patchTargets(switch2, tree, switch2); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3658 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3659 |
stmtList.append(switch2); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3660 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3661 |
return make.Block(0L, stmtList.toList()); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3662 |
} else { |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3663 |
JCSwitchExpression switch2 = make.SwitchExpression(make.Ident(dollar_tmp), lb.toList()); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3664 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3665 |
// Rewire up old unlabeled break statements to the |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3666 |
// replacement switch being created. |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3667 |
patchTargets(switch2, tree, switch2); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3668 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3669 |
switch2.setType(tree.type); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3670 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3671 |
LetExpr res = make.LetExpr(stmtList.toList(), switch2); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3672 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3673 |
res.needsCond = true; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3674 |
res.setType(tree.type); |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3675 |
|
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3676 |
return res; |
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3677 |
} |
4142 | 3678 |
} |
3679 |
} |
|
3680 |
||
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3681 |
@Override |
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3682 |
public void visitBreak(JCBreak tree) { |
55306 | 3683 |
result = tree; |
3684 |
} |
|
3685 |
||
3686 |
@Override |
|
3687 |
public void visitYield(JCYield tree) { |
|
3688 |
tree.value = translate(tree.value, tree.target.type); |
|
52794
e4ba5414c8b4
8214031: Assertion error in value break statement with conditional operator in switch expression
jlahoda
parents:
52038
diff
changeset
|
3689 |
result = tree; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3690 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3691 |
|
10 | 3692 |
public void visitNewArray(JCNewArray tree) { |
3693 |
tree.elemtype = translate(tree.elemtype); |
|
3694 |
for (List<JCExpression> t = tree.dims; t.tail != null; t = t.tail) |
|
3695 |
if (t.head != null) t.head = translate(t.head, syms.intType); |
|
3696 |
tree.elems = translate(tree.elems, types.elemtype(tree.type)); |
|
3697 |
result = tree; |
|
3698 |
} |
|
3699 |
||
3700 |
public void visitSelect(JCFieldAccess tree) { |
|
3701 |
// need to special case-access of the form C.super.x |
|
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3702 |
// these will always need an access method, unless C |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3703 |
// is a default interface subclassed by the current class. |
10 | 3704 |
boolean qualifiedSuperAccess = |
10950 | 3705 |
tree.selected.hasTag(SELECT) && |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3706 |
TreeInfo.name(tree.selected) == names._super && |
14547 | 3707 |
!types.isDirectSuperInterface(((JCFieldAccess)tree.selected).selected.type.tsym, currentClass); |
10 | 3708 |
tree.selected = translate(tree.selected); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3709 |
if (tree.name == names._class) { |
10 | 3710 |
result = classOf(tree.selected); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3711 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3712 |
else if (tree.name == names._super && |
14547 | 3713 |
types.isDirectSuperInterface(tree.selected.type.tsym, currentClass)) { |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3714 |
//default super call!! Not a classic qualified super call |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3715 |
TypeSymbol supSym = tree.selected.type.tsym; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3716 |
Assert.checkNonNull(types.asSuper(currentClass.type, supSym)); |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3717 |
result = tree; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3718 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3719 |
else if (tree.name == names._this || tree.name == names._super) { |
10 | 3720 |
result = makeThis(tree.pos(), tree.selected.type.tsym); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
3721 |
} |
10 | 3722 |
else |
3723 |
result = access(tree.sym, tree, enclOp, qualifiedSuperAccess); |
|
3724 |
} |
|
3725 |
||
3726 |
public void visitLetExpr(LetExpr tree) { |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
50735
diff
changeset
|
3727 |
tree.defs = translate(tree.defs); |
10 | 3728 |
tree.expr = translate(tree.expr, tree.type); |
3729 |
result = tree; |
|
3730 |
} |
|
3731 |
||
3732 |
// There ought to be nothing to rewrite here; |
|
3733 |
// we don't generate code. |
|
3734 |
public void visitAnnotation(JCAnnotation tree) { |
|
3735 |
result = tree; |
|
3736 |
} |
|
3737 |
||
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
3738 |
@Override |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
3739 |
public void visitTry(JCTry tree) { |
19927
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3740 |
if (tree.resources.nonEmpty()) { |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3741 |
result = makeTwrTry(tree); |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3742 |
return; |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3743 |
} |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3744 |
|
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3745 |
boolean hasBody = tree.body.getStatements().nonEmpty(); |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3746 |
boolean hasCatchers = tree.catchers.nonEmpty(); |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3747 |
boolean hasFinally = tree.finalizer != null && |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3748 |
tree.finalizer.getStatements().nonEmpty(); |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3749 |
|
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3750 |
if (!hasCatchers && !hasFinally) { |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3751 |
result = translate(tree.body); |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3752 |
return; |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3753 |
} |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3754 |
|
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3755 |
if (!hasBody) { |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3756 |
if (hasFinally) { |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3757 |
result = translate(tree.finalizer); |
16336
cf436a619e34
6181889: Empty try/finally results in bytecodes being generated
vromero
parents:
16334
diff
changeset
|
3758 |
} else { |
19927
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3759 |
result = translate(tree.body); |
16336
cf436a619e34
6181889: Empty try/finally results in bytecodes being generated
vromero
parents:
16334
diff
changeset
|
3760 |
} |
19927
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3761 |
return; |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
3762 |
} |
19927
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3763 |
|
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3764 |
// no optimizations possible |
8793c5627ba7
8024398: javac, compiler crashes with try with empty body
vromero
parents:
19923
diff
changeset
|
3765 |
super.visitTry(tree); |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
3766 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6029
diff
changeset
|
3767 |
|
10 | 3768 |
/************************************************************************** |
3769 |
* main method |
|
3770 |
*************************************************************************/ |
|
3771 |
||
3772 |
/** Translate a toplevel class and return a list consisting of |
|
3773 |
* the translated class and translated versions of all inner classes. |
|
3774 |
* @param env The attribution environment current at the class definition. |
|
3775 |
* We need this for resolving some additional symbols. |
|
3776 |
* @param cdef The tree representing the class definition. |
|
3777 |
*/ |
|
3778 |
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) { |
|
3779 |
ListBuffer<JCTree> translated = null; |
|
3780 |
try { |
|
3781 |
attrEnv = env; |
|
3782 |
this.make = make; |
|
11055 | 3783 |
endPosTable = env.toplevel.endPositions; |
10 | 3784 |
currentClass = null; |
3785 |
currentMethodDef = null; |
|
10950 | 3786 |
outermostClassDef = (cdef.hasTag(CLASSDEF)) ? (JCClassDecl)cdef : null; |
10 | 3787 |
outermostMemberDef = null; |
22163 | 3788 |
this.translated = new ListBuffer<>(); |
3789 |
classdefs = new HashMap<>(); |
|
3790 |
actualSymbols = new HashMap<>(); |
|
3791 |
freevarCache = new HashMap<>(); |
|
47323
6cb6ef406e97
8169345: javac crash when local from enclosing context is captured multiple times
mcimadamore
parents:
47216
diff
changeset
|
3792 |
proxies = new HashMap<>(); |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
3793 |
twrVars = WriteableScope.create(syms.noSymbol); |
10 | 3794 |
outerThisStack = List.nil(); |
22163 | 3795 |
accessNums = new HashMap<>(); |
3796 |
accessSyms = new HashMap<>(); |
|
3797 |
accessConstrs = new HashMap<>(); |
|
6029 | 3798 |
accessConstrTags = List.nil(); |
22163 | 3799 |
accessed = new ListBuffer<>(); |
10 | 3800 |
translate(cdef, (JCExpression)null); |
3801 |
for (List<Symbol> l = accessed.toList(); l.nonEmpty(); l = l.tail) |
|
3802 |
makeAccessible(l.head); |
|
3803 |
for (EnumMapping map : enumSwitchMap.values()) |
|
3804 |
map.translate(); |
|
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3300
diff
changeset
|
3805 |
checkConflicts(this.translated.toList()); |
6029 | 3806 |
checkAccessConstructorTags(); |
10 | 3807 |
translated = this.translated; |
3808 |
} finally { |
|
3809 |
// note that recursive invocations of this method fail hard |
|
3810 |
attrEnv = null; |
|
3811 |
this.make = null; |
|
11055 | 3812 |
endPosTable = null; |
10 | 3813 |
currentClass = null; |
3814 |
currentMethodDef = null; |
|
3815 |
outermostClassDef = null; |
|
3816 |
outermostMemberDef = null; |
|
3817 |
this.translated = null; |
|
3818 |
classdefs = null; |
|
3819 |
actualSymbols = null; |
|
3820 |
freevarCache = null; |
|
3821 |
proxies = null; |
|
3822 |
outerThisStack = null; |
|
3823 |
accessNums = null; |
|
3824 |
accessSyms = null; |
|
3825 |
accessConstrs = null; |
|
6029 | 3826 |
accessConstrTags = null; |
10 | 3827 |
accessed = null; |
3828 |
enumSwitchMap.clear(); |
|
21005
4db633bd7696
8025141: java.lang.ClassFormatError: Illegal field modifiers in class (...)
jlahoda
parents:
20249
diff
changeset
|
3829 |
assertionsDisabledClassCache = null; |
10 | 3830 |
} |
3831 |
return translated.toList(); |
|
3832 |
} |
|
3833 |
} |