author | vromero |
Mon, 08 Aug 2016 11:48:02 -0700 | |
changeset 40236 | 48d9e26a8fa4 |
parent 36779 | 9a030c4d2591 |
child 42407 | f3702cff2933 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36776
5e260fa5208a
8150733: NPE when compiling module-info.java with a class declaration in a non-module mode
vromero
parents:
36526
diff
changeset
|
2 |
* Copyright (c) 1999, 2016, 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.tree; |
|
27 |
||
13077 | 28 |
|
14952 | 29 |
|
10 | 30 |
import com.sun.source.tree.Tree; |
22440
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
31 |
import com.sun.source.util.TreePath; |
13077 | 32 |
import com.sun.tools.javac.code.*; |
10 | 33 |
import com.sun.tools.javac.comp.AttrContext; |
34 |
import com.sun.tools.javac.comp.Env; |
|
13077 | 35 |
import com.sun.tools.javac.tree.JCTree.*; |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
36 |
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; |
10 | 37 |
import com.sun.tools.javac.util.*; |
38 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
39 |
|
10 | 40 |
import static com.sun.tools.javac.code.Flags.*; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
41 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
42 |
import static com.sun.tools.javac.code.TypeTag.BOT; |
10950 | 43 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
44 |
import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK; |
|
45 |
import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED; |
|
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
46 |
|
36526 | 47 |
import javax.tools.JavaFileObject; |
10 | 48 |
|
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
49 |
import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.LEFT; |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
50 |
import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.RIGHT; |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
51 |
|
10 | 52 |
/** Utility class containing inspector methods for trees. |
53 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
54 |
* <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
|
55 |
* If you write code that depends on this, you do so at your own risk. |
10 | 56 |
* This code and its internal interfaces are subject to change or |
57 |
* deletion without notice.</b> |
|
58 |
*/ |
|
59 |
public class TreeInfo { |
|
60 |
||
12080
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
61 |
public static List<JCExpression> args(JCTree t) { |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
62 |
switch (t.getTag()) { |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
63 |
case APPLY: |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
64 |
return ((JCMethodInvocation)t).args; |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
65 |
case NEWCLASS: |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
66 |
return ((JCNewClass)t).args; |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
67 |
default: |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
68 |
return null; |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
69 |
} |
23101f54df44
7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents:
11142
diff
changeset
|
70 |
} |
10 | 71 |
|
72 |
/** Is tree a constructor declaration? |
|
73 |
*/ |
|
74 |
public static boolean isConstructor(JCTree tree) { |
|
10950 | 75 |
if (tree.hasTag(METHODDEF)) { |
10 | 76 |
Name name = ((JCMethodDecl) tree).name; |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
77 |
return name == name.table.names.init; |
10 | 78 |
} else { |
79 |
return false; |
|
80 |
} |
|
81 |
} |
|
82 |
||
24903
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
83 |
public static boolean isReceiverParam(JCTree tree) { |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
84 |
if (tree.hasTag(VARDEF)) { |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
85 |
return ((JCVariableDecl)tree).nameexpr != null; |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
86 |
} else { |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
87 |
return false; |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
88 |
} |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
89 |
} |
41e1812d6b4c
8027886: javac allows illegal receiver parameters
pgovereau
parents:
24219
diff
changeset
|
90 |
|
10 | 91 |
/** Is there a constructor declaration in the given list of trees? |
92 |
*/ |
|
93 |
public static boolean hasConstructors(List<JCTree> trees) { |
|
94 |
for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail) |
|
95 |
if (isConstructor(l.head)) return true; |
|
96 |
return false; |
|
97 |
} |
|
98 |
||
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
99 |
public static boolean isMultiCatch(JCCatch catchClause) { |
10950 | 100 |
return catchClause.param.vartype.hasTag(TYPEUNION); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
101 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5321
diff
changeset
|
102 |
|
10 | 103 |
/** Is statement an initializer for a synthetic field? |
104 |
*/ |
|
105 |
public static boolean isSyntheticInit(JCTree stat) { |
|
10950 | 106 |
if (stat.hasTag(EXEC)) { |
10 | 107 |
JCExpressionStatement exec = (JCExpressionStatement)stat; |
10950 | 108 |
if (exec.expr.hasTag(ASSIGN)) { |
10 | 109 |
JCAssign assign = (JCAssign)exec.expr; |
10950 | 110 |
if (assign.lhs.hasTag(SELECT)) { |
10 | 111 |
JCFieldAccess select = (JCFieldAccess)assign.lhs; |
112 |
if (select.sym != null && |
|
113 |
(select.sym.flags() & SYNTHETIC) != 0) { |
|
114 |
Name selected = name(select.selected); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
115 |
if (selected != null && selected == selected.table.names._this) |
10 | 116 |
return true; |
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
return false; |
|
122 |
} |
|
123 |
||
124 |
/** If the expression is a method call, return the method name, null |
|
125 |
* otherwise. */ |
|
126 |
public static Name calledMethodName(JCTree tree) { |
|
10950 | 127 |
if (tree.hasTag(EXEC)) { |
10 | 128 |
JCExpressionStatement exec = (JCExpressionStatement)tree; |
10950 | 129 |
if (exec.expr.hasTag(APPLY)) { |
10 | 130 |
Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth); |
131 |
return mname; |
|
132 |
} |
|
133 |
} |
|
134 |
return null; |
|
135 |
} |
|
136 |
||
137 |
/** Is this a call to this or super? |
|
138 |
*/ |
|
139 |
public static boolean isSelfCall(JCTree tree) { |
|
140 |
Name name = calledMethodName(tree); |
|
141 |
if (name != null) { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
142 |
Names names = name.table.names; |
10 | 143 |
return name==names._this || name==names._super; |
144 |
} else { |
|
145 |
return false; |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
/** Is this a call to super? |
|
150 |
*/ |
|
151 |
public static boolean isSuperCall(JCTree tree) { |
|
152 |
Name name = calledMethodName(tree); |
|
153 |
if (name != null) { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
154 |
Names names = name.table.names; |
10 | 155 |
return name==names._super; |
156 |
} else { |
|
157 |
return false; |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** Is this a constructor whose first (non-synthetic) statement is not |
|
162 |
* of the form this(...)? |
|
163 |
*/ |
|
164 |
public static boolean isInitialConstructor(JCTree tree) { |
|
165 |
JCMethodInvocation app = firstConstructorCall(tree); |
|
166 |
if (app == null) return false; |
|
167 |
Name meth = name(app.meth); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
168 |
return meth == null || meth != meth.table.names._this; |
10 | 169 |
} |
170 |
||
171 |
/** Return the first call in a constructor definition. */ |
|
172 |
public static JCMethodInvocation firstConstructorCall(JCTree tree) { |
|
10950 | 173 |
if (!tree.hasTag(METHODDEF)) return null; |
10 | 174 |
JCMethodDecl md = (JCMethodDecl) tree; |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1206
diff
changeset
|
175 |
Names names = md.name.table.names; |
10 | 176 |
if (md.name != names.init) return null; |
177 |
if (md.body == null) return null; |
|
178 |
List<JCStatement> stats = md.body.stats; |
|
179 |
// Synthetic initializations can appear before the super call. |
|
180 |
while (stats.nonEmpty() && isSyntheticInit(stats.head)) |
|
181 |
stats = stats.tail; |
|
182 |
if (stats.isEmpty()) return null; |
|
10950 | 183 |
if (!stats.head.hasTag(EXEC)) return null; |
10 | 184 |
JCExpressionStatement exec = (JCExpressionStatement) stats.head; |
10950 | 185 |
if (!exec.expr.hasTag(APPLY)) return null; |
10 | 186 |
return (JCMethodInvocation)exec.expr; |
187 |
} |
|
188 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
189 |
/** Return true if a tree represents a diamond new expr. */ |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
190 |
public static boolean isDiamond(JCTree tree) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
191 |
switch(tree.getTag()) { |
10950 | 192 |
case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty(); |
193 |
case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz); |
|
15718 | 194 |
case ANNOTATED_TYPE: return isDiamond(((JCAnnotatedType)tree).underlyingType); |
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
195 |
default: return false; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
196 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
197 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
198 |
|
29776
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
199 |
/** Return true if the given tree represents a type elided anonymous class instance creation. */ |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
200 |
public static boolean isAnonymousDiamond(JCTree tree) { |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
201 |
switch(tree.getTag()) { |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
202 |
case NEWCLASS: { |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
203 |
JCNewClass nc = (JCNewClass)tree; |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
204 |
return nc.def != null && isDiamond(nc.clazz); |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
205 |
} |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
206 |
case ANNOTATED_TYPE: return isAnonymousDiamond(((JCAnnotatedType)tree).underlyingType); |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
207 |
default: return false; |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
208 |
} |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
209 |
} |
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
29051
diff
changeset
|
210 |
|
12916
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
211 |
public static boolean isEnumInit(JCTree tree) { |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
212 |
switch (tree.getTag()) { |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
213 |
case VARDEF: |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
214 |
return (((JCVariableDecl)tree).mods.flags & ENUM) != 0; |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
215 |
default: |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
216 |
return false; |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
217 |
} |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
218 |
} |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
219 |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
220 |
/** set 'polyKind' on given tree */ |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
221 |
public static void setPolyKind(JCTree tree, PolyKind pkind) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
222 |
switch (tree.getTag()) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
223 |
case APPLY: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
224 |
((JCMethodInvocation)tree).polyKind = pkind; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
225 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
226 |
case NEWCLASS: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
227 |
((JCNewClass)tree).polyKind = pkind; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
228 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
229 |
case REFERENCE: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
230 |
((JCMemberReference)tree).refPolyKind = pkind; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
231 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
232 |
default: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
233 |
throw new AssertionError("Unexpected tree: " + tree); |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
234 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
235 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
236 |
|
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
237 |
/** set 'varargsElement' on given tree */ |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
238 |
public static void setVarargsElement(JCTree tree, Type varargsElement) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
239 |
switch (tree.getTag()) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
240 |
case APPLY: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
241 |
((JCMethodInvocation)tree).varargsElement = varargsElement; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
242 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
243 |
case NEWCLASS: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
244 |
((JCNewClass)tree).varargsElement = varargsElement; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
245 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
246 |
case REFERENCE: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
247 |
((JCMemberReference)tree).varargsElement = varargsElement; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
248 |
break; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
249 |
default: |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
250 |
throw new AssertionError("Unexpected tree: " + tree); |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14952
diff
changeset
|
251 |
} |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
252 |
} |
14722
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
253 |
|
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
254 |
/** Return true if the tree corresponds to an expression statement */ |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
255 |
public static boolean isExpressionStatement(JCExpression tree) { |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
256 |
switch(tree.getTag()) { |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
257 |
case PREINC: case PREDEC: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
258 |
case POSTINC: case POSTDEC: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
259 |
case ASSIGN: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
260 |
case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
261 |
case SL_ASG: case SR_ASG: case USR_ASG: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
262 |
case PLUS_ASG: case MINUS_ASG: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
263 |
case MUL_ASG: case DIV_ASG: case MOD_ASG: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
264 |
case APPLY: case NEWCLASS: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
265 |
case ERRONEOUS: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
266 |
return true; |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
267 |
default: |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
268 |
return false; |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
269 |
} |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
270 |
} |
aaa39655aa2e
8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents:
14359
diff
changeset
|
271 |
|
28142
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
272 |
/** Return true if the tree corresponds to a statement */ |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
273 |
public static boolean isStatement(JCTree tree) { |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
274 |
return (tree instanceof JCStatement) && |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
275 |
!tree.hasTag(CLASSDEF) && |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
276 |
!tree.hasTag(Tag.BLOCK) && |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
277 |
!tree.hasTag(METHODDEF); |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
278 |
} |
32a6b1af81b1
8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
27224
diff
changeset
|
279 |
|
11142 | 280 |
/** |
281 |
* Return true if the AST corresponds to a static select of the kind A.B |
|
282 |
*/ |
|
283 |
public static boolean isStaticSelector(JCTree base, Names names) { |
|
284 |
if (base == null) |
|
285 |
return false; |
|
286 |
switch (base.getTag()) { |
|
287 |
case IDENT: |
|
288 |
JCIdent id = (JCIdent)base; |
|
289 |
return id.name != names._this && |
|
290 |
id.name != names._super && |
|
291 |
isStaticSym(base); |
|
292 |
case SELECT: |
|
293 |
return isStaticSym(base) && |
|
294 |
isStaticSelector(((JCFieldAccess)base).selected, names); |
|
295 |
case TYPEAPPLY: |
|
14062
b7439971a094
7177386: Add attribution support for method references
mcimadamore
parents:
14058
diff
changeset
|
296 |
case TYPEARRAY: |
11142 | 297 |
return true; |
15718 | 298 |
case ANNOTATED_TYPE: |
299 |
return isStaticSelector(((JCAnnotatedType)base).underlyingType, names); |
|
11142 | 300 |
default: |
301 |
return false; |
|
302 |
} |
|
303 |
} |
|
304 |
//where |
|
305 |
private static boolean isStaticSym(JCTree tree) { |
|
306 |
Symbol sym = symbol(tree); |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
307 |
return (sym.kind == TYP || sym.kind == PCK); |
11142 | 308 |
} |
309 |
||
10 | 310 |
/** Return true if a tree represents the null literal. */ |
311 |
public static boolean isNull(JCTree tree) { |
|
10950 | 312 |
if (!tree.hasTag(LITERAL)) |
10 | 313 |
return false; |
314 |
JCLiteral lit = (JCLiteral) tree; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14267
diff
changeset
|
315 |
return (lit.typetag == BOT); |
10 | 316 |
} |
317 |
||
22440
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
318 |
/** Return true iff this tree is a child of some annotation. */ |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
319 |
public static boolean isInAnnotation(Env<?> env, JCTree tree) { |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
320 |
TreePath tp = TreePath.getPath(env.toplevel, tree); |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
321 |
if (tp != null) { |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
322 |
for (Tree t : tp) { |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
323 |
if (t.getKind() == Tree.Kind.ANNOTATION) |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
324 |
return true; |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
325 |
} |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
326 |
} |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
327 |
return false; |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
328 |
} |
d40c30326317
8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents:
22163
diff
changeset
|
329 |
|
13077 | 330 |
public static String getCommentText(Env<?> env, JCTree tree) { |
331 |
DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL)) |
|
332 |
? ((JCCompilationUnit) tree).docComments |
|
333 |
: env.toplevel.docComments; |
|
13078
11d5e1c88864
7178297: provide mapping from doc comment position to source file position
jjg
parents:
13077
diff
changeset
|
334 |
return (docComments == null) ? null : docComments.getCommentText(tree); |
13077 | 335 |
} |
336 |
||
14952 | 337 |
public static DCTree.DCDocComment getCommentTree(Env<?> env, JCTree tree) { |
338 |
DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL)) |
|
339 |
? ((JCCompilationUnit) tree).docComments |
|
340 |
: env.toplevel.docComments; |
|
341 |
return (docComments == null) ? null : docComments.getCommentTree(tree); |
|
342 |
} |
|
343 |
||
10 | 344 |
/** The position of the first statement in a block, or the position of |
345 |
* the block itself if it is empty. |
|
346 |
*/ |
|
347 |
public static int firstStatPos(JCTree tree) { |
|
10950 | 348 |
if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty()) |
10 | 349 |
return ((JCBlock) tree).stats.head.pos; |
350 |
else |
|
351 |
return tree.pos; |
|
352 |
} |
|
353 |
||
354 |
/** The end position of given tree, if it is a block with |
|
355 |
* defined endpos. |
|
356 |
*/ |
|
357 |
public static int endPos(JCTree tree) { |
|
10950 | 358 |
if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS) |
10 | 359 |
return ((JCBlock) tree).endpos; |
10950 | 360 |
else if (tree.hasTag(SYNCHRONIZED)) |
10 | 361 |
return endPos(((JCSynchronized) tree).body); |
10950 | 362 |
else if (tree.hasTag(TRY)) { |
10 | 363 |
JCTry t = (JCTry) tree; |
13442
41b5b4736385
7178324: Crash when compiling for(i : x) try(AutoCloseable x = ...) {}
sundar
parents:
13078
diff
changeset
|
364 |
return endPos((t.finalizer != null) ? t.finalizer |
41b5b4736385
7178324: Crash when compiling for(i : x) try(AutoCloseable x = ...) {}
sundar
parents:
13078
diff
changeset
|
365 |
: (t.catchers.nonEmpty() ? t.catchers.last().body : t.body)); |
10 | 366 |
} else |
367 |
return tree.pos; |
|
368 |
} |
|
369 |
||
370 |
||
371 |
/** Get the start position for a tree node. The start position is |
|
372 |
* defined to be the position of the first character of the first |
|
373 |
* token of the node's source text. |
|
374 |
* @param tree The tree node |
|
375 |
*/ |
|
376 |
public static int getStartPos(JCTree tree) { |
|
377 |
if (tree == null) |
|
378 |
return Position.NOPOS; |
|
379 |
||
380 |
switch(tree.getTag()) { |
|
24069 | 381 |
case PACKAGEDEF: { |
382 |
JCPackageDecl pd = (JCPackageDecl)tree; |
|
383 |
return pd.annotations.isEmpty() ? pd.pos : |
|
384 |
pd.annotations.head.pos; |
|
385 |
} |
|
10950 | 386 |
case APPLY: |
387 |
return getStartPos(((JCMethodInvocation) tree).meth); |
|
388 |
case ASSIGN: |
|
389 |
return getStartPos(((JCAssign) tree).lhs); |
|
390 |
case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: |
|
391 |
case SL_ASG: case SR_ASG: case USR_ASG: |
|
392 |
case PLUS_ASG: case MINUS_ASG: case MUL_ASG: |
|
393 |
case DIV_ASG: case MOD_ASG: |
|
394 |
case OR: case AND: case BITOR: |
|
395 |
case BITXOR: case BITAND: case EQ: |
|
396 |
case NE: case LT: case GT: |
|
397 |
case LE: case GE: case SL: |
|
398 |
case SR: case USR: case PLUS: |
|
399 |
case MINUS: case MUL: case DIV: |
|
400 |
case MOD: |
|
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
401 |
case POSTINC: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
402 |
case POSTDEC: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
403 |
return getStartPos(((JCOperatorExpression) tree).getOperand(LEFT)); |
10950 | 404 |
case CLASSDEF: { |
405 |
JCClassDecl node = (JCClassDecl)tree; |
|
406 |
if (node.mods.pos != Position.NOPOS) |
|
407 |
return node.mods.pos; |
|
408 |
break; |
|
10 | 409 |
} |
10950 | 410 |
case CONDEXPR: |
411 |
return getStartPos(((JCConditional) tree).cond); |
|
412 |
case EXEC: |
|
413 |
return getStartPos(((JCExpressionStatement) tree).expr); |
|
414 |
case INDEXED: |
|
415 |
return getStartPos(((JCArrayAccess) tree).indexed); |
|
416 |
case METHODDEF: { |
|
417 |
JCMethodDecl node = (JCMethodDecl)tree; |
|
418 |
if (node.mods.pos != Position.NOPOS) |
|
419 |
return node.mods.pos; |
|
420 |
if (node.typarams.nonEmpty()) // List.nil() used for no typarams |
|
421 |
return getStartPos(node.typarams.head); |
|
422 |
return node.restype == null ? node.pos : getStartPos(node.restype); |
|
423 |
} |
|
424 |
case SELECT: |
|
425 |
return getStartPos(((JCFieldAccess) tree).selected); |
|
426 |
case TYPEAPPLY: |
|
427 |
return getStartPos(((JCTypeApply) tree).clazz); |
|
428 |
case TYPEARRAY: |
|
429 |
return getStartPos(((JCArrayTypeTree) tree).elemtype); |
|
430 |
case TYPETEST: |
|
431 |
return getStartPos(((JCInstanceOf) tree).expr); |
|
15385 | 432 |
case ANNOTATED_TYPE: { |
433 |
JCAnnotatedType node = (JCAnnotatedType) tree; |
|
434 |
if (node.annotations.nonEmpty()) { |
|
435 |
if (node.underlyingType.hasTag(TYPEARRAY) || |
|
436 |
node.underlyingType.hasTag(SELECT)) { |
|
437 |
return getStartPos(node.underlyingType); |
|
438 |
} else { |
|
439 |
return getStartPos(node.annotations.head); |
|
440 |
} |
|
441 |
} else { |
|
442 |
return getStartPos(node.underlyingType); |
|
443 |
} |
|
444 |
} |
|
10950 | 445 |
case NEWCLASS: { |
446 |
JCNewClass node = (JCNewClass)tree; |
|
447 |
if (node.encl != null) |
|
448 |
return getStartPos(node.encl); |
|
449 |
break; |
|
450 |
} |
|
451 |
case VARDEF: { |
|
452 |
JCVariableDecl node = (JCVariableDecl)tree; |
|
453 |
if (node.mods.pos != Position.NOPOS) { |
|
454 |
return node.mods.pos; |
|
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
455 |
} else if (node.vartype == null) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
456 |
//if there's no type (partially typed lambda parameter) |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
457 |
//simply return node position |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
458 |
return node.pos; |
10950 | 459 |
} else { |
460 |
return getStartPos(node.vartype); |
|
461 |
} |
|
462 |
} |
|
463 |
case ERRONEOUS: { |
|
464 |
JCErroneous node = (JCErroneous)tree; |
|
465 |
if (node.errs != null && node.errs.nonEmpty()) |
|
466 |
return getStartPos(node.errs.head); |
|
467 |
} |
|
10 | 468 |
} |
469 |
return tree.pos; |
|
470 |
} |
|
471 |
||
472 |
/** The end position of given tree, given a table of end positions generated by the parser |
|
473 |
*/ |
|
11055 | 474 |
public static int getEndPos(JCTree tree, EndPosTable endPosTable) { |
10 | 475 |
if (tree == null) |
476 |
return Position.NOPOS; |
|
477 |
||
11055 | 478 |
if (endPosTable == null) { |
10 | 479 |
// fall back on limited info in the tree |
480 |
return endPos(tree); |
|
481 |
} |
|
482 |
||
11055 | 483 |
int mapPos = endPosTable.getEndPos(tree); |
484 |
if (mapPos != Position.NOPOS) |
|
10 | 485 |
return mapPos; |
486 |
||
487 |
switch(tree.getTag()) { |
|
10950 | 488 |
case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: |
489 |
case SL_ASG: case SR_ASG: case USR_ASG: |
|
490 |
case PLUS_ASG: case MINUS_ASG: case MUL_ASG: |
|
491 |
case DIV_ASG: case MOD_ASG: |
|
492 |
case OR: case AND: case BITOR: |
|
493 |
case BITXOR: case BITAND: case EQ: |
|
494 |
case NE: case LT: case GT: |
|
495 |
case LE: case GE: case SL: |
|
496 |
case SR: case USR: case PLUS: |
|
497 |
case MINUS: case MUL: case DIV: |
|
498 |
case MOD: |
|
40236
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
499 |
case POS: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
500 |
case NEG: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
501 |
case NOT: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
502 |
case COMPL: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
503 |
case PREINC: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
504 |
case PREDEC: |
48d9e26a8fa4
8162874: AST nodes representing operators should have a common superclass
vromero
parents:
36779
diff
changeset
|
505 |
return getEndPos(((JCOperatorExpression) tree).getOperand(RIGHT), endPosTable); |
10950 | 506 |
case CASE: |
11055 | 507 |
return getEndPos(((JCCase) tree).stats.last(), endPosTable); |
10950 | 508 |
case CATCH: |
11055 | 509 |
return getEndPos(((JCCatch) tree).body, endPosTable); |
10950 | 510 |
case CONDEXPR: |
11055 | 511 |
return getEndPos(((JCConditional) tree).falsepart, endPosTable); |
10950 | 512 |
case FORLOOP: |
11055 | 513 |
return getEndPos(((JCForLoop) tree).body, endPosTable); |
10950 | 514 |
case FOREACHLOOP: |
11055 | 515 |
return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable); |
10950 | 516 |
case IF: { |
517 |
JCIf node = (JCIf)tree; |
|
518 |
if (node.elsepart == null) { |
|
11055 | 519 |
return getEndPos(node.thenpart, endPosTable); |
10950 | 520 |
} else { |
11055 | 521 |
return getEndPos(node.elsepart, endPosTable); |
10950 | 522 |
} |
10 | 523 |
} |
10950 | 524 |
case LABELLED: |
11055 | 525 |
return getEndPos(((JCLabeledStatement) tree).body, endPosTable); |
10950 | 526 |
case MODIFIERS: |
11055 | 527 |
return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable); |
10950 | 528 |
case SYNCHRONIZED: |
11055 | 529 |
return getEndPos(((JCSynchronized) tree).body, endPosTable); |
10950 | 530 |
case TOPLEVEL: |
11055 | 531 |
return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable); |
10950 | 532 |
case TRY: { |
533 |
JCTry node = (JCTry)tree; |
|
534 |
if (node.finalizer != null) { |
|
11055 | 535 |
return getEndPos(node.finalizer, endPosTable); |
10950 | 536 |
} else if (!node.catchers.isEmpty()) { |
11055 | 537 |
return getEndPos(node.catchers.last(), endPosTable); |
10950 | 538 |
} else { |
11055 | 539 |
return getEndPos(node.body, endPosTable); |
10950 | 540 |
} |
10 | 541 |
} |
10950 | 542 |
case WILDCARD: |
11055 | 543 |
return getEndPos(((JCWildcard) tree).inner, endPosTable); |
10950 | 544 |
case TYPECAST: |
11055 | 545 |
return getEndPos(((JCTypeCast) tree).expr, endPosTable); |
10950 | 546 |
case TYPETEST: |
11055 | 547 |
return getEndPos(((JCInstanceOf) tree).clazz, endPosTable); |
10950 | 548 |
case WHILELOOP: |
11055 | 549 |
return getEndPos(((JCWhileLoop) tree).body, endPosTable); |
15385 | 550 |
case ANNOTATED_TYPE: |
551 |
return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable); |
|
10950 | 552 |
case ERRONEOUS: { |
553 |
JCErroneous node = (JCErroneous)tree; |
|
554 |
if (node.errs != null && node.errs.nonEmpty()) |
|
11055 | 555 |
return getEndPos(node.errs.last(), endPosTable); |
10950 | 556 |
} |
10 | 557 |
} |
558 |
return Position.NOPOS; |
|
559 |
} |
|
560 |
||
561 |
||
562 |
/** A DiagnosticPosition with the preferred position set to the |
|
563 |
* end position of given tree, if it is a block with |
|
564 |
* defined endpos. |
|
565 |
*/ |
|
566 |
public static DiagnosticPosition diagEndPos(final JCTree tree) { |
|
567 |
final int endPos = TreeInfo.endPos(tree); |
|
568 |
return new DiagnosticPosition() { |
|
569 |
public JCTree getTree() { return tree; } |
|
570 |
public int getStartPosition() { return TreeInfo.getStartPos(tree); } |
|
571 |
public int getPreferredPosition() { return endPos; } |
|
11055 | 572 |
public int getEndPosition(EndPosTable endPosTable) { |
10 | 573 |
return TreeInfo.getEndPos(tree, endPosTable); |
574 |
} |
|
575 |
}; |
|
576 |
} |
|
577 |
||
578 |
/** The position of the finalizer of given try/synchronized statement. |
|
579 |
*/ |
|
580 |
public static int finalizerPos(JCTree tree) { |
|
10950 | 581 |
if (tree.hasTag(TRY)) { |
10 | 582 |
JCTry t = (JCTry) tree; |
8032 | 583 |
Assert.checkNonNull(t.finalizer); |
10 | 584 |
return firstStatPos(t.finalizer); |
10950 | 585 |
} else if (tree.hasTag(SYNCHRONIZED)) { |
10 | 586 |
return endPos(((JCSynchronized) tree).body); |
587 |
} else { |
|
588 |
throw new AssertionError(); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
/** Find the position for reporting an error about a symbol, where |
|
593 |
* that symbol is defined somewhere in the given tree. */ |
|
594 |
public static int positionFor(final Symbol sym, final JCTree tree) { |
|
595 |
JCTree decl = declarationFor(sym, tree); |
|
596 |
return ((decl != null) ? decl : tree).pos; |
|
597 |
} |
|
598 |
||
599 |
/** Find the position for reporting an error about a symbol, where |
|
600 |
* that symbol is defined somewhere in the given tree. */ |
|
601 |
public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) { |
|
602 |
JCTree decl = declarationFor(sym, tree); |
|
603 |
return ((decl != null) ? decl : tree).pos(); |
|
604 |
} |
|
605 |
||
606 |
/** Find the declaration for a symbol, where |
|
607 |
* that symbol is defined somewhere in the given tree. */ |
|
608 |
public static JCTree declarationFor(final Symbol sym, final JCTree tree) { |
|
609 |
class DeclScanner extends TreeScanner { |
|
610 |
JCTree result = null; |
|
611 |
public void scan(JCTree tree) { |
|
612 |
if (tree!=null && result==null) |
|
613 |
tree.accept(this); |
|
614 |
} |
|
615 |
public void visitTopLevel(JCCompilationUnit that) { |
|
616 |
if (that.packge == sym) result = that; |
|
617 |
else super.visitTopLevel(that); |
|
618 |
} |
|
36779
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36776
diff
changeset
|
619 |
public void visitModuleDef(JCModuleDecl that) { |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36776
diff
changeset
|
620 |
if (that.sym == sym) result = that; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36776
diff
changeset
|
621 |
// no need to scan within module declaration |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36776
diff
changeset
|
622 |
} |
25289
9573116c764c
8042345: getDocComment fails for doc comments on PackageElement found in package-info.java
pgovereau
parents:
24903
diff
changeset
|
623 |
public void visitPackageDef(JCPackageDecl that) { |
9573116c764c
8042345: getDocComment fails for doc comments on PackageElement found in package-info.java
pgovereau
parents:
24903
diff
changeset
|
624 |
if (that.packge == sym) result = that; |
9573116c764c
8042345: getDocComment fails for doc comments on PackageElement found in package-info.java
pgovereau
parents:
24903
diff
changeset
|
625 |
else super.visitPackageDef(that); |
9573116c764c
8042345: getDocComment fails for doc comments on PackageElement found in package-info.java
pgovereau
parents:
24903
diff
changeset
|
626 |
} |
10 | 627 |
public void visitClassDef(JCClassDecl that) { |
628 |
if (that.sym == sym) result = that; |
|
629 |
else super.visitClassDef(that); |
|
630 |
} |
|
631 |
public void visitMethodDef(JCMethodDecl that) { |
|
632 |
if (that.sym == sym) result = that; |
|
633 |
else super.visitMethodDef(that); |
|
634 |
} |
|
635 |
public void visitVarDef(JCVariableDecl that) { |
|
636 |
if (that.sym == sym) result = that; |
|
637 |
else super.visitVarDef(that); |
|
638 |
} |
|
6586
0d40dc0c06cb
6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents:
5847
diff
changeset
|
639 |
public void visitTypeParameter(JCTypeParameter that) { |
7619
1bedd48ff024
6990209: JCK7-compiler lang/ICLS/icls006/icls00603/icls00603a.html#icls00603src test fails.
jjh
parents:
7074
diff
changeset
|
640 |
if (that.type != null && that.type.tsym == sym) result = that; |
6586
0d40dc0c06cb
6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents:
5847
diff
changeset
|
641 |
else super.visitTypeParameter(that); |
0d40dc0c06cb
6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents:
5847
diff
changeset
|
642 |
} |
10 | 643 |
} |
644 |
DeclScanner s = new DeclScanner(); |
|
645 |
tree.accept(s); |
|
646 |
return s.result; |
|
647 |
} |
|
648 |
||
649 |
public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) { |
|
650 |
return scopeFor(pathFor(node, unit)); |
|
651 |
} |
|
652 |
||
653 |
public static Env<AttrContext> scopeFor(List<JCTree> path) { |
|
654 |
// TODO: not implemented yet |
|
655 |
throw new UnsupportedOperationException("not implemented yet"); |
|
656 |
} |
|
657 |
||
658 |
public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) { |
|
659 |
class Result extends Error { |
|
660 |
static final long serialVersionUID = -5942088234594905625L; |
|
661 |
List<JCTree> path; |
|
662 |
Result(List<JCTree> path) { |
|
663 |
this.path = path; |
|
664 |
} |
|
665 |
} |
|
666 |
class PathFinder extends TreeScanner { |
|
667 |
List<JCTree> path = List.nil(); |
|
668 |
public void scan(JCTree tree) { |
|
669 |
if (tree != null) { |
|
670 |
path = path.prepend(tree); |
|
671 |
if (tree == node) |
|
672 |
throw new Result(path); |
|
673 |
super.scan(tree); |
|
674 |
path = path.tail; |
|
675 |
} |
|
676 |
} |
|
677 |
} |
|
678 |
try { |
|
679 |
new PathFinder().scan(unit); |
|
680 |
} catch (Result result) { |
|
681 |
return result.path; |
|
682 |
} |
|
683 |
return List.nil(); |
|
684 |
} |
|
685 |
||
686 |
/** Return the statement referenced by a label. |
|
687 |
* If the label refers to a loop or switch, return that switch |
|
688 |
* otherwise return the labelled statement itself |
|
689 |
*/ |
|
690 |
public static JCTree referencedStatement(JCLabeledStatement tree) { |
|
691 |
JCTree t = tree; |
|
692 |
do t = ((JCLabeledStatement) t).body; |
|
10950 | 693 |
while (t.hasTag(LABELLED)); |
10 | 694 |
switch (t.getTag()) { |
10950 | 695 |
case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH: |
10 | 696 |
return t; |
697 |
default: |
|
698 |
return tree; |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
/** Skip parens and return the enclosed expression |
|
703 |
*/ |
|
704 |
public static JCExpression skipParens(JCExpression tree) { |
|
10950 | 705 |
while (tree.hasTag(PARENS)) { |
10 | 706 |
tree = ((JCParens) tree).expr; |
707 |
} |
|
708 |
return tree; |
|
709 |
} |
|
710 |
||
711 |
/** Skip parens and return the enclosed expression |
|
712 |
*/ |
|
713 |
public static JCTree skipParens(JCTree tree) { |
|
10950 | 714 |
if (tree.hasTag(PARENS)) |
10 | 715 |
return skipParens((JCParens)tree); |
716 |
else |
|
717 |
return tree; |
|
718 |
} |
|
719 |
||
720 |
/** Return the types of a list of trees. |
|
721 |
*/ |
|
722 |
public static List<Type> types(List<? extends JCTree> trees) { |
|
22163 | 723 |
ListBuffer<Type> ts = new ListBuffer<>(); |
10 | 724 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
725 |
ts.append(l.head.type); |
|
726 |
return ts.toList(); |
|
727 |
} |
|
728 |
||
729 |
/** If this tree is an identifier or a field or a parameterized type, |
|
730 |
* return its name, otherwise return null. |
|
731 |
*/ |
|
732 |
public static Name name(JCTree tree) { |
|
733 |
switch (tree.getTag()) { |
|
10950 | 734 |
case IDENT: |
10 | 735 |
return ((JCIdent) tree).name; |
10950 | 736 |
case SELECT: |
10 | 737 |
return ((JCFieldAccess) tree).name; |
10950 | 738 |
case TYPEAPPLY: |
10 | 739 |
return name(((JCTypeApply) tree).clazz); |
740 |
default: |
|
741 |
return null; |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
/** If this tree is a qualified identifier, its return fully qualified name, |
|
746 |
* otherwise return null. |
|
747 |
*/ |
|
748 |
public static Name fullName(JCTree tree) { |
|
749 |
tree = skipParens(tree); |
|
750 |
switch (tree.getTag()) { |
|
10950 | 751 |
case IDENT: |
10 | 752 |
return ((JCIdent) tree).name; |
10950 | 753 |
case SELECT: |
10 | 754 |
Name sname = fullName(((JCFieldAccess) tree).selected); |
755 |
return sname == null ? null : sname.append('.', name(tree)); |
|
756 |
default: |
|
757 |
return null; |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
public static Symbol symbolFor(JCTree node) { |
|
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
762 |
Symbol sym = symbolForImpl(node); |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
763 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
764 |
return sym != null ? sym.baseSymbol() : null; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
765 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
766 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
767 |
private static Symbol symbolForImpl(JCTree node) { |
10 | 768 |
node = skipParens(node); |
769 |
switch (node.getTag()) { |
|
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
770 |
case TOPLEVEL: |
36526 | 771 |
JCCompilationUnit cut = (JCCompilationUnit) node; |
772 |
if (isModuleInfo(cut) && cut.defs.nonEmpty() && cut.defs.head.hasTag(MODULEDEF)) |
|
773 |
return symbolFor(cut.defs.head); |
|
774 |
return cut.packge; |
|
775 |
case MODULEDEF: |
|
776 |
return ((JCModuleDecl) node).sym; |
|
24069 | 777 |
case PACKAGEDEF: |
778 |
return ((JCPackageDecl) node).packge; |
|
10950 | 779 |
case CLASSDEF: |
10 | 780 |
return ((JCClassDecl) node).sym; |
10950 | 781 |
case METHODDEF: |
10 | 782 |
return ((JCMethodDecl) node).sym; |
10950 | 783 |
case VARDEF: |
10 | 784 |
return ((JCVariableDecl) node).sym; |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
785 |
case IDENT: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
786 |
return ((JCIdent) node).sym; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
787 |
case SELECT: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
788 |
return ((JCFieldAccess) node).sym; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
789 |
case REFERENCE: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
790 |
return ((JCMemberReference) node).sym; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
791 |
case NEWCLASS: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
792 |
return ((JCNewClass) node).constructor; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
793 |
case APPLY: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
794 |
return symbolFor(((JCMethodInvocation) node).meth); |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
795 |
case TYPEAPPLY: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
796 |
return symbolFor(((JCTypeApply) node).clazz); |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
797 |
case ANNOTATION: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
798 |
case TYPE_ANNOTATION: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
799 |
case TYPEPARAMETER: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
800 |
if (node.type != null) |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
801 |
return node.type.tsym; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16975
diff
changeset
|
802 |
return null; |
10 | 803 |
default: |
804 |
return null; |
|
805 |
} |
|
806 |
} |
|
807 |
||
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
808 |
public static boolean isDeclaration(JCTree node) { |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
809 |
node = skipParens(node); |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
810 |
switch (node.getTag()) { |
24069 | 811 |
case PACKAGEDEF: |
10950 | 812 |
case CLASSDEF: |
813 |
case METHODDEF: |
|
814 |
case VARDEF: |
|
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
815 |
return true; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
816 |
default: |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
817 |
return false; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
818 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
819 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
820 |
|
10 | 821 |
/** If this tree is an identifier or a field, return its symbol, |
822 |
* otherwise return null. |
|
823 |
*/ |
|
824 |
public static Symbol symbol(JCTree tree) { |
|
825 |
tree = skipParens(tree); |
|
826 |
switch (tree.getTag()) { |
|
10950 | 827 |
case IDENT: |
10 | 828 |
return ((JCIdent) tree).sym; |
10950 | 829 |
case SELECT: |
10 | 830 |
return ((JCFieldAccess) tree).sym; |
10950 | 831 |
case TYPEAPPLY: |
10 | 832 |
return symbol(((JCTypeApply) tree).clazz); |
15385 | 833 |
case ANNOTATED_TYPE: |
834 |
return symbol(((JCAnnotatedType) tree).underlyingType); |
|
24219 | 835 |
case REFERENCE: |
836 |
return ((JCMemberReference) tree).sym; |
|
10 | 837 |
default: |
838 |
return null; |
|
839 |
} |
|
840 |
} |
|
841 |
||
842 |
/** Return true if this is a nonstatic selection. */ |
|
843 |
public static boolean nonstaticSelect(JCTree tree) { |
|
844 |
tree = skipParens(tree); |
|
10950 | 845 |
if (!tree.hasTag(SELECT)) return false; |
10 | 846 |
JCFieldAccess s = (JCFieldAccess) tree; |
847 |
Symbol e = symbol(s.selected); |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
848 |
return e == null || (e.kind != PCK && e.kind != TYP); |
10 | 849 |
} |
850 |
||
851 |
/** If this tree is an identifier or a field, set its symbol, otherwise skip. |
|
852 |
*/ |
|
853 |
public static void setSymbol(JCTree tree, Symbol sym) { |
|
854 |
tree = skipParens(tree); |
|
855 |
switch (tree.getTag()) { |
|
10950 | 856 |
case IDENT: |
10 | 857 |
((JCIdent) tree).sym = sym; break; |
10950 | 858 |
case SELECT: |
10 | 859 |
((JCFieldAccess) tree).sym = sym; break; |
860 |
default: |
|
861 |
} |
|
862 |
} |
|
863 |
||
864 |
/** If this tree is a declaration or a block, return its flags field, |
|
865 |
* otherwise return 0. |
|
866 |
*/ |
|
867 |
public static long flags(JCTree tree) { |
|
868 |
switch (tree.getTag()) { |
|
10950 | 869 |
case VARDEF: |
10 | 870 |
return ((JCVariableDecl) tree).mods.flags; |
10950 | 871 |
case METHODDEF: |
10 | 872 |
return ((JCMethodDecl) tree).mods.flags; |
10950 | 873 |
case CLASSDEF: |
10 | 874 |
return ((JCClassDecl) tree).mods.flags; |
10950 | 875 |
case BLOCK: |
10 | 876 |
return ((JCBlock) tree).flags; |
877 |
default: |
|
878 |
return 0; |
|
879 |
} |
|
880 |
} |
|
881 |
||
882 |
/** Return first (smallest) flag in `flags': |
|
883 |
* pre: flags != 0 |
|
884 |
*/ |
|
885 |
public static long firstFlag(long flags) { |
|
14267
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14062
diff
changeset
|
886 |
long flag = 1; |
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14062
diff
changeset
|
887 |
while ((flag & flags & ExtendedStandardFlags) == 0) |
10 | 888 |
flag = flag << 1; |
889 |
return flag; |
|
890 |
} |
|
891 |
||
892 |
/** Return flags as a string, separated by " ". |
|
893 |
*/ |
|
894 |
public static String flagNames(long flags) { |
|
14267
6321fbe0cf50
7192245: Add parser support for default methods
mcimadamore
parents:
14062
diff
changeset
|
895 |
return Flags.toString(flags & ExtendedStandardFlags).trim(); |
10 | 896 |
} |
897 |
||
898 |
/** Operator precedences values. |
|
899 |
*/ |
|
900 |
public static final int |
|
901 |
notExpression = -1, // not an expression |
|
902 |
noPrec = 0, // no enclosing expression |
|
903 |
assignPrec = 1, |
|
904 |
assignopPrec = 2, |
|
905 |
condPrec = 3, |
|
906 |
orPrec = 4, |
|
907 |
andPrec = 5, |
|
908 |
bitorPrec = 6, |
|
909 |
bitxorPrec = 7, |
|
910 |
bitandPrec = 8, |
|
911 |
eqPrec = 9, |
|
912 |
ordPrec = 10, |
|
913 |
shiftPrec = 11, |
|
914 |
addPrec = 12, |
|
915 |
mulPrec = 13, |
|
916 |
prefixPrec = 14, |
|
917 |
postfixPrec = 15, |
|
918 |
precCount = 16; |
|
919 |
||
920 |
||
921 |
/** Map operators to their precedence levels. |
|
922 |
*/ |
|
10950 | 923 |
public static int opPrec(JCTree.Tag op) { |
10 | 924 |
switch(op) { |
10950 | 925 |
case POS: |
926 |
case NEG: |
|
927 |
case NOT: |
|
928 |
case COMPL: |
|
929 |
case PREINC: |
|
930 |
case PREDEC: return prefixPrec; |
|
931 |
case POSTINC: |
|
932 |
case POSTDEC: |
|
933 |
case NULLCHK: return postfixPrec; |
|
934 |
case ASSIGN: return assignPrec; |
|
935 |
case BITOR_ASG: |
|
936 |
case BITXOR_ASG: |
|
937 |
case BITAND_ASG: |
|
938 |
case SL_ASG: |
|
939 |
case SR_ASG: |
|
940 |
case USR_ASG: |
|
941 |
case PLUS_ASG: |
|
942 |
case MINUS_ASG: |
|
943 |
case MUL_ASG: |
|
944 |
case DIV_ASG: |
|
945 |
case MOD_ASG: return assignopPrec; |
|
946 |
case OR: return orPrec; |
|
947 |
case AND: return andPrec; |
|
948 |
case EQ: |
|
949 |
case NE: return eqPrec; |
|
950 |
case LT: |
|
951 |
case GT: |
|
952 |
case LE: |
|
953 |
case GE: return ordPrec; |
|
954 |
case BITOR: return bitorPrec; |
|
955 |
case BITXOR: return bitxorPrec; |
|
956 |
case BITAND: return bitandPrec; |
|
957 |
case SL: |
|
958 |
case SR: |
|
959 |
case USR: return shiftPrec; |
|
960 |
case PLUS: |
|
961 |
case MINUS: return addPrec; |
|
962 |
case MUL: |
|
963 |
case DIV: |
|
964 |
case MOD: return mulPrec; |
|
965 |
case TYPETEST: return ordPrec; |
|
10 | 966 |
default: throw new AssertionError(); |
967 |
} |
|
968 |
} |
|
969 |
||
10950 | 970 |
static Tree.Kind tagToKind(JCTree.Tag tag) { |
10 | 971 |
switch (tag) { |
972 |
// Postfix expressions |
|
10950 | 973 |
case POSTINC: // _ ++ |
10 | 974 |
return Tree.Kind.POSTFIX_INCREMENT; |
10950 | 975 |
case POSTDEC: // _ -- |
10 | 976 |
return Tree.Kind.POSTFIX_DECREMENT; |
977 |
||
978 |
// Unary operators |
|
10950 | 979 |
case PREINC: // ++ _ |
10 | 980 |
return Tree.Kind.PREFIX_INCREMENT; |
10950 | 981 |
case PREDEC: // -- _ |
10 | 982 |
return Tree.Kind.PREFIX_DECREMENT; |
10950 | 983 |
case POS: // + |
10 | 984 |
return Tree.Kind.UNARY_PLUS; |
10950 | 985 |
case NEG: // - |
10 | 986 |
return Tree.Kind.UNARY_MINUS; |
10950 | 987 |
case COMPL: // ~ |
10 | 988 |
return Tree.Kind.BITWISE_COMPLEMENT; |
10950 | 989 |
case NOT: // ! |
10 | 990 |
return Tree.Kind.LOGICAL_COMPLEMENT; |
991 |
||
992 |
// Binary operators |
|
993 |
||
994 |
// Multiplicative operators |
|
10950 | 995 |
case MUL: // * |
10 | 996 |
return Tree.Kind.MULTIPLY; |
10950 | 997 |
case DIV: // / |
10 | 998 |
return Tree.Kind.DIVIDE; |
10950 | 999 |
case MOD: // % |
10 | 1000 |
return Tree.Kind.REMAINDER; |
1001 |
||
1002 |
// Additive operators |
|
10950 | 1003 |
case PLUS: // + |
10 | 1004 |
return Tree.Kind.PLUS; |
10950 | 1005 |
case MINUS: // - |
10 | 1006 |
return Tree.Kind.MINUS; |
1007 |
||
1008 |
// Shift operators |
|
10950 | 1009 |
case SL: // << |
10 | 1010 |
return Tree.Kind.LEFT_SHIFT; |
10950 | 1011 |
case SR: // >> |
10 | 1012 |
return Tree.Kind.RIGHT_SHIFT; |
10950 | 1013 |
case USR: // >>> |
10 | 1014 |
return Tree.Kind.UNSIGNED_RIGHT_SHIFT; |
1015 |
||
1016 |
// Relational operators |
|
10950 | 1017 |
case LT: // < |
10 | 1018 |
return Tree.Kind.LESS_THAN; |
10950 | 1019 |
case GT: // > |
10 | 1020 |
return Tree.Kind.GREATER_THAN; |
10950 | 1021 |
case LE: // <= |
10 | 1022 |
return Tree.Kind.LESS_THAN_EQUAL; |
10950 | 1023 |
case GE: // >= |
10 | 1024 |
return Tree.Kind.GREATER_THAN_EQUAL; |
1025 |
||
1026 |
// Equality operators |
|
10950 | 1027 |
case EQ: // == |
10 | 1028 |
return Tree.Kind.EQUAL_TO; |
10950 | 1029 |
case NE: // != |
10 | 1030 |
return Tree.Kind.NOT_EQUAL_TO; |
1031 |
||
1032 |
// Bitwise and logical operators |
|
10950 | 1033 |
case BITAND: // & |
10 | 1034 |
return Tree.Kind.AND; |
10950 | 1035 |
case BITXOR: // ^ |
10 | 1036 |
return Tree.Kind.XOR; |
10950 | 1037 |
case BITOR: // | |
10 | 1038 |
return Tree.Kind.OR; |
1039 |
||
1040 |
// Conditional operators |
|
10950 | 1041 |
case AND: // && |
10 | 1042 |
return Tree.Kind.CONDITIONAL_AND; |
10950 | 1043 |
case OR: // || |
10 | 1044 |
return Tree.Kind.CONDITIONAL_OR; |
1045 |
||
1046 |
// Assignment operators |
|
10950 | 1047 |
case MUL_ASG: // *= |
10 | 1048 |
return Tree.Kind.MULTIPLY_ASSIGNMENT; |
10950 | 1049 |
case DIV_ASG: // /= |
10 | 1050 |
return Tree.Kind.DIVIDE_ASSIGNMENT; |
10950 | 1051 |
case MOD_ASG: // %= |
10 | 1052 |
return Tree.Kind.REMAINDER_ASSIGNMENT; |
10950 | 1053 |
case PLUS_ASG: // += |
10 | 1054 |
return Tree.Kind.PLUS_ASSIGNMENT; |
10950 | 1055 |
case MINUS_ASG: // -= |
10 | 1056 |
return Tree.Kind.MINUS_ASSIGNMENT; |
10950 | 1057 |
case SL_ASG: // <<= |
10 | 1058 |
return Tree.Kind.LEFT_SHIFT_ASSIGNMENT; |
10950 | 1059 |
case SR_ASG: // >>= |
10 | 1060 |
return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT; |
10950 | 1061 |
case USR_ASG: // >>>= |
10 | 1062 |
return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT; |
10950 | 1063 |
case BITAND_ASG: // &= |
10 | 1064 |
return Tree.Kind.AND_ASSIGNMENT; |
10950 | 1065 |
case BITXOR_ASG: // ^= |
10 | 1066 |
return Tree.Kind.XOR_ASSIGNMENT; |
10950 | 1067 |
case BITOR_ASG: // |= |
10 | 1068 |
return Tree.Kind.OR_ASSIGNMENT; |
1069 |
||
1070 |
// Null check (implementation detail), for example, __.getClass() |
|
10950 | 1071 |
case NULLCHK: |
10 | 1072 |
return Tree.Kind.OTHER; |
1073 |
||
15385 | 1074 |
case ANNOTATION: |
1075 |
return Tree.Kind.ANNOTATION; |
|
1076 |
case TYPE_ANNOTATION: |
|
1077 |
return Tree.Kind.TYPE_ANNOTATION; |
|
1078 |
||
10 | 1079 |
default: |
1080 |
return null; |
|
1081 |
} |
|
1082 |
} |
|
3149 | 1083 |
|
1084 |
/** |
|
15385 | 1085 |
* Returns the underlying type of the tree if it is an annotated type, |
1086 |
* or the tree itself otherwise. |
|
3149 | 1087 |
*/ |
1088 |
public static JCExpression typeIn(JCExpression tree) { |
|
1089 |
switch (tree.getTag()) { |
|
15385 | 1090 |
case ANNOTATED_TYPE: |
1091 |
return ((JCAnnotatedType)tree).underlyingType; |
|
10950 | 1092 |
case IDENT: /* simple names */ |
1093 |
case TYPEIDENT: /* primitive name */ |
|
1094 |
case SELECT: /* qualified name */ |
|
1095 |
case TYPEARRAY: /* array types */ |
|
1096 |
case WILDCARD: /* wild cards */ |
|
1097 |
case TYPEPARAMETER: /* type parameters */ |
|
1098 |
case TYPEAPPLY: /* parameterized types */ |
|
15385 | 1099 |
case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */ |
3149 | 1100 |
return tree; |
1101 |
default: |
|
1102 |
throw new AssertionError("Unexpected type tree: " + tree); |
|
1103 |
} |
|
1104 |
} |
|
4705
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
1105 |
|
15385 | 1106 |
/* Return the inner-most type of a type tree. |
1107 |
* For an array that contains an annotated type, return that annotated type. |
|
1108 |
* TODO: currently only used by Pretty. Describe behavior better. |
|
1109 |
*/ |
|
4705
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
1110 |
public static JCTree innermostType(JCTree type) { |
15385 | 1111 |
JCTree lastAnnotatedType = null; |
1112 |
JCTree cur = type; |
|
1113 |
loop: while (true) { |
|
1114 |
switch (cur.getTag()) { |
|
1115 |
case TYPEARRAY: |
|
1116 |
lastAnnotatedType = null; |
|
1117 |
cur = ((JCArrayTypeTree)cur).elemtype; |
|
1118 |
break; |
|
1119 |
case WILDCARD: |
|
1120 |
lastAnnotatedType = null; |
|
1121 |
cur = ((JCWildcard)cur).inner; |
|
1122 |
break; |
|
1123 |
case ANNOTATED_TYPE: |
|
1124 |
lastAnnotatedType = cur; |
|
1125 |
cur = ((JCAnnotatedType)cur).underlyingType; |
|
1126 |
break; |
|
1127 |
default: |
|
1128 |
break loop; |
|
1129 |
} |
|
1130 |
} |
|
1131 |
if (lastAnnotatedType!=null) { |
|
1132 |
return lastAnnotatedType; |
|
1133 |
} else { |
|
1134 |
return cur; |
|
4705
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
1135 |
} |
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
1136 |
} |
15385 | 1137 |
|
1138 |
private static class TypeAnnotationFinder extends TreeScanner { |
|
1139 |
public boolean foundTypeAnno = false; |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1140 |
|
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1141 |
@Override |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1142 |
public void scan(JCTree tree) { |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1143 |
if (foundTypeAnno || tree == null) |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1144 |
return; |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1145 |
super.scan(tree); |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1146 |
} |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17557
diff
changeset
|
1147 |
|
15385 | 1148 |
public void visitAnnotation(JCAnnotation tree) { |
1149 |
foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION); |
|
1150 |
} |
|
1151 |
} |
|
1152 |
||
1153 |
public static boolean containsTypeAnnotation(JCTree e) { |
|
1154 |
TypeAnnotationFinder finder = new TypeAnnotationFinder(); |
|
1155 |
finder.scan(e); |
|
1156 |
return finder.foundTypeAnno; |
|
1157 |
} |
|
36526 | 1158 |
|
1159 |
public static boolean isModuleInfo(JCCompilationUnit tree) { |
|
36776
5e260fa5208a
8150733: NPE when compiling module-info.java with a class declaration in a non-module mode
vromero
parents:
36526
diff
changeset
|
1160 |
return tree.sourcefile.isNameCompatible("module-info", JavaFileObject.Kind.SOURCE) |
5e260fa5208a
8150733: NPE when compiling module-info.java with a class declaration in a non-module mode
vromero
parents:
36526
diff
changeset
|
1161 |
&& tree.defs.nonEmpty() && tree.defs.head.hasTag(MODULEDEF); |
36526 | 1162 |
} |
1163 |
||
1164 |
public static JCModuleDecl getModule(JCCompilationUnit t) { |
|
1165 |
if (t.defs.nonEmpty()) { |
|
1166 |
JCTree def = t.defs.head; |
|
1167 |
if (def.hasTag(MODULEDEF)) |
|
1168 |
return (JCModuleDecl) def; |
|
1169 |
} |
|
1170 |
return null; |
|
1171 |
} |
|
1172 |
||
1173 |
public static boolean isPackageInfo(JCCompilationUnit tree) { |
|
1174 |
return tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); |
|
1175 |
} |
|
10 | 1176 |
} |