Fri, 05 Oct 2012 14:35:24 +0100
7177385: Add attribution support for lambda expressions
Summary: Add support for function descriptor lookup, functional interface inference and lambda expression type-checking
Reviewed-by: jjg, dlsmith
duke@1 | 1 | /* |
mcimadamore@1219 | 2 | * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.comp; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.tools.javac.code.*; |
mcimadamore@1347 | 29 | import com.sun.tools.javac.comp.DeferredAttr.AttrMode; |
mcimadamore@1347 | 30 | import com.sun.tools.javac.comp.Infer.InferenceContext; |
mcimadamore@1347 | 31 | import com.sun.tools.javac.comp.Infer.InferenceContext.FreeTypeListener; |
duke@1 | 32 | import com.sun.tools.javac.jvm.*; |
duke@1 | 33 | import com.sun.tools.javac.tree.*; |
duke@1 | 34 | import com.sun.tools.javac.util.*; |
duke@1 | 35 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
duke@1 | 36 | import com.sun.tools.javac.util.List; |
duke@1 | 37 | |
duke@1 | 38 | import com.sun.tools.javac.jvm.Target; |
mcimadamore@795 | 39 | import com.sun.tools.javac.code.Lint.LintCategory; |
duke@1 | 40 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 41 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 42 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@1238 | 43 | import com.sun.tools.javac.comp.Check.CheckContext; |
duke@1 | 44 | |
duke@1 | 45 | import com.sun.source.tree.IdentifierTree; |
mcimadamore@1348 | 46 | import com.sun.source.tree.LambdaExpressionTree; |
duke@1 | 47 | import com.sun.source.tree.MemberSelectTree; |
duke@1 | 48 | import com.sun.source.tree.TreeVisitor; |
duke@1 | 49 | import com.sun.source.util.SimpleTreeVisitor; |
duke@1 | 50 | |
mcimadamore@1347 | 51 | import java.util.*; |
mcimadamore@1347 | 52 | import java.util.Set; |
mcimadamore@1347 | 53 | import javax.lang.model.element.ElementKind; |
mcimadamore@1347 | 54 | import javax.tools.JavaFileObject; |
mcimadamore@1347 | 55 | |
duke@1 | 56 | import static com.sun.tools.javac.code.Flags.*; |
jjg@1127 | 57 | import static com.sun.tools.javac.code.Flags.ANNOTATION; |
jjg@1127 | 58 | import static com.sun.tools.javac.code.Flags.BLOCK; |
duke@1 | 59 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1127 | 60 | import static com.sun.tools.javac.code.Kinds.ERRONEOUS; |
duke@1 | 61 | import static com.sun.tools.javac.code.TypeTags.*; |
jjg@1127 | 62 | import static com.sun.tools.javac.code.TypeTags.WILDCARD; |
jjg@1127 | 63 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
duke@1 | 64 | |
duke@1 | 65 | /** This is the main context-dependent analysis phase in GJC. It |
duke@1 | 66 | * encompasses name resolution, type checking and constant folding as |
duke@1 | 67 | * subtasks. Some subtasks involve auxiliary classes. |
duke@1 | 68 | * @see Check |
duke@1 | 69 | * @see Resolve |
duke@1 | 70 | * @see ConstFold |
duke@1 | 71 | * @see Infer |
duke@1 | 72 | * |
jjg@581 | 73 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 74 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 75 | * This code and its internal interfaces are subject to change or |
duke@1 | 76 | * deletion without notice.</b> |
duke@1 | 77 | */ |
duke@1 | 78 | public class Attr extends JCTree.Visitor { |
duke@1 | 79 | protected static final Context.Key<Attr> attrKey = |
duke@1 | 80 | new Context.Key<Attr>(); |
duke@1 | 81 | |
jjg@113 | 82 | final Names names; |
duke@1 | 83 | final Log log; |
duke@1 | 84 | final Symtab syms; |
duke@1 | 85 | final Resolve rs; |
mcimadamore@537 | 86 | final Infer infer; |
mcimadamore@1347 | 87 | final DeferredAttr deferredAttr; |
duke@1 | 88 | final Check chk; |
mcimadamore@1348 | 89 | final Flow flow; |
duke@1 | 90 | final MemberEnter memberEnter; |
duke@1 | 91 | final TreeMaker make; |
duke@1 | 92 | final ConstFold cfolder; |
duke@1 | 93 | final Enter enter; |
duke@1 | 94 | final Target target; |
duke@1 | 95 | final Types types; |
mcimadamore@89 | 96 | final JCDiagnostic.Factory diags; |
duke@1 | 97 | final Annotate annotate; |
mcimadamore@852 | 98 | final DeferredLintHandler deferredLintHandler; |
duke@1 | 99 | |
duke@1 | 100 | public static Attr instance(Context context) { |
duke@1 | 101 | Attr instance = context.get(attrKey); |
duke@1 | 102 | if (instance == null) |
duke@1 | 103 | instance = new Attr(context); |
duke@1 | 104 | return instance; |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | protected Attr(Context context) { |
duke@1 | 108 | context.put(attrKey, this); |
duke@1 | 109 | |
jjg@113 | 110 | names = Names.instance(context); |
duke@1 | 111 | log = Log.instance(context); |
duke@1 | 112 | syms = Symtab.instance(context); |
duke@1 | 113 | rs = Resolve.instance(context); |
duke@1 | 114 | chk = Check.instance(context); |
mcimadamore@1348 | 115 | flow = Flow.instance(context); |
duke@1 | 116 | memberEnter = MemberEnter.instance(context); |
duke@1 | 117 | make = TreeMaker.instance(context); |
duke@1 | 118 | enter = Enter.instance(context); |
mcimadamore@537 | 119 | infer = Infer.instance(context); |
mcimadamore@1347 | 120 | deferredAttr = DeferredAttr.instance(context); |
duke@1 | 121 | cfolder = ConstFold.instance(context); |
duke@1 | 122 | target = Target.instance(context); |
duke@1 | 123 | types = Types.instance(context); |
mcimadamore@89 | 124 | diags = JCDiagnostic.Factory.instance(context); |
duke@1 | 125 | annotate = Annotate.instance(context); |
mcimadamore@852 | 126 | deferredLintHandler = DeferredLintHandler.instance(context); |
duke@1 | 127 | |
duke@1 | 128 | Options options = Options.instance(context); |
duke@1 | 129 | |
duke@1 | 130 | Source source = Source.instance(context); |
duke@1 | 131 | allowGenerics = source.allowGenerics(); |
duke@1 | 132 | allowVarargs = source.allowVarargs(); |
duke@1 | 133 | allowEnums = source.allowEnums(); |
duke@1 | 134 | allowBoxing = source.allowBoxing(); |
duke@1 | 135 | allowCovariantReturns = source.allowCovariantReturns(); |
duke@1 | 136 | allowAnonOuterThis = source.allowAnonOuterThis(); |
darcy@430 | 137 | allowStringsInSwitch = source.allowStringsInSwitch(); |
mcimadamore@1347 | 138 | allowPoly = source.allowPoly() && options.isSet("allowPoly"); |
mcimadamore@1348 | 139 | allowLambda = source.allowLambda(); |
darcy@430 | 140 | sourceName = source.name; |
jjg@700 | 141 | relax = (options.isSet("-retrofit") || |
jjg@700 | 142 | options.isSet("-relax")); |
mcimadamore@731 | 143 | findDiamonds = options.get("findDiamond") != null && |
mcimadamore@731 | 144 | source.allowDiamond(); |
jjg@700 | 145 | useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning"); |
mcimadamore@1348 | 146 | identifyLambdaCandidate = options.getBoolean("identifyLambdaCandidate", false); |
mcimadamore@1238 | 147 | |
mcimadamore@1238 | 148 | statInfo = new ResultInfo(NIL, Type.noType); |
mcimadamore@1238 | 149 | varInfo = new ResultInfo(VAR, Type.noType); |
mcimadamore@1238 | 150 | unknownExprInfo = new ResultInfo(VAL, Type.noType); |
mcimadamore@1238 | 151 | unknownTypeInfo = new ResultInfo(TYP, Type.noType); |
mcimadamore@1348 | 152 | recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext); |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | /** Switch: relax some constraints for retrofit mode. |
duke@1 | 156 | */ |
duke@1 | 157 | boolean relax; |
duke@1 | 158 | |
mcimadamore@1347 | 159 | /** Switch: support target-typing inference |
mcimadamore@1347 | 160 | */ |
mcimadamore@1347 | 161 | boolean allowPoly; |
mcimadamore@1347 | 162 | |
duke@1 | 163 | /** Switch: support generics? |
duke@1 | 164 | */ |
duke@1 | 165 | boolean allowGenerics; |
duke@1 | 166 | |
duke@1 | 167 | /** Switch: allow variable-arity methods. |
duke@1 | 168 | */ |
duke@1 | 169 | boolean allowVarargs; |
duke@1 | 170 | |
duke@1 | 171 | /** Switch: support enums? |
duke@1 | 172 | */ |
duke@1 | 173 | boolean allowEnums; |
duke@1 | 174 | |
duke@1 | 175 | /** Switch: support boxing and unboxing? |
duke@1 | 176 | */ |
duke@1 | 177 | boolean allowBoxing; |
duke@1 | 178 | |
duke@1 | 179 | /** Switch: support covariant result types? |
duke@1 | 180 | */ |
duke@1 | 181 | boolean allowCovariantReturns; |
duke@1 | 182 | |
mcimadamore@1348 | 183 | /** Switch: support lambda expressions ? |
mcimadamore@1348 | 184 | */ |
mcimadamore@1348 | 185 | boolean allowLambda; |
mcimadamore@1348 | 186 | |
duke@1 | 187 | /** Switch: allow references to surrounding object from anonymous |
duke@1 | 188 | * objects during constructor call? |
duke@1 | 189 | */ |
duke@1 | 190 | boolean allowAnonOuterThis; |
duke@1 | 191 | |
mcimadamore@731 | 192 | /** Switch: generates a warning if diamond can be safely applied |
mcimadamore@731 | 193 | * to a given new expression |
mcimadamore@731 | 194 | */ |
mcimadamore@731 | 195 | boolean findDiamonds; |
mcimadamore@731 | 196 | |
mcimadamore@731 | 197 | /** |
mcimadamore@731 | 198 | * Internally enables/disables diamond finder feature |
mcimadamore@731 | 199 | */ |
mcimadamore@731 | 200 | static final boolean allowDiamondFinder = true; |
mcimadamore@731 | 201 | |
duke@1 | 202 | /** |
duke@1 | 203 | * Switch: warn about use of variable before declaration? |
duke@1 | 204 | * RFE: 6425594 |
duke@1 | 205 | */ |
duke@1 | 206 | boolean useBeforeDeclarationWarning; |
duke@1 | 207 | |
jjg@377 | 208 | /** |
mcimadamore@1348 | 209 | * Switch: generate warnings whenever an anonymous inner class that is convertible |
mcimadamore@1348 | 210 | * to a lambda expression is found |
mcimadamore@1348 | 211 | */ |
mcimadamore@1348 | 212 | boolean identifyLambdaCandidate; |
mcimadamore@1348 | 213 | |
mcimadamore@1348 | 214 | /** |
darcy@430 | 215 | * Switch: allow strings in switch? |
darcy@430 | 216 | */ |
darcy@430 | 217 | boolean allowStringsInSwitch; |
darcy@430 | 218 | |
darcy@430 | 219 | /** |
darcy@430 | 220 | * Switch: name of source level; used for error reporting. |
darcy@430 | 221 | */ |
darcy@430 | 222 | String sourceName; |
darcy@430 | 223 | |
duke@1 | 224 | /** Check kind and type of given tree against protokind and prototype. |
duke@1 | 225 | * If check succeeds, store type in tree and return it. |
duke@1 | 226 | * If check fails, store errType in tree and return it. |
duke@1 | 227 | * No checks are performed if the prototype is a method type. |
jjg@110 | 228 | * It is not necessary in this case since we know that kind and type |
duke@1 | 229 | * are correct. |
duke@1 | 230 | * |
duke@1 | 231 | * @param tree The tree whose kind and type is checked |
duke@1 | 232 | * @param owntype The computed type of the tree |
duke@1 | 233 | * @param ownkind The computed kind of the tree |
mcimadamore@1220 | 234 | * @param resultInfo The expected result of the tree |
duke@1 | 235 | */ |
mcimadamore@1347 | 236 | Type check(final JCTree tree, final Type found, final int ownkind, final ResultInfo resultInfo) { |
mcimadamore@1347 | 237 | InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext(); |
mcimadamore@1347 | 238 | Type owntype = found; |
mcimadamore@1220 | 239 | if (owntype.tag != ERROR && resultInfo.pt.tag != METHOD && resultInfo.pt.tag != FORALL) { |
mcimadamore@1347 | 240 | if (inferenceContext.free(found)) { |
mcimadamore@1347 | 241 | inferenceContext.addFreeTypeListener(List.of(found, resultInfo.pt), new FreeTypeListener() { |
mcimadamore@1347 | 242 | @Override |
mcimadamore@1347 | 243 | public void typesInferred(InferenceContext inferenceContext) { |
mcimadamore@1347 | 244 | ResultInfo pendingResult = |
mcimadamore@1347 | 245 | resultInfo.dup(inferenceContext.asInstType(resultInfo.pt, types)); |
mcimadamore@1347 | 246 | check(tree, inferenceContext.asInstType(found, types), ownkind, pendingResult); |
mcimadamore@1347 | 247 | } |
mcimadamore@1347 | 248 | }); |
mcimadamore@1347 | 249 | return tree.type = resultInfo.pt; |
duke@1 | 250 | } else { |
mcimadamore@1347 | 251 | if ((ownkind & ~resultInfo.pkind) == 0) { |
mcimadamore@1347 | 252 | owntype = resultInfo.check(tree, owntype); |
mcimadamore@1347 | 253 | } else { |
mcimadamore@1347 | 254 | log.error(tree.pos(), "unexpected.type", |
mcimadamore@1347 | 255 | kindNames(resultInfo.pkind), |
mcimadamore@1347 | 256 | kindName(ownkind)); |
mcimadamore@1347 | 257 | owntype = types.createErrorType(owntype); |
mcimadamore@1347 | 258 | } |
duke@1 | 259 | } |
duke@1 | 260 | } |
duke@1 | 261 | tree.type = owntype; |
duke@1 | 262 | return owntype; |
duke@1 | 263 | } |
duke@1 | 264 | |
duke@1 | 265 | /** Is given blank final variable assignable, i.e. in a scope where it |
duke@1 | 266 | * may be assigned to even though it is final? |
duke@1 | 267 | * @param v The blank final variable. |
duke@1 | 268 | * @param env The current environment. |
duke@1 | 269 | */ |
duke@1 | 270 | boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) { |
mcimadamore@1297 | 271 | Symbol owner = owner(env); |
duke@1 | 272 | // owner refers to the innermost variable, method or |
duke@1 | 273 | // initializer block declaration at this point. |
duke@1 | 274 | return |
duke@1 | 275 | v.owner == owner |
duke@1 | 276 | || |
duke@1 | 277 | ((owner.name == names.init || // i.e. we are in a constructor |
duke@1 | 278 | owner.kind == VAR || // i.e. we are in a variable initializer |
duke@1 | 279 | (owner.flags() & BLOCK) != 0) // i.e. we are in an initializer block |
duke@1 | 280 | && |
duke@1 | 281 | v.owner == owner.owner |
duke@1 | 282 | && |
duke@1 | 283 | ((v.flags() & STATIC) != 0) == Resolve.isStatic(env)); |
duke@1 | 284 | } |
duke@1 | 285 | |
mcimadamore@1297 | 286 | /** |
mcimadamore@1297 | 287 | * Return the innermost enclosing owner symbol in a given attribution context |
mcimadamore@1297 | 288 | */ |
mcimadamore@1297 | 289 | Symbol owner(Env<AttrContext> env) { |
mcimadamore@1297 | 290 | while (true) { |
mcimadamore@1297 | 291 | switch (env.tree.getTag()) { |
mcimadamore@1297 | 292 | case VARDEF: |
mcimadamore@1297 | 293 | //a field can be owner |
mcimadamore@1297 | 294 | VarSymbol vsym = ((JCVariableDecl)env.tree).sym; |
mcimadamore@1297 | 295 | if (vsym.owner.kind == TYP) { |
mcimadamore@1297 | 296 | return vsym; |
mcimadamore@1297 | 297 | } |
mcimadamore@1297 | 298 | break; |
mcimadamore@1297 | 299 | case METHODDEF: |
mcimadamore@1297 | 300 | //method def is always an owner |
mcimadamore@1297 | 301 | return ((JCMethodDecl)env.tree).sym; |
mcimadamore@1297 | 302 | case CLASSDEF: |
mcimadamore@1297 | 303 | //class def is always an owner |
mcimadamore@1297 | 304 | return ((JCClassDecl)env.tree).sym; |
mcimadamore@1348 | 305 | case LAMBDA: |
mcimadamore@1348 | 306 | //a lambda is an owner - return a fresh synthetic method symbol |
mcimadamore@1348 | 307 | return new MethodSymbol(0, names.empty, null, syms.methodClass); |
mcimadamore@1297 | 308 | case BLOCK: |
mcimadamore@1297 | 309 | //static/instance init blocks are owner |
mcimadamore@1297 | 310 | Symbol blockSym = env.info.scope.owner; |
mcimadamore@1297 | 311 | if ((blockSym.flags() & BLOCK) != 0) { |
mcimadamore@1297 | 312 | return blockSym; |
mcimadamore@1297 | 313 | } |
mcimadamore@1297 | 314 | break; |
mcimadamore@1297 | 315 | case TOPLEVEL: |
mcimadamore@1297 | 316 | //toplevel is always an owner (for pkge decls) |
mcimadamore@1297 | 317 | return env.info.scope.owner; |
mcimadamore@1297 | 318 | } |
mcimadamore@1297 | 319 | Assert.checkNonNull(env.next); |
mcimadamore@1297 | 320 | env = env.next; |
mcimadamore@1297 | 321 | } |
mcimadamore@1297 | 322 | } |
mcimadamore@1297 | 323 | |
duke@1 | 324 | /** Check that variable can be assigned to. |
duke@1 | 325 | * @param pos The current source code position. |
duke@1 | 326 | * @param v The assigned varaible |
duke@1 | 327 | * @param base If the variable is referred to in a Select, the part |
duke@1 | 328 | * to the left of the `.', null otherwise. |
duke@1 | 329 | * @param env The current environment. |
duke@1 | 330 | */ |
duke@1 | 331 | void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) { |
duke@1 | 332 | if ((v.flags() & FINAL) != 0 && |
duke@1 | 333 | ((v.flags() & HASINIT) != 0 |
duke@1 | 334 | || |
duke@1 | 335 | !((base == null || |
jjg@1127 | 336 | (base.hasTag(IDENT) && TreeInfo.name(base) == names._this)) && |
duke@1 | 337 | isAssignableAsBlankFinal(v, env)))) { |
darcy@609 | 338 | if (v.isResourceVariable()) { //TWR resource |
mcimadamore@743 | 339 | log.error(pos, "try.resource.may.not.be.assigned", v); |
darcy@609 | 340 | } else { |
darcy@609 | 341 | log.error(pos, "cant.assign.val.to.final.var", v); |
darcy@609 | 342 | } |
duke@1 | 343 | } |
duke@1 | 344 | } |
duke@1 | 345 | |
duke@1 | 346 | /** Does tree represent a static reference to an identifier? |
duke@1 | 347 | * It is assumed that tree is either a SELECT or an IDENT. |
duke@1 | 348 | * We have to weed out selects from non-type names here. |
duke@1 | 349 | * @param tree The candidate tree. |
duke@1 | 350 | */ |
duke@1 | 351 | boolean isStaticReference(JCTree tree) { |
jjg@1127 | 352 | if (tree.hasTag(SELECT)) { |
duke@1 | 353 | Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected); |
duke@1 | 354 | if (lsym == null || lsym.kind != TYP) { |
duke@1 | 355 | return false; |
duke@1 | 356 | } |
duke@1 | 357 | } |
duke@1 | 358 | return true; |
duke@1 | 359 | } |
duke@1 | 360 | |
duke@1 | 361 | /** Is this symbol a type? |
duke@1 | 362 | */ |
duke@1 | 363 | static boolean isType(Symbol sym) { |
duke@1 | 364 | return sym != null && sym.kind == TYP; |
duke@1 | 365 | } |
duke@1 | 366 | |
duke@1 | 367 | /** The current `this' symbol. |
duke@1 | 368 | * @param env The current environment. |
duke@1 | 369 | */ |
duke@1 | 370 | Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) { |
duke@1 | 371 | return rs.resolveSelf(pos, env, env.enclClass.sym, names._this); |
duke@1 | 372 | } |
duke@1 | 373 | |
duke@1 | 374 | /** Attribute a parsed identifier. |
duke@1 | 375 | * @param tree Parsed identifier name |
duke@1 | 376 | * @param topLevel The toplevel to use |
duke@1 | 377 | */ |
duke@1 | 378 | public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) { |
duke@1 | 379 | Env<AttrContext> localEnv = enter.topLevelEnv(topLevel); |
duke@1 | 380 | localEnv.enclClass = make.ClassDef(make.Modifiers(0), |
duke@1 | 381 | syms.errSymbol.name, |
duke@1 | 382 | null, null, null, null); |
duke@1 | 383 | localEnv.enclClass.sym = syms.errSymbol; |
duke@1 | 384 | return tree.accept(identAttributer, localEnv); |
duke@1 | 385 | } |
duke@1 | 386 | // where |
duke@1 | 387 | private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer(); |
duke@1 | 388 | private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> { |
duke@1 | 389 | @Override |
duke@1 | 390 | public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) { |
duke@1 | 391 | Symbol site = visit(node.getExpression(), env); |
duke@1 | 392 | if (site.kind == ERR) |
duke@1 | 393 | return site; |
duke@1 | 394 | Name name = (Name)node.getIdentifier(); |
duke@1 | 395 | if (site.kind == PCK) { |
duke@1 | 396 | env.toplevel.packge = (PackageSymbol)site; |
duke@1 | 397 | return rs.findIdentInPackage(env, (TypeSymbol)site, name, TYP | PCK); |
duke@1 | 398 | } else { |
duke@1 | 399 | env.enclClass.sym = (ClassSymbol)site; |
duke@1 | 400 | return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site); |
duke@1 | 401 | } |
duke@1 | 402 | } |
duke@1 | 403 | |
duke@1 | 404 | @Override |
duke@1 | 405 | public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) { |
duke@1 | 406 | return rs.findIdent(env, (Name)node.getName(), TYP | PCK); |
duke@1 | 407 | } |
duke@1 | 408 | } |
duke@1 | 409 | |
duke@1 | 410 | public Type coerce(Type etype, Type ttype) { |
duke@1 | 411 | return cfolder.coerce(etype, ttype); |
duke@1 | 412 | } |
duke@1 | 413 | |
duke@1 | 414 | public Type attribType(JCTree node, TypeSymbol sym) { |
duke@1 | 415 | Env<AttrContext> env = enter.typeEnvs.get(sym); |
duke@1 | 416 | Env<AttrContext> localEnv = env.dup(node, env.info.dup()); |
mcimadamore@1220 | 417 | return attribTree(node, localEnv, unknownTypeInfo); |
mcimadamore@1220 | 418 | } |
mcimadamore@1220 | 419 | |
mcimadamore@1220 | 420 | public Type attribImportQualifier(JCImport tree, Env<AttrContext> env) { |
mcimadamore@1220 | 421 | // Attribute qualifying package or class. |
mcimadamore@1220 | 422 | JCFieldAccess s = (JCFieldAccess)tree.qualid; |
mcimadamore@1220 | 423 | return attribTree(s.selected, |
mcimadamore@1220 | 424 | env, |
mcimadamore@1220 | 425 | new ResultInfo(tree.staticImport ? TYP : (TYP | PCK), |
mcimadamore@1220 | 426 | Type.noType)); |
duke@1 | 427 | } |
duke@1 | 428 | |
duke@1 | 429 | public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) { |
duke@1 | 430 | breakTree = tree; |
mcimadamore@303 | 431 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
duke@1 | 432 | try { |
duke@1 | 433 | attribExpr(expr, env); |
duke@1 | 434 | } catch (BreakAttr b) { |
duke@1 | 435 | return b.env; |
sundar@669 | 436 | } catch (AssertionError ae) { |
sundar@669 | 437 | if (ae.getCause() instanceof BreakAttr) { |
sundar@669 | 438 | return ((BreakAttr)(ae.getCause())).env; |
sundar@669 | 439 | } else { |
sundar@669 | 440 | throw ae; |
sundar@669 | 441 | } |
duke@1 | 442 | } finally { |
duke@1 | 443 | breakTree = null; |
duke@1 | 444 | log.useSource(prev); |
duke@1 | 445 | } |
duke@1 | 446 | return env; |
duke@1 | 447 | } |
duke@1 | 448 | |
duke@1 | 449 | public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) { |
duke@1 | 450 | breakTree = tree; |
mcimadamore@303 | 451 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
duke@1 | 452 | try { |
duke@1 | 453 | attribStat(stmt, env); |
duke@1 | 454 | } catch (BreakAttr b) { |
duke@1 | 455 | return b.env; |
sundar@669 | 456 | } catch (AssertionError ae) { |
sundar@669 | 457 | if (ae.getCause() instanceof BreakAttr) { |
sundar@669 | 458 | return ((BreakAttr)(ae.getCause())).env; |
sundar@669 | 459 | } else { |
sundar@669 | 460 | throw ae; |
sundar@669 | 461 | } |
duke@1 | 462 | } finally { |
duke@1 | 463 | breakTree = null; |
duke@1 | 464 | log.useSource(prev); |
duke@1 | 465 | } |
duke@1 | 466 | return env; |
duke@1 | 467 | } |
duke@1 | 468 | |
duke@1 | 469 | private JCTree breakTree = null; |
duke@1 | 470 | |
duke@1 | 471 | private static class BreakAttr extends RuntimeException { |
duke@1 | 472 | static final long serialVersionUID = -6924771130405446405L; |
duke@1 | 473 | private Env<AttrContext> env; |
duke@1 | 474 | private BreakAttr(Env<AttrContext> env) { |
mcimadamore@1347 | 475 | this.env = copyEnv(env); |
mcimadamore@1347 | 476 | } |
mcimadamore@1347 | 477 | |
mcimadamore@1347 | 478 | private Env<AttrContext> copyEnv(Env<AttrContext> env) { |
mcimadamore@1347 | 479 | Env<AttrContext> newEnv = |
mcimadamore@1347 | 480 | env.dup(env.tree, env.info.dup(copyScope(env.info.scope))); |
mcimadamore@1347 | 481 | if (newEnv.outer != null) { |
mcimadamore@1347 | 482 | newEnv.outer = copyEnv(newEnv.outer); |
mcimadamore@1347 | 483 | } |
mcimadamore@1347 | 484 | return newEnv; |
mcimadamore@1347 | 485 | } |
mcimadamore@1347 | 486 | |
mcimadamore@1347 | 487 | private Scope copyScope(Scope sc) { |
mcimadamore@1347 | 488 | Scope newScope = new Scope(sc.owner); |
mcimadamore@1347 | 489 | List<Symbol> elemsList = List.nil(); |
mcimadamore@1347 | 490 | while (sc != null) { |
mcimadamore@1347 | 491 | for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) { |
mcimadamore@1347 | 492 | elemsList = elemsList.prepend(e.sym); |
mcimadamore@1347 | 493 | } |
mcimadamore@1347 | 494 | sc = sc.next; |
mcimadamore@1347 | 495 | } |
mcimadamore@1347 | 496 | for (Symbol s : elemsList) { |
mcimadamore@1347 | 497 | newScope.enter(s); |
mcimadamore@1347 | 498 | } |
mcimadamore@1347 | 499 | return newScope; |
duke@1 | 500 | } |
duke@1 | 501 | } |
duke@1 | 502 | |
mcimadamore@1238 | 503 | class ResultInfo { |
mcimadamore@1347 | 504 | final int pkind; |
mcimadamore@1347 | 505 | final Type pt; |
mcimadamore@1347 | 506 | final CheckContext checkContext; |
mcimadamore@1220 | 507 | |
mcimadamore@1220 | 508 | ResultInfo(int pkind, Type pt) { |
mcimadamore@1238 | 509 | this(pkind, pt, chk.basicHandler); |
mcimadamore@1238 | 510 | } |
mcimadamore@1238 | 511 | |
mcimadamore@1238 | 512 | protected ResultInfo(int pkind, Type pt, CheckContext checkContext) { |
mcimadamore@1220 | 513 | this.pkind = pkind; |
mcimadamore@1220 | 514 | this.pt = pt; |
mcimadamore@1238 | 515 | this.checkContext = checkContext; |
mcimadamore@1238 | 516 | } |
mcimadamore@1238 | 517 | |
mcimadamore@1347 | 518 | protected Type check(final DiagnosticPosition pos, final Type found) { |
mcimadamore@1238 | 519 | return chk.checkType(pos, found, pt, checkContext); |
mcimadamore@1220 | 520 | } |
mcimadamore@1347 | 521 | |
mcimadamore@1347 | 522 | protected ResultInfo dup(Type newPt) { |
mcimadamore@1347 | 523 | return new ResultInfo(pkind, newPt, checkContext); |
mcimadamore@1347 | 524 | } |
mcimadamore@1220 | 525 | } |
mcimadamore@1220 | 526 | |
mcimadamore@1348 | 527 | class RecoveryInfo extends ResultInfo { |
mcimadamore@1348 | 528 | |
mcimadamore@1348 | 529 | public RecoveryInfo(final DeferredAttr.DeferredAttrContext deferredAttrContext) { |
mcimadamore@1348 | 530 | super(Kinds.VAL, Type.recoveryType, new Check.NestedCheckContext(chk.basicHandler) { |
mcimadamore@1348 | 531 | @Override |
mcimadamore@1348 | 532 | public DeferredAttr.DeferredAttrContext deferredAttrContext() { |
mcimadamore@1348 | 533 | return deferredAttrContext; |
mcimadamore@1348 | 534 | } |
mcimadamore@1348 | 535 | @Override |
mcimadamore@1348 | 536 | public boolean compatible(Type found, Type req, Warner warn) { |
mcimadamore@1348 | 537 | return true; |
mcimadamore@1348 | 538 | } |
mcimadamore@1348 | 539 | @Override |
mcimadamore@1348 | 540 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
mcimadamore@1348 | 541 | //do nothing |
mcimadamore@1348 | 542 | } |
mcimadamore@1348 | 543 | }); |
mcimadamore@1348 | 544 | } |
mcimadamore@1348 | 545 | |
mcimadamore@1348 | 546 | @Override |
mcimadamore@1348 | 547 | protected Type check(DiagnosticPosition pos, Type found) { |
mcimadamore@1348 | 548 | return chk.checkNonVoid(pos, super.check(pos, found)); |
mcimadamore@1348 | 549 | } |
mcimadamore@1348 | 550 | } |
mcimadamore@1348 | 551 | |
mcimadamore@1347 | 552 | final ResultInfo statInfo; |
mcimadamore@1347 | 553 | final ResultInfo varInfo; |
mcimadamore@1347 | 554 | final ResultInfo unknownExprInfo; |
mcimadamore@1347 | 555 | final ResultInfo unknownTypeInfo; |
mcimadamore@1348 | 556 | final ResultInfo recoveryInfo; |
mcimadamore@1220 | 557 | |
mcimadamore@1220 | 558 | Type pt() { |
mcimadamore@1220 | 559 | return resultInfo.pt; |
mcimadamore@1220 | 560 | } |
mcimadamore@1220 | 561 | |
mcimadamore@1220 | 562 | int pkind() { |
mcimadamore@1220 | 563 | return resultInfo.pkind; |
mcimadamore@1220 | 564 | } |
duke@1 | 565 | |
duke@1 | 566 | /* ************************************************************************ |
duke@1 | 567 | * Visitor methods |
duke@1 | 568 | *************************************************************************/ |
duke@1 | 569 | |
duke@1 | 570 | /** Visitor argument: the current environment. |
duke@1 | 571 | */ |
duke@1 | 572 | Env<AttrContext> env; |
duke@1 | 573 | |
mcimadamore@1220 | 574 | /** Visitor argument: the currently expected attribution result. |
duke@1 | 575 | */ |
mcimadamore@1220 | 576 | ResultInfo resultInfo; |
duke@1 | 577 | |
duke@1 | 578 | /** Visitor result: the computed type. |
duke@1 | 579 | */ |
duke@1 | 580 | Type result; |
duke@1 | 581 | |
duke@1 | 582 | /** Visitor method: attribute a tree, catching any completion failure |
duke@1 | 583 | * exceptions. Return the tree's type. |
duke@1 | 584 | * |
duke@1 | 585 | * @param tree The tree to be visited. |
duke@1 | 586 | * @param env The environment visitor argument. |
mcimadamore@1220 | 587 | * @param resultInfo The result info visitor argument. |
duke@1 | 588 | */ |
mcimadamore@1347 | 589 | Type attribTree(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
duke@1 | 590 | Env<AttrContext> prevEnv = this.env; |
mcimadamore@1220 | 591 | ResultInfo prevResult = this.resultInfo; |
duke@1 | 592 | try { |
duke@1 | 593 | this.env = env; |
mcimadamore@1220 | 594 | this.resultInfo = resultInfo; |
duke@1 | 595 | tree.accept(this); |
duke@1 | 596 | if (tree == breakTree) |
duke@1 | 597 | throw new BreakAttr(env); |
duke@1 | 598 | return result; |
duke@1 | 599 | } catch (CompletionFailure ex) { |
duke@1 | 600 | tree.type = syms.errType; |
duke@1 | 601 | return chk.completionError(tree.pos(), ex); |
duke@1 | 602 | } finally { |
duke@1 | 603 | this.env = prevEnv; |
mcimadamore@1220 | 604 | this.resultInfo = prevResult; |
duke@1 | 605 | } |
duke@1 | 606 | } |
duke@1 | 607 | |
duke@1 | 608 | /** Derived visitor method: attribute an expression tree. |
duke@1 | 609 | */ |
duke@1 | 610 | public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) { |
mcimadamore@1238 | 611 | return attribTree(tree, env, new ResultInfo(VAL, pt.tag != ERROR ? pt : Type.noType)); |
darcy@609 | 612 | } |
darcy@609 | 613 | |
duke@1 | 614 | /** Derived visitor method: attribute an expression tree with |
duke@1 | 615 | * no constraints on the computed type. |
duke@1 | 616 | */ |
duke@1 | 617 | Type attribExpr(JCTree tree, Env<AttrContext> env) { |
mcimadamore@1220 | 618 | return attribTree(tree, env, unknownExprInfo); |
duke@1 | 619 | } |
duke@1 | 620 | |
duke@1 | 621 | /** Derived visitor method: attribute a type tree. |
duke@1 | 622 | */ |
duke@1 | 623 | Type attribType(JCTree tree, Env<AttrContext> env) { |
mcimadamore@537 | 624 | Type result = attribType(tree, env, Type.noType); |
mcimadamore@537 | 625 | return result; |
mcimadamore@537 | 626 | } |
mcimadamore@537 | 627 | |
mcimadamore@537 | 628 | /** Derived visitor method: attribute a type tree. |
mcimadamore@537 | 629 | */ |
mcimadamore@537 | 630 | Type attribType(JCTree tree, Env<AttrContext> env, Type pt) { |
mcimadamore@1220 | 631 | Type result = attribTree(tree, env, new ResultInfo(TYP, pt)); |
duke@1 | 632 | return result; |
duke@1 | 633 | } |
duke@1 | 634 | |
duke@1 | 635 | /** Derived visitor method: attribute a statement or definition tree. |
duke@1 | 636 | */ |
duke@1 | 637 | public Type attribStat(JCTree tree, Env<AttrContext> env) { |
mcimadamore@1220 | 638 | return attribTree(tree, env, statInfo); |
duke@1 | 639 | } |
duke@1 | 640 | |
duke@1 | 641 | /** Attribute a list of expressions, returning a list of types. |
duke@1 | 642 | */ |
duke@1 | 643 | List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) { |
duke@1 | 644 | ListBuffer<Type> ts = new ListBuffer<Type>(); |
duke@1 | 645 | for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
duke@1 | 646 | ts.append(attribExpr(l.head, env, pt)); |
duke@1 | 647 | return ts.toList(); |
duke@1 | 648 | } |
duke@1 | 649 | |
duke@1 | 650 | /** Attribute a list of statements, returning nothing. |
duke@1 | 651 | */ |
duke@1 | 652 | <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) { |
duke@1 | 653 | for (List<T> l = trees; l.nonEmpty(); l = l.tail) |
duke@1 | 654 | attribStat(l.head, env); |
duke@1 | 655 | } |
duke@1 | 656 | |
duke@1 | 657 | /** Attribute the arguments in a method call, returning a list of types. |
duke@1 | 658 | */ |
duke@1 | 659 | List<Type> attribArgs(List<JCExpression> trees, Env<AttrContext> env) { |
duke@1 | 660 | ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
mcimadamore@1347 | 661 | for (JCExpression arg : trees) { |
mcimadamore@1347 | 662 | Type argtype = allowPoly && TreeInfo.isPoly(arg, env.tree) ? |
mcimadamore@1347 | 663 | deferredAttr.new DeferredType(arg, env) : |
mcimadamore@1347 | 664 | chk.checkNonVoid(arg, attribExpr(arg, env, Infer.anyPoly)); |
mcimadamore@1347 | 665 | argtypes.append(argtype); |
mcimadamore@1347 | 666 | } |
duke@1 | 667 | return argtypes.toList(); |
duke@1 | 668 | } |
duke@1 | 669 | |
duke@1 | 670 | /** Attribute a type argument list, returning a list of types. |
jrose@267 | 671 | * Caller is responsible for calling checkRefTypes. |
duke@1 | 672 | */ |
jrose@267 | 673 | List<Type> attribAnyTypes(List<JCExpression> trees, Env<AttrContext> env) { |
duke@1 | 674 | ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
duke@1 | 675 | for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
jrose@267 | 676 | argtypes.append(attribType(l.head, env)); |
duke@1 | 677 | return argtypes.toList(); |
duke@1 | 678 | } |
duke@1 | 679 | |
jrose@267 | 680 | /** Attribute a type argument list, returning a list of types. |
jrose@267 | 681 | * Check that all the types are references. |
jrose@267 | 682 | */ |
jrose@267 | 683 | List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) { |
jrose@267 | 684 | List<Type> types = attribAnyTypes(trees, env); |
jrose@267 | 685 | return chk.checkRefTypes(trees, types); |
jrose@267 | 686 | } |
duke@1 | 687 | |
duke@1 | 688 | /** |
duke@1 | 689 | * Attribute type variables (of generic classes or methods). |
duke@1 | 690 | * Compound types are attributed later in attribBounds. |
duke@1 | 691 | * @param typarams the type variables to enter |
duke@1 | 692 | * @param env the current environment |
duke@1 | 693 | */ |
duke@1 | 694 | void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env) { |
duke@1 | 695 | for (JCTypeParameter tvar : typarams) { |
duke@1 | 696 | TypeVar a = (TypeVar)tvar.type; |
mcimadamore@42 | 697 | a.tsym.flags_field |= UNATTRIBUTED; |
mcimadamore@42 | 698 | a.bound = Type.noType; |
duke@1 | 699 | if (!tvar.bounds.isEmpty()) { |
duke@1 | 700 | List<Type> bounds = List.of(attribType(tvar.bounds.head, env)); |
duke@1 | 701 | for (JCExpression bound : tvar.bounds.tail) |
duke@1 | 702 | bounds = bounds.prepend(attribType(bound, env)); |
duke@1 | 703 | types.setBounds(a, bounds.reverse()); |
duke@1 | 704 | } else { |
duke@1 | 705 | // if no bounds are given, assume a single bound of |
duke@1 | 706 | // java.lang.Object. |
duke@1 | 707 | types.setBounds(a, List.of(syms.objectType)); |
duke@1 | 708 | } |
mcimadamore@42 | 709 | a.tsym.flags_field &= ~UNATTRIBUTED; |
duke@1 | 710 | } |
duke@1 | 711 | for (JCTypeParameter tvar : typarams) |
duke@1 | 712 | chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type); |
duke@1 | 713 | attribStats(typarams, env); |
mcimadamore@42 | 714 | } |
mcimadamore@42 | 715 | |
mcimadamore@42 | 716 | void attribBounds(List<JCTypeParameter> typarams) { |
duke@1 | 717 | for (JCTypeParameter typaram : typarams) { |
duke@1 | 718 | Type bound = typaram.type.getUpperBound(); |
duke@1 | 719 | if (bound != null && bound.tsym instanceof ClassSymbol) { |
duke@1 | 720 | ClassSymbol c = (ClassSymbol)bound.tsym; |
duke@1 | 721 | if ((c.flags_field & COMPOUND) != 0) { |
jjg@816 | 722 | Assert.check((c.flags_field & UNATTRIBUTED) != 0, c); |
duke@1 | 723 | attribClass(typaram.pos(), c); |
duke@1 | 724 | } |
duke@1 | 725 | } |
duke@1 | 726 | } |
duke@1 | 727 | } |
duke@1 | 728 | |
duke@1 | 729 | /** |
duke@1 | 730 | * Attribute the type references in a list of annotations. |
duke@1 | 731 | */ |
duke@1 | 732 | void attribAnnotationTypes(List<JCAnnotation> annotations, |
duke@1 | 733 | Env<AttrContext> env) { |
duke@1 | 734 | for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
duke@1 | 735 | JCAnnotation a = al.head; |
duke@1 | 736 | attribType(a.annotationType, env); |
duke@1 | 737 | } |
duke@1 | 738 | } |
duke@1 | 739 | |
jjg@841 | 740 | /** |
jjg@841 | 741 | * Attribute a "lazy constant value". |
jjg@841 | 742 | * @param env The env for the const value |
jjg@841 | 743 | * @param initializer The initializer for the const value |
jjg@841 | 744 | * @param type The expected type, or null |
jjg@841 | 745 | * @see VarSymbol#setlazyConstValue |
jjg@841 | 746 | */ |
jjg@841 | 747 | public Object attribLazyConstantValue(Env<AttrContext> env, |
jjg@841 | 748 | JCTree.JCExpression initializer, |
jjg@841 | 749 | Type type) { |
jjg@841 | 750 | |
jjg@841 | 751 | // in case no lint value has been set up for this env, scan up |
jjg@841 | 752 | // env stack looking for smallest enclosing env for which it is set. |
jjg@841 | 753 | Env<AttrContext> lintEnv = env; |
jjg@841 | 754 | while (lintEnv.info.lint == null) |
jjg@841 | 755 | lintEnv = lintEnv.next; |
jjg@841 | 756 | |
jjg@841 | 757 | // Having found the enclosing lint value, we can initialize the lint value for this class |
jjg@1078 | 758 | // ... but ... |
jjg@1078 | 759 | // There's a problem with evaluating annotations in the right order, such that |
jjg@1078 | 760 | // env.info.enclVar.attributes_field might not yet have been evaluated, and so might be |
jjg@1078 | 761 | // null. In that case, calling augment will throw an NPE. To avoid this, for now we |
jjg@1078 | 762 | // revert to the jdk 6 behavior and ignore the (unevaluated) attributes. |
jfranck@1313 | 763 | if (env.info.enclVar.annotations.pendingCompletion()) { |
jjg@1078 | 764 | env.info.lint = lintEnv.info.lint; |
jfranck@1313 | 765 | } else { |
jfranck@1313 | 766 | env.info.lint = lintEnv.info.lint.augment(env.info.enclVar.annotations, |
jfranck@1313 | 767 | env.info.enclVar.flags()); |
jfranck@1313 | 768 | } |
jjg@841 | 769 | |
jjg@841 | 770 | Lint prevLint = chk.setLint(env.info.lint); |
jjg@841 | 771 | JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile); |
jjg@841 | 772 | |
jjg@841 | 773 | try { |
jjg@841 | 774 | Type itype = attribExpr(initializer, env, type); |
jjg@841 | 775 | if (itype.constValue() != null) |
jjg@841 | 776 | return coerce(itype, type).constValue(); |
jjg@841 | 777 | else |
jjg@841 | 778 | return null; |
jjg@841 | 779 | } finally { |
jjg@841 | 780 | env.info.lint = prevLint; |
jjg@841 | 781 | log.useSource(prevSource); |
jjg@841 | 782 | } |
jjg@841 | 783 | } |
jjg@841 | 784 | |
duke@1 | 785 | /** Attribute type reference in an `extends' or `implements' clause. |
mcimadamore@537 | 786 | * Supertypes of anonymous inner classes are usually already attributed. |
duke@1 | 787 | * |
duke@1 | 788 | * @param tree The tree making up the type reference. |
duke@1 | 789 | * @param env The environment current at the reference. |
duke@1 | 790 | * @param classExpected true if only a class is expected here. |
duke@1 | 791 | * @param interfaceExpected true if only an interface is expected here. |
duke@1 | 792 | */ |
duke@1 | 793 | Type attribBase(JCTree tree, |
duke@1 | 794 | Env<AttrContext> env, |
duke@1 | 795 | boolean classExpected, |
duke@1 | 796 | boolean interfaceExpected, |
duke@1 | 797 | boolean checkExtensible) { |
mcimadamore@537 | 798 | Type t = tree.type != null ? |
mcimadamore@537 | 799 | tree.type : |
mcimadamore@537 | 800 | attribType(tree, env); |
duke@1 | 801 | return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible); |
duke@1 | 802 | } |
duke@1 | 803 | Type checkBase(Type t, |
duke@1 | 804 | JCTree tree, |
duke@1 | 805 | Env<AttrContext> env, |
duke@1 | 806 | boolean classExpected, |
duke@1 | 807 | boolean interfaceExpected, |
duke@1 | 808 | boolean checkExtensible) { |
jjg@664 | 809 | if (t.isErroneous()) |
jjg@664 | 810 | return t; |
duke@1 | 811 | if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) { |
duke@1 | 812 | // check that type variable is already visible |
duke@1 | 813 | if (t.getUpperBound() == null) { |
duke@1 | 814 | log.error(tree.pos(), "illegal.forward.ref"); |
jjg@110 | 815 | return types.createErrorType(t); |
duke@1 | 816 | } |
duke@1 | 817 | } else { |
duke@1 | 818 | t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics); |
duke@1 | 819 | } |
duke@1 | 820 | if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { |
duke@1 | 821 | log.error(tree.pos(), "intf.expected.here"); |
duke@1 | 822 | // return errType is necessary since otherwise there might |
duke@1 | 823 | // be undetected cycles which cause attribution to loop |
jjg@110 | 824 | return types.createErrorType(t); |
duke@1 | 825 | } else if (checkExtensible && |
duke@1 | 826 | classExpected && |
duke@1 | 827 | (t.tsym.flags() & INTERFACE) != 0) { |
jjg@664 | 828 | log.error(tree.pos(), "no.intf.expected.here"); |
jjg@110 | 829 | return types.createErrorType(t); |
duke@1 | 830 | } |
duke@1 | 831 | if (checkExtensible && |
duke@1 | 832 | ((t.tsym.flags() & FINAL) != 0)) { |
duke@1 | 833 | log.error(tree.pos(), |
duke@1 | 834 | "cant.inherit.from.final", t.tsym); |
duke@1 | 835 | } |
duke@1 | 836 | chk.checkNonCyclic(tree.pos(), t); |
duke@1 | 837 | return t; |
duke@1 | 838 | } |
duke@1 | 839 | |
mcimadamore@1269 | 840 | Type attribIdentAsEnumType(Env<AttrContext> env, JCIdent id) { |
mcimadamore@1269 | 841 | Assert.check((env.enclClass.sym.flags() & ENUM) != 0); |
mcimadamore@1269 | 842 | id.type = env.info.scope.owner.type; |
mcimadamore@1269 | 843 | id.sym = env.info.scope.owner; |
mcimadamore@1269 | 844 | return id.type; |
mcimadamore@1269 | 845 | } |
mcimadamore@1269 | 846 | |
duke@1 | 847 | public void visitClassDef(JCClassDecl tree) { |
duke@1 | 848 | // Local classes have not been entered yet, so we need to do it now: |
duke@1 | 849 | if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) |
duke@1 | 850 | enter.classEnter(tree, env); |
duke@1 | 851 | |
duke@1 | 852 | ClassSymbol c = tree.sym; |
duke@1 | 853 | if (c == null) { |
duke@1 | 854 | // exit in case something drastic went wrong during enter. |
duke@1 | 855 | result = null; |
duke@1 | 856 | } else { |
duke@1 | 857 | // make sure class has been completed: |
duke@1 | 858 | c.complete(); |
duke@1 | 859 | |
duke@1 | 860 | // If this class appears as an anonymous class |
duke@1 | 861 | // in a superclass constructor call where |
duke@1 | 862 | // no explicit outer instance is given, |
duke@1 | 863 | // disable implicit outer instance from being passed. |
duke@1 | 864 | // (This would be an illegal access to "this before super"). |
duke@1 | 865 | if (env.info.isSelfCall && |
jjg@1127 | 866 | env.tree.hasTag(NEWCLASS) && |
duke@1 | 867 | ((JCNewClass) env.tree).encl == null) |
duke@1 | 868 | { |
duke@1 | 869 | c.flags_field |= NOOUTERTHIS; |
duke@1 | 870 | } |
duke@1 | 871 | attribClass(tree.pos(), c); |
duke@1 | 872 | result = tree.type = c.type; |
duke@1 | 873 | } |
duke@1 | 874 | } |
duke@1 | 875 | |
duke@1 | 876 | public void visitMethodDef(JCMethodDecl tree) { |
duke@1 | 877 | MethodSymbol m = tree.sym; |
duke@1 | 878 | |
jfranck@1313 | 879 | Lint lint = env.info.lint.augment(m.annotations, m.flags()); |
duke@1 | 880 | Lint prevLint = chk.setLint(lint); |
mcimadamore@795 | 881 | MethodSymbol prevMethod = chk.setMethod(m); |
duke@1 | 882 | try { |
mcimadamore@852 | 883 | deferredLintHandler.flush(tree.pos()); |
duke@1 | 884 | chk.checkDeprecatedAnnotation(tree.pos(), m); |
duke@1 | 885 | |
mcimadamore@42 | 886 | attribBounds(tree.typarams); |
duke@1 | 887 | |
duke@1 | 888 | // If we override any other methods, check that we do so properly. |
duke@1 | 889 | // JLS ??? |
mcimadamore@858 | 890 | if (m.isStatic()) { |
mcimadamore@858 | 891 | chk.checkHideClashes(tree.pos(), env.enclClass.type, m); |
mcimadamore@858 | 892 | } else { |
mcimadamore@858 | 893 | chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m); |
mcimadamore@858 | 894 | } |
duke@1 | 895 | chk.checkOverride(tree, m); |
duke@1 | 896 | |
duke@1 | 897 | // Create a new environment with local scope |
duke@1 | 898 | // for attributing the method. |
duke@1 | 899 | Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env); |
duke@1 | 900 | |
duke@1 | 901 | localEnv.info.lint = lint; |
duke@1 | 902 | |
duke@1 | 903 | // Enter all type parameters into the local method scope. |
duke@1 | 904 | for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
duke@1 | 905 | localEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
duke@1 | 906 | |
duke@1 | 907 | ClassSymbol owner = env.enclClass.sym; |
duke@1 | 908 | if ((owner.flags() & ANNOTATION) != 0 && |
duke@1 | 909 | tree.params.nonEmpty()) |
duke@1 | 910 | log.error(tree.params.head.pos(), |
duke@1 | 911 | "intf.annotation.members.cant.have.params"); |
duke@1 | 912 | |
duke@1 | 913 | // Attribute all value parameters. |
duke@1 | 914 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
duke@1 | 915 | attribStat(l.head, localEnv); |
duke@1 | 916 | } |
duke@1 | 917 | |
mcimadamore@795 | 918 | chk.checkVarargsMethodDecl(localEnv, tree); |
mcimadamore@580 | 919 | |
duke@1 | 920 | // Check that type parameters are well-formed. |
mcimadamore@122 | 921 | chk.validate(tree.typarams, localEnv); |
duke@1 | 922 | |
duke@1 | 923 | // Check that result type is well-formed. |
mcimadamore@122 | 924 | chk.validate(tree.restype, localEnv); |
mcimadamore@629 | 925 | |
mcimadamore@629 | 926 | // annotation method checks |
mcimadamore@629 | 927 | if ((owner.flags() & ANNOTATION) != 0) { |
mcimadamore@629 | 928 | // annotation method cannot have throws clause |
mcimadamore@629 | 929 | if (tree.thrown.nonEmpty()) { |
mcimadamore@629 | 930 | log.error(tree.thrown.head.pos(), |
mcimadamore@629 | 931 | "throws.not.allowed.in.intf.annotation"); |
mcimadamore@629 | 932 | } |
mcimadamore@629 | 933 | // annotation method cannot declare type-parameters |
mcimadamore@629 | 934 | if (tree.typarams.nonEmpty()) { |
mcimadamore@629 | 935 | log.error(tree.typarams.head.pos(), |
mcimadamore@629 | 936 | "intf.annotation.members.cant.have.type.params"); |
mcimadamore@629 | 937 | } |
mcimadamore@629 | 938 | // validate annotation method's return type (could be an annotation type) |
duke@1 | 939 | chk.validateAnnotationType(tree.restype); |
mcimadamore@629 | 940 | // ensure that annotation method does not clash with members of Object/Annotation |
duke@1 | 941 | chk.validateAnnotationMethod(tree.pos(), m); |
duke@1 | 942 | |
mcimadamore@634 | 943 | if (tree.defaultValue != null) { |
mcimadamore@634 | 944 | // if default value is an annotation, check it is a well-formed |
mcimadamore@634 | 945 | // annotation value (e.g. no duplicate values, no missing values, etc.) |
mcimadamore@634 | 946 | chk.validateAnnotationTree(tree.defaultValue); |
mcimadamore@634 | 947 | } |
mcimadamore@629 | 948 | } |
mcimadamore@629 | 949 | |
duke@1 | 950 | for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) |
duke@1 | 951 | chk.checkType(l.head.pos(), l.head.type, syms.throwableType); |
duke@1 | 952 | |
duke@1 | 953 | if (tree.body == null) { |
duke@1 | 954 | // Empty bodies are only allowed for |
duke@1 | 955 | // abstract, native, or interface methods, or for methods |
duke@1 | 956 | // in a retrofit signature class. |
duke@1 | 957 | if ((owner.flags() & INTERFACE) == 0 && |
duke@1 | 958 | (tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && |
duke@1 | 959 | !relax) |
duke@1 | 960 | log.error(tree.pos(), "missing.meth.body.or.decl.abstract"); |
duke@1 | 961 | if (tree.defaultValue != null) { |
duke@1 | 962 | if ((owner.flags() & ANNOTATION) == 0) |
duke@1 | 963 | log.error(tree.pos(), |
duke@1 | 964 | "default.allowed.in.intf.annotation.member"); |
duke@1 | 965 | } |
duke@1 | 966 | } else if ((owner.flags() & INTERFACE) != 0) { |
duke@1 | 967 | log.error(tree.body.pos(), "intf.meth.cant.have.body"); |
duke@1 | 968 | } else if ((tree.mods.flags & ABSTRACT) != 0) { |
duke@1 | 969 | log.error(tree.pos(), "abstract.meth.cant.have.body"); |
duke@1 | 970 | } else if ((tree.mods.flags & NATIVE) != 0) { |
duke@1 | 971 | log.error(tree.pos(), "native.meth.cant.have.body"); |
duke@1 | 972 | } else { |
duke@1 | 973 | // Add an implicit super() call unless an explicit call to |
duke@1 | 974 | // super(...) or this(...) is given |
duke@1 | 975 | // or we are compiling class java.lang.Object. |
duke@1 | 976 | if (tree.name == names.init && owner.type != syms.objectType) { |
duke@1 | 977 | JCBlock body = tree.body; |
duke@1 | 978 | if (body.stats.isEmpty() || |
duke@1 | 979 | !TreeInfo.isSelfCall(body.stats.head)) { |
duke@1 | 980 | body.stats = body.stats. |
duke@1 | 981 | prepend(memberEnter.SuperCall(make.at(body.pos), |
duke@1 | 982 | List.<Type>nil(), |
duke@1 | 983 | List.<JCVariableDecl>nil(), |
duke@1 | 984 | false)); |
duke@1 | 985 | } else if ((env.enclClass.sym.flags() & ENUM) != 0 && |
duke@1 | 986 | (tree.mods.flags & GENERATEDCONSTR) == 0 && |
duke@1 | 987 | TreeInfo.isSuperCall(body.stats.head)) { |
duke@1 | 988 | // enum constructors are not allowed to call super |
duke@1 | 989 | // directly, so make sure there aren't any super calls |
duke@1 | 990 | // in enum constructors, except in the compiler |
duke@1 | 991 | // generated one. |
duke@1 | 992 | log.error(tree.body.stats.head.pos(), |
duke@1 | 993 | "call.to.super.not.allowed.in.enum.ctor", |
duke@1 | 994 | env.enclClass.sym); |
duke@1 | 995 | } |
duke@1 | 996 | } |
duke@1 | 997 | |
duke@1 | 998 | // Attribute method body. |
duke@1 | 999 | attribStat(tree.body, localEnv); |
duke@1 | 1000 | } |
duke@1 | 1001 | localEnv.info.scope.leave(); |
duke@1 | 1002 | result = tree.type = m.type; |
duke@1 | 1003 | chk.validateAnnotations(tree.mods.annotations, m); |
duke@1 | 1004 | } |
duke@1 | 1005 | finally { |
duke@1 | 1006 | chk.setLint(prevLint); |
mcimadamore@795 | 1007 | chk.setMethod(prevMethod); |
duke@1 | 1008 | } |
duke@1 | 1009 | } |
duke@1 | 1010 | |
duke@1 | 1011 | public void visitVarDef(JCVariableDecl tree) { |
duke@1 | 1012 | // Local variables have not been entered yet, so we need to do it now: |
duke@1 | 1013 | if (env.info.scope.owner.kind == MTH) { |
duke@1 | 1014 | if (tree.sym != null) { |
duke@1 | 1015 | // parameters have already been entered |
duke@1 | 1016 | env.info.scope.enter(tree.sym); |
duke@1 | 1017 | } else { |
duke@1 | 1018 | memberEnter.memberEnter(tree, env); |
duke@1 | 1019 | annotate.flush(); |
duke@1 | 1020 | } |
duke@1 | 1021 | } |
duke@1 | 1022 | |
duke@1 | 1023 | VarSymbol v = tree.sym; |
jfranck@1313 | 1024 | Lint lint = env.info.lint.augment(v.annotations, v.flags()); |
duke@1 | 1025 | Lint prevLint = chk.setLint(lint); |
duke@1 | 1026 | |
mcimadamore@165 | 1027 | // Check that the variable's declared type is well-formed. |
mcimadamore@165 | 1028 | chk.validate(tree.vartype, env); |
mcimadamore@852 | 1029 | deferredLintHandler.flush(tree.pos()); |
mcimadamore@165 | 1030 | |
duke@1 | 1031 | try { |
duke@1 | 1032 | chk.checkDeprecatedAnnotation(tree.pos(), v); |
duke@1 | 1033 | |
duke@1 | 1034 | if (tree.init != null) { |
mcimadamore@1348 | 1035 | if ((v.flags_field & FINAL) != 0 && |
mcimadamore@1348 | 1036 | !tree.init.hasTag(NEWCLASS) && |
mcimadamore@1348 | 1037 | !tree.init.hasTag(LAMBDA)) { |
duke@1 | 1038 | // In this case, `v' is final. Ensure that it's initializer is |
duke@1 | 1039 | // evaluated. |
duke@1 | 1040 | v.getConstValue(); // ensure initializer is evaluated |
duke@1 | 1041 | } else { |
duke@1 | 1042 | // Attribute initializer in a new environment |
duke@1 | 1043 | // with the declared variable as owner. |
duke@1 | 1044 | // Check that initializer conforms to variable's declared type. |
duke@1 | 1045 | Env<AttrContext> initEnv = memberEnter.initEnv(tree, env); |
duke@1 | 1046 | initEnv.info.lint = lint; |
duke@1 | 1047 | // In order to catch self-references, we set the variable's |
duke@1 | 1048 | // declaration position to maximal possible value, effectively |
duke@1 | 1049 | // marking the variable as undefined. |
mcimadamore@94 | 1050 | initEnv.info.enclVar = v; |
duke@1 | 1051 | attribExpr(tree.init, initEnv, v.type); |
duke@1 | 1052 | } |
duke@1 | 1053 | } |
duke@1 | 1054 | result = tree.type = v.type; |
duke@1 | 1055 | chk.validateAnnotations(tree.mods.annotations, v); |
duke@1 | 1056 | } |
duke@1 | 1057 | finally { |
duke@1 | 1058 | chk.setLint(prevLint); |
duke@1 | 1059 | } |
duke@1 | 1060 | } |
duke@1 | 1061 | |
duke@1 | 1062 | public void visitSkip(JCSkip tree) { |
duke@1 | 1063 | result = null; |
duke@1 | 1064 | } |
duke@1 | 1065 | |
duke@1 | 1066 | public void visitBlock(JCBlock tree) { |
duke@1 | 1067 | if (env.info.scope.owner.kind == TYP) { |
duke@1 | 1068 | // Block is a static or instance initializer; |
duke@1 | 1069 | // let the owner of the environment be a freshly |
duke@1 | 1070 | // created BLOCK-method. |
duke@1 | 1071 | Env<AttrContext> localEnv = |
duke@1 | 1072 | env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
duke@1 | 1073 | localEnv.info.scope.owner = |
duke@1 | 1074 | new MethodSymbol(tree.flags | BLOCK, names.empty, null, |
duke@1 | 1075 | env.info.scope.owner); |
duke@1 | 1076 | if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++; |
duke@1 | 1077 | attribStats(tree.stats, localEnv); |
duke@1 | 1078 | } else { |
duke@1 | 1079 | // Create a new local environment with a local scope. |
duke@1 | 1080 | Env<AttrContext> localEnv = |
duke@1 | 1081 | env.dup(tree, env.info.dup(env.info.scope.dup())); |
mcimadamore@1347 | 1082 | try { |
mcimadamore@1347 | 1083 | attribStats(tree.stats, localEnv); |
mcimadamore@1347 | 1084 | } finally { |
mcimadamore@1347 | 1085 | localEnv.info.scope.leave(); |
mcimadamore@1347 | 1086 | } |
duke@1 | 1087 | } |
duke@1 | 1088 | result = null; |
duke@1 | 1089 | } |
duke@1 | 1090 | |
duke@1 | 1091 | public void visitDoLoop(JCDoWhileLoop tree) { |
duke@1 | 1092 | attribStat(tree.body, env.dup(tree)); |
duke@1 | 1093 | attribExpr(tree.cond, env, syms.booleanType); |
duke@1 | 1094 | result = null; |
duke@1 | 1095 | } |
duke@1 | 1096 | |
duke@1 | 1097 | public void visitWhileLoop(JCWhileLoop tree) { |
duke@1 | 1098 | attribExpr(tree.cond, env, syms.booleanType); |
duke@1 | 1099 | attribStat(tree.body, env.dup(tree)); |
duke@1 | 1100 | result = null; |
duke@1 | 1101 | } |
duke@1 | 1102 | |
duke@1 | 1103 | public void visitForLoop(JCForLoop tree) { |
duke@1 | 1104 | Env<AttrContext> loopEnv = |
duke@1 | 1105 | env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
mcimadamore@1347 | 1106 | try { |
mcimadamore@1347 | 1107 | attribStats(tree.init, loopEnv); |
mcimadamore@1347 | 1108 | if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType); |
mcimadamore@1347 | 1109 | loopEnv.tree = tree; // before, we were not in loop! |
mcimadamore@1347 | 1110 | attribStats(tree.step, loopEnv); |
mcimadamore@1347 | 1111 | attribStat(tree.body, loopEnv); |
mcimadamore@1347 | 1112 | result = null; |
mcimadamore@1347 | 1113 | } |
mcimadamore@1347 | 1114 | finally { |
mcimadamore@1347 | 1115 | loopEnv.info.scope.leave(); |
mcimadamore@1347 | 1116 | } |
duke@1 | 1117 | } |
duke@1 | 1118 | |
duke@1 | 1119 | public void visitForeachLoop(JCEnhancedForLoop tree) { |
duke@1 | 1120 | Env<AttrContext> loopEnv = |
duke@1 | 1121 | env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
mcimadamore@1347 | 1122 | try { |
mcimadamore@1347 | 1123 | attribStat(tree.var, loopEnv); |
mcimadamore@1347 | 1124 | Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv)); |
mcimadamore@1347 | 1125 | chk.checkNonVoid(tree.pos(), exprType); |
mcimadamore@1347 | 1126 | Type elemtype = types.elemtype(exprType); // perhaps expr is an array? |
mcimadamore@1347 | 1127 | if (elemtype == null) { |
mcimadamore@1347 | 1128 | // or perhaps expr implements Iterable<T>? |
mcimadamore@1347 | 1129 | Type base = types.asSuper(exprType, syms.iterableType.tsym); |
mcimadamore@1347 | 1130 | if (base == null) { |
mcimadamore@1347 | 1131 | log.error(tree.expr.pos(), |
mcimadamore@1347 | 1132 | "foreach.not.applicable.to.type", |
mcimadamore@1347 | 1133 | exprType, |
mcimadamore@1347 | 1134 | diags.fragment("type.req.array.or.iterable")); |
mcimadamore@1347 | 1135 | elemtype = types.createErrorType(exprType); |
mcimadamore@1347 | 1136 | } else { |
mcimadamore@1347 | 1137 | List<Type> iterableParams = base.allparams(); |
mcimadamore@1347 | 1138 | elemtype = iterableParams.isEmpty() |
mcimadamore@1347 | 1139 | ? syms.objectType |
mcimadamore@1347 | 1140 | : types.upperBound(iterableParams.head); |
mcimadamore@1347 | 1141 | } |
duke@1 | 1142 | } |
mcimadamore@1347 | 1143 | chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type); |
mcimadamore@1347 | 1144 | loopEnv.tree = tree; // before, we were not in loop! |
mcimadamore@1347 | 1145 | attribStat(tree.body, loopEnv); |
mcimadamore@1347 | 1146 | result = null; |
duke@1 | 1147 | } |
mcimadamore@1347 | 1148 | finally { |
mcimadamore@1347 | 1149 | loopEnv.info.scope.leave(); |
mcimadamore@1347 | 1150 | } |
duke@1 | 1151 | } |
duke@1 | 1152 | |
duke@1 | 1153 | public void visitLabelled(JCLabeledStatement tree) { |
duke@1 | 1154 | // Check that label is not used in an enclosing statement |
duke@1 | 1155 | Env<AttrContext> env1 = env; |
jjg@1127 | 1156 | while (env1 != null && !env1.tree.hasTag(CLASSDEF)) { |
jjg@1127 | 1157 | if (env1.tree.hasTag(LABELLED) && |
duke@1 | 1158 | ((JCLabeledStatement) env1.tree).label == tree.label) { |
duke@1 | 1159 | log.error(tree.pos(), "label.already.in.use", |
duke@1 | 1160 | tree.label); |
duke@1 | 1161 | break; |
duke@1 | 1162 | } |
duke@1 | 1163 | env1 = env1.next; |
duke@1 | 1164 | } |
duke@1 | 1165 | |
duke@1 | 1166 | attribStat(tree.body, env.dup(tree)); |
duke@1 | 1167 | result = null; |
duke@1 | 1168 | } |
duke@1 | 1169 | |
duke@1 | 1170 | public void visitSwitch(JCSwitch tree) { |
duke@1 | 1171 | Type seltype = attribExpr(tree.selector, env); |
duke@1 | 1172 | |
duke@1 | 1173 | Env<AttrContext> switchEnv = |
duke@1 | 1174 | env.dup(tree, env.info.dup(env.info.scope.dup())); |
duke@1 | 1175 | |
mcimadamore@1347 | 1176 | try { |
mcimadamore@1347 | 1177 | |
mcimadamore@1347 | 1178 | boolean enumSwitch = |
mcimadamore@1347 | 1179 | allowEnums && |
mcimadamore@1347 | 1180 | (seltype.tsym.flags() & Flags.ENUM) != 0; |
mcimadamore@1347 | 1181 | boolean stringSwitch = false; |
mcimadamore@1347 | 1182 | if (types.isSameType(seltype, syms.stringType)) { |
mcimadamore@1347 | 1183 | if (allowStringsInSwitch) { |
mcimadamore@1347 | 1184 | stringSwitch = true; |
mcimadamore@1347 | 1185 | } else { |
mcimadamore@1347 | 1186 | log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName); |
mcimadamore@1347 | 1187 | } |
darcy@430 | 1188 | } |
mcimadamore@1347 | 1189 | if (!enumSwitch && !stringSwitch) |
mcimadamore@1347 | 1190 | seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); |
mcimadamore@1347 | 1191 | |
mcimadamore@1347 | 1192 | // Attribute all cases and |
mcimadamore@1347 | 1193 | // check that there are no duplicate case labels or default clauses. |
mcimadamore@1347 | 1194 | Set<Object> labels = new HashSet<Object>(); // The set of case labels. |
mcimadamore@1347 | 1195 | boolean hasDefault = false; // Is there a default label? |
mcimadamore@1347 | 1196 | for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { |
mcimadamore@1347 | 1197 | JCCase c = l.head; |
mcimadamore@1347 | 1198 | Env<AttrContext> caseEnv = |
mcimadamore@1347 | 1199 | switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup())); |
mcimadamore@1347 | 1200 | try { |
mcimadamore@1347 | 1201 | if (c.pat != null) { |
mcimadamore@1347 | 1202 | if (enumSwitch) { |
mcimadamore@1347 | 1203 | Symbol sym = enumConstant(c.pat, seltype); |
mcimadamore@1347 | 1204 | if (sym == null) { |
mcimadamore@1347 | 1205 | log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum"); |
mcimadamore@1347 | 1206 | } else if (!labels.add(sym)) { |
mcimadamore@1347 | 1207 | log.error(c.pos(), "duplicate.case.label"); |
mcimadamore@1347 | 1208 | } |
mcimadamore@1347 | 1209 | } else { |
mcimadamore@1347 | 1210 | Type pattype = attribExpr(c.pat, switchEnv, seltype); |
mcimadamore@1347 | 1211 | if (pattype.tag != ERROR) { |
mcimadamore@1347 | 1212 | if (pattype.constValue() == null) { |
mcimadamore@1347 | 1213 | log.error(c.pat.pos(), |
mcimadamore@1347 | 1214 | (stringSwitch ? "string.const.req" : "const.expr.req")); |
mcimadamore@1347 | 1215 | } else if (labels.contains(pattype.constValue())) { |
mcimadamore@1347 | 1216 | log.error(c.pos(), "duplicate.case.label"); |
mcimadamore@1347 | 1217 | } else { |
mcimadamore@1347 | 1218 | labels.add(pattype.constValue()); |
mcimadamore@1347 | 1219 | } |
mcimadamore@1347 | 1220 | } |
mcimadamore@1347 | 1221 | } |
mcimadamore@1347 | 1222 | } else if (hasDefault) { |
mcimadamore@1347 | 1223 | log.error(c.pos(), "duplicate.default.label"); |
mcimadamore@1347 | 1224 | } else { |
mcimadamore@1347 | 1225 | hasDefault = true; |
mcimadamore@1347 | 1226 | } |
mcimadamore@1347 | 1227 | attribStats(c.stats, caseEnv); |
mcimadamore@1347 | 1228 | } finally { |
mcimadamore@1347 | 1229 | caseEnv.info.scope.leave(); |
mcimadamore@1347 | 1230 | addVars(c.stats, switchEnv.info.scope); |
mcimadamore@1347 | 1231 | } |
mcimadamore@1347 | 1232 | } |
mcimadamore@1347 | 1233 | |
mcimadamore@1347 | 1234 | result = null; |
darcy@430 | 1235 | } |
mcimadamore@1347 | 1236 | finally { |
mcimadamore@1347 | 1237 | switchEnv.info.scope.leave(); |
duke@1 | 1238 | } |
duke@1 | 1239 | } |
duke@1 | 1240 | // where |
duke@1 | 1241 | /** Add any variables defined in stats to the switch scope. */ |
duke@1 | 1242 | private static void addVars(List<JCStatement> stats, Scope switchScope) { |
duke@1 | 1243 | for (;stats.nonEmpty(); stats = stats.tail) { |
duke@1 | 1244 | JCTree stat = stats.head; |
jjg@1127 | 1245 | if (stat.hasTag(VARDEF)) |
duke@1 | 1246 | switchScope.enter(((JCVariableDecl) stat).sym); |
duke@1 | 1247 | } |
duke@1 | 1248 | } |
duke@1 | 1249 | // where |
duke@1 | 1250 | /** Return the selected enumeration constant symbol, or null. */ |
duke@1 | 1251 | private Symbol enumConstant(JCTree tree, Type enumType) { |
jjg@1127 | 1252 | if (!tree.hasTag(IDENT)) { |
duke@1 | 1253 | log.error(tree.pos(), "enum.label.must.be.unqualified.enum"); |
duke@1 | 1254 | return syms.errSymbol; |
duke@1 | 1255 | } |
duke@1 | 1256 | JCIdent ident = (JCIdent)tree; |
duke@1 | 1257 | Name name = ident.name; |
duke@1 | 1258 | for (Scope.Entry e = enumType.tsym.members().lookup(name); |
duke@1 | 1259 | e.scope != null; e = e.next()) { |
duke@1 | 1260 | if (e.sym.kind == VAR) { |
duke@1 | 1261 | Symbol s = ident.sym = e.sym; |
duke@1 | 1262 | ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated |
duke@1 | 1263 | ident.type = s.type; |
duke@1 | 1264 | return ((s.flags_field & Flags.ENUM) == 0) |
duke@1 | 1265 | ? null : s; |
duke@1 | 1266 | } |
duke@1 | 1267 | } |
duke@1 | 1268 | return null; |
duke@1 | 1269 | } |
duke@1 | 1270 | |
duke@1 | 1271 | public void visitSynchronized(JCSynchronized tree) { |
duke@1 | 1272 | chk.checkRefType(tree.pos(), attribExpr(tree.lock, env)); |
duke@1 | 1273 | attribStat(tree.body, env); |
duke@1 | 1274 | result = null; |
duke@1 | 1275 | } |
duke@1 | 1276 | |
duke@1 | 1277 | public void visitTry(JCTry tree) { |
darcy@609 | 1278 | // Create a new local environment with a local |
darcy@609 | 1279 | Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup())); |
mcimadamore@1347 | 1280 | try { |
mcimadamore@1347 | 1281 | boolean isTryWithResource = tree.resources.nonEmpty(); |
mcimadamore@1347 | 1282 | // Create a nested environment for attributing the try block if needed |
mcimadamore@1347 | 1283 | Env<AttrContext> tryEnv = isTryWithResource ? |
mcimadamore@1347 | 1284 | env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) : |
mcimadamore@1347 | 1285 | localEnv; |
mcimadamore@1347 | 1286 | try { |
mcimadamore@1347 | 1287 | // Attribute resource declarations |
mcimadamore@1347 | 1288 | for (JCTree resource : tree.resources) { |
mcimadamore@1347 | 1289 | CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) { |
mcimadamore@1347 | 1290 | @Override |
mcimadamore@1347 | 1291 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
mcimadamore@1347 | 1292 | chk.basicHandler.report(pos, diags.fragment("try.not.applicable.to.type", details)); |
mcimadamore@1347 | 1293 | } |
mcimadamore@1347 | 1294 | }; |
mcimadamore@1347 | 1295 | ResultInfo twrResult = new ResultInfo(VAL, syms.autoCloseableType, twrContext); |
mcimadamore@1347 | 1296 | if (resource.hasTag(VARDEF)) { |
mcimadamore@1347 | 1297 | attribStat(resource, tryEnv); |
mcimadamore@1347 | 1298 | twrResult.check(resource, resource.type); |
mcimadamore@1347 | 1299 | |
mcimadamore@1347 | 1300 | //check that resource type cannot throw InterruptedException |
mcimadamore@1347 | 1301 | checkAutoCloseable(resource.pos(), localEnv, resource.type); |
mcimadamore@1347 | 1302 | |
mcimadamore@1347 | 1303 | VarSymbol var = (VarSymbol)TreeInfo.symbolFor(resource); |
mcimadamore@1347 | 1304 | var.setData(ElementKind.RESOURCE_VARIABLE); |
mcimadamore@1347 | 1305 | } else { |
mcimadamore@1347 | 1306 | attribTree(resource, tryEnv, twrResult); |
mcimadamore@1347 | 1307 | } |
mcimadamore@1238 | 1308 | } |
mcimadamore@1347 | 1309 | // Attribute body |
mcimadamore@1347 | 1310 | attribStat(tree.body, tryEnv); |
mcimadamore@1347 | 1311 | } finally { |
mcimadamore@1347 | 1312 | if (isTryWithResource) |
mcimadamore@1347 | 1313 | tryEnv.info.scope.leave(); |
darcy@609 | 1314 | } |
mcimadamore@1347 | 1315 | |
mcimadamore@1347 | 1316 | // Attribute catch clauses |
mcimadamore@1347 | 1317 | for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { |
mcimadamore@1347 | 1318 | JCCatch c = l.head; |
mcimadamore@1347 | 1319 | Env<AttrContext> catchEnv = |
mcimadamore@1347 | 1320 | localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup())); |
mcimadamore@1347 | 1321 | try { |
mcimadamore@1347 | 1322 | Type ctype = attribStat(c.param, catchEnv); |
mcimadamore@1347 | 1323 | if (TreeInfo.isMultiCatch(c)) { |
mcimadamore@1347 | 1324 | //multi-catch parameter is implicitly marked as final |
mcimadamore@1347 | 1325 | c.param.sym.flags_field |= FINAL | UNION; |
mcimadamore@1347 | 1326 | } |
mcimadamore@1347 | 1327 | if (c.param.sym.kind == Kinds.VAR) { |
mcimadamore@1347 | 1328 | c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER); |
mcimadamore@1347 | 1329 | } |
mcimadamore@1347 | 1330 | chk.checkType(c.param.vartype.pos(), |
mcimadamore@1347 | 1331 | chk.checkClassType(c.param.vartype.pos(), ctype), |
mcimadamore@1347 | 1332 | syms.throwableType); |
mcimadamore@1347 | 1333 | attribStat(c.body, catchEnv); |
mcimadamore@1347 | 1334 | } finally { |
mcimadamore@1347 | 1335 | catchEnv.info.scope.leave(); |
mcimadamore@1347 | 1336 | } |
mcimadamore@1347 | 1337 | } |
mcimadamore@1347 | 1338 | |
mcimadamore@1347 | 1339 | // Attribute finalizer |
mcimadamore@1347 | 1340 | if (tree.finalizer != null) attribStat(tree.finalizer, localEnv); |
mcimadamore@1347 | 1341 | result = null; |
darcy@609 | 1342 | } |
mcimadamore@1347 | 1343 | finally { |
mcimadamore@1347 | 1344 | localEnv.info.scope.leave(); |
duke@1 | 1345 | } |
duke@1 | 1346 | } |
duke@1 | 1347 | |
mcimadamore@951 | 1348 | void checkAutoCloseable(DiagnosticPosition pos, Env<AttrContext> env, Type resource) { |
mcimadamore@951 | 1349 | if (!resource.isErroneous() && |
darcy@1207 | 1350 | types.asSuper(resource, syms.autoCloseableType.tsym) != null && |
darcy@1207 | 1351 | !types.isSameType(resource, syms.autoCloseableType)) { // Don't emit warning for AutoCloseable itself |
mcimadamore@951 | 1352 | Symbol close = syms.noSymbol; |
mcimadamore@1347 | 1353 | Filter<JCDiagnostic> prevDeferDiagsFilter = log.deferredDiagFilter; |
mcimadamore@951 | 1354 | Queue<JCDiagnostic> prevDeferredDiags = log.deferredDiagnostics; |
mcimadamore@951 | 1355 | try { |
mcimadamore@1347 | 1356 | log.deferAll(); |
mcimadamore@951 | 1357 | log.deferredDiagnostics = ListBuffer.lb(); |
mcimadamore@951 | 1358 | close = rs.resolveQualifiedMethod(pos, |
mcimadamore@951 | 1359 | env, |
mcimadamore@951 | 1360 | resource, |
mcimadamore@951 | 1361 | names.close, |
mcimadamore@951 | 1362 | List.<Type>nil(), |
mcimadamore@951 | 1363 | List.<Type>nil()); |
mcimadamore@951 | 1364 | } |
mcimadamore@951 | 1365 | finally { |
mcimadamore@1347 | 1366 | log.deferredDiagFilter = prevDeferDiagsFilter; |
mcimadamore@951 | 1367 | log.deferredDiagnostics = prevDeferredDiags; |
mcimadamore@951 | 1368 | } |
mcimadamore@951 | 1369 | if (close.kind == MTH && |
mcimadamore@951 | 1370 | close.overrides(syms.autoCloseableClose, resource.tsym, types, true) && |
mcimadamore@951 | 1371 | chk.isHandled(syms.interruptedExceptionType, types.memberType(resource, close).getThrownTypes()) && |
mcimadamore@951 | 1372 | env.info.lint.isEnabled(LintCategory.TRY)) { |
mcimadamore@951 | 1373 | log.warning(LintCategory.TRY, pos, "try.resource.throws.interrupted.exc", resource); |
mcimadamore@951 | 1374 | } |
mcimadamore@951 | 1375 | } |
mcimadamore@951 | 1376 | } |
mcimadamore@951 | 1377 | |
duke@1 | 1378 | public void visitConditional(JCConditional tree) { |
mcimadamore@1347 | 1379 | Type condtype = attribExpr(tree.cond, env, syms.booleanType); |
mcimadamore@1347 | 1380 | |
mcimadamore@1347 | 1381 | boolean standaloneConditional = !allowPoly || |
mcimadamore@1347 | 1382 | pt().tag == NONE && pt() != Type.recoveryType || |
mcimadamore@1347 | 1383 | isBooleanOrNumeric(env, tree); |
mcimadamore@1347 | 1384 | |
mcimadamore@1347 | 1385 | if (!standaloneConditional && resultInfo.pt.tag == VOID) { |
mcimadamore@1347 | 1386 | //cannot get here (i.e. it means we are returning from void method - which is already an error) |
mcimadamore@1347 | 1387 | result = tree.type = types.createErrorType(resultInfo.pt); |
mcimadamore@1347 | 1388 | return; |
mcimadamore@1347 | 1389 | } |
mcimadamore@1347 | 1390 | |
mcimadamore@1347 | 1391 | ResultInfo condInfo = standaloneConditional ? |
mcimadamore@1347 | 1392 | unknownExprInfo : |
mcimadamore@1347 | 1393 | new ResultInfo(VAL, pt(), new Check.NestedCheckContext(resultInfo.checkContext) { |
mcimadamore@1347 | 1394 | //this will use enclosing check context to check compatibility of |
mcimadamore@1347 | 1395 | //subexpression against target type; if we are in a method check context, |
mcimadamore@1347 | 1396 | //depending on whether boxing is allowed, we could have incompatibilities |
mcimadamore@1347 | 1397 | @Override |
mcimadamore@1347 | 1398 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
mcimadamore@1347 | 1399 | enclosingContext.report(pos, diags.fragment("incompatible.type.in.conditional", details)); |
mcimadamore@1347 | 1400 | } |
mcimadamore@1347 | 1401 | }); |
mcimadamore@1347 | 1402 | |
mcimadamore@1347 | 1403 | Type truetype = attribTree(tree.truepart, env, condInfo); |
mcimadamore@1347 | 1404 | Type falsetype = attribTree(tree.falsepart, env, condInfo); |
mcimadamore@1347 | 1405 | |
mcimadamore@1347 | 1406 | Type owntype = standaloneConditional ? condType(tree, truetype, falsetype) : pt(); |
mcimadamore@1347 | 1407 | if (condtype.constValue() != null && |
mcimadamore@1347 | 1408 | truetype.constValue() != null && |
mcimadamore@1347 | 1409 | falsetype.constValue() != null) { |
mcimadamore@1347 | 1410 | //constant folding |
mcimadamore@1347 | 1411 | owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype); |
mcimadamore@1347 | 1412 | } |
mcimadamore@1347 | 1413 | result = check(tree, owntype, VAL, resultInfo); |
duke@1 | 1414 | } |
duke@1 | 1415 | //where |
mcimadamore@1347 | 1416 | @SuppressWarnings("fallthrough") |
mcimadamore@1347 | 1417 | private boolean isBooleanOrNumeric(Env<AttrContext> env, JCExpression tree) { |
mcimadamore@1347 | 1418 | switch (tree.getTag()) { |
mcimadamore@1347 | 1419 | case LITERAL: return ((JCLiteral)tree).typetag < CLASS; |
mcimadamore@1347 | 1420 | case LAMBDA: case REFERENCE: return false; |
mcimadamore@1347 | 1421 | case PARENS: return isBooleanOrNumeric(env, ((JCParens)tree).expr); |
mcimadamore@1347 | 1422 | case CONDEXPR: |
mcimadamore@1347 | 1423 | JCConditional condTree = (JCConditional)tree; |
mcimadamore@1347 | 1424 | return isBooleanOrNumeric(env, condTree.truepart) && |
mcimadamore@1347 | 1425 | isBooleanOrNumeric(env, condTree.falsepart); |
mcimadamore@1347 | 1426 | default: |
mcimadamore@1347 | 1427 | Type speculativeType = deferredAttr.attribSpeculative(tree, env, unknownExprInfo).type; |
mcimadamore@1347 | 1428 | speculativeType = types.unboxedTypeOrType(speculativeType); |
mcimadamore@1347 | 1429 | return speculativeType.tag <= BOOLEAN; |
mcimadamore@1347 | 1430 | } |
mcimadamore@1347 | 1431 | } |
mcimadamore@1347 | 1432 | |
duke@1 | 1433 | /** Compute the type of a conditional expression, after |
mcimadamore@1347 | 1434 | * checking that it exists. See JLS 15.25. Does not take into |
duke@1 | 1435 | * account the special case where condition and both arms |
duke@1 | 1436 | * are constants. |
duke@1 | 1437 | * |
duke@1 | 1438 | * @param pos The source position to be used for error |
duke@1 | 1439 | * diagnostics. |
duke@1 | 1440 | * @param thentype The type of the expression's then-part. |
duke@1 | 1441 | * @param elsetype The type of the expression's else-part. |
duke@1 | 1442 | */ |
mcimadamore@1347 | 1443 | private Type condType(DiagnosticPosition pos, |
duke@1 | 1444 | Type thentype, Type elsetype) { |
duke@1 | 1445 | // If same type, that is the result |
duke@1 | 1446 | if (types.isSameType(thentype, elsetype)) |
duke@1 | 1447 | return thentype.baseType(); |
duke@1 | 1448 | |
duke@1 | 1449 | Type thenUnboxed = (!allowBoxing || thentype.isPrimitive()) |
duke@1 | 1450 | ? thentype : types.unboxedType(thentype); |
duke@1 | 1451 | Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive()) |
duke@1 | 1452 | ? elsetype : types.unboxedType(elsetype); |
duke@1 | 1453 | |
duke@1 | 1454 | // Otherwise, if both arms can be converted to a numeric |
duke@1 | 1455 | // type, return the least numeric type that fits both arms |
duke@1 | 1456 | // (i.e. return larger of the two, or return int if one |
duke@1 | 1457 | // arm is short, the other is char). |
duke@1 | 1458 | if (thenUnboxed.isPrimitive() && elseUnboxed.isPrimitive()) { |
duke@1 | 1459 | // If one arm has an integer subrange type (i.e., byte, |
duke@1 | 1460 | // short, or char), and the other is an integer constant |
duke@1 | 1461 | // that fits into the subrange, return the subrange type. |
duke@1 | 1462 | if (thenUnboxed.tag < INT && elseUnboxed.tag == INT && |
duke@1 | 1463 | types.isAssignable(elseUnboxed, thenUnboxed)) |
duke@1 | 1464 | return thenUnboxed.baseType(); |
duke@1 | 1465 | if (elseUnboxed.tag < INT && thenUnboxed.tag == INT && |
duke@1 | 1466 | types.isAssignable(thenUnboxed, elseUnboxed)) |
duke@1 | 1467 | return elseUnboxed.baseType(); |
duke@1 | 1468 | |
duke@1 | 1469 | for (int i = BYTE; i < VOID; i++) { |
duke@1 | 1470 | Type candidate = syms.typeOfTag[i]; |
duke@1 | 1471 | if (types.isSubtype(thenUnboxed, candidate) && |
duke@1 | 1472 | types.isSubtype(elseUnboxed, candidate)) |
duke@1 | 1473 | return candidate; |
duke@1 | 1474 | } |
duke@1 | 1475 | } |
duke@1 | 1476 | |
duke@1 | 1477 | // Those were all the cases that could result in a primitive |
duke@1 | 1478 | if (allowBoxing) { |
duke@1 | 1479 | if (thentype.isPrimitive()) |
duke@1 | 1480 | thentype = types.boxedClass(thentype).type; |
duke@1 | 1481 | if (elsetype.isPrimitive()) |
duke@1 | 1482 | elsetype = types.boxedClass(elsetype).type; |
duke@1 | 1483 | } |
duke@1 | 1484 | |
duke@1 | 1485 | if (types.isSubtype(thentype, elsetype)) |
duke@1 | 1486 | return elsetype.baseType(); |
duke@1 | 1487 | if (types.isSubtype(elsetype, thentype)) |
duke@1 | 1488 | return thentype.baseType(); |
duke@1 | 1489 | |
duke@1 | 1490 | if (!allowBoxing || thentype.tag == VOID || elsetype.tag == VOID) { |
duke@1 | 1491 | log.error(pos, "neither.conditional.subtype", |
duke@1 | 1492 | thentype, elsetype); |
duke@1 | 1493 | return thentype.baseType(); |
duke@1 | 1494 | } |
duke@1 | 1495 | |
duke@1 | 1496 | // both are known to be reference types. The result is |
duke@1 | 1497 | // lub(thentype,elsetype). This cannot fail, as it will |
duke@1 | 1498 | // always be possible to infer "Object" if nothing better. |
duke@1 | 1499 | return types.lub(thentype.baseType(), elsetype.baseType()); |
duke@1 | 1500 | } |
duke@1 | 1501 | |
duke@1 | 1502 | public void visitIf(JCIf tree) { |
duke@1 | 1503 | attribExpr(tree.cond, env, syms.booleanType); |
duke@1 | 1504 | attribStat(tree.thenpart, env); |
duke@1 | 1505 | if (tree.elsepart != null) |
duke@1 | 1506 | attribStat(tree.elsepart, env); |
duke@1 | 1507 | chk.checkEmptyIf(tree); |
duke@1 | 1508 | result = null; |
duke@1 | 1509 | } |
duke@1 | 1510 | |
duke@1 | 1511 | public void visitExec(JCExpressionStatement tree) { |
mcimadamore@674 | 1512 | //a fresh environment is required for 292 inference to work properly --- |
mcimadamore@674 | 1513 | //see Infer.instantiatePolymorphicSignatureInstance() |
mcimadamore@674 | 1514 | Env<AttrContext> localEnv = env.dup(tree); |
mcimadamore@674 | 1515 | attribExpr(tree.expr, localEnv); |
duke@1 | 1516 | result = null; |
duke@1 | 1517 | } |
duke@1 | 1518 | |
duke@1 | 1519 | public void visitBreak(JCBreak tree) { |
duke@1 | 1520 | tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
duke@1 | 1521 | result = null; |
duke@1 | 1522 | } |
duke@1 | 1523 | |
duke@1 | 1524 | public void visitContinue(JCContinue tree) { |
duke@1 | 1525 | tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
duke@1 | 1526 | result = null; |
duke@1 | 1527 | } |
duke@1 | 1528 | //where |
duke@1 | 1529 | /** Return the target of a break or continue statement, if it exists, |
duke@1 | 1530 | * report an error if not. |
duke@1 | 1531 | * Note: The target of a labelled break or continue is the |
duke@1 | 1532 | * (non-labelled) statement tree referred to by the label, |
duke@1 | 1533 | * not the tree representing the labelled statement itself. |
duke@1 | 1534 | * |
duke@1 | 1535 | * @param pos The position to be used for error diagnostics |
duke@1 | 1536 | * @param tag The tag of the jump statement. This is either |
duke@1 | 1537 | * Tree.BREAK or Tree.CONTINUE. |
duke@1 | 1538 | * @param label The label of the jump statement, or null if no |
duke@1 | 1539 | * label is given. |
duke@1 | 1540 | * @param env The environment current at the jump statement. |
duke@1 | 1541 | */ |
duke@1 | 1542 | private JCTree findJumpTarget(DiagnosticPosition pos, |
jjg@1127 | 1543 | JCTree.Tag tag, |
duke@1 | 1544 | Name label, |
duke@1 | 1545 | Env<AttrContext> env) { |
duke@1 | 1546 | // Search environments outwards from the point of jump. |
duke@1 | 1547 | Env<AttrContext> env1 = env; |
duke@1 | 1548 | LOOP: |
duke@1 | 1549 | while (env1 != null) { |
duke@1 | 1550 | switch (env1.tree.getTag()) { |
mcimadamore@1348 | 1551 | case LABELLED: |
mcimadamore@1348 | 1552 | JCLabeledStatement labelled = (JCLabeledStatement)env1.tree; |
mcimadamore@1348 | 1553 | if (label == labelled.label) { |
mcimadamore@1348 | 1554 | // If jump is a continue, check that target is a loop. |
mcimadamore@1348 | 1555 | if (tag == CONTINUE) { |
mcimadamore@1348 | 1556 | if (!labelled.body.hasTag(DOLOOP) && |
mcimadamore@1348 | 1557 | !labelled.body.hasTag(WHILELOOP) && |
mcimadamore@1348 | 1558 | !labelled.body.hasTag(FORLOOP) && |
mcimadamore@1348 | 1559 | !labelled.body.hasTag(FOREACHLOOP)) |
mcimadamore@1348 | 1560 | log.error(pos, "not.loop.label", label); |
mcimadamore@1348 | 1561 | // Found labelled statement target, now go inwards |
mcimadamore@1348 | 1562 | // to next non-labelled tree. |
mcimadamore@1348 | 1563 | return TreeInfo.referencedStatement(labelled); |
mcimadamore@1348 | 1564 | } else { |
mcimadamore@1348 | 1565 | return labelled; |
mcimadamore@1348 | 1566 | } |
duke@1 | 1567 | } |
mcimadamore@1348 | 1568 | break; |
mcimadamore@1348 | 1569 | case DOLOOP: |
mcimadamore@1348 | 1570 | case WHILELOOP: |
mcimadamore@1348 | 1571 | case FORLOOP: |
mcimadamore@1348 | 1572 | case FOREACHLOOP: |
mcimadamore@1348 | 1573 | if (label == null) return env1.tree; |
mcimadamore@1348 | 1574 | break; |
mcimadamore@1348 | 1575 | case SWITCH: |
mcimadamore@1348 | 1576 | if (label == null && tag == BREAK) return env1.tree; |
mcimadamore@1348 | 1577 | break; |
mcimadamore@1348 | 1578 | case LAMBDA: |
mcimadamore@1348 | 1579 | case METHODDEF: |
mcimadamore@1348 | 1580 | case CLASSDEF: |
mcimadamore@1348 | 1581 | break LOOP; |
mcimadamore@1348 | 1582 | default: |
duke@1 | 1583 | } |
duke@1 | 1584 | env1 = env1.next; |
duke@1 | 1585 | } |
duke@1 | 1586 | if (label != null) |
duke@1 | 1587 | log.error(pos, "undef.label", label); |
jjg@1127 | 1588 | else if (tag == CONTINUE) |
duke@1 | 1589 | log.error(pos, "cont.outside.loop"); |
duke@1 | 1590 | else |
duke@1 | 1591 | log.error(pos, "break.outside.switch.loop"); |
duke@1 | 1592 | return null; |
duke@1 | 1593 | } |
duke@1 | 1594 | |
duke@1 | 1595 | public void visitReturn(JCReturn tree) { |
duke@1 | 1596 | // Check that there is an enclosing method which is |
duke@1 | 1597 | // nested within than the enclosing class. |
mcimadamore@1347 | 1598 | if (env.info.returnResult == null) { |
duke@1 | 1599 | log.error(tree.pos(), "ret.outside.meth"); |
duke@1 | 1600 | } else { |
duke@1 | 1601 | // Attribute return expression, if it exists, and check that |
duke@1 | 1602 | // it conforms to result type of enclosing method. |
mcimadamore@1347 | 1603 | if (tree.expr != null) { |
mcimadamore@1347 | 1604 | if (env.info.returnResult.pt.tag == VOID) { |
duke@1 | 1605 | log.error(tree.expr.pos(), |
duke@1 | 1606 | "cant.ret.val.from.meth.decl.void"); |
mcimadamore@1347 | 1607 | } |
mcimadamore@1347 | 1608 | attribTree(tree.expr, env, env.info.returnResult); |
mcimadamore@1347 | 1609 | } else if (env.info.returnResult.pt.tag != VOID) { |
duke@1 | 1610 | log.error(tree.pos(), "missing.ret.val"); |
duke@1 | 1611 | } |
duke@1 | 1612 | } |
duke@1 | 1613 | result = null; |
duke@1 | 1614 | } |
duke@1 | 1615 | |
duke@1 | 1616 | public void visitThrow(JCThrow tree) { |
duke@1 | 1617 | attribExpr(tree.expr, env, syms.throwableType); |
duke@1 | 1618 | result = null; |
duke@1 | 1619 | } |
duke@1 | 1620 | |
duke@1 | 1621 | public void visitAssert(JCAssert tree) { |
duke@1 | 1622 | attribExpr(tree.cond, env, syms.booleanType); |
duke@1 | 1623 | if (tree.detail != null) { |
duke@1 | 1624 | chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env)); |
duke@1 | 1625 | } |
duke@1 | 1626 | result = null; |
duke@1 | 1627 | } |
duke@1 | 1628 | |
duke@1 | 1629 | /** Visitor method for method invocations. |
duke@1 | 1630 | * NOTE: The method part of an application will have in its type field |
duke@1 | 1631 | * the return type of the method, not the method's type itself! |
duke@1 | 1632 | */ |
duke@1 | 1633 | public void visitApply(JCMethodInvocation tree) { |
duke@1 | 1634 | // The local environment of a method application is |
duke@1 | 1635 | // a new environment nested in the current one. |
duke@1 | 1636 | Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
duke@1 | 1637 | |
duke@1 | 1638 | // The types of the actual method arguments. |
duke@1 | 1639 | List<Type> argtypes; |
duke@1 | 1640 | |
duke@1 | 1641 | // The types of the actual method type arguments. |
duke@1 | 1642 | List<Type> typeargtypes = null; |
duke@1 | 1643 | |
duke@1 | 1644 | Name methName = TreeInfo.name(tree.meth); |
duke@1 | 1645 | |
duke@1 | 1646 | boolean isConstructorCall = |
duke@1 | 1647 | methName == names._this || methName == names._super; |
duke@1 | 1648 | |
duke@1 | 1649 | if (isConstructorCall) { |
duke@1 | 1650 | // We are seeing a ...this(...) or ...super(...) call. |
duke@1 | 1651 | // Check that this is the first statement in a constructor. |
duke@1 | 1652 | if (checkFirstConstructorStat(tree, env)) { |
duke@1 | 1653 | |
duke@1 | 1654 | // Record the fact |
duke@1 | 1655 | // that this is a constructor call (using isSelfCall). |
duke@1 | 1656 | localEnv.info.isSelfCall = true; |
duke@1 | 1657 | |
duke@1 | 1658 | // Attribute arguments, yielding list of argument types. |
duke@1 | 1659 | argtypes = attribArgs(tree.args, localEnv); |
duke@1 | 1660 | typeargtypes = attribTypes(tree.typeargs, localEnv); |
duke@1 | 1661 | |
duke@1 | 1662 | // Variable `site' points to the class in which the called |
duke@1 | 1663 | // constructor is defined. |
duke@1 | 1664 | Type site = env.enclClass.sym.type; |
duke@1 | 1665 | if (methName == names._super) { |
duke@1 | 1666 | if (site == syms.objectType) { |
duke@1 | 1667 | log.error(tree.meth.pos(), "no.superclass", site); |
jjg@110 | 1668 | site = types.createErrorType(syms.objectType); |
duke@1 | 1669 | } else { |
duke@1 | 1670 | site = types.supertype(site); |
duke@1 | 1671 | } |
duke@1 | 1672 | } |
duke@1 | 1673 | |
duke@1 | 1674 | if (site.tag == CLASS) { |
mcimadamore@361 | 1675 | Type encl = site.getEnclosingType(); |
mcimadamore@361 | 1676 | while (encl != null && encl.tag == TYPEVAR) |
mcimadamore@361 | 1677 | encl = encl.getUpperBound(); |
mcimadamore@361 | 1678 | if (encl.tag == CLASS) { |
duke@1 | 1679 | // we are calling a nested class |
duke@1 | 1680 | |
jjg@1127 | 1681 | if (tree.meth.hasTag(SELECT)) { |
duke@1 | 1682 | JCTree qualifier = ((JCFieldAccess) tree.meth).selected; |
duke@1 | 1683 | |
duke@1 | 1684 | // We are seeing a prefixed call, of the form |
duke@1 | 1685 | // <expr>.super(...). |
duke@1 | 1686 | // Check that the prefix expression conforms |
duke@1 | 1687 | // to the outer instance type of the class. |
duke@1 | 1688 | chk.checkRefType(qualifier.pos(), |
duke@1 | 1689 | attribExpr(qualifier, localEnv, |
mcimadamore@361 | 1690 | encl)); |
duke@1 | 1691 | } else if (methName == names._super) { |
duke@1 | 1692 | // qualifier omitted; check for existence |
duke@1 | 1693 | // of an appropriate implicit qualifier. |
duke@1 | 1694 | rs.resolveImplicitThis(tree.meth.pos(), |
mcimadamore@901 | 1695 | localEnv, site, true); |
duke@1 | 1696 | } |
jjg@1127 | 1697 | } else if (tree.meth.hasTag(SELECT)) { |
duke@1 | 1698 | log.error(tree.meth.pos(), "illegal.qual.not.icls", |
duke@1 | 1699 | site.tsym); |
duke@1 | 1700 | } |
duke@1 | 1701 | |
duke@1 | 1702 | // if we're calling a java.lang.Enum constructor, |
duke@1 | 1703 | // prefix the implicit String and int parameters |
duke@1 | 1704 | if (site.tsym == syms.enumSym && allowEnums) |
duke@1 | 1705 | argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType); |
duke@1 | 1706 | |
duke@1 | 1707 | // Resolve the called constructor under the assumption |
duke@1 | 1708 | // that we are referring to a superclass instance of the |
duke@1 | 1709 | // current instance (JLS ???). |
duke@1 | 1710 | boolean selectSuperPrev = localEnv.info.selectSuper; |
duke@1 | 1711 | localEnv.info.selectSuper = true; |
mcimadamore@1347 | 1712 | localEnv.info.pendingResolutionPhase = null; |
duke@1 | 1713 | Symbol sym = rs.resolveConstructor( |
duke@1 | 1714 | tree.meth.pos(), localEnv, site, argtypes, typeargtypes); |
duke@1 | 1715 | localEnv.info.selectSuper = selectSuperPrev; |
duke@1 | 1716 | |
duke@1 | 1717 | // Set method symbol to resolved constructor... |
duke@1 | 1718 | TreeInfo.setSymbol(tree.meth, sym); |
duke@1 | 1719 | |
duke@1 | 1720 | // ...and check that it is legal in the current context. |
duke@1 | 1721 | // (this will also set the tree's type) |
mcimadamore@1268 | 1722 | Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes); |
mcimadamore@1347 | 1723 | checkId(tree.meth, site, sym, localEnv, new ResultInfo(MTH, mpt)); |
duke@1 | 1724 | } |
duke@1 | 1725 | // Otherwise, `site' is an error type and we do nothing |
duke@1 | 1726 | } |
duke@1 | 1727 | result = tree.type = syms.voidType; |
duke@1 | 1728 | } else { |
duke@1 | 1729 | // Otherwise, we are seeing a regular method call. |
duke@1 | 1730 | // Attribute the arguments, yielding list of argument types, ... |
duke@1 | 1731 | argtypes = attribArgs(tree.args, localEnv); |
jrose@267 | 1732 | typeargtypes = attribAnyTypes(tree.typeargs, localEnv); |
duke@1 | 1733 | |
duke@1 | 1734 | // ... and attribute the method using as a prototype a methodtype |
duke@1 | 1735 | // whose formal argument types is exactly the list of actual |
duke@1 | 1736 | // arguments (this will also set the method symbol). |
mcimadamore@1268 | 1737 | Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes); |
mcimadamore@1347 | 1738 | localEnv.info.pendingResolutionPhase = null; |
mcimadamore@1347 | 1739 | Type mtype = attribTree(tree.meth, localEnv, new ResultInfo(VAL, mpt, resultInfo.checkContext)); |
duke@1 | 1740 | |
duke@1 | 1741 | // Compute the result type. |
duke@1 | 1742 | Type restype = mtype.getReturnType(); |
mcimadamore@689 | 1743 | if (restype.tag == WILDCARD) |
mcimadamore@689 | 1744 | throw new AssertionError(mtype); |
duke@1 | 1745 | |
duke@1 | 1746 | // as a special case, array.clone() has a result that is |
duke@1 | 1747 | // the same as static type of the array being cloned |
jjg@1127 | 1748 | if (tree.meth.hasTag(SELECT) && |
duke@1 | 1749 | allowCovariantReturns && |
duke@1 | 1750 | methName == names.clone && |
duke@1 | 1751 | types.isArray(((JCFieldAccess) tree.meth).selected.type)) |
duke@1 | 1752 | restype = ((JCFieldAccess) tree.meth).selected.type; |
duke@1 | 1753 | |
duke@1 | 1754 | // as a special case, x.getClass() has type Class<? extends |X|> |
duke@1 | 1755 | if (allowGenerics && |
duke@1 | 1756 | methName == names.getClass && tree.args.isEmpty()) { |
jjg@1127 | 1757 | Type qualifier = (tree.meth.hasTag(SELECT)) |
duke@1 | 1758 | ? ((JCFieldAccess) tree.meth).selected.type |
duke@1 | 1759 | : env.enclClass.sym.type; |
duke@1 | 1760 | restype = new |
duke@1 | 1761 | ClassType(restype.getEnclosingType(), |
duke@1 | 1762 | List.<Type>of(new WildcardType(types.erasure(qualifier), |
duke@1 | 1763 | BoundKind.EXTENDS, |
duke@1 | 1764 | syms.boundClass)), |
duke@1 | 1765 | restype.tsym); |
duke@1 | 1766 | } |
duke@1 | 1767 | |
mcimadamore@820 | 1768 | chk.checkRefTypes(tree.typeargs, typeargtypes); |
jrose@267 | 1769 | |
duke@1 | 1770 | // Check that value of resulting type is admissible in the |
duke@1 | 1771 | // current context. Also, capture the return type |
mcimadamore@1220 | 1772 | result = check(tree, capture(restype), VAL, resultInfo); |
mcimadamore@1219 | 1773 | |
mcimadamore@1347 | 1774 | if (localEnv.info.lastResolveVarargs()) |
mcimadamore@1219 | 1775 | Assert.check(result.isErroneous() || tree.varargsElement != null); |
duke@1 | 1776 | } |
mcimadamore@122 | 1777 | chk.validate(tree.typeargs, localEnv); |
duke@1 | 1778 | } |
duke@1 | 1779 | //where |
duke@1 | 1780 | /** Check that given application node appears as first statement |
duke@1 | 1781 | * in a constructor call. |
duke@1 | 1782 | * @param tree The application node |
duke@1 | 1783 | * @param env The environment current at the application. |
duke@1 | 1784 | */ |
duke@1 | 1785 | boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) { |
duke@1 | 1786 | JCMethodDecl enclMethod = env.enclMethod; |
duke@1 | 1787 | if (enclMethod != null && enclMethod.name == names.init) { |
duke@1 | 1788 | JCBlock body = enclMethod.body; |
jjg@1127 | 1789 | if (body.stats.head.hasTag(EXEC) && |
duke@1 | 1790 | ((JCExpressionStatement) body.stats.head).expr == tree) |
duke@1 | 1791 | return true; |
duke@1 | 1792 | } |
duke@1 | 1793 | log.error(tree.pos(),"call.must.be.first.stmt.in.ctor", |
duke@1 | 1794 | TreeInfo.name(tree.meth)); |
duke@1 | 1795 | return false; |
duke@1 | 1796 | } |
duke@1 | 1797 | |
duke@1 | 1798 | /** Obtain a method type with given argument types. |
duke@1 | 1799 | */ |
mcimadamore@1268 | 1800 | Type newMethodTemplate(Type restype, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1347 | 1801 | MethodType mt = new MethodType(argtypes, restype, List.<Type>nil(), syms.methodClass); |
duke@1 | 1802 | return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt); |
duke@1 | 1803 | } |
duke@1 | 1804 | |
mcimadamore@1347 | 1805 | public void visitNewClass(final JCNewClass tree) { |
jjg@110 | 1806 | Type owntype = types.createErrorType(tree.type); |
duke@1 | 1807 | |
duke@1 | 1808 | // The local environment of a class creation is |
duke@1 | 1809 | // a new environment nested in the current one. |
duke@1 | 1810 | Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
duke@1 | 1811 | |
duke@1 | 1812 | // The anonymous inner class definition of the new expression, |
duke@1 | 1813 | // if one is defined by it. |
duke@1 | 1814 | JCClassDecl cdef = tree.def; |
duke@1 | 1815 | |
duke@1 | 1816 | // If enclosing class is given, attribute it, and |
duke@1 | 1817 | // complete class name to be fully qualified |
duke@1 | 1818 | JCExpression clazz = tree.clazz; // Class field following new |
duke@1 | 1819 | JCExpression clazzid = // Identifier in class field |
jjg@1127 | 1820 | (clazz.hasTag(TYPEAPPLY)) |
duke@1 | 1821 | ? ((JCTypeApply) clazz).clazz |
duke@1 | 1822 | : clazz; |
duke@1 | 1823 | |
duke@1 | 1824 | JCExpression clazzid1 = clazzid; // The same in fully qualified form |
duke@1 | 1825 | |
duke@1 | 1826 | if (tree.encl != null) { |
duke@1 | 1827 | // We are seeing a qualified new, of the form |
duke@1 | 1828 | // <expr>.new C <...> (...) ... |
duke@1 | 1829 | // In this case, we let clazz stand for the name of the |
duke@1 | 1830 | // allocated class C prefixed with the type of the qualifier |
duke@1 | 1831 | // expression, so that we can |
duke@1 | 1832 | // resolve it with standard techniques later. I.e., if |
duke@1 | 1833 | // <expr> has type T, then <expr>.new C <...> (...) |
duke@1 | 1834 | // yields a clazz T.C. |
duke@1 | 1835 | Type encltype = chk.checkRefType(tree.encl.pos(), |
duke@1 | 1836 | attribExpr(tree.encl, env)); |
duke@1 | 1837 | clazzid1 = make.at(clazz.pos).Select(make.Type(encltype), |
duke@1 | 1838 | ((JCIdent) clazzid).name); |
jjg@1127 | 1839 | if (clazz.hasTag(TYPEAPPLY)) |
duke@1 | 1840 | clazz = make.at(tree.pos). |
duke@1 | 1841 | TypeApply(clazzid1, |
duke@1 | 1842 | ((JCTypeApply) clazz).arguments); |
duke@1 | 1843 | else |
duke@1 | 1844 | clazz = clazzid1; |
duke@1 | 1845 | } |
duke@1 | 1846 | |
duke@1 | 1847 | // Attribute clazz expression and store |
duke@1 | 1848 | // symbol + type back into the attributed tree. |
mcimadamore@1269 | 1849 | Type clazztype = TreeInfo.isEnumInit(env.tree) ? |
mcimadamore@1269 | 1850 | attribIdentAsEnumType(env, (JCIdent)clazz) : |
mcimadamore@1269 | 1851 | attribType(clazz, env); |
mcimadamore@1269 | 1852 | |
mcimadamore@914 | 1853 | clazztype = chk.checkDiamond(tree, clazztype); |
mcimadamore@122 | 1854 | chk.validate(clazz, localEnv); |
duke@1 | 1855 | if (tree.encl != null) { |
duke@1 | 1856 | // We have to work in this case to store |
duke@1 | 1857 | // symbol + type back into the attributed tree. |
duke@1 | 1858 | tree.clazz.type = clazztype; |
duke@1 | 1859 | TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1)); |
duke@1 | 1860 | clazzid.type = ((JCIdent) clazzid).sym.type; |
duke@1 | 1861 | if (!clazztype.isErroneous()) { |
duke@1 | 1862 | if (cdef != null && clazztype.tsym.isInterface()) { |
duke@1 | 1863 | log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new"); |
duke@1 | 1864 | } else if (clazztype.tsym.isStatic()) { |
duke@1 | 1865 | log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym); |
duke@1 | 1866 | } |
duke@1 | 1867 | } |
duke@1 | 1868 | } else if (!clazztype.tsym.isInterface() && |
duke@1 | 1869 | clazztype.getEnclosingType().tag == CLASS) { |
duke@1 | 1870 | // Check for the existence of an apropos outer instance |
duke@1 | 1871 | rs.resolveImplicitThis(tree.pos(), env, clazztype); |
duke@1 | 1872 | } |
duke@1 | 1873 | |
duke@1 | 1874 | // Attribute constructor arguments. |
duke@1 | 1875 | List<Type> argtypes = attribArgs(tree.args, localEnv); |
duke@1 | 1876 | List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); |
duke@1 | 1877 | |
duke@1 | 1878 | // If we have made no mistakes in the class type... |
duke@1 | 1879 | if (clazztype.tag == CLASS) { |
duke@1 | 1880 | // Enums may not be instantiated except implicitly |
duke@1 | 1881 | if (allowEnums && |
duke@1 | 1882 | (clazztype.tsym.flags_field&Flags.ENUM) != 0 && |
jjg@1127 | 1883 | (!env.tree.hasTag(VARDEF) || |
duke@1 | 1884 | (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 || |
duke@1 | 1885 | ((JCVariableDecl) env.tree).init != tree)) |
duke@1 | 1886 | log.error(tree.pos(), "enum.cant.be.instantiated"); |
duke@1 | 1887 | // Check that class is not abstract |
duke@1 | 1888 | if (cdef == null && |
duke@1 | 1889 | (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
duke@1 | 1890 | log.error(tree.pos(), "abstract.cant.be.instantiated", |
duke@1 | 1891 | clazztype.tsym); |
duke@1 | 1892 | } else if (cdef != null && clazztype.tsym.isInterface()) { |
duke@1 | 1893 | // Check that no constructor arguments are given to |
duke@1 | 1894 | // anonymous classes implementing an interface |
duke@1 | 1895 | if (!argtypes.isEmpty()) |
duke@1 | 1896 | log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args"); |
duke@1 | 1897 | |
duke@1 | 1898 | if (!typeargtypes.isEmpty()) |
duke@1 | 1899 | log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs"); |
duke@1 | 1900 | |
duke@1 | 1901 | // Error recovery: pretend no arguments were supplied. |
duke@1 | 1902 | argtypes = List.nil(); |
duke@1 | 1903 | typeargtypes = List.nil(); |
mcimadamore@1347 | 1904 | } else if (TreeInfo.isDiamond(tree)) { |
mcimadamore@1347 | 1905 | ClassType site = new ClassType(clazztype.getEnclosingType(), |
mcimadamore@1347 | 1906 | clazztype.tsym.type.getTypeArguments(), |
mcimadamore@1347 | 1907 | clazztype.tsym); |
mcimadamore@1347 | 1908 | |
mcimadamore@1347 | 1909 | Env<AttrContext> diamondEnv = localEnv.dup(tree); |
mcimadamore@1347 | 1910 | diamondEnv.info.selectSuper = cdef != null; |
mcimadamore@1347 | 1911 | diamondEnv.info.pendingResolutionPhase = null; |
mcimadamore@1347 | 1912 | |
mcimadamore@1347 | 1913 | //if the type of the instance creation expression is a class type |
mcimadamore@1347 | 1914 | //apply method resolution inference (JLS 15.12.2.7). The return type |
mcimadamore@1347 | 1915 | //of the resolved constructor will be a partially instantiated type |
mcimadamore@1347 | 1916 | Symbol constructor = rs.resolveDiamond(tree.pos(), |
mcimadamore@1347 | 1917 | diamondEnv, |
mcimadamore@1347 | 1918 | site, |
mcimadamore@1347 | 1919 | argtypes, |
mcimadamore@1347 | 1920 | typeargtypes); |
mcimadamore@1347 | 1921 | tree.constructor = constructor.baseSymbol(); |
mcimadamore@1347 | 1922 | |
mcimadamore@1347 | 1923 | final TypeSymbol csym = clazztype.tsym; |
mcimadamore@1347 | 1924 | ResultInfo diamondResult = new ResultInfo(MTH, newMethodTemplate(resultInfo.pt, argtypes, typeargtypes), new Check.NestedCheckContext(resultInfo.checkContext) { |
mcimadamore@1347 | 1925 | @Override |
mcimadamore@1347 | 1926 | public void report(DiagnosticPosition _unused, JCDiagnostic details) { |
mcimadamore@1347 | 1927 | enclosingContext.report(tree.clazz, |
mcimadamore@1347 | 1928 | diags.fragment("cant.apply.diamond.1", diags.fragment("diamond", csym), details)); |
mcimadamore@1347 | 1929 | } |
mcimadamore@1347 | 1930 | }); |
mcimadamore@1347 | 1931 | Type constructorType = tree.constructorType = types.createErrorType(clazztype); |
mcimadamore@1347 | 1932 | constructorType = checkId(tree, site, |
mcimadamore@1347 | 1933 | constructor, |
mcimadamore@1347 | 1934 | diamondEnv, |
mcimadamore@1347 | 1935 | diamondResult); |
mcimadamore@1347 | 1936 | |
mcimadamore@1347 | 1937 | tree.clazz.type = types.createErrorType(clazztype); |
mcimadamore@1347 | 1938 | if (!constructorType.isErroneous()) { |
mcimadamore@1347 | 1939 | tree.clazz.type = clazztype = constructorType.getReturnType(); |
mcimadamore@1347 | 1940 | tree.constructorType = types.createMethodTypeWithReturn(constructorType, syms.voidType); |
mcimadamore@1347 | 1941 | } |
mcimadamore@1347 | 1942 | clazztype = chk.checkClassType(tree.clazz, tree.clazz.type, true); |
duke@1 | 1943 | } |
duke@1 | 1944 | |
duke@1 | 1945 | // Resolve the called constructor under the assumption |
duke@1 | 1946 | // that we are referring to a superclass instance of the |
duke@1 | 1947 | // current instance (JLS ???). |
mcimadamore@1347 | 1948 | else { |
mcimadamore@1010 | 1949 | //the following code alters some of the fields in the current |
mcimadamore@1010 | 1950 | //AttrContext - hence, the current context must be dup'ed in |
mcimadamore@1010 | 1951 | //order to avoid downstream failures |
mcimadamore@1010 | 1952 | Env<AttrContext> rsEnv = localEnv.dup(tree); |
mcimadamore@1010 | 1953 | rsEnv.info.selectSuper = cdef != null; |
mcimadamore@1347 | 1954 | rsEnv.info.pendingResolutionPhase = null; |
duke@1 | 1955 | tree.constructor = rs.resolveConstructor( |
mcimadamore@1010 | 1956 | tree.pos(), rsEnv, clazztype, argtypes, typeargtypes); |
mcimadamore@1341 | 1957 | if (cdef == null) { //do not check twice! |
mcimadamore@1341 | 1958 | tree.constructorType = checkId(tree, |
mcimadamore@1341 | 1959 | clazztype, |
mcimadamore@1341 | 1960 | tree.constructor, |
mcimadamore@1341 | 1961 | rsEnv, |
mcimadamore@1347 | 1962 | new ResultInfo(MTH, newMethodTemplate(syms.voidType, argtypes, typeargtypes))); |
mcimadamore@1347 | 1963 | if (rsEnv.info.lastResolveVarargs()) |
mcimadamore@1341 | 1964 | Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null); |
mcimadamore@1341 | 1965 | } |
mcimadamore@1347 | 1966 | findDiamondIfNeeded(localEnv, tree, clazztype); |
duke@1 | 1967 | } |
duke@1 | 1968 | |
duke@1 | 1969 | if (cdef != null) { |
duke@1 | 1970 | // We are seeing an anonymous class instance creation. |
duke@1 | 1971 | // In this case, the class instance creation |
duke@1 | 1972 | // expression |
duke@1 | 1973 | // |
duke@1 | 1974 | // E.new <typeargs1>C<typargs2>(args) { ... } |
duke@1 | 1975 | // |
duke@1 | 1976 | // is represented internally as |
duke@1 | 1977 | // |
duke@1 | 1978 | // E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } ) . |
duke@1 | 1979 | // |
duke@1 | 1980 | // This expression is then *transformed* as follows: |
duke@1 | 1981 | // |
duke@1 | 1982 | // (1) add a STATIC flag to the class definition |
duke@1 | 1983 | // if the current environment is static |
duke@1 | 1984 | // (2) add an extends or implements clause |
duke@1 | 1985 | // (3) add a constructor. |
duke@1 | 1986 | // |
duke@1 | 1987 | // For instance, if C is a class, and ET is the type of E, |
duke@1 | 1988 | // the expression |
duke@1 | 1989 | // |
duke@1 | 1990 | // E.new <typeargs1>C<typargs2>(args) { ... } |
duke@1 | 1991 | // |
duke@1 | 1992 | // is translated to (where X is a fresh name and typarams is the |
duke@1 | 1993 | // parameter list of the super constructor): |
duke@1 | 1994 | // |
duke@1 | 1995 | // new <typeargs1>X(<*nullchk*>E, args) where |
duke@1 | 1996 | // X extends C<typargs2> { |
duke@1 | 1997 | // <typarams> X(ET e, args) { |
duke@1 | 1998 | // e.<typeargs1>super(args) |
duke@1 | 1999 | // } |
duke@1 | 2000 | // ... |
duke@1 | 2001 | // } |
duke@1 | 2002 | if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC; |
mcimadamore@536 | 2003 | |
duke@1 | 2004 | if (clazztype.tsym.isInterface()) { |
duke@1 | 2005 | cdef.implementing = List.of(clazz); |
duke@1 | 2006 | } else { |
duke@1 | 2007 | cdef.extending = clazz; |
duke@1 | 2008 | } |
duke@1 | 2009 | |
duke@1 | 2010 | attribStat(cdef, localEnv); |
duke@1 | 2011 | |
mcimadamore@1348 | 2012 | checkLambdaCandidate(tree, cdef.sym, clazztype); |
mcimadamore@1348 | 2013 | |
duke@1 | 2014 | // If an outer instance is given, |
duke@1 | 2015 | // prefix it to the constructor arguments |
duke@1 | 2016 | // and delete it from the new expression |
duke@1 | 2017 | if (tree.encl != null && !clazztype.tsym.isInterface()) { |
duke@1 | 2018 | tree.args = tree.args.prepend(makeNullCheck(tree.encl)); |
duke@1 | 2019 | argtypes = argtypes.prepend(tree.encl.type); |
duke@1 | 2020 | tree.encl = null; |
duke@1 | 2021 | } |
duke@1 | 2022 | |
duke@1 | 2023 | // Reassign clazztype and recompute constructor. |
duke@1 | 2024 | clazztype = cdef.sym.type; |
mcimadamore@1341 | 2025 | Symbol sym = tree.constructor = rs.resolveConstructor( |
mcimadamore@1341 | 2026 | tree.pos(), localEnv, clazztype, argtypes, typeargtypes); |
mcimadamore@1341 | 2027 | Assert.check(sym.kind < AMBIGUOUS); |
duke@1 | 2028 | tree.constructor = sym; |
mcimadamore@1341 | 2029 | tree.constructorType = checkId(tree, |
mcimadamore@1341 | 2030 | clazztype, |
mcimadamore@1341 | 2031 | tree.constructor, |
mcimadamore@1341 | 2032 | localEnv, |
mcimadamore@1347 | 2033 | new ResultInfo(VAL, newMethodTemplate(syms.voidType, argtypes, typeargtypes))); |
duke@1 | 2034 | } |
duke@1 | 2035 | |
duke@1 | 2036 | if (tree.constructor != null && tree.constructor.kind == MTH) |
duke@1 | 2037 | owntype = clazztype; |
duke@1 | 2038 | } |
mcimadamore@1220 | 2039 | result = check(tree, owntype, VAL, resultInfo); |
mcimadamore@122 | 2040 | chk.validate(tree.typeargs, localEnv); |
duke@1 | 2041 | } |
mcimadamore@1347 | 2042 | //where |
mcimadamore@1347 | 2043 | void findDiamondIfNeeded(Env<AttrContext> env, JCNewClass tree, Type clazztype) { |
mcimadamore@1347 | 2044 | if (tree.def == null && |
mcimadamore@1347 | 2045 | !clazztype.isErroneous() && |
mcimadamore@1347 | 2046 | clazztype.getTypeArguments().nonEmpty() && |
mcimadamore@1347 | 2047 | findDiamonds) { |
mcimadamore@1347 | 2048 | JCTypeApply ta = (JCTypeApply)tree.clazz; |
mcimadamore@1347 | 2049 | List<JCExpression> prevTypeargs = ta.arguments; |
mcimadamore@1347 | 2050 | try { |
mcimadamore@1347 | 2051 | //create a 'fake' diamond AST node by removing type-argument trees |
mcimadamore@1347 | 2052 | ta.arguments = List.nil(); |
mcimadamore@1347 | 2053 | ResultInfo findDiamondResult = new ResultInfo(VAL, |
mcimadamore@1347 | 2054 | resultInfo.checkContext.inferenceContext().free(resultInfo.pt) ? Type.noType : pt()); |
mcimadamore@1347 | 2055 | Type inferred = deferredAttr.attribSpeculative(tree, env, findDiamondResult).type; |
mcimadamore@1347 | 2056 | if (!inferred.isErroneous() && |
mcimadamore@1347 | 2057 | types.isAssignable(inferred, pt().tag == NONE ? syms.objectType : pt(), Warner.noWarnings)) { |
mcimadamore@1347 | 2058 | String key = types.isSameType(clazztype, inferred) ? |
mcimadamore@1347 | 2059 | "diamond.redundant.args" : |
mcimadamore@1347 | 2060 | "diamond.redundant.args.1"; |
mcimadamore@1347 | 2061 | log.warning(tree.clazz.pos(), key, clazztype, inferred); |
mcimadamore@1347 | 2062 | } |
mcimadamore@1347 | 2063 | } finally { |
mcimadamore@1347 | 2064 | ta.arguments = prevTypeargs; |
mcimadamore@1347 | 2065 | } |
mcimadamore@1347 | 2066 | } |
mcimadamore@537 | 2067 | } |
mcimadamore@950 | 2068 | |
mcimadamore@1348 | 2069 | private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) { |
mcimadamore@1348 | 2070 | if (allowLambda && |
mcimadamore@1348 | 2071 | identifyLambdaCandidate && |
mcimadamore@1348 | 2072 | clazztype.tag == CLASS && |
mcimadamore@1348 | 2073 | pt().tag != NONE && |
mcimadamore@1348 | 2074 | types.isFunctionalInterface(clazztype.tsym)) { |
mcimadamore@1348 | 2075 | Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym); |
mcimadamore@1348 | 2076 | int count = 0; |
mcimadamore@1348 | 2077 | boolean found = false; |
mcimadamore@1348 | 2078 | for (Symbol sym : csym.members().getElements()) { |
mcimadamore@1348 | 2079 | if ((sym.flags() & SYNTHETIC) != 0 || |
mcimadamore@1348 | 2080 | sym.isConstructor()) continue; |
mcimadamore@1348 | 2081 | count++; |
mcimadamore@1348 | 2082 | if (sym.kind != MTH || |
mcimadamore@1348 | 2083 | !sym.name.equals(descriptor.name)) continue; |
mcimadamore@1348 | 2084 | Type mtype = types.memberType(clazztype, sym); |
mcimadamore@1348 | 2085 | if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) { |
mcimadamore@1348 | 2086 | found = true; |
mcimadamore@1348 | 2087 | } |
mcimadamore@1348 | 2088 | } |
mcimadamore@1348 | 2089 | if (found && count == 1) { |
mcimadamore@1348 | 2090 | log.note(tree.def, "potential.lambda.found"); |
mcimadamore@1348 | 2091 | } |
mcimadamore@1348 | 2092 | } |
mcimadamore@1348 | 2093 | } |
mcimadamore@1348 | 2094 | |
duke@1 | 2095 | /** Make an attributed null check tree. |
duke@1 | 2096 | */ |
duke@1 | 2097 | public JCExpression makeNullCheck(JCExpression arg) { |
duke@1 | 2098 | // optimization: X.this is never null; skip null check |
duke@1 | 2099 | Name name = TreeInfo.name(arg); |
duke@1 | 2100 | if (name == names._this || name == names._super) return arg; |
duke@1 | 2101 | |
jjg@1127 | 2102 | JCTree.Tag optag = NULLCHK; |
duke@1 | 2103 | JCUnary tree = make.at(arg.pos).Unary(optag, arg); |
duke@1 | 2104 | tree.operator = syms.nullcheck; |
duke@1 | 2105 | tree.type = arg.type; |
duke@1 | 2106 | return tree; |
duke@1 | 2107 | } |
duke@1 | 2108 | |
duke@1 | 2109 | public void visitNewArray(JCNewArray tree) { |
jjg@110 | 2110 | Type owntype = types.createErrorType(tree.type); |
mcimadamore@1347 | 2111 | Env<AttrContext> localEnv = env.dup(tree); |
duke@1 | 2112 | Type elemtype; |
duke@1 | 2113 | if (tree.elemtype != null) { |
mcimadamore@1347 | 2114 | elemtype = attribType(tree.elemtype, localEnv); |
mcimadamore@1347 | 2115 | chk.validate(tree.elemtype, localEnv); |
duke@1 | 2116 | owntype = elemtype; |
duke@1 | 2117 | for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
mcimadamore@1347 | 2118 | attribExpr(l.head, localEnv, syms.intType); |
duke@1 | 2119 | owntype = new ArrayType(owntype, syms.arrayClass); |
duke@1 | 2120 | } |
duke@1 | 2121 | } else { |
duke@1 | 2122 | // we are seeing an untyped aggregate { ... } |
duke@1 | 2123 | // this is allowed only if the prototype is an array |
mcimadamore@1220 | 2124 | if (pt().tag == ARRAY) { |
mcimadamore@1220 | 2125 | elemtype = types.elemtype(pt()); |
duke@1 | 2126 | } else { |
mcimadamore@1220 | 2127 | if (pt().tag != ERROR) { |
duke@1 | 2128 | log.error(tree.pos(), "illegal.initializer.for.type", |
mcimadamore@1220 | 2129 | pt()); |
duke@1 | 2130 | } |
mcimadamore@1220 | 2131 | elemtype = types.createErrorType(pt()); |
duke@1 | 2132 | } |
duke@1 | 2133 | } |
duke@1 | 2134 | if (tree.elems != null) { |
mcimadamore@1347 | 2135 | attribExprs(tree.elems, localEnv, elemtype); |
duke@1 | 2136 | owntype = new ArrayType(elemtype, syms.arrayClass); |
duke@1 | 2137 | } |
duke@1 | 2138 | if (!types.isReifiable(elemtype)) |
duke@1 | 2139 | log.error(tree.pos(), "generic.array.creation"); |
mcimadamore@1220 | 2140 | result = check(tree, owntype, VAL, resultInfo); |
duke@1 | 2141 | } |
duke@1 | 2142 | |
mcimadamore@1348 | 2143 | /* |
mcimadamore@1348 | 2144 | * A lambda expression can only be attributed when a target-type is available. |
mcimadamore@1348 | 2145 | * In addition, if the target-type is that of a functional interface whose |
mcimadamore@1348 | 2146 | * descriptor contains inference variables in argument position the lambda expression |
mcimadamore@1348 | 2147 | * is 'stuck' (see DeferredAttr). |
mcimadamore@1348 | 2148 | */ |
mcimadamore@1144 | 2149 | @Override |
mcimadamore@1348 | 2150 | public void visitLambda(final JCLambda that) { |
mcimadamore@1348 | 2151 | if (pt().isErroneous() || (pt().tag == NONE && pt() != Type.recoveryType)) { |
mcimadamore@1348 | 2152 | if (pt().tag == NONE) { |
mcimadamore@1348 | 2153 | //lambda only allowed in assignment or method invocation/cast context |
mcimadamore@1348 | 2154 | log.error(that.pos(), "unexpected.lambda"); |
mcimadamore@1348 | 2155 | } |
mcimadamore@1348 | 2156 | result = that.type = types.createErrorType(pt()); |
mcimadamore@1348 | 2157 | return; |
mcimadamore@1348 | 2158 | } |
mcimadamore@1348 | 2159 | //create an environment for attribution of the lambda expression |
mcimadamore@1348 | 2160 | final Env<AttrContext> localEnv = lambdaEnv(that, env); |
mcimadamore@1348 | 2161 | boolean needsRecovery = resultInfo.checkContext.deferredAttrContext() == deferredAttr.emptyDeferredAttrContext || |
mcimadamore@1348 | 2162 | resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK; |
mcimadamore@1348 | 2163 | try { |
mcimadamore@1348 | 2164 | List<Type> explicitParamTypes = null; |
mcimadamore@1348 | 2165 | if (TreeInfo.isExplicitLambda(that)) { |
mcimadamore@1348 | 2166 | //attribute lambda parameters |
mcimadamore@1348 | 2167 | attribStats(that.params, localEnv); |
mcimadamore@1348 | 2168 | explicitParamTypes = TreeInfo.types(that.params); |
mcimadamore@1348 | 2169 | } |
mcimadamore@1348 | 2170 | |
mcimadamore@1348 | 2171 | Type target = infer.instantiateFunctionalInterface(that, pt(), explicitParamTypes, resultInfo.checkContext); |
mcimadamore@1348 | 2172 | Type lambdaType = (target == Type.recoveryType) ? |
mcimadamore@1348 | 2173 | fallbackDescriptorType(that) : |
mcimadamore@1348 | 2174 | types.findDescriptorType(target); |
mcimadamore@1348 | 2175 | |
mcimadamore@1348 | 2176 | if (!TreeInfo.isExplicitLambda(that)) { |
mcimadamore@1348 | 2177 | //add param type info in the AST |
mcimadamore@1348 | 2178 | List<Type> actuals = lambdaType.getParameterTypes(); |
mcimadamore@1348 | 2179 | List<JCVariableDecl> params = that.params; |
mcimadamore@1348 | 2180 | |
mcimadamore@1348 | 2181 | boolean arityMismatch = false; |
mcimadamore@1348 | 2182 | |
mcimadamore@1348 | 2183 | while (params.nonEmpty()) { |
mcimadamore@1348 | 2184 | if (actuals.isEmpty()) { |
mcimadamore@1348 | 2185 | //not enough actuals to perform lambda parameter inference |
mcimadamore@1348 | 2186 | arityMismatch = true; |
mcimadamore@1348 | 2187 | } |
mcimadamore@1348 | 2188 | //reset previously set info |
mcimadamore@1348 | 2189 | Type argType = arityMismatch ? |
mcimadamore@1348 | 2190 | syms.errType : |
mcimadamore@1348 | 2191 | actuals.head; |
mcimadamore@1348 | 2192 | params.head.vartype = make.Type(argType); |
mcimadamore@1348 | 2193 | params.head.sym = null; |
mcimadamore@1348 | 2194 | actuals = actuals.isEmpty() ? |
mcimadamore@1348 | 2195 | actuals : |
mcimadamore@1348 | 2196 | actuals.tail; |
mcimadamore@1348 | 2197 | params = params.tail; |
mcimadamore@1348 | 2198 | } |
mcimadamore@1348 | 2199 | |
mcimadamore@1348 | 2200 | //attribute lambda parameters |
mcimadamore@1348 | 2201 | attribStats(that.params, localEnv); |
mcimadamore@1348 | 2202 | |
mcimadamore@1348 | 2203 | if (arityMismatch) { |
mcimadamore@1348 | 2204 | resultInfo.checkContext.report(that, diags.fragment("incompatible.arg.types.in.lambda")); |
mcimadamore@1348 | 2205 | result = that.type = types.createErrorType(target); |
mcimadamore@1348 | 2206 | return; |
mcimadamore@1348 | 2207 | } |
mcimadamore@1348 | 2208 | } |
mcimadamore@1348 | 2209 | |
mcimadamore@1348 | 2210 | //from this point on, no recovery is needed; if we are in assignment context |
mcimadamore@1348 | 2211 | //we will be able to attribute the whole lambda body, regardless of errors; |
mcimadamore@1348 | 2212 | //if we are in a 'check' method context, and the lambda is not compatible |
mcimadamore@1348 | 2213 | //with the target-type, it will be recovered anyway in Attr.checkId |
mcimadamore@1348 | 2214 | needsRecovery = false; |
mcimadamore@1348 | 2215 | |
mcimadamore@1348 | 2216 | ResultInfo bodyResultInfo = lambdaType.getReturnType() == Type.recoveryType ? |
mcimadamore@1348 | 2217 | recoveryInfo : |
mcimadamore@1348 | 2218 | new ResultInfo(VAL, lambdaType.getReturnType(), new LambdaReturnContext(resultInfo.checkContext)); |
mcimadamore@1348 | 2219 | localEnv.info.returnResult = bodyResultInfo; |
mcimadamore@1348 | 2220 | |
mcimadamore@1348 | 2221 | if (that.getBodyKind() == JCLambda.BodyKind.EXPRESSION) { |
mcimadamore@1348 | 2222 | attribTree(that.getBody(), localEnv, bodyResultInfo); |
mcimadamore@1348 | 2223 | } else { |
mcimadamore@1348 | 2224 | JCBlock body = (JCBlock)that.body; |
mcimadamore@1348 | 2225 | attribStats(body.stats, localEnv); |
mcimadamore@1348 | 2226 | } |
mcimadamore@1348 | 2227 | |
mcimadamore@1348 | 2228 | result = check(that, target, VAL, resultInfo); |
mcimadamore@1348 | 2229 | |
mcimadamore@1348 | 2230 | boolean isSpeculativeRound = |
mcimadamore@1348 | 2231 | resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE; |
mcimadamore@1348 | 2232 | |
mcimadamore@1348 | 2233 | postAttr(that); |
mcimadamore@1348 | 2234 | flow.analyzeLambda(env, that, make, isSpeculativeRound); |
mcimadamore@1348 | 2235 | |
mcimadamore@1348 | 2236 | checkLambdaCompatible(that, lambdaType, resultInfo.checkContext, isSpeculativeRound); |
mcimadamore@1348 | 2237 | |
mcimadamore@1348 | 2238 | if (!isSpeculativeRound) { |
mcimadamore@1348 | 2239 | checkAccessibleFunctionalDescriptor(that, localEnv, resultInfo.checkContext.inferenceContext(), lambdaType); |
mcimadamore@1348 | 2240 | } |
mcimadamore@1348 | 2241 | result = check(that, target, VAL, resultInfo); |
mcimadamore@1348 | 2242 | } catch (Types.FunctionDescriptorLookupError ex) { |
mcimadamore@1348 | 2243 | JCDiagnostic cause = ex.getDiagnostic(); |
mcimadamore@1348 | 2244 | resultInfo.checkContext.report(that, cause); |
mcimadamore@1348 | 2245 | result = that.type = types.createErrorType(pt()); |
mcimadamore@1348 | 2246 | return; |
mcimadamore@1348 | 2247 | } finally { |
mcimadamore@1348 | 2248 | localEnv.info.scope.leave(); |
mcimadamore@1348 | 2249 | if (needsRecovery) { |
mcimadamore@1348 | 2250 | attribTree(that, env, recoveryInfo); |
mcimadamore@1348 | 2251 | } |
mcimadamore@1348 | 2252 | } |
mcimadamore@1144 | 2253 | } |
mcimadamore@1348 | 2254 | //where |
mcimadamore@1348 | 2255 | private Type fallbackDescriptorType(JCExpression tree) { |
mcimadamore@1348 | 2256 | switch (tree.getTag()) { |
mcimadamore@1348 | 2257 | case LAMBDA: |
mcimadamore@1348 | 2258 | JCLambda lambda = (JCLambda)tree; |
mcimadamore@1348 | 2259 | List<Type> argtypes = List.nil(); |
mcimadamore@1348 | 2260 | for (JCVariableDecl param : lambda.params) { |
mcimadamore@1348 | 2261 | argtypes = param.vartype != null ? |
mcimadamore@1348 | 2262 | argtypes.append(param.vartype.type) : |
mcimadamore@1348 | 2263 | argtypes.append(syms.errType); |
mcimadamore@1348 | 2264 | } |
mcimadamore@1348 | 2265 | return new MethodType(argtypes, Type.recoveryType, List.<Type>nil(), syms.methodClass); |
mcimadamore@1348 | 2266 | case REFERENCE: |
mcimadamore@1348 | 2267 | return new MethodType(List.<Type>nil(), Type.recoveryType, List.<Type>nil(), syms.methodClass); |
mcimadamore@1348 | 2268 | default: |
mcimadamore@1348 | 2269 | Assert.error("Cannot get here!"); |
mcimadamore@1348 | 2270 | } |
mcimadamore@1348 | 2271 | return null; |
mcimadamore@1348 | 2272 | } |
mcimadamore@1348 | 2273 | |
mcimadamore@1348 | 2274 | private void checkAccessibleFunctionalDescriptor(final DiagnosticPosition pos, |
mcimadamore@1348 | 2275 | final Env<AttrContext> env, final InferenceContext inferenceContext, final Type desc) { |
mcimadamore@1348 | 2276 | if (inferenceContext.free(desc)) { |
mcimadamore@1348 | 2277 | inferenceContext.addFreeTypeListener(List.of(desc), new FreeTypeListener() { |
mcimadamore@1348 | 2278 | @Override |
mcimadamore@1348 | 2279 | public void typesInferred(InferenceContext inferenceContext) { |
mcimadamore@1348 | 2280 | checkAccessibleFunctionalDescriptor(pos, env, inferenceContext, inferenceContext.asInstType(desc, types)); |
mcimadamore@1348 | 2281 | } |
mcimadamore@1348 | 2282 | }); |
mcimadamore@1348 | 2283 | } else { |
mcimadamore@1348 | 2284 | chk.checkAccessibleFunctionalDescriptor(pos, env, desc); |
mcimadamore@1348 | 2285 | } |
mcimadamore@1348 | 2286 | } |
mcimadamore@1348 | 2287 | |
mcimadamore@1348 | 2288 | /** |
mcimadamore@1348 | 2289 | * Lambda/method reference have a special check context that ensures |
mcimadamore@1348 | 2290 | * that i.e. a lambda return type is compatible with the expected |
mcimadamore@1348 | 2291 | * type according to both the inherited context and the assignment |
mcimadamore@1348 | 2292 | * context. |
mcimadamore@1348 | 2293 | */ |
mcimadamore@1348 | 2294 | class LambdaReturnContext extends Check.NestedCheckContext { |
mcimadamore@1348 | 2295 | public LambdaReturnContext(CheckContext enclosingContext) { |
mcimadamore@1348 | 2296 | super(enclosingContext); |
mcimadamore@1348 | 2297 | } |
mcimadamore@1348 | 2298 | |
mcimadamore@1348 | 2299 | @Override |
mcimadamore@1348 | 2300 | public boolean compatible(Type found, Type req, Warner warn) { |
mcimadamore@1348 | 2301 | //return type must be compatible in both current context and assignment context |
mcimadamore@1348 | 2302 | return types.isAssignable(found, inferenceContext().asFree(req, types), warn) && |
mcimadamore@1348 | 2303 | super.compatible(found, req, warn); |
mcimadamore@1348 | 2304 | } |
mcimadamore@1348 | 2305 | @Override |
mcimadamore@1348 | 2306 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
mcimadamore@1348 | 2307 | enclosingContext.report(pos, diags.fragment("incompatible.ret.type.in.lambda", details)); |
mcimadamore@1348 | 2308 | } |
mcimadamore@1348 | 2309 | } |
mcimadamore@1348 | 2310 | |
mcimadamore@1348 | 2311 | /** |
mcimadamore@1348 | 2312 | * Lambda compatibility. Check that given return types, thrown types, parameter types |
mcimadamore@1348 | 2313 | * are compatible with the expected functional interface descriptor. This means that: |
mcimadamore@1348 | 2314 | * (i) parameter types must be identical to those of the target descriptor; (ii) return |
mcimadamore@1348 | 2315 | * types must be compatible with the return type of the expected descriptor; |
mcimadamore@1348 | 2316 | * (iii) thrown types must be 'included' in the thrown types list of the expected |
mcimadamore@1348 | 2317 | * descriptor. |
mcimadamore@1348 | 2318 | */ |
mcimadamore@1348 | 2319 | private void checkLambdaCompatible(JCLambda tree, Type descriptor, CheckContext checkContext, boolean speculativeAttr) { |
mcimadamore@1348 | 2320 | Type returnType = checkContext.inferenceContext().asFree(descriptor.getReturnType(), types); |
mcimadamore@1348 | 2321 | |
mcimadamore@1348 | 2322 | //return values have already been checked - but if lambda has no return |
mcimadamore@1348 | 2323 | //values, we must ensure that void/value compatibility is correct; |
mcimadamore@1348 | 2324 | //this amounts at checking that, if a lambda body can complete normally, |
mcimadamore@1348 | 2325 | //the descriptor's return type must be void |
mcimadamore@1348 | 2326 | if (tree.getBodyKind() == JCLambda.BodyKind.STATEMENT && tree.canCompleteNormally && |
mcimadamore@1348 | 2327 | returnType.tag != VOID && returnType != Type.recoveryType) { |
mcimadamore@1348 | 2328 | checkContext.report(tree, diags.fragment("incompatible.ret.type.in.lambda", |
mcimadamore@1348 | 2329 | diags.fragment("missing.ret.val", returnType))); |
mcimadamore@1348 | 2330 | } |
mcimadamore@1348 | 2331 | |
mcimadamore@1348 | 2332 | List<Type> argTypes = checkContext.inferenceContext().asFree(descriptor.getParameterTypes(), types); |
mcimadamore@1348 | 2333 | if (!types.isSameTypes(argTypes, TreeInfo.types(tree.params))) { |
mcimadamore@1348 | 2334 | checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda")); |
mcimadamore@1348 | 2335 | } |
mcimadamore@1348 | 2336 | |
mcimadamore@1348 | 2337 | if (!speculativeAttr) { |
mcimadamore@1348 | 2338 | List<Type> thrownTypes = checkContext.inferenceContext().asFree(descriptor.getThrownTypes(), types); |
mcimadamore@1348 | 2339 | if (chk.unhandled(tree.inferredThrownTypes == null ? List.<Type>nil() : tree.inferredThrownTypes, thrownTypes).nonEmpty()) { |
mcimadamore@1348 | 2340 | log.error(tree, "incompatible.thrown.types.in.lambda", tree.inferredThrownTypes); |
mcimadamore@1348 | 2341 | } |
mcimadamore@1348 | 2342 | } |
mcimadamore@1348 | 2343 | } |
mcimadamore@1348 | 2344 | |
mcimadamore@1348 | 2345 | private Env<AttrContext> lambdaEnv(JCLambda that, Env<AttrContext> env) { |
mcimadamore@1348 | 2346 | Env<AttrContext> lambdaEnv; |
mcimadamore@1348 | 2347 | Symbol owner = env.info.scope.owner; |
mcimadamore@1348 | 2348 | if (owner.kind == VAR && owner.owner.kind == TYP) { |
mcimadamore@1348 | 2349 | //field initializer |
mcimadamore@1348 | 2350 | lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dupUnshared())); |
mcimadamore@1348 | 2351 | lambdaEnv.info.scope.owner = |
mcimadamore@1348 | 2352 | new MethodSymbol(0, names.empty, null, |
mcimadamore@1348 | 2353 | env.info.scope.owner); |
mcimadamore@1348 | 2354 | } else { |
mcimadamore@1348 | 2355 | lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dup())); |
mcimadamore@1348 | 2356 | } |
mcimadamore@1348 | 2357 | return lambdaEnv; |
mcimadamore@1348 | 2358 | } |
mcimadamore@1145 | 2359 | |
duke@1 | 2360 | public void visitParens(JCParens tree) { |
mcimadamore@1220 | 2361 | Type owntype = attribTree(tree.expr, env, resultInfo); |
mcimadamore@1220 | 2362 | result = check(tree, owntype, pkind(), resultInfo); |
duke@1 | 2363 | Symbol sym = TreeInfo.symbol(tree); |
duke@1 | 2364 | if (sym != null && (sym.kind&(TYP|PCK)) != 0) |
duke@1 | 2365 | log.error(tree.pos(), "illegal.start.of.type"); |
duke@1 | 2366 | } |
duke@1 | 2367 | |
duke@1 | 2368 | public void visitAssign(JCAssign tree) { |
mcimadamore@1220 | 2369 | Type owntype = attribTree(tree.lhs, env.dup(tree), varInfo); |
duke@1 | 2370 | Type capturedType = capture(owntype); |
duke@1 | 2371 | attribExpr(tree.rhs, env, owntype); |
mcimadamore@1220 | 2372 | result = check(tree, capturedType, VAL, resultInfo); |
duke@1 | 2373 | } |
duke@1 | 2374 | |
duke@1 | 2375 | public void visitAssignop(JCAssignOp tree) { |
duke@1 | 2376 | // Attribute arguments. |
mcimadamore@1220 | 2377 | Type owntype = attribTree(tree.lhs, env, varInfo); |
duke@1 | 2378 | Type operand = attribExpr(tree.rhs, env); |
duke@1 | 2379 | // Find operator. |
duke@1 | 2380 | Symbol operator = tree.operator = rs.resolveBinaryOperator( |
jjg@1127 | 2381 | tree.pos(), tree.getTag().noAssignOp(), env, |
duke@1 | 2382 | owntype, operand); |
duke@1 | 2383 | |
mcimadamore@853 | 2384 | if (operator.kind == MTH && |
mcimadamore@853 | 2385 | !owntype.isErroneous() && |
mcimadamore@853 | 2386 | !operand.isErroneous()) { |
duke@1 | 2387 | chk.checkOperator(tree.pos(), |
duke@1 | 2388 | (OperatorSymbol)operator, |
jjg@1127 | 2389 | tree.getTag().noAssignOp(), |
duke@1 | 2390 | owntype, |
duke@1 | 2391 | operand); |
jjg@9 | 2392 | chk.checkDivZero(tree.rhs.pos(), operator, operand); |
jjg@9 | 2393 | chk.checkCastable(tree.rhs.pos(), |
jjg@9 | 2394 | operator.type.getReturnType(), |
jjg@9 | 2395 | owntype); |
duke@1 | 2396 | } |
mcimadamore@1220 | 2397 | result = check(tree, owntype, VAL, resultInfo); |
duke@1 | 2398 | } |
duke@1 | 2399 | |
duke@1 | 2400 | public void visitUnary(JCUnary tree) { |
duke@1 | 2401 | // Attribute arguments. |
jjg@1127 | 2402 | Type argtype = (tree.getTag().isIncOrDecUnaryOp()) |
mcimadamore@1220 | 2403 | ? attribTree(tree.arg, env, varInfo) |
duke@1 | 2404 | : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env)); |
duke@1 | 2405 | |
duke@1 | 2406 | // Find operator. |
duke@1 | 2407 | Symbol operator = tree.operator = |
duke@1 | 2408 | rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype); |
duke@1 | 2409 | |
jjg@110 | 2410 | Type owntype = types.createErrorType(tree.type); |
mcimadamore@853 | 2411 | if (operator.kind == MTH && |
mcimadamore@853 | 2412 | !argtype.isErroneous()) { |
jjg@1127 | 2413 | owntype = (tree.getTag().isIncOrDecUnaryOp()) |
duke@1 | 2414 | ? tree.arg.type |
duke@1 | 2415 | : operator.type.getReturnType(); |
duke@1 | 2416 | int opc = ((OperatorSymbol)operator).opcode; |
duke@1 | 2417 | |
duke@1 | 2418 | // If the argument is constant, fold it. |
duke@1 | 2419 | if (argtype.constValue() != null) { |
duke@1 | 2420 | Type ctype = cfolder.fold1(opc, argtype); |
duke@1 | 2421 | if (ctype != null) { |
duke@1 | 2422 | owntype = cfolder.coerce(ctype, owntype); |
duke@1 | 2423 | |
duke@1 | 2424 | // Remove constant types from arguments to |
duke@1 | 2425 | // conserve space. The parser will fold concatenations |
duke@1 | 2426 | // of string literals; the code here also |
duke@1 | 2427 | // gets rid of intermediate results when some of the |
duke@1 | 2428 | // operands are constant identifiers. |
duke@1 | 2429 | if (tree.arg.type.tsym == syms.stringType.tsym) { |
duke@1 | 2430 | tree.arg.type = syms.stringType; |
duke@1 | 2431 | } |
duke@1 | 2432 | } |
duke@1 | 2433 | } |
duke@1 | 2434 | } |
mcimadamore@1220 | 2435 | result = check(tree, owntype, VAL, resultInfo); |
duke@1 | 2436 | } |
duke@1 | 2437 | |
duke@1 | 2438 | public void visitBinary(JCBinary tree) { |
duke@1 | 2439 | // Attribute arguments. |
duke@1 | 2440 | Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env)); |
duke@1 | 2441 | Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env)); |
duke@1 | 2442 | |
duke@1 | 2443 | // Find operator. |
duke@1 | 2444 | Symbol operator = tree.operator = |
duke@1 | 2445 | rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right); |
duke@1 | 2446 | |
jjg@110 | 2447 | Type owntype = types.createErrorType(tree.type); |
mcimadamore@853 | 2448 | if (operator.kind == MTH && |
mcimadamore@853 | 2449 | !left.isErroneous() && |
mcimadamore@853 | 2450 | !right.isErroneous()) { |
duke@1 | 2451 | owntype = operator.type.getReturnType(); |
duke@1 | 2452 | int opc = chk.checkOperator(tree.lhs.pos(), |
duke@1 | 2453 | (OperatorSymbol)operator, |
duke@1 | 2454 | tree.getTag(), |
duke@1 | 2455 | left, |
duke@1 | 2456 | right); |
duke@1 | 2457 | |
duke@1 | 2458 | // If both arguments are constants, fold them. |
duke@1 | 2459 | if (left.constValue() != null && right.constValue() != null) { |
duke@1 | 2460 | Type ctype = cfolder.fold2(opc, left, right); |
duke@1 | 2461 | if (ctype != null) { |
duke@1 | 2462 | owntype = cfolder.coerce(ctype, owntype); |
duke@1 | 2463 | |
duke@1 | 2464 | // Remove constant types from arguments to |
duke@1 | 2465 | // conserve space. The parser will fold concatenations |
duke@1 | 2466 | // of string literals; the code here also |
duke@1 | 2467 | // gets rid of intermediate results when some of the |
duke@1 | 2468 | // operands are constant identifiers. |
duke@1 | 2469 | if (tree.lhs.type.tsym == syms.stringType.tsym) { |
duke@1 | 2470 | tree.lhs.type = syms.stringType; |
duke@1 | 2471 | } |
duke@1 | 2472 | if (tree.rhs.type.tsym == syms.stringType.tsym) { |
duke@1 | 2473 | tree.rhs.type = syms.stringType; |
duke@1 | 2474 | } |
duke@1 | 2475 | } |
duke@1 | 2476 | } |
duke@1 | 2477 | |
duke@1 | 2478 | // Check that argument types of a reference ==, != are |
duke@1 | 2479 | // castable to each other, (JLS???). |
duke@1 | 2480 | if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) { |
duke@1 | 2481 | if (!types.isCastable(left, right, new Warner(tree.pos()))) { |
duke@1 | 2482 | log.error(tree.pos(), "incomparable.types", left, right); |
duke@1 | 2483 | } |
duke@1 | 2484 | } |
duke@1 | 2485 | |
duke@1 | 2486 | chk.checkDivZero(tree.rhs.pos(), operator, right); |
duke@1 | 2487 | } |
mcimadamore@1220 | 2488 | result = check(tree, owntype, VAL, resultInfo); |
duke@1 | 2489 | } |
duke@1 | 2490 | |
mcimadamore@1347 | 2491 | public void visitTypeCast(final JCTypeCast tree) { |
duke@1 | 2492 | Type clazztype = attribType(tree.clazz, env); |
mcimadamore@638 | 2493 | chk.validate(tree.clazz, env, false); |
mcimadamore@674 | 2494 | //a fresh environment is required for 292 inference to work properly --- |
mcimadamore@674 | 2495 | //see Infer.instantiatePolymorphicSignatureInstance() |
mcimadamore@674 | 2496 | Env<AttrContext> localEnv = env.dup(tree); |
mcimadamore@1347 | 2497 | //should we propagate the target type? |
mcimadamore@1347 | 2498 | final ResultInfo castInfo; |
mcimadamore@1347 | 2499 | final boolean isPoly = TreeInfo.isPoly(tree.expr, tree); |
mcimadamore@1347 | 2500 | if (isPoly) { |
mcimadamore@1347 | 2501 | //expression is a poly - we need to propagate target type info |
mcimadamore@1347 | 2502 | castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) { |
mcimadamore@1347 | 2503 | @Override |
mcimadamore@1347 | 2504 | public boolean compatible(Type found, Type req, Warner warn) { |
mcimadamore@1347 | 2505 | return types.isCastable(found, req, warn); |
mcimadamore@1347 | 2506 | } |
mcimadamore@1347 | 2507 | }); |
mcimadamore@1347 | 2508 | } else { |
mcimadamore@1347 | 2509 | //standalone cast - target-type info is not propagated |
mcimadamore@1347 | 2510 | castInfo = unknownExprInfo; |
mcimadamore@1347 | 2511 | } |
mcimadamore@1347 | 2512 | Type exprtype = attribTree(tree.expr, localEnv, castInfo); |
mcimadamore@1347 | 2513 | Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
duke@1 | 2514 | if (exprtype.constValue() != null) |
duke@1 | 2515 | owntype = cfolder.coerce(exprtype, owntype); |
mcimadamore@1220 | 2516 | result = check(tree, capture(owntype), VAL, resultInfo); |
mcimadamore@1347 | 2517 | if (!isPoly) |
mcimadamore@1347 | 2518 | chk.checkRedundantCast(localEnv, tree); |
duke@1 | 2519 | } |
duke@1 | 2520 | |
duke@1 | 2521 | public void visitTypeTest(JCInstanceOf tree) { |
duke@1 | 2522 | Type exprtype = chk.checkNullOrRefType( |
duke@1 | 2523 | tree.expr.pos(), attribExpr(tree.expr, env)); |
duke@1 | 2524 | Type clazztype = chk.checkReifiableReferenceType( |
duke@1 | 2525 | tree.clazz.pos(), attribType(tree.clazz, env)); |
mcimadamore@638 | 2526 | chk.validate(tree.clazz, env, false); |
duke@1 | 2527 | chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
mcimadamore@1220 | 2528 | result = check(tree, syms.booleanType, VAL, resultInfo); |
duke@1 | 2529 | } |
duke@1 | 2530 | |
duke@1 | 2531 | public void visitIndexed(JCArrayAccess tree) { |
jjg@110 | 2532 | Type owntype = types.createErrorType(tree.type); |
duke@1 | 2533 | Type atype = attribExpr(tree.indexed, env); |
duke@1 | 2534 | attribExpr(tree.index, env, syms.intType); |
duke@1 | 2535 | if (types.isArray(atype)) |
duke@1 | 2536 | owntype = types.elemtype(atype); |
duke@1 | 2537 | else if (atype.tag != ERROR) |
duke@1 | 2538 | log.error(tree.pos(), "array.req.but.found", atype); |
mcimadamore@1220 | 2539 | if ((pkind() & VAR) == 0) owntype = capture(owntype); |
mcimadamore@1220 | 2540 | result = check(tree, owntype, VAR, resultInfo); |
duke@1 | 2541 | } |
duke@1 | 2542 | |
duke@1 | 2543 | public void visitIdent(JCIdent tree) { |
duke@1 | 2544 | Symbol sym; |
duke@1 | 2545 | |
duke@1 | 2546 | // Find symbol |
mcimadamore@1220 | 2547 | if (pt().tag == METHOD || pt().tag == FORALL) { |
duke@1 | 2548 | // If we are looking for a method, the prototype `pt' will be a |
duke@1 | 2549 | // method type with the type of the call's arguments as parameters. |
mcimadamore@1347 | 2550 | env.info.pendingResolutionPhase = null; |
mcimadamore@1220 | 2551 | sym = rs.resolveMethod(tree.pos(), env, tree.name, pt().getParameterTypes(), pt().getTypeArguments()); |
duke@1 | 2552 | } else if (tree.sym != null && tree.sym.kind != VAR) { |
duke@1 | 2553 | sym = tree.sym; |
duke@1 | 2554 | } else { |
mcimadamore@1220 | 2555 | sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind()); |
duke@1 | 2556 | } |
duke@1 | 2557 | tree.sym = sym; |
duke@1 | 2558 | |
duke@1 | 2559 | // (1) Also find the environment current for the class where |
duke@1 | 2560 | // sym is defined (`symEnv'). |
duke@1 | 2561 | // Only for pre-tiger versions (1.4 and earlier): |
duke@1 | 2562 | // (2) Also determine whether we access symbol out of an anonymous |
duke@1 | 2563 | // class in a this or super call. This is illegal for instance |
duke@1 | 2564 | // members since such classes don't carry a this$n link. |
duke@1 | 2565 | // (`noOuterThisPath'). |
duke@1 | 2566 | Env<AttrContext> symEnv = env; |
duke@1 | 2567 | boolean noOuterThisPath = false; |
duke@1 | 2568 | if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class |
duke@1 | 2569 | (sym.kind & (VAR | MTH | TYP)) != 0 && |
duke@1 | 2570 | sym.owner.kind == TYP && |
duke@1 | 2571 | tree.name != names._this && tree.name != names._super) { |
duke@1 | 2572 | |
duke@1 | 2573 | // Find environment in which identifier is defined. |
duke@1 | 2574 | while (symEnv.outer != null && |
duke@1 | 2575 | !sym.isMemberOf(symEnv.enclClass.sym, types)) { |
duke@1 | 2576 | if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0) |
duke@1 | 2577 | noOuterThisPath = !allowAnonOuterThis; |
duke@1 | 2578 | symEnv = symEnv.outer; |
duke@1 | 2579 | } |
duke@1 | 2580 | } |
duke@1 | 2581 | |
duke@1 | 2582 | // If symbol is a variable, ... |
duke@1 | 2583 | if (sym.kind == VAR) { |
duke@1 | 2584 | VarSymbol v = (VarSymbol)sym; |
duke@1 | 2585 | |
duke@1 | 2586 | // ..., evaluate its initializer, if it has one, and check for |
duke@1 | 2587 | // illegal forward reference. |
duke@1 | 2588 | checkInit(tree, env, v, false); |
duke@1 | 2589 | |
duke@1 | 2590 | // If we are expecting a variable (as opposed to a value), check |
duke@1 | 2591 | // that the variable is assignable in the current environment. |
mcimadamore@1220 | 2592 | if (pkind() == VAR) |
duke@1 | 2593 | checkAssignable(tree.pos(), v, null, env); |
duke@1 | 2594 | } |
duke@1 | 2595 | |
duke@1 | 2596 | // In a constructor body, |
duke@1 | 2597 | // if symbol is a field or instance method, check that it is |
duke@1 | 2598 | // not accessed before the supertype constructor is called. |
duke@1 | 2599 | if ((symEnv.info.isSelfCall || noOuterThisPath) && |
duke@1 | 2600 | (sym.kind & (VAR | MTH)) != 0 && |
duke@1 | 2601 | sym.owner.kind == TYP && |
duke@1 | 2602 | (sym.flags() & STATIC) == 0) { |
duke@1 | 2603 | chk.earlyRefError(tree.pos(), sym.kind == VAR ? sym : thisSym(tree.pos(), env)); |
duke@1 | 2604 | } |
duke@1 | 2605 | Env<AttrContext> env1 = env; |
mcimadamore@28 | 2606 | if (sym.kind != ERR && sym.kind != TYP && sym.owner != null && sym.owner != env1.enclClass.sym) { |
duke@1 | 2607 | // If the found symbol is inaccessible, then it is |
duke@1 | 2608 | // accessed through an enclosing instance. Locate this |
duke@1 | 2609 | // enclosing instance: |
duke@1 | 2610 | while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym)) |
duke@1 | 2611 | env1 = env1.outer; |
duke@1 | 2612 | } |
mcimadamore@1347 | 2613 | result = checkId(tree, env1.enclClass.sym.type, sym, env, resultInfo); |
duke@1 | 2614 | } |
duke@1 | 2615 | |
duke@1 | 2616 | public void visitSelect(JCFieldAccess tree) { |
duke@1 | 2617 | // Determine the expected kind of the qualifier expression. |
duke@1 | 2618 | int skind = 0; |
duke@1 | 2619 | if (tree.name == names._this || tree.name == names._super || |
duke@1 | 2620 | tree.name == names._class) |
duke@1 | 2621 | { |
duke@1 | 2622 | skind = TYP; |
duke@1 | 2623 | } else { |
mcimadamore@1220 | 2624 | if ((pkind() & PCK) != 0) skind = skind | PCK; |
mcimadamore@1220 | 2625 | if ((pkind() & TYP) != 0) skind = skind | TYP | PCK; |
mcimadamore@1220 | 2626 | if ((pkind() & (VAL | MTH)) != 0) skind = skind | VAL | TYP; |
duke@1 | 2627 | } |
duke@1 | 2628 | |
duke@1 | 2629 | // Attribute the qualifier expression, and determine its symbol (if any). |
mcimadamore@1220 | 2630 | Type site = attribTree(tree.selected, env, new ResultInfo(skind, Infer.anyPoly)); |
mcimadamore@1220 | 2631 | if ((pkind() & (PCK | TYP)) == 0) |
duke@1 | 2632 | site = capture(site); // Capture field access |
duke@1 | 2633 | |
duke@1 | 2634 | // don't allow T.class T[].class, etc |
duke@1 | 2635 | if (skind == TYP) { |
duke@1 | 2636 | Type elt = site; |
duke@1 | 2637 | while (elt.tag == ARRAY) |
duke@1 | 2638 | elt = ((ArrayType)elt).elemtype; |
duke@1 | 2639 | if (elt.tag == TYPEVAR) { |
duke@1 | 2640 | log.error(tree.pos(), "type.var.cant.be.deref"); |
jjg@110 | 2641 | result = types.createErrorType(tree.type); |
duke@1 | 2642 | return; |
duke@1 | 2643 | } |
duke@1 | 2644 | } |
duke@1 | 2645 | |
duke@1 | 2646 | // If qualifier symbol is a type or `super', assert `selectSuper' |
duke@1 | 2647 | // for the selection. This is relevant for determining whether |
duke@1 | 2648 | // protected symbols are accessible. |
duke@1 | 2649 | Symbol sitesym = TreeInfo.symbol(tree.selected); |
duke@1 | 2650 | boolean selectSuperPrev = env.info.selectSuper; |
duke@1 | 2651 | env.info.selectSuper = |
duke@1 | 2652 | sitesym != null && |
duke@1 | 2653 | sitesym.name == names._super; |
duke@1 | 2654 | |
duke@1 | 2655 | // Determine the symbol represented by the selection. |
mcimadamore@1347 | 2656 | env.info.pendingResolutionPhase = null; |
mcimadamore@1220 | 2657 | Symbol sym = selectSym(tree, sitesym, site, env, resultInfo); |
mcimadamore@1220 | 2658 | if (sym.exists() && !isType(sym) && (pkind() & (PCK | TYP)) != 0) { |
duke@1 | 2659 | site = capture(site); |
mcimadamore@1220 | 2660 | sym = selectSym(tree, sitesym, site, env, resultInfo); |
duke@1 | 2661 | } |
mcimadamore@1347 | 2662 | boolean varArgs = env.info.lastResolveVarargs(); |
duke@1 | 2663 | tree.sym = sym; |
duke@1 | 2664 | |
mcimadamore@27 | 2665 | if (site.tag == TYPEVAR && !isType(sym) && sym.kind != ERR) { |
mcimadamore@27 | 2666 | while (site.tag == TYPEVAR) site = site.getUpperBound(); |
mcimadamore@27 | 2667 | site = capture(site); |
mcimadamore@27 | 2668 | } |
duke@1 | 2669 | |
duke@1 | 2670 | // If that symbol is a variable, ... |
duke@1 | 2671 | if (sym.kind == VAR) { |
duke@1 | 2672 | VarSymbol v = (VarSymbol)sym; |
duke@1 | 2673 | |
duke@1 | 2674 | // ..., evaluate its initializer, if it has one, and check for |
duke@1 | 2675 | // illegal forward reference. |
duke@1 | 2676 | checkInit(tree, env, v, true); |
duke@1 | 2677 | |
duke@1 | 2678 | // If we are expecting a variable (as opposed to a value), check |
duke@1 | 2679 | // that the variable is assignable in the current environment. |
mcimadamore@1220 | 2680 | if (pkind() == VAR) |
duke@1 | 2681 | checkAssignable(tree.pos(), v, tree.selected, env); |
duke@1 | 2682 | } |
duke@1 | 2683 | |
darcy@609 | 2684 | if (sitesym != null && |
darcy@609 | 2685 | sitesym.kind == VAR && |
darcy@609 | 2686 | ((VarSymbol)sitesym).isResourceVariable() && |
darcy@609 | 2687 | sym.kind == MTH && |
mcimadamore@954 | 2688 | sym.name.equals(names.close) && |
darcy@609 | 2689 | sym.overrides(syms.autoCloseableClose, sitesym.type.tsym, types, true) && |
mcimadamore@795 | 2690 | env.info.lint.isEnabled(LintCategory.TRY)) { |
mcimadamore@795 | 2691 | log.warning(LintCategory.TRY, tree, "try.explicit.close.call"); |
darcy@609 | 2692 | } |
darcy@609 | 2693 | |
duke@1 | 2694 | // Disallow selecting a type from an expression |
duke@1 | 2695 | if (isType(sym) && (sitesym==null || (sitesym.kind&(TYP|PCK)) == 0)) { |
mcimadamore@1220 | 2696 | tree.type = check(tree.selected, pt(), |
mcimadamore@1220 | 2697 | sitesym == null ? VAL : sitesym.kind, new ResultInfo(TYP|PCK, pt())); |
duke@1 | 2698 | } |
duke@1 | 2699 | |
duke@1 | 2700 | if (isType(sitesym)) { |
duke@1 | 2701 | if (sym.name == names._this) { |
duke@1 | 2702 | // If `C' is the currently compiled class, check that |
duke@1 | 2703 | // C.this' does not appear in a call to a super(...) |
duke@1 | 2704 | if (env.info.isSelfCall && |
duke@1 | 2705 | site.tsym == env.enclClass.sym) { |
duke@1 | 2706 | chk.earlyRefError(tree.pos(), sym); |
duke@1 | 2707 | } |
duke@1 | 2708 | } else { |
duke@1 | 2709 | // Check if type-qualified fields or methods are static (JLS) |
duke@1 | 2710 | if ((sym.flags() & STATIC) == 0 && |
duke@1 | 2711 | sym.name != names._super && |
duke@1 | 2712 | (sym.kind == VAR || sym.kind == MTH)) { |
mcimadamore@1347 | 2713 | rs.accessBase(rs.new StaticError(sym), |
duke@1 | 2714 | tree.pos(), site, sym.name, true); |
duke@1 | 2715 | } |
duke@1 | 2716 | } |
jjg@505 | 2717 | } else if (sym.kind != ERR && (sym.flags() & STATIC) != 0 && sym.name != names._class) { |
jjg@505 | 2718 | // If the qualified item is not a type and the selected item is static, report |
jjg@505 | 2719 | // a warning. Make allowance for the class of an array type e.g. Object[].class) |
jjg@505 | 2720 | chk.warnStatic(tree, "static.not.qualified.by.type", Kinds.kindName(sym.kind), sym.owner); |
duke@1 | 2721 | } |
duke@1 | 2722 | |
duke@1 | 2723 | // If we are selecting an instance member via a `super', ... |
duke@1 | 2724 | if (env.info.selectSuper && (sym.flags() & STATIC) == 0) { |
duke@1 | 2725 | |
duke@1 | 2726 | // Check that super-qualified symbols are not abstract (JLS) |
duke@1 | 2727 | rs.checkNonAbstract(tree.pos(), sym); |
duke@1 | 2728 | |
duke@1 | 2729 | if (site.isRaw()) { |
duke@1 | 2730 | // Determine argument types for site. |
duke@1 | 2731 | Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym); |
duke@1 | 2732 | if (site1 != null) site = site1; |
duke@1 | 2733 | } |
duke@1 | 2734 | } |
duke@1 | 2735 | |
duke@1 | 2736 | env.info.selectSuper = selectSuperPrev; |
mcimadamore@1347 | 2737 | result = checkId(tree, site, sym, env, resultInfo); |
duke@1 | 2738 | } |
duke@1 | 2739 | //where |
duke@1 | 2740 | /** Determine symbol referenced by a Select expression, |
duke@1 | 2741 | * |
duke@1 | 2742 | * @param tree The select tree. |
duke@1 | 2743 | * @param site The type of the selected expression, |
duke@1 | 2744 | * @param env The current environment. |
mcimadamore@1220 | 2745 | * @param resultInfo The current result. |
duke@1 | 2746 | */ |
duke@1 | 2747 | private Symbol selectSym(JCFieldAccess tree, |
mcimadamore@829 | 2748 | Symbol location, |
duke@1 | 2749 | Type site, |
duke@1 | 2750 | Env<AttrContext> env, |
mcimadamore@1220 | 2751 | ResultInfo resultInfo) { |
duke@1 | 2752 | DiagnosticPosition pos = tree.pos(); |
duke@1 | 2753 | Name name = tree.name; |
duke@1 | 2754 | switch (site.tag) { |
duke@1 | 2755 | case PACKAGE: |
mcimadamore@1347 | 2756 | return rs.accessBase( |
mcimadamore@1220 | 2757 | rs.findIdentInPackage(env, site.tsym, name, resultInfo.pkind), |
mcimadamore@829 | 2758 | pos, location, site, name, true); |
duke@1 | 2759 | case ARRAY: |
duke@1 | 2760 | case CLASS: |
mcimadamore@1220 | 2761 | if (resultInfo.pt.tag == METHOD || resultInfo.pt.tag == FORALL) { |
duke@1 | 2762 | return rs.resolveQualifiedMethod( |
mcimadamore@1220 | 2763 | pos, env, location, site, name, resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments()); |
duke@1 | 2764 | } else if (name == names._this || name == names._super) { |
duke@1 | 2765 | return rs.resolveSelf(pos, env, site.tsym, name); |
duke@1 | 2766 | } else if (name == names._class) { |
duke@1 | 2767 | // In this case, we have already made sure in |
duke@1 | 2768 | // visitSelect that qualifier expression is a type. |
duke@1 | 2769 | Type t = syms.classType; |
duke@1 | 2770 | List<Type> typeargs = allowGenerics |
duke@1 | 2771 | ? List.of(types.erasure(site)) |
duke@1 | 2772 | : List.<Type>nil(); |
duke@1 | 2773 | t = new ClassType(t.getEnclosingType(), typeargs, t.tsym); |
duke@1 | 2774 | return new VarSymbol( |
duke@1 | 2775 | STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
duke@1 | 2776 | } else { |
duke@1 | 2777 | // We are seeing a plain identifier as selector. |
mcimadamore@1220 | 2778 | Symbol sym = rs.findIdentInType(env, site, name, resultInfo.pkind); |
mcimadamore@1220 | 2779 | if ((resultInfo.pkind & ERRONEOUS) == 0) |
mcimadamore@1347 | 2780 | sym = rs.accessBase(sym, pos, location, site, name, true); |
duke@1 | 2781 | return sym; |
duke@1 | 2782 | } |
duke@1 | 2783 | case WILDCARD: |
duke@1 | 2784 | throw new AssertionError(tree); |
duke@1 | 2785 | case TYPEVAR: |
duke@1 | 2786 | // Normally, site.getUpperBound() shouldn't be null. |
duke@1 | 2787 | // It should only happen during memberEnter/attribBase |
mcimadamore@829 | 2788 | // when determining the super type which *must* beac |
duke@1 | 2789 | // done before attributing the type variables. In |
duke@1 | 2790 | // other words, we are seeing this illegal program: |
duke@1 | 2791 | // class B<T> extends A<T.foo> {} |
duke@1 | 2792 | Symbol sym = (site.getUpperBound() != null) |
mcimadamore@1220 | 2793 | ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo) |
duke@1 | 2794 | : null; |
mcimadamore@361 | 2795 | if (sym == null) { |
duke@1 | 2796 | log.error(pos, "type.var.cant.be.deref"); |
duke@1 | 2797 | return syms.errSymbol; |
duke@1 | 2798 | } else { |
mcimadamore@155 | 2799 | Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ? |
mcimadamore@155 | 2800 | rs.new AccessError(env, site, sym) : |
mcimadamore@155 | 2801 | sym; |
mcimadamore@1347 | 2802 | rs.accessBase(sym2, pos, location, site, name, true); |
duke@1 | 2803 | return sym; |
duke@1 | 2804 | } |
duke@1 | 2805 | case ERROR: |
duke@1 | 2806 | // preserve identifier names through errors |
jjg@110 | 2807 | return types.createErrorType(name, site.tsym, site).tsym; |
duke@1 | 2808 | default: |
duke@1 | 2809 | // The qualifier expression is of a primitive type -- only |
duke@1 | 2810 | // .class is allowed for these. |
duke@1 | 2811 | if (name == names._class) { |
duke@1 | 2812 | // In this case, we have already made sure in Select that |
duke@1 | 2813 | // qualifier expression is a type. |
duke@1 | 2814 | Type t = syms.classType; |
duke@1 | 2815 | Type arg = types.boxedClass(site).type; |
duke@1 | 2816 | t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym); |
duke@1 | 2817 | return new VarSymbol( |
duke@1 | 2818 | STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
duke@1 | 2819 | } else { |
duke@1 | 2820 | log.error(pos, "cant.deref", site); |
duke@1 | 2821 | return syms.errSymbol; |
duke@1 | 2822 | } |
duke@1 | 2823 | } |
duke@1 | 2824 | } |
duke@1 | 2825 | |
duke@1 | 2826 | /** Determine type of identifier or select expression and check that |
duke@1 | 2827 | * (1) the referenced symbol is not deprecated |
duke@1 | 2828 | * (2) the symbol's type is safe (@see checkSafe) |
duke@1 | 2829 | * (3) if symbol is a variable, check that its type and kind are |
duke@1 | 2830 | * compatible with the prototype and protokind. |
duke@1 | 2831 | * (4) if symbol is an instance field of a raw type, |
duke@1 | 2832 | * which is being assigned to, issue an unchecked warning if its |
duke@1 | 2833 | * type changes under erasure. |
duke@1 | 2834 | * (5) if symbol is an instance method of a raw type, issue an |
duke@1 | 2835 | * unchecked warning if its argument types change under erasure. |
duke@1 | 2836 | * If checks succeed: |
duke@1 | 2837 | * If symbol is a constant, return its constant type |
duke@1 | 2838 | * else if symbol is a method, return its result type |
duke@1 | 2839 | * otherwise return its type. |
duke@1 | 2840 | * Otherwise return errType. |
duke@1 | 2841 | * |
duke@1 | 2842 | * @param tree The syntax tree representing the identifier |
duke@1 | 2843 | * @param site If this is a select, the type of the selected |
duke@1 | 2844 | * expression, otherwise the type of the current class. |
duke@1 | 2845 | * @param sym The symbol representing the identifier. |
duke@1 | 2846 | * @param env The current environment. |
mcimadamore@1220 | 2847 | * @param resultInfo The expected result |
duke@1 | 2848 | */ |
duke@1 | 2849 | Type checkId(JCTree tree, |
duke@1 | 2850 | Type site, |
duke@1 | 2851 | Symbol sym, |
duke@1 | 2852 | Env<AttrContext> env, |
mcimadamore@1347 | 2853 | ResultInfo resultInfo) { |
mcimadamore@1347 | 2854 | Type pt = resultInfo.pt.tag == FORALL || resultInfo.pt.tag == METHOD ? |
mcimadamore@1347 | 2855 | resultInfo.pt.map(deferredAttr.new DeferredTypeMap(AttrMode.SPECULATIVE, sym, env.info.pendingResolutionPhase)) : |
mcimadamore@1347 | 2856 | resultInfo.pt; |
mcimadamore@1347 | 2857 | |
mcimadamore@1347 | 2858 | DeferredAttr.DeferredTypeMap recoveryMap = |
mcimadamore@1347 | 2859 | deferredAttr.new RecoveryDeferredTypeMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase); |
mcimadamore@1347 | 2860 | |
mcimadamore@1347 | 2861 | if (pt.isErroneous()) { |
mcimadamore@1347 | 2862 | Type.map(resultInfo.pt.getParameterTypes(), recoveryMap); |
mcimadamore@1347 | 2863 | return types.createErrorType(site); |
mcimadamore@1347 | 2864 | } |
duke@1 | 2865 | Type owntype; // The computed type of this identifier occurrence. |
duke@1 | 2866 | switch (sym.kind) { |
duke@1 | 2867 | case TYP: |
duke@1 | 2868 | // For types, the computed type equals the symbol's type, |
duke@1 | 2869 | // except for two situations: |
duke@1 | 2870 | owntype = sym.type; |
duke@1 | 2871 | if (owntype.tag == CLASS) { |
duke@1 | 2872 | Type ownOuter = owntype.getEnclosingType(); |
duke@1 | 2873 | |
duke@1 | 2874 | // (a) If the symbol's type is parameterized, erase it |
duke@1 | 2875 | // because no type parameters were given. |
duke@1 | 2876 | // We recover generic outer type later in visitTypeApply. |
duke@1 | 2877 | if (owntype.tsym.type.getTypeArguments().nonEmpty()) { |
duke@1 | 2878 | owntype = types.erasure(owntype); |
duke@1 | 2879 | } |
duke@1 | 2880 | |
duke@1 | 2881 | // (b) If the symbol's type is an inner class, then |
duke@1 | 2882 | // we have to interpret its outer type as a superclass |
duke@1 | 2883 | // of the site type. Example: |
duke@1 | 2884 | // |
duke@1 | 2885 | // class Tree<A> { class Visitor { ... } } |
duke@1 | 2886 | // class PointTree extends Tree<Point> { ... } |
duke@1 | 2887 | // ...PointTree.Visitor... |
duke@1 | 2888 | // |
duke@1 | 2889 | // Then the type of the last expression above is |
duke@1 | 2890 | // Tree<Point>.Visitor. |
duke@1 | 2891 | else if (ownOuter.tag == CLASS && site != ownOuter) { |
duke@1 | 2892 | Type normOuter = site; |
duke@1 | 2893 | if (normOuter.tag == CLASS) |
duke@1 | 2894 | normOuter = types.asEnclosingSuper(site, ownOuter.tsym); |
duke@1 | 2895 | if (normOuter == null) // perhaps from an import |
duke@1 | 2896 | normOuter = types.erasure(ownOuter); |
duke@1 | 2897 | if (normOuter != ownOuter) |
duke@1 | 2898 | owntype = new ClassType( |
duke@1 | 2899 | normOuter, List.<Type>nil(), owntype.tsym); |
duke@1 | 2900 | } |
duke@1 | 2901 | } |
duke@1 | 2902 | break; |
duke@1 | 2903 | case VAR: |
duke@1 | 2904 | VarSymbol v = (VarSymbol)sym; |
duke@1 | 2905 | // Test (4): if symbol is an instance field of a raw type, |
duke@1 | 2906 | // which is being assigned to, issue an unchecked warning if |
duke@1 | 2907 | // its type changes under erasure. |
duke@1 | 2908 | if (allowGenerics && |
mcimadamore@1220 | 2909 | resultInfo.pkind == VAR && |
duke@1 | 2910 | v.owner.kind == TYP && |
duke@1 | 2911 | (v.flags() & STATIC) == 0 && |
duke@1 | 2912 | (site.tag == CLASS || site.tag == TYPEVAR)) { |
duke@1 | 2913 | Type s = types.asOuterSuper(site, v.owner); |
duke@1 | 2914 | if (s != null && |
duke@1 | 2915 | s.isRaw() && |
duke@1 | 2916 | !types.isSameType(v.type, v.erasure(types))) { |
duke@1 | 2917 | chk.warnUnchecked(tree.pos(), |
duke@1 | 2918 | "unchecked.assign.to.var", |
duke@1 | 2919 | v, s); |
duke@1 | 2920 | } |
duke@1 | 2921 | } |
duke@1 | 2922 | // The computed type of a variable is the type of the |
duke@1 | 2923 | // variable symbol, taken as a member of the site type. |
duke@1 | 2924 | owntype = (sym.owner.kind == TYP && |
duke@1 | 2925 | sym.name != names._this && sym.name != names._super) |
duke@1 | 2926 | ? types.memberType(site, sym) |
duke@1 | 2927 | : sym.type; |
duke@1 | 2928 | |
duke@1 | 2929 | // If the variable is a constant, record constant value in |
duke@1 | 2930 | // computed type. |
duke@1 | 2931 | if (v.getConstValue() != null && isStaticReference(tree)) |
duke@1 | 2932 | owntype = owntype.constType(v.getConstValue()); |
duke@1 | 2933 | |
mcimadamore@1220 | 2934 | if (resultInfo.pkind == VAL) { |
duke@1 | 2935 | owntype = capture(owntype); // capture "names as expressions" |
duke@1 | 2936 | } |
duke@1 | 2937 | break; |
duke@1 | 2938 | case MTH: { |
mcimadamore@1268 | 2939 | owntype = checkMethod(site, sym, |
mcimadamore@1268 | 2940 | new ResultInfo(VAL, resultInfo.pt.getReturnType(), resultInfo.checkContext), |
mcimadamore@1341 | 2941 | env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(), |
mcimadamore@1347 | 2942 | resultInfo.pt.getTypeArguments()); |
duke@1 | 2943 | break; |
duke@1 | 2944 | } |
duke@1 | 2945 | case PCK: case ERR: |
mcimadamore@1347 | 2946 | Type.map(resultInfo.pt.getParameterTypes(), recoveryMap); |
duke@1 | 2947 | owntype = sym.type; |
duke@1 | 2948 | break; |
duke@1 | 2949 | default: |
duke@1 | 2950 | throw new AssertionError("unexpected kind: " + sym.kind + |
duke@1 | 2951 | " in tree " + tree); |
duke@1 | 2952 | } |
duke@1 | 2953 | |
duke@1 | 2954 | // Test (1): emit a `deprecation' warning if symbol is deprecated. |
duke@1 | 2955 | // (for constructors, the error was given when the constructor was |
duke@1 | 2956 | // resolved) |
mcimadamore@852 | 2957 | |
mcimadamore@852 | 2958 | if (sym.name != names.init) { |
mcimadamore@852 | 2959 | chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym); |
mcimadamore@852 | 2960 | chk.checkSunAPI(tree.pos(), sym); |
jjg@377 | 2961 | } |
duke@1 | 2962 | |
duke@1 | 2963 | // Test (3): if symbol is a variable, check that its type and |
duke@1 | 2964 | // kind are compatible with the prototype and protokind. |
mcimadamore@1220 | 2965 | return check(tree, owntype, sym.kind, resultInfo); |
duke@1 | 2966 | } |
duke@1 | 2967 | |
duke@1 | 2968 | /** Check that variable is initialized and evaluate the variable's |
duke@1 | 2969 | * initializer, if not yet done. Also check that variable is not |
duke@1 | 2970 | * referenced before it is defined. |
duke@1 | 2971 | * @param tree The tree making up the variable reference. |
duke@1 | 2972 | * @param env The current environment. |
duke@1 | 2973 | * @param v The variable's symbol. |
duke@1 | 2974 | */ |
duke@1 | 2975 | private void checkInit(JCTree tree, |
duke@1 | 2976 | Env<AttrContext> env, |
duke@1 | 2977 | VarSymbol v, |
duke@1 | 2978 | boolean onlyWarning) { |
duke@1 | 2979 | // System.err.println(v + " " + ((v.flags() & STATIC) != 0) + " " + |
duke@1 | 2980 | // tree.pos + " " + v.pos + " " + |
duke@1 | 2981 | // Resolve.isStatic(env));//DEBUG |
duke@1 | 2982 | |
duke@1 | 2983 | // A forward reference is diagnosed if the declaration position |
duke@1 | 2984 | // of the variable is greater than the current tree position |
duke@1 | 2985 | // and the tree and variable definition occur in the same class |
duke@1 | 2986 | // definition. Note that writes don't count as references. |
duke@1 | 2987 | // This check applies only to class and instance |
duke@1 | 2988 | // variables. Local variables follow different scope rules, |
duke@1 | 2989 | // and are subject to definite assignment checking. |
mcimadamore@94 | 2990 | if ((env.info.enclVar == v || v.pos > tree.pos) && |
duke@1 | 2991 | v.owner.kind == TYP && |
mcimadamore@1297 | 2992 | canOwnInitializer(owner(env)) && |
duke@1 | 2993 | v.owner == env.info.scope.owner.enclClass() && |
duke@1 | 2994 | ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) && |
jjg@1127 | 2995 | (!env.tree.hasTag(ASSIGN) || |
duke@1 | 2996 | TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) { |
mcimadamore@94 | 2997 | String suffix = (env.info.enclVar == v) ? |
mcimadamore@94 | 2998 | "self.ref" : "forward.ref"; |
mcimadamore@18 | 2999 | if (!onlyWarning || isStaticEnumField(v)) { |
mcimadamore@94 | 3000 | log.error(tree.pos(), "illegal." + suffix); |
duke@1 | 3001 | } else if (useBeforeDeclarationWarning) { |
mcimadamore@94 | 3002 | log.warning(tree.pos(), suffix, v); |
duke@1 | 3003 | } |
duke@1 | 3004 | } |
duke@1 | 3005 | |
duke@1 | 3006 | v.getConstValue(); // ensure initializer is evaluated |
duke@1 | 3007 | |
duke@1 | 3008 | checkEnumInitializer(tree, env, v); |
duke@1 | 3009 | } |
duke@1 | 3010 | |
duke@1 | 3011 | /** |
duke@1 | 3012 | * Check for illegal references to static members of enum. In |
duke@1 | 3013 | * an enum type, constructors and initializers may not |
duke@1 | 3014 | * reference its static members unless they are constant. |
duke@1 | 3015 | * |
duke@1 | 3016 | * @param tree The tree making up the variable reference. |
duke@1 | 3017 | * @param env The current environment. |
duke@1 | 3018 | * @param v The variable's symbol. |
jjh@972 | 3019 | * @jls section 8.9 Enums |
duke@1 | 3020 | */ |
duke@1 | 3021 | private void checkEnumInitializer(JCTree tree, Env<AttrContext> env, VarSymbol v) { |
jjh@972 | 3022 | // JLS: |
duke@1 | 3023 | // |
duke@1 | 3024 | // "It is a compile-time error to reference a static field |
duke@1 | 3025 | // of an enum type that is not a compile-time constant |
duke@1 | 3026 | // (15.28) from constructors, instance initializer blocks, |
duke@1 | 3027 | // or instance variable initializer expressions of that |
duke@1 | 3028 | // type. It is a compile-time error for the constructors, |
duke@1 | 3029 | // instance initializer blocks, or instance variable |
duke@1 | 3030 | // initializer expressions of an enum constant e to refer |
duke@1 | 3031 | // to itself or to an enum constant of the same type that |
duke@1 | 3032 | // is declared to the right of e." |
mcimadamore@18 | 3033 | if (isStaticEnumField(v)) { |
duke@1 | 3034 | ClassSymbol enclClass = env.info.scope.owner.enclClass(); |
duke@1 | 3035 | |
duke@1 | 3036 | if (enclClass == null || enclClass.owner == null) |
duke@1 | 3037 | return; |
duke@1 | 3038 | |
duke@1 | 3039 | // See if the enclosing class is the enum (or a |
duke@1 | 3040 | // subclass thereof) declaring v. If not, this |
duke@1 | 3041 | // reference is OK. |
duke@1 | 3042 | if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type)) |
duke@1 | 3043 | return; |
duke@1 | 3044 | |
duke@1 | 3045 | // If the reference isn't from an initializer, then |
duke@1 | 3046 | // the reference is OK. |
duke@1 | 3047 | if (!Resolve.isInitializer(env)) |
duke@1 | 3048 | return; |
duke@1 | 3049 | |
duke@1 | 3050 | log.error(tree.pos(), "illegal.enum.static.ref"); |
duke@1 | 3051 | } |
duke@1 | 3052 | } |
duke@1 | 3053 | |
mcimadamore@18 | 3054 | /** Is the given symbol a static, non-constant field of an Enum? |
mcimadamore@18 | 3055 | * Note: enum literals should not be regarded as such |
mcimadamore@18 | 3056 | */ |
mcimadamore@18 | 3057 | private boolean isStaticEnumField(VarSymbol v) { |
mcimadamore@18 | 3058 | return Flags.isEnum(v.owner) && |
mcimadamore@18 | 3059 | Flags.isStatic(v) && |
mcimadamore@18 | 3060 | !Flags.isConstant(v) && |
mcimadamore@18 | 3061 | v.name != names._class; |
duke@1 | 3062 | } |
duke@1 | 3063 | |
duke@1 | 3064 | /** Can the given symbol be the owner of code which forms part |
duke@1 | 3065 | * if class initialization? This is the case if the symbol is |
duke@1 | 3066 | * a type or field, or if the symbol is the synthetic method. |
duke@1 | 3067 | * owning a block. |
duke@1 | 3068 | */ |
duke@1 | 3069 | private boolean canOwnInitializer(Symbol sym) { |
duke@1 | 3070 | return |
duke@1 | 3071 | (sym.kind & (VAR | TYP)) != 0 || |
duke@1 | 3072 | (sym.kind == MTH && (sym.flags() & BLOCK) != 0); |
duke@1 | 3073 | } |
duke@1 | 3074 | |
duke@1 | 3075 | Warner noteWarner = new Warner(); |
duke@1 | 3076 | |
duke@1 | 3077 | /** |
mcimadamore@1219 | 3078 | * Check that method arguments conform to its instantiation. |
duke@1 | 3079 | **/ |
duke@1 | 3080 | public Type checkMethod(Type site, |
duke@1 | 3081 | Symbol sym, |
mcimadamore@1268 | 3082 | ResultInfo resultInfo, |
mcimadamore@1268 | 3083 | Env<AttrContext> env, |
mcimadamore@1268 | 3084 | final List<JCExpression> argtrees, |
mcimadamore@1268 | 3085 | List<Type> argtypes, |
mcimadamore@1347 | 3086 | List<Type> typeargtypes) { |
duke@1 | 3087 | // Test (5): if symbol is an instance method of a raw type, issue |
duke@1 | 3088 | // an unchecked warning if its argument types change under erasure. |
duke@1 | 3089 | if (allowGenerics && |
duke@1 | 3090 | (sym.flags() & STATIC) == 0 && |
duke@1 | 3091 | (site.tag == CLASS || site.tag == TYPEVAR)) { |
duke@1 | 3092 | Type s = types.asOuterSuper(site, sym.owner); |
duke@1 | 3093 | if (s != null && s.isRaw() && |
duke@1 | 3094 | !types.isSameTypes(sym.type.getParameterTypes(), |
duke@1 | 3095 | sym.erasure(types).getParameterTypes())) { |
duke@1 | 3096 | chk.warnUnchecked(env.tree.pos(), |
duke@1 | 3097 | "unchecked.call.mbr.of.raw.type", |
duke@1 | 3098 | sym, s); |
duke@1 | 3099 | } |
duke@1 | 3100 | } |
duke@1 | 3101 | |
duke@1 | 3102 | // Compute the identifier's instantiated type. |
duke@1 | 3103 | // For methods, we need to compute the instance type by |
duke@1 | 3104 | // Resolve.instantiate from the symbol's type as well as |
duke@1 | 3105 | // any type arguments and value arguments. |
mcimadamore@795 | 3106 | noteWarner.clear(); |
mcimadamore@1296 | 3107 | try { |
mcimadamore@1347 | 3108 | Type owntype = rs.checkMethod( |
mcimadamore@1296 | 3109 | env, |
mcimadamore@1296 | 3110 | site, |
mcimadamore@1296 | 3111 | sym, |
mcimadamore@1296 | 3112 | resultInfo, |
mcimadamore@1296 | 3113 | argtypes, |
mcimadamore@1296 | 3114 | typeargtypes, |
mcimadamore@1296 | 3115 | noteWarner); |
mcimadamore@1296 | 3116 | |
mcimadamore@1347 | 3117 | return chk.checkMethod(owntype, sym, env, argtrees, argtypes, env.info.lastResolveVarargs(), |
mcimadamore@1296 | 3118 | noteWarner.hasNonSilentLint(LintCategory.UNCHECKED)); |
mcimadamore@1296 | 3119 | } catch (Infer.InferenceException ex) { |
mcimadamore@1296 | 3120 | //invalid target type - propagate exception outwards or report error |
mcimadamore@1296 | 3121 | //depending on the current check context |
mcimadamore@1296 | 3122 | resultInfo.checkContext.report(env.tree.pos(), ex.getDiagnostic()); |
mcimadamore@1296 | 3123 | return types.createErrorType(site); |
mcimadamore@1296 | 3124 | } catch (Resolve.InapplicableMethodException ex) { |
mcimadamore@1347 | 3125 | Assert.error(ex.getDiagnostic().getMessage(Locale.getDefault())); |
mcimadamore@1296 | 3126 | return null; |
mcimadamore@1296 | 3127 | } |
mcimadamore@1219 | 3128 | } |
mcimadamore@1219 | 3129 | |
duke@1 | 3130 | public void visitLiteral(JCLiteral tree) { |
duke@1 | 3131 | result = check( |
mcimadamore@1220 | 3132 | tree, litType(tree.typetag).constType(tree.value), VAL, resultInfo); |
duke@1 | 3133 | } |
duke@1 | 3134 | //where |
duke@1 | 3135 | /** Return the type of a literal with given type tag. |
duke@1 | 3136 | */ |
duke@1 | 3137 | Type litType(int tag) { |
duke@1 | 3138 | return (tag == TypeTags.CLASS) ? syms.stringType : syms.typeOfTag[tag]; |
duke@1 | 3139 | } |
duke@1 | 3140 | |
duke@1 | 3141 | public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
mcimadamore@1220 | 3142 | result = check(tree, syms.typeOfTag[tree.typetag], TYP, resultInfo); |
duke@1 | 3143 | } |
duke@1 | 3144 | |
duke@1 | 3145 | public void visitTypeArray(JCArrayTypeTree tree) { |
duke@1 | 3146 | Type etype = attribType(tree.elemtype, env); |
duke@1 | 3147 | Type type = new ArrayType(etype, syms.arrayClass); |
mcimadamore@1220 | 3148 | result = check(tree, type, TYP, resultInfo); |
duke@1 | 3149 | } |
duke@1 | 3150 | |
duke@1 | 3151 | /** Visitor method for parameterized types. |
duke@1 | 3152 | * Bound checking is left until later, since types are attributed |
duke@1 | 3153 | * before supertype structure is completely known |
duke@1 | 3154 | */ |
duke@1 | 3155 | public void visitTypeApply(JCTypeApply tree) { |
jjg@110 | 3156 | Type owntype = types.createErrorType(tree.type); |
duke@1 | 3157 | |
duke@1 | 3158 | // Attribute functor part of application and make sure it's a class. |
duke@1 | 3159 | Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env)); |
duke@1 | 3160 | |
duke@1 | 3161 | // Attribute type parameters |
duke@1 | 3162 | List<Type> actuals = attribTypes(tree.arguments, env); |
duke@1 | 3163 | |
duke@1 | 3164 | if (clazztype.tag == CLASS) { |
duke@1 | 3165 | List<Type> formals = clazztype.tsym.type.getTypeArguments(); |
mcimadamore@1060 | 3166 | if (actuals.isEmpty()) //diamond |
mcimadamore@1060 | 3167 | actuals = formals; |
mcimadamore@1060 | 3168 | |
mcimadamore@1060 | 3169 | if (actuals.length() == formals.length()) { |
duke@1 | 3170 | List<Type> a = actuals; |
duke@1 | 3171 | List<Type> f = formals; |
duke@1 | 3172 | while (a.nonEmpty()) { |
duke@1 | 3173 | a.head = a.head.withTypeVar(f.head); |
duke@1 | 3174 | a = a.tail; |
duke@1 | 3175 | f = f.tail; |
duke@1 | 3176 | } |
duke@1 | 3177 | // Compute the proper generic outer |
duke@1 | 3178 | Type clazzOuter = clazztype.getEnclosingType(); |
duke@1 | 3179 | if (clazzOuter.tag == CLASS) { |
duke@1 | 3180 | Type site; |
jjg@308 | 3181 | JCExpression clazz = TreeInfo.typeIn(tree.clazz); |
jjg@1127 | 3182 | if (clazz.hasTag(IDENT)) { |
duke@1 | 3183 | site = env.enclClass.sym.type; |
jjg@1127 | 3184 | } else if (clazz.hasTag(SELECT)) { |
jjg@308 | 3185 | site = ((JCFieldAccess) clazz).selected.type; |
duke@1 | 3186 | } else throw new AssertionError(""+tree); |
duke@1 | 3187 | if (clazzOuter.tag == CLASS && site != clazzOuter) { |
duke@1 | 3188 | if (site.tag == CLASS) |
duke@1 | 3189 | site = types.asOuterSuper(site, clazzOuter.tsym); |
duke@1 | 3190 | if (site == null) |
duke@1 | 3191 | site = types.erasure(clazzOuter); |
duke@1 | 3192 | clazzOuter = site; |
duke@1 | 3193 | } |
duke@1 | 3194 | } |
mcimadamore@536 | 3195 | owntype = new ClassType(clazzOuter, actuals, clazztype.tsym); |
duke@1 | 3196 | } else { |
duke@1 | 3197 | if (formals.length() != 0) { |
duke@1 | 3198 | log.error(tree.pos(), "wrong.number.type.args", |
duke@1 | 3199 | Integer.toString(formals.length())); |
duke@1 | 3200 | } else { |
duke@1 | 3201 | log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym); |
duke@1 | 3202 | } |
jjg@110 | 3203 | owntype = types.createErrorType(tree.type); |
duke@1 | 3204 | } |
duke@1 | 3205 | } |
mcimadamore@1220 | 3206 | result = check(tree, owntype, TYP, resultInfo); |
duke@1 | 3207 | } |
duke@1 | 3208 | |
darcy@969 | 3209 | public void visitTypeUnion(JCTypeUnion tree) { |
mcimadamore@774 | 3210 | ListBuffer<Type> multicatchTypes = ListBuffer.lb(); |
jjg@988 | 3211 | ListBuffer<Type> all_multicatchTypes = null; // lazy, only if needed |
mcimadamore@774 | 3212 | for (JCExpression typeTree : tree.alternatives) { |
mcimadamore@774 | 3213 | Type ctype = attribType(typeTree, env); |
mcimadamore@774 | 3214 | ctype = chk.checkType(typeTree.pos(), |
mcimadamore@774 | 3215 | chk.checkClassType(typeTree.pos(), ctype), |
mcimadamore@774 | 3216 | syms.throwableType); |
mcimadamore@949 | 3217 | if (!ctype.isErroneous()) { |
darcy@969 | 3218 | //check that alternatives of a union type are pairwise |
mcimadamore@949 | 3219 | //unrelated w.r.t. subtyping |
mcimadamore@949 | 3220 | if (chk.intersects(ctype, multicatchTypes.toList())) { |
mcimadamore@949 | 3221 | for (Type t : multicatchTypes) { |
mcimadamore@949 | 3222 | boolean sub = types.isSubtype(ctype, t); |
mcimadamore@949 | 3223 | boolean sup = types.isSubtype(t, ctype); |
mcimadamore@949 | 3224 | if (sub || sup) { |
mcimadamore@949 | 3225 | //assume 'a' <: 'b' |
mcimadamore@949 | 3226 | Type a = sub ? ctype : t; |
mcimadamore@949 | 3227 | Type b = sub ? t : ctype; |
mcimadamore@949 | 3228 | log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b); |
mcimadamore@949 | 3229 | } |
mcimadamore@949 | 3230 | } |
mcimadamore@949 | 3231 | } |
mcimadamore@949 | 3232 | multicatchTypes.append(ctype); |
jjg@988 | 3233 | if (all_multicatchTypes != null) |
jjg@988 | 3234 | all_multicatchTypes.append(ctype); |
jjg@988 | 3235 | } else { |
jjg@988 | 3236 | if (all_multicatchTypes == null) { |
jjg@988 | 3237 | all_multicatchTypes = ListBuffer.lb(); |
jjg@988 | 3238 | all_multicatchTypes.appendList(multicatchTypes); |
jjg@988 | 3239 | } |
jjg@988 | 3240 | all_multicatchTypes.append(ctype); |
mcimadamore@949 | 3241 | } |
mcimadamore@774 | 3242 | } |
mcimadamore@1220 | 3243 | Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, resultInfo); |
jjg@988 | 3244 | if (t.tag == CLASS) { |
jjg@988 | 3245 | List<Type> alternatives = |
jjg@988 | 3246 | ((all_multicatchTypes == null) ? multicatchTypes : all_multicatchTypes).toList(); |
jjg@988 | 3247 | t = new UnionClassType((ClassType) t, alternatives); |
jjg@988 | 3248 | } |
jjg@988 | 3249 | tree.type = result = t; |
mcimadamore@550 | 3250 | } |
mcimadamore@550 | 3251 | |
duke@1 | 3252 | public void visitTypeParameter(JCTypeParameter tree) { |
duke@1 | 3253 | TypeVar a = (TypeVar)tree.type; |
duke@1 | 3254 | Set<Type> boundSet = new HashSet<Type>(); |
duke@1 | 3255 | if (a.bound.isErroneous()) |
duke@1 | 3256 | return; |
duke@1 | 3257 | List<Type> bs = types.getBounds(a); |
duke@1 | 3258 | if (tree.bounds.nonEmpty()) { |
duke@1 | 3259 | // accept class or interface or typevar as first bound. |
duke@1 | 3260 | Type b = checkBase(bs.head, tree.bounds.head, env, false, false, false); |
duke@1 | 3261 | boundSet.add(types.erasure(b)); |
mcimadamore@159 | 3262 | if (b.isErroneous()) { |
mcimadamore@159 | 3263 | a.bound = b; |
mcimadamore@159 | 3264 | } |
mcimadamore@159 | 3265 | else if (b.tag == TYPEVAR) { |
duke@1 | 3266 | // if first bound was a typevar, do not accept further bounds. |
duke@1 | 3267 | if (tree.bounds.tail.nonEmpty()) { |
duke@1 | 3268 | log.error(tree.bounds.tail.head.pos(), |
duke@1 | 3269 | "type.var.may.not.be.followed.by.other.bounds"); |
duke@1 | 3270 | tree.bounds = List.of(tree.bounds.head); |
mcimadamore@7 | 3271 | a.bound = bs.head; |
duke@1 | 3272 | } |
duke@1 | 3273 | } else { |
duke@1 | 3274 | // if first bound was a class or interface, accept only interfaces |
duke@1 | 3275 | // as further bounds. |
duke@1 | 3276 | for (JCExpression bound : tree.bounds.tail) { |
duke@1 | 3277 | bs = bs.tail; |
duke@1 | 3278 | Type i = checkBase(bs.head, bound, env, false, true, false); |
mcimadamore@159 | 3279 | if (i.isErroneous()) |
mcimadamore@159 | 3280 | a.bound = i; |
mcimadamore@159 | 3281 | else if (i.tag == CLASS) |
duke@1 | 3282 | chk.checkNotRepeated(bound.pos(), types.erasure(i), boundSet); |
duke@1 | 3283 | } |
duke@1 | 3284 | } |
duke@1 | 3285 | } |
duke@1 | 3286 | bs = types.getBounds(a); |
duke@1 | 3287 | |
duke@1 | 3288 | // in case of multiple bounds ... |
duke@1 | 3289 | if (bs.length() > 1) { |
duke@1 | 3290 | // ... the variable's bound is a class type flagged COMPOUND |
duke@1 | 3291 | // (see comment for TypeVar.bound). |
duke@1 | 3292 | // In this case, generate a class tree that represents the |
duke@1 | 3293 | // bound class, ... |
jjg@904 | 3294 | JCExpression extending; |
duke@1 | 3295 | List<JCExpression> implementing; |
duke@1 | 3296 | if ((bs.head.tsym.flags() & INTERFACE) == 0) { |
duke@1 | 3297 | extending = tree.bounds.head; |
duke@1 | 3298 | implementing = tree.bounds.tail; |
duke@1 | 3299 | } else { |
duke@1 | 3300 | extending = null; |
duke@1 | 3301 | implementing = tree.bounds; |
duke@1 | 3302 | } |
duke@1 | 3303 | JCClassDecl cd = make.at(tree.pos).ClassDef( |
duke@1 | 3304 | make.Modifiers(PUBLIC | ABSTRACT), |
duke@1 | 3305 | tree.name, List.<JCTypeParameter>nil(), |
duke@1 | 3306 | extending, implementing, List.<JCTree>nil()); |
duke@1 | 3307 | |
duke@1 | 3308 | ClassSymbol c = (ClassSymbol)a.getUpperBound().tsym; |
jjg@816 | 3309 | Assert.check((c.flags() & COMPOUND) != 0); |
duke@1 | 3310 | cd.sym = c; |
duke@1 | 3311 | c.sourcefile = env.toplevel.sourcefile; |
duke@1 | 3312 | |
duke@1 | 3313 | // ... and attribute the bound class |
duke@1 | 3314 | c.flags_field |= UNATTRIBUTED; |
duke@1 | 3315 | Env<AttrContext> cenv = enter.classEnv(cd, env); |
duke@1 | 3316 | enter.typeEnvs.put(c, cenv); |
duke@1 | 3317 | } |
duke@1 | 3318 | } |
duke@1 | 3319 | |
duke@1 | 3320 | |
duke@1 | 3321 | public void visitWildcard(JCWildcard tree) { |
duke@1 | 3322 | //- System.err.println("visitWildcard("+tree+");");//DEBUG |
duke@1 | 3323 | Type type = (tree.kind.kind == BoundKind.UNBOUND) |
duke@1 | 3324 | ? syms.objectType |
duke@1 | 3325 | : attribType(tree.inner, env); |
duke@1 | 3326 | result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type), |
duke@1 | 3327 | tree.kind.kind, |
duke@1 | 3328 | syms.boundClass), |
mcimadamore@1220 | 3329 | TYP, resultInfo); |
duke@1 | 3330 | } |
duke@1 | 3331 | |
duke@1 | 3332 | public void visitAnnotation(JCAnnotation tree) { |
mcimadamore@1220 | 3333 | log.error(tree.pos(), "annotation.not.valid.for.type", pt()); |
duke@1 | 3334 | result = tree.type = syms.errType; |
duke@1 | 3335 | } |
duke@1 | 3336 | |
duke@1 | 3337 | public void visitErroneous(JCErroneous tree) { |
duke@1 | 3338 | if (tree.errs != null) |
duke@1 | 3339 | for (JCTree err : tree.errs) |
mcimadamore@1220 | 3340 | attribTree(err, env, new ResultInfo(ERR, pt())); |
duke@1 | 3341 | result = tree.type = syms.errType; |
duke@1 | 3342 | } |
duke@1 | 3343 | |
duke@1 | 3344 | /** Default visitor method for all other trees. |
duke@1 | 3345 | */ |
duke@1 | 3346 | public void visitTree(JCTree tree) { |
duke@1 | 3347 | throw new AssertionError(); |
duke@1 | 3348 | } |
duke@1 | 3349 | |
jjg@931 | 3350 | /** |
jjg@931 | 3351 | * Attribute an env for either a top level tree or class declaration. |
jjg@931 | 3352 | */ |
jjg@931 | 3353 | public void attrib(Env<AttrContext> env) { |
jjg@1127 | 3354 | if (env.tree.hasTag(TOPLEVEL)) |
jjg@931 | 3355 | attribTopLevel(env); |
jjg@931 | 3356 | else |
jjg@931 | 3357 | attribClass(env.tree.pos(), env.enclClass.sym); |
jjg@931 | 3358 | } |
jjg@931 | 3359 | |
jjg@931 | 3360 | /** |
jjg@931 | 3361 | * Attribute a top level tree. These trees are encountered when the |
jjg@931 | 3362 | * package declaration has annotations. |
jjg@931 | 3363 | */ |
jjg@931 | 3364 | public void attribTopLevel(Env<AttrContext> env) { |
jjg@931 | 3365 | JCCompilationUnit toplevel = env.toplevel; |
jjg@931 | 3366 | try { |
jjg@931 | 3367 | annotate.flush(); |
jjg@931 | 3368 | chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge); |
jjg@931 | 3369 | } catch (CompletionFailure ex) { |
jjg@931 | 3370 | chk.completionError(toplevel.pos(), ex); |
jjg@931 | 3371 | } |
jjg@931 | 3372 | } |
jjg@931 | 3373 | |
duke@1 | 3374 | /** Main method: attribute class definition associated with given class symbol. |
duke@1 | 3375 | * reporting completion failures at the given position. |
duke@1 | 3376 | * @param pos The source position at which completion errors are to be |
duke@1 | 3377 | * reported. |
duke@1 | 3378 | * @param c The class symbol whose definition will be attributed. |
duke@1 | 3379 | */ |
duke@1 | 3380 | public void attribClass(DiagnosticPosition pos, ClassSymbol c) { |
duke@1 | 3381 | try { |
duke@1 | 3382 | annotate.flush(); |
duke@1 | 3383 | attribClass(c); |
duke@1 | 3384 | } catch (CompletionFailure ex) { |
duke@1 | 3385 | chk.completionError(pos, ex); |
duke@1 | 3386 | } |
duke@1 | 3387 | } |
duke@1 | 3388 | |
duke@1 | 3389 | /** Attribute class definition associated with given class symbol. |
duke@1 | 3390 | * @param c The class symbol whose definition will be attributed. |
duke@1 | 3391 | */ |
duke@1 | 3392 | void attribClass(ClassSymbol c) throws CompletionFailure { |
duke@1 | 3393 | if (c.type.tag == ERROR) return; |
duke@1 | 3394 | |
duke@1 | 3395 | // Check for cycles in the inheritance graph, which can arise from |
duke@1 | 3396 | // ill-formed class files. |
duke@1 | 3397 | chk.checkNonCyclic(null, c.type); |
duke@1 | 3398 | |
duke@1 | 3399 | Type st = types.supertype(c.type); |
duke@1 | 3400 | if ((c.flags_field & Flags.COMPOUND) == 0) { |
duke@1 | 3401 | // First, attribute superclass. |
duke@1 | 3402 | if (st.tag == CLASS) |
duke@1 | 3403 | attribClass((ClassSymbol)st.tsym); |
duke@1 | 3404 | |
duke@1 | 3405 | // Next attribute owner, if it is a class. |
duke@1 | 3406 | if (c.owner.kind == TYP && c.owner.type.tag == CLASS) |
duke@1 | 3407 | attribClass((ClassSymbol)c.owner); |
duke@1 | 3408 | } |
duke@1 | 3409 | |
duke@1 | 3410 | // The previous operations might have attributed the current class |
duke@1 | 3411 | // if there was a cycle. So we test first whether the class is still |
duke@1 | 3412 | // UNATTRIBUTED. |
duke@1 | 3413 | if ((c.flags_field & UNATTRIBUTED) != 0) { |
duke@1 | 3414 | c.flags_field &= ~UNATTRIBUTED; |
duke@1 | 3415 | |
duke@1 | 3416 | // Get environment current at the point of class definition. |
duke@1 | 3417 | Env<AttrContext> env = enter.typeEnvs.get(c); |
duke@1 | 3418 | |
duke@1 | 3419 | // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized, |
duke@1 | 3420 | // because the annotations were not available at the time the env was created. Therefore, |
duke@1 | 3421 | // we look up the environment chain for the first enclosing environment for which the |
duke@1 | 3422 | // lint value is set. Typically, this is the parent env, but might be further if there |
duke@1 | 3423 | // are any envs created as a result of TypeParameter nodes. |
duke@1 | 3424 | Env<AttrContext> lintEnv = env; |
duke@1 | 3425 | while (lintEnv.info.lint == null) |
duke@1 | 3426 | lintEnv = lintEnv.next; |
duke@1 | 3427 | |
duke@1 | 3428 | // Having found the enclosing lint value, we can initialize the lint value for this class |
jfranck@1313 | 3429 | env.info.lint = lintEnv.info.lint.augment(c.annotations, c.flags()); |
duke@1 | 3430 | |
duke@1 | 3431 | Lint prevLint = chk.setLint(env.info.lint); |
duke@1 | 3432 | JavaFileObject prev = log.useSource(c.sourcefile); |
mcimadamore@1347 | 3433 | ResultInfo prevReturnRes = env.info.returnResult; |
duke@1 | 3434 | |
duke@1 | 3435 | try { |
mcimadamore@1347 | 3436 | env.info.returnResult = null; |
duke@1 | 3437 | // java.lang.Enum may not be subclassed by a non-enum |
duke@1 | 3438 | if (st.tsym == syms.enumSym && |
duke@1 | 3439 | ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0)) |
duke@1 | 3440 | log.error(env.tree.pos(), "enum.no.subclassing"); |
duke@1 | 3441 | |
duke@1 | 3442 | // Enums may not be extended by source-level classes |
duke@1 | 3443 | if (st.tsym != null && |
duke@1 | 3444 | ((st.tsym.flags_field & Flags.ENUM) != 0) && |
mcimadamore@82 | 3445 | ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && |
duke@1 | 3446 | !target.compilerBootstrap(c)) { |
duke@1 | 3447 | log.error(env.tree.pos(), "enum.types.not.extensible"); |
duke@1 | 3448 | } |
duke@1 | 3449 | attribClassBody(env, c); |
duke@1 | 3450 | |
duke@1 | 3451 | chk.checkDeprecatedAnnotation(env.tree.pos(), c); |
duke@1 | 3452 | } finally { |
mcimadamore@1347 | 3453 | env.info.returnResult = prevReturnRes; |
duke@1 | 3454 | log.useSource(prev); |
duke@1 | 3455 | chk.setLint(prevLint); |
duke@1 | 3456 | } |
duke@1 | 3457 | |
duke@1 | 3458 | } |
duke@1 | 3459 | } |
duke@1 | 3460 | |
duke@1 | 3461 | public void visitImport(JCImport tree) { |
duke@1 | 3462 | // nothing to do |
duke@1 | 3463 | } |
duke@1 | 3464 | |
duke@1 | 3465 | /** Finish the attribution of a class. */ |
duke@1 | 3466 | private void attribClassBody(Env<AttrContext> env, ClassSymbol c) { |
duke@1 | 3467 | JCClassDecl tree = (JCClassDecl)env.tree; |
jjg@816 | 3468 | Assert.check(c == tree.sym); |
duke@1 | 3469 | |
duke@1 | 3470 | // Validate annotations |
duke@1 | 3471 | chk.validateAnnotations(tree.mods.annotations, c); |
duke@1 | 3472 | |
duke@1 | 3473 | // Validate type parameters, supertype and interfaces. |
mcimadamore@42 | 3474 | attribBounds(tree.typarams); |
mcimadamore@537 | 3475 | if (!c.isAnonymous()) { |
mcimadamore@537 | 3476 | //already checked if anonymous |
mcimadamore@537 | 3477 | chk.validate(tree.typarams, env); |
mcimadamore@537 | 3478 | chk.validate(tree.extending, env); |
mcimadamore@537 | 3479 | chk.validate(tree.implementing, env); |
mcimadamore@537 | 3480 | } |
duke@1 | 3481 | |
duke@1 | 3482 | // If this is a non-abstract class, check that it has no abstract |
duke@1 | 3483 | // methods or unimplemented methods of an implemented interface. |
duke@1 | 3484 | if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) { |
duke@1 | 3485 | if (!relax) |
duke@1 | 3486 | chk.checkAllDefined(tree.pos(), c); |
duke@1 | 3487 | } |
duke@1 | 3488 | |
duke@1 | 3489 | if ((c.flags() & ANNOTATION) != 0) { |
duke@1 | 3490 | if (tree.implementing.nonEmpty()) |
duke@1 | 3491 | log.error(tree.implementing.head.pos(), |
duke@1 | 3492 | "cant.extend.intf.annotation"); |
duke@1 | 3493 | if (tree.typarams.nonEmpty()) |
duke@1 | 3494 | log.error(tree.typarams.head.pos(), |
duke@1 | 3495 | "intf.annotation.cant.have.type.params"); |
jfranck@1313 | 3496 | |
jfranck@1313 | 3497 | // If this annotation has a @ContainedBy, validate |
jfranck@1313 | 3498 | Attribute.Compound containedBy = c.attribute(syms.containedByType.tsym); |
jfranck@1313 | 3499 | if (containedBy != null) { |
jfranck@1313 | 3500 | // get diagnositc position for error reporting |
jfranck@1313 | 3501 | DiagnosticPosition cbPos = getDiagnosticPosition(tree, containedBy.type); |
jfranck@1313 | 3502 | Assert.checkNonNull(cbPos); |
jfranck@1313 | 3503 | |
jfranck@1313 | 3504 | chk.validateContainedBy(c, containedBy, cbPos); |
jfranck@1313 | 3505 | } |
jfranck@1313 | 3506 | |
jfranck@1313 | 3507 | // If this annotation has a @ContainerFor, validate |
jfranck@1313 | 3508 | Attribute.Compound containerFor = c.attribute(syms.containerForType.tsym); |
jfranck@1313 | 3509 | if (containerFor != null) { |
jfranck@1313 | 3510 | // get diagnositc position for error reporting |
jfranck@1313 | 3511 | DiagnosticPosition cfPos = getDiagnosticPosition(tree, containerFor.type); |
jfranck@1313 | 3512 | Assert.checkNonNull(cfPos); |
jfranck@1313 | 3513 | |
jfranck@1313 | 3514 | chk.validateContainerFor(c, containerFor, cfPos); |
jfranck@1313 | 3515 | } |
duke@1 | 3516 | } else { |
duke@1 | 3517 | // Check that all extended classes and interfaces |
duke@1 | 3518 | // are compatible (i.e. no two define methods with same arguments |
duke@1 | 3519 | // yet different return types). (JLS 8.4.6.3) |
duke@1 | 3520 | chk.checkCompatibleSupertypes(tree.pos(), c.type); |
duke@1 | 3521 | } |
duke@1 | 3522 | |
duke@1 | 3523 | // Check that class does not import the same parameterized interface |
duke@1 | 3524 | // with two different argument lists. |
duke@1 | 3525 | chk.checkClassBounds(tree.pos(), c.type); |
duke@1 | 3526 | |
duke@1 | 3527 | tree.type = c.type; |
duke@1 | 3528 | |
jjg@816 | 3529 | for (List<JCTypeParameter> l = tree.typarams; |
jjg@816 | 3530 | l.nonEmpty(); l = l.tail) { |
jjg@816 | 3531 | Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope); |
duke@1 | 3532 | } |
duke@1 | 3533 | |
duke@1 | 3534 | // Check that a generic class doesn't extend Throwable |
duke@1 | 3535 | if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) |
duke@1 | 3536 | log.error(tree.extending.pos(), "generic.throwable"); |
duke@1 | 3537 | |
duke@1 | 3538 | // Check that all methods which implement some |
duke@1 | 3539 | // method conform to the method they implement. |
duke@1 | 3540 | chk.checkImplementations(tree); |
duke@1 | 3541 | |
mcimadamore@951 | 3542 | //check that a resource implementing AutoCloseable cannot throw InterruptedException |
mcimadamore@951 | 3543 | checkAutoCloseable(tree.pos(), env, c.type); |
mcimadamore@951 | 3544 | |
duke@1 | 3545 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 3546 | // Attribute declaration |
duke@1 | 3547 | attribStat(l.head, env); |
duke@1 | 3548 | // Check that declarations in inner classes are not static (JLS 8.1.2) |
duke@1 | 3549 | // Make an exception for static constants. |
duke@1 | 3550 | if (c.owner.kind != PCK && |
duke@1 | 3551 | ((c.flags() & STATIC) == 0 || c.name == names.empty) && |
duke@1 | 3552 | (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) { |
duke@1 | 3553 | Symbol sym = null; |
jjg@1127 | 3554 | if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym; |
duke@1 | 3555 | if (sym == null || |
duke@1 | 3556 | sym.kind != VAR || |
duke@1 | 3557 | ((VarSymbol) sym).getConstValue() == null) |
mcimadamore@855 | 3558 | log.error(l.head.pos(), "icls.cant.have.static.decl", c); |
duke@1 | 3559 | } |
duke@1 | 3560 | } |
duke@1 | 3561 | |
duke@1 | 3562 | // Check for cycles among non-initial constructors. |
duke@1 | 3563 | chk.checkCyclicConstructors(tree); |
duke@1 | 3564 | |
duke@1 | 3565 | // Check for cycles among annotation elements. |
duke@1 | 3566 | chk.checkNonCyclicElements(tree); |
duke@1 | 3567 | |
duke@1 | 3568 | // Check for proper use of serialVersionUID |
mcimadamore@795 | 3569 | if (env.info.lint.isEnabled(LintCategory.SERIAL) && |
duke@1 | 3570 | isSerializable(c) && |
duke@1 | 3571 | (c.flags() & Flags.ENUM) == 0 && |
duke@1 | 3572 | (c.flags() & ABSTRACT) == 0) { |
duke@1 | 3573 | checkSerialVersionUID(tree, c); |
duke@1 | 3574 | } |
duke@1 | 3575 | } |
duke@1 | 3576 | // where |
jfranck@1313 | 3577 | /** get a diagnostic position for an attribute of Type t, or null if attribute missing */ |
jfranck@1313 | 3578 | private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) { |
jfranck@1313 | 3579 | for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) { |
jfranck@1313 | 3580 | if (types.isSameType(al.head.annotationType.type, t)) |
jfranck@1313 | 3581 | return al.head.pos(); |
jfranck@1313 | 3582 | } |
jfranck@1313 | 3583 | |
jfranck@1313 | 3584 | return null; |
jfranck@1313 | 3585 | } |
jfranck@1313 | 3586 | |
duke@1 | 3587 | /** check if a class is a subtype of Serializable, if that is available. */ |
duke@1 | 3588 | private boolean isSerializable(ClassSymbol c) { |
duke@1 | 3589 | try { |
duke@1 | 3590 | syms.serializableType.complete(); |
duke@1 | 3591 | } |
duke@1 | 3592 | catch (CompletionFailure e) { |
duke@1 | 3593 | return false; |
duke@1 | 3594 | } |
duke@1 | 3595 | return types.isSubtype(c.type, syms.serializableType); |
duke@1 | 3596 | } |
duke@1 | 3597 | |
duke@1 | 3598 | /** Check that an appropriate serialVersionUID member is defined. */ |
duke@1 | 3599 | private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) { |
duke@1 | 3600 | |
duke@1 | 3601 | // check for presence of serialVersionUID |
duke@1 | 3602 | Scope.Entry e = c.members().lookup(names.serialVersionUID); |
duke@1 | 3603 | while (e.scope != null && e.sym.kind != VAR) e = e.next(); |
duke@1 | 3604 | if (e.scope == null) { |
mcimadamore@795 | 3605 | log.warning(LintCategory.SERIAL, |
jjg@612 | 3606 | tree.pos(), "missing.SVUID", c); |
duke@1 | 3607 | return; |
duke@1 | 3608 | } |
duke@1 | 3609 | |
duke@1 | 3610 | // check that it is static final |
duke@1 | 3611 | VarSymbol svuid = (VarSymbol)e.sym; |
duke@1 | 3612 | if ((svuid.flags() & (STATIC | FINAL)) != |
duke@1 | 3613 | (STATIC | FINAL)) |
mcimadamore@795 | 3614 | log.warning(LintCategory.SERIAL, |
jjg@612 | 3615 | TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c); |
duke@1 | 3616 | |
duke@1 | 3617 | // check that it is long |
duke@1 | 3618 | else if (svuid.type.tag != TypeTags.LONG) |
mcimadamore@795 | 3619 | log.warning(LintCategory.SERIAL, |
jjg@612 | 3620 | TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c); |
duke@1 | 3621 | |
duke@1 | 3622 | // check constant |
duke@1 | 3623 | else if (svuid.getConstValue() == null) |
mcimadamore@795 | 3624 | log.warning(LintCategory.SERIAL, |
jjg@612 | 3625 | TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c); |
duke@1 | 3626 | } |
duke@1 | 3627 | |
duke@1 | 3628 | private Type capture(Type type) { |
duke@1 | 3629 | return types.capture(type); |
duke@1 | 3630 | } |
jjg@308 | 3631 | |
mcimadamore@676 | 3632 | // <editor-fold desc="post-attribution visitor"> |
mcimadamore@676 | 3633 | |
mcimadamore@676 | 3634 | /** |
mcimadamore@676 | 3635 | * Handle missing types/symbols in an AST. This routine is useful when |
mcimadamore@676 | 3636 | * the compiler has encountered some errors (which might have ended up |
mcimadamore@676 | 3637 | * terminating attribution abruptly); if the compiler is used in fail-over |
mcimadamore@676 | 3638 | * mode (e.g. by an IDE) and the AST contains semantic errors, this routine |
mcimadamore@676 | 3639 | * prevents NPE to be progagated during subsequent compilation steps. |
mcimadamore@676 | 3640 | */ |
mcimadamore@1348 | 3641 | public void postAttr(JCTree tree) { |
mcimadamore@1348 | 3642 | new PostAttrAnalyzer().scan(tree); |
mcimadamore@676 | 3643 | } |
mcimadamore@676 | 3644 | |
mcimadamore@676 | 3645 | class PostAttrAnalyzer extends TreeScanner { |
mcimadamore@676 | 3646 | |
mcimadamore@676 | 3647 | private void initTypeIfNeeded(JCTree that) { |
mcimadamore@676 | 3648 | if (that.type == null) { |
mcimadamore@676 | 3649 | that.type = syms.unknownType; |
mcimadamore@676 | 3650 | } |
mcimadamore@676 | 3651 | } |
mcimadamore@676 | 3652 | |
mcimadamore@676 | 3653 | @Override |
mcimadamore@676 | 3654 | public void scan(JCTree tree) { |
mcimadamore@676 | 3655 | if (tree == null) return; |
mcimadamore@676 | 3656 | if (tree instanceof JCExpression) { |
mcimadamore@676 | 3657 | initTypeIfNeeded(tree); |
mcimadamore@676 | 3658 | } |
mcimadamore@676 | 3659 | super.scan(tree); |
mcimadamore@676 | 3660 | } |
mcimadamore@676 | 3661 | |
mcimadamore@676 | 3662 | @Override |
mcimadamore@676 | 3663 | public void visitIdent(JCIdent that) { |
mcimadamore@676 | 3664 | if (that.sym == null) { |
mcimadamore@676 | 3665 | that.sym = syms.unknownSymbol; |
mcimadamore@676 | 3666 | } |
mcimadamore@676 | 3667 | } |
mcimadamore@676 | 3668 | |
mcimadamore@676 | 3669 | @Override |
mcimadamore@676 | 3670 | public void visitSelect(JCFieldAccess that) { |
mcimadamore@676 | 3671 | if (that.sym == null) { |
mcimadamore@676 | 3672 | that.sym = syms.unknownSymbol; |
mcimadamore@676 | 3673 | } |
mcimadamore@676 | 3674 | super.visitSelect(that); |
mcimadamore@676 | 3675 | } |
mcimadamore@676 | 3676 | |
mcimadamore@676 | 3677 | @Override |
mcimadamore@676 | 3678 | public void visitClassDef(JCClassDecl that) { |
mcimadamore@676 | 3679 | initTypeIfNeeded(that); |
mcimadamore@676 | 3680 | if (that.sym == null) { |
mcimadamore@676 | 3681 | that.sym = new ClassSymbol(0, that.name, that.type, syms.noSymbol); |
mcimadamore@676 | 3682 | } |
mcimadamore@676 | 3683 | super.visitClassDef(that); |
mcimadamore@676 | 3684 | } |
mcimadamore@676 | 3685 | |
mcimadamore@676 | 3686 | @Override |
mcimadamore@676 | 3687 | public void visitMethodDef(JCMethodDecl that) { |
mcimadamore@676 | 3688 | initTypeIfNeeded(that); |
mcimadamore@676 | 3689 | if (that.sym == null) { |
mcimadamore@676 | 3690 | that.sym = new MethodSymbol(0, that.name, that.type, syms.noSymbol); |
mcimadamore@676 | 3691 | } |
mcimadamore@676 | 3692 | super.visitMethodDef(that); |
mcimadamore@676 | 3693 | } |
mcimadamore@676 | 3694 | |
mcimadamore@676 | 3695 | @Override |
mcimadamore@676 | 3696 | public void visitVarDef(JCVariableDecl that) { |
mcimadamore@676 | 3697 | initTypeIfNeeded(that); |
mcimadamore@676 | 3698 | if (that.sym == null) { |
mcimadamore@676 | 3699 | that.sym = new VarSymbol(0, that.name, that.type, syms.noSymbol); |
mcimadamore@676 | 3700 | that.sym.adr = 0; |
mcimadamore@676 | 3701 | } |
mcimadamore@676 | 3702 | super.visitVarDef(that); |
mcimadamore@676 | 3703 | } |
mcimadamore@676 | 3704 | |
mcimadamore@676 | 3705 | @Override |
mcimadamore@676 | 3706 | public void visitNewClass(JCNewClass that) { |
mcimadamore@676 | 3707 | if (that.constructor == null) { |
mcimadamore@676 | 3708 | that.constructor = new MethodSymbol(0, names.init, syms.unknownType, syms.noSymbol); |
mcimadamore@676 | 3709 | } |
mcimadamore@676 | 3710 | if (that.constructorType == null) { |
mcimadamore@676 | 3711 | that.constructorType = syms.unknownType; |
mcimadamore@676 | 3712 | } |
mcimadamore@676 | 3713 | super.visitNewClass(that); |
mcimadamore@676 | 3714 | } |
mcimadamore@676 | 3715 | |
mcimadamore@676 | 3716 | @Override |
jjg@1049 | 3717 | public void visitAssignop(JCAssignOp that) { |
jjg@1049 | 3718 | if (that.operator == null) |
jjg@1049 | 3719 | that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol); |
jjg@1049 | 3720 | super.visitAssignop(that); |
jjg@1049 | 3721 | } |
jjg@1049 | 3722 | |
jjg@1049 | 3723 | @Override |
mcimadamore@676 | 3724 | public void visitBinary(JCBinary that) { |
mcimadamore@676 | 3725 | if (that.operator == null) |
mcimadamore@676 | 3726 | that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol); |
mcimadamore@676 | 3727 | super.visitBinary(that); |
mcimadamore@676 | 3728 | } |
mcimadamore@676 | 3729 | |
mcimadamore@676 | 3730 | @Override |
mcimadamore@676 | 3731 | public void visitUnary(JCUnary that) { |
mcimadamore@676 | 3732 | if (that.operator == null) |
mcimadamore@676 | 3733 | that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol); |
mcimadamore@676 | 3734 | super.visitUnary(that); |
mcimadamore@676 | 3735 | } |
mcimadamore@676 | 3736 | } |
mcimadamore@676 | 3737 | // </editor-fold> |
duke@1 | 3738 | } |