ArgumentAttr.java revision 4077:24582dd2649a
1/* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javac.comp; 27 28import com.sun.source.tree.LambdaExpressionTree.BodyKind; 29import com.sun.tools.javac.code.Flags; 30import com.sun.tools.javac.code.Symbol; 31import com.sun.tools.javac.code.Symtab; 32import com.sun.tools.javac.code.Type; 33import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError; 34import com.sun.tools.javac.comp.Attr.ResultInfo; 35import com.sun.tools.javac.comp.Attr.TargetInfo; 36import com.sun.tools.javac.comp.Check.CheckContext; 37import com.sun.tools.javac.comp.DeferredAttr.AttrMode; 38import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext; 39import com.sun.tools.javac.comp.DeferredAttr.DeferredType; 40import com.sun.tools.javac.comp.DeferredAttr.DeferredTypeCompleter; 41import com.sun.tools.javac.comp.DeferredAttr.LambdaReturnScanner; 42import com.sun.tools.javac.comp.Infer.PartiallyInferredMethodType; 43import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase; 44import com.sun.tools.javac.tree.JCTree; 45import com.sun.tools.javac.tree.JCTree.JCConditional; 46import com.sun.tools.javac.tree.JCTree.JCExpression; 47import com.sun.tools.javac.tree.JCTree.JCLambda; 48import com.sun.tools.javac.tree.JCTree.JCLambda.ParameterKind; 49import com.sun.tools.javac.tree.JCTree.JCMemberReference; 50import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; 51import com.sun.tools.javac.tree.JCTree.JCNewClass; 52import com.sun.tools.javac.tree.JCTree.JCParens; 53import com.sun.tools.javac.tree.JCTree.JCReturn; 54import com.sun.tools.javac.tree.TreeCopier; 55import com.sun.tools.javac.tree.TreeInfo; 56import com.sun.tools.javac.util.Assert; 57import com.sun.tools.javac.util.Context; 58import com.sun.tools.javac.util.DiagnosticSource; 59import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 60import com.sun.tools.javac.util.List; 61import com.sun.tools.javac.util.ListBuffer; 62import com.sun.tools.javac.util.Log; 63 64import java.util.HashMap; 65import java.util.LinkedHashMap; 66import java.util.Map; 67import java.util.Optional; 68import java.util.function.Function; 69import java.util.function.Supplier; 70 71import static com.sun.tools.javac.code.TypeTag.ARRAY; 72import static com.sun.tools.javac.code.TypeTag.DEFERRED; 73import static com.sun.tools.javac.code.TypeTag.FORALL; 74import static com.sun.tools.javac.code.TypeTag.METHOD; 75import static com.sun.tools.javac.code.TypeTag.VOID; 76 77/** 78 * This class performs attribution of method/constructor arguments when target-typing is enabled 79 * (source >= 8); for each argument that is potentially a poly expression, this class builds 80 * a rich representation (see {@link ArgumentType} which can then be used for performing fast overload 81 * checks without requiring multiple attribution passes over the same code. 82 * 83 * The attribution strategy for a given method/constructor argument A is as follows: 84 * 85 * - if A is potentially a poly expression (i.e. diamond instance creation expression), a speculative 86 * pass over A is performed; the results of such speculative attribution are then saved in a special 87 * type, so that enclosing overload resolution can be carried by simply checking compatibility against the 88 * type determined during this speculative pass. 89 * 90 * - if A is a standalone expression, regular atributtion takes place. 91 * 92 * To minimize the speculative work, a cache is used, so that already computed argument types 93 * associated with a given unique source location are never recomputed multiple times. 94 */ 95public class ArgumentAttr extends JCTree.Visitor { 96 97 protected static final Context.Key<ArgumentAttr> methodAttrKey = new Context.Key<>(); 98 99 private final DeferredAttr deferredAttr; 100 private final Attr attr; 101 private final Symtab syms; 102 private final Log log; 103 104 /** Attribution environment to be used. */ 105 private Env<AttrContext> env; 106 107 /** Result of method attribution. */ 108 private Type result; 109 110 /** Cache for argument types; behavior is influences by the currrently selected cache policy. */ 111 Map<UniquePos, ArgumentType<?>> argumentTypeCache = new LinkedHashMap<>(); 112 113 public static ArgumentAttr instance(Context context) { 114 ArgumentAttr instance = context.get(methodAttrKey); 115 if (instance == null) 116 instance = new ArgumentAttr(context); 117 return instance; 118 } 119 120 protected ArgumentAttr(Context context) { 121 context.put(methodAttrKey, this); 122 deferredAttr = DeferredAttr.instance(context); 123 attr = Attr.instance(context); 124 syms = Symtab.instance(context); 125 log = Log.instance(context); 126 } 127 128 /** 129 * Set the results of method attribution. 130 */ 131 void setResult(JCExpression tree, Type type) { 132 result = type; 133 if (env.info.isSpeculative) { 134 //if we are in a speculative branch we can save the type in the tree itself 135 //as there's no risk of polluting the original tree. 136 tree.type = result; 137 } 138 } 139 140 /** 141 * Checks a type in the speculative tree against a given result; the type can be either a plain 142 * type or an argument type,in which case a more complex check is required. 143 */ 144 Type checkSpeculative(JCExpression expr, ResultInfo resultInfo) { 145 return checkSpeculative(expr, expr.type, resultInfo); 146 } 147 148 /** 149 * Checks a type in the speculative tree against a given result; the type can be either a plain 150 * type or an argument type,in which case a more complex check is required. 151 */ 152 Type checkSpeculative(DiagnosticPosition pos, Type t, ResultInfo resultInfo) { 153 if (t.hasTag(DEFERRED)) { 154 return ((DeferredType)t).check(resultInfo); 155 } else { 156 return resultInfo.check(pos, t); 157 } 158 } 159 160 /** 161 * Returns a local caching context in which argument types can safely be cached without 162 * the risk of polluting enclosing contexts. This is useful when attempting speculative 163 * attribution of potentially erroneous expressions, which could end up polluting the cache. 164 */ 165 LocalCacheContext withLocalCacheContext() { 166 return new LocalCacheContext(); 167 } 168 169 /** 170 * Local cache context; this class keeps track of the previous cache and reverts to it 171 * when the {@link LocalCacheContext#leave()} method is called. 172 */ 173 class LocalCacheContext { 174 Map<UniquePos, ArgumentType<?>> prevCache; 175 176 public LocalCacheContext() { 177 this.prevCache = argumentTypeCache; 178 argumentTypeCache = new HashMap<>(); 179 } 180 181 public void leave() { 182 argumentTypeCache = prevCache; 183 } 184 } 185 186 /** 187 * Main entry point for attributing an argument with given tree and attribution environment. 188 */ 189 Type attribArg(JCTree tree, Env<AttrContext> env) { 190 Env<AttrContext> prevEnv = this.env; 191 try { 192 this.env = env; 193 tree.accept(this); 194 return result; 195 } finally { 196 this.env = prevEnv; 197 } 198 } 199 200 @Override 201 public void visitTree(JCTree that) { 202 //delegates to Attr 203 that.accept(attr); 204 result = attr.result; 205 } 206 207 /** 208 * Process a method argument; this method takes care of performing a speculative pass over the 209 * argument tree and calling a well-defined entry point to build the argument type associated 210 * with such tree. 211 */ 212 @SuppressWarnings("unchecked") 213 <T extends JCExpression, Z extends ArgumentType<T>> void processArg(T that, Function<T, Z> argumentTypeFactory) { 214 UniquePos pos = new UniquePos(that); 215 processArg(that, () -> { 216 T speculativeTree = (T)deferredAttr.attribSpeculative(that, env, attr.new MethodAttrInfo() { 217 @Override 218 protected void attr(JCTree tree, Env<AttrContext> env) { 219 //avoid speculative attribution loops 220 if (!new UniquePos(tree).equals(pos)) { 221 super.attr(tree, env); 222 } else { 223 visitTree(tree); 224 } 225 } 226 }); 227 return argumentTypeFactory.apply(speculativeTree); 228 }); 229 } 230 231 /** 232 * Process a method argument; this method allows the caller to specify a custom speculative attribution 233 * logic (this is used e.g. for lambdas). 234 */ 235 @SuppressWarnings("unchecked") 236 <T extends JCExpression, Z extends ArgumentType<T>> void processArg(T that, Supplier<Z> argumentTypeFactory) { 237 UniquePos pos = new UniquePos(that); 238 Z cached = (Z)argumentTypeCache.get(pos); 239 if (cached != null) { 240 //dup existing speculative type 241 setResult(that, cached.dup(that, env)); 242 } else { 243 Z res = argumentTypeFactory.get(); 244 argumentTypeCache.put(pos, res); 245 setResult(that, res); 246 } 247 } 248 249 @Override 250 public void visitParens(JCParens that) { 251 processArg(that, speculativeTree -> new ParensType(that, env, speculativeTree)); 252 } 253 254 @Override 255 public void visitConditional(JCConditional that) { 256 processArg(that, speculativeTree -> new ConditionalType(that, env, speculativeTree)); 257 } 258 259 @Override 260 public void visitReference(JCMemberReference tree) { 261 //perform arity-based check 262 Env<AttrContext> localEnv = env.dup(tree); 263 JCExpression exprTree; 264 exprTree = (JCExpression)deferredAttr.attribSpeculative(tree.getQualifierExpression(), localEnv, 265 attr.memberReferenceQualifierResult(tree), 266 withLocalCacheContext()); 267 JCMemberReference mref2 = new TreeCopier<Void>(attr.make).copy(tree); 268 mref2.expr = exprTree; 269 Symbol lhsSym = TreeInfo.symbol(exprTree); 270 localEnv.info.selectSuper = lhsSym != null && lhsSym.name == lhsSym.name.table.names._super; 271 Symbol res = 272 attr.rs.getMemberReference(tree, localEnv, mref2, 273 exprTree.type, tree.name); 274 if (!res.kind.isResolutionError()) { 275 tree.sym = res; 276 } 277 if (res.kind.isResolutionTargetError() || 278 res.type != null && res.type.hasTag(FORALL) || 279 (res.flags() & Flags.VARARGS) != 0 || 280 (TreeInfo.isStaticSelector(exprTree, tree.name.table.names) && 281 exprTree.type.isRaw() && !exprTree.type.hasTag(ARRAY))) { 282 tree.setOverloadKind(JCMemberReference.OverloadKind.OVERLOADED); 283 } else { 284 tree.setOverloadKind(JCMemberReference.OverloadKind.UNOVERLOADED); 285 } 286 //return a plain old deferred type for this 287 setResult(tree, deferredAttr.new DeferredType(tree, env)); 288 } 289 290 @Override 291 public void visitLambda(JCLambda that) { 292 if (that.paramKind == ParameterKind.EXPLICIT) { 293 //if lambda is explicit, we can save info in the corresponding argument type 294 processArg(that, () -> { 295 JCLambda speculativeLambda = 296 deferredAttr.attribSpeculativeLambda(that, env, attr.methodAttrInfo); 297 return new ExplicitLambdaType(that, env, speculativeLambda); 298 }); 299 } else { 300 //otherwise just use a deferred type 301 setResult(that, deferredAttr.new DeferredType(that, env)); 302 } 303 } 304 305 @Override 306 public void visitApply(JCMethodInvocation that) { 307 if (that.getTypeArguments().isEmpty()) { 308 processArg(that, speculativeTree -> new ResolvedMethodType(that, env, speculativeTree)); 309 } else { 310 //not a poly expression, just call Attr 311 setResult(that, attr.attribTree(that, env, attr.unknownExprInfo)); 312 } 313 } 314 315 @Override 316 public void visitNewClass(JCNewClass that) { 317 if (TreeInfo.isDiamond(that)) { 318 processArg(that, speculativeTree -> new ResolvedConstructorType(that, env, speculativeTree)); 319 } else { 320 //not a poly expression, just call Attr 321 setResult(that, attr.attribTree(that, env, attr.unknownExprInfo)); 322 } 323 } 324 325 /** 326 * An argument type is similar to a plain deferred type; the most important difference is that 327 * the completion logic associated with argument types allows speculative attribution to be skipped 328 * during overload resolution - that is, an argument type always has enough information to 329 * perform an overload check without the need of calling back to Attr. This extra information 330 * is typically stored in the form of a speculative tree. 331 */ 332 abstract class ArgumentType<T extends JCExpression> extends DeferredType implements DeferredTypeCompleter { 333 334 /** The speculative tree carrying type information. */ 335 T speculativeTree; 336 337 /** Types associated with this argument (one type per possible target result). */ 338 Map<ResultInfo, Type> speculativeTypes; 339 340 public ArgumentType(JCExpression tree, Env<AttrContext> env, T speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 341 deferredAttr.super(tree, env); 342 this.speculativeTree = speculativeTree; 343 this.speculativeTypes = speculativeTypes; 344 } 345 346 @Override 347 final DeferredTypeCompleter completer() { 348 return this; 349 } 350 351 @Override 352 final public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 353 Assert.check(dt == this); 354 if (deferredAttrContext.mode == AttrMode.SPECULATIVE) { 355 Type t = (resultInfo.pt == Type.recoveryType) ? 356 deferredAttr.basicCompleter.complete(dt, resultInfo, deferredAttrContext) : 357 overloadCheck(resultInfo, deferredAttrContext); 358 speculativeTypes.put(resultInfo, t); 359 return t; 360 } else { 361 if (!env.info.isSpeculative) { 362 argumentTypeCache.remove(new UniquePos(dt.tree)); 363 } 364 return deferredAttr.basicCompleter.complete(dt, resultInfo, deferredAttrContext); 365 } 366 } 367 368 @Override 369 Type speculativeType(Symbol msym, MethodResolutionPhase phase) { 370 if (pertinentToApplicability) { 371 for (Map.Entry<ResultInfo, Type> _entry : speculativeTypes.entrySet()) { 372 DeferredAttrContext deferredAttrContext = _entry.getKey().checkContext.deferredAttrContext(); 373 if (deferredAttrContext.phase == phase && deferredAttrContext.msym == msym) { 374 return _entry.getValue(); 375 } 376 } 377 return Type.noType; 378 } else { 379 return super.speculativeType(msym, phase); 380 } 381 } 382 383 @Override 384 JCTree speculativeTree(DeferredAttrContext deferredAttrContext) { 385 return pertinentToApplicability ? speculativeTree : super.speculativeTree(deferredAttrContext); 386 } 387 388 /** 389 * Performs an overload check against a given target result. 390 */ 391 abstract Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext); 392 393 /** 394 * Creates a copy of this argument type with given tree and environment. 395 */ 396 abstract ArgumentType<T> dup(T tree, Env<AttrContext> env); 397 } 398 399 /** 400 * Argument type for parenthesized expression. 401 */ 402 class ParensType extends ArgumentType<JCParens> { 403 ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens) { 404 this(tree, env, speculativeParens, new HashMap<>()); 405 } 406 407 ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens, Map<ResultInfo, Type> speculativeTypes) { 408 super(tree, env, speculativeParens, speculativeTypes); 409 } 410 411 @Override 412 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 413 return checkSpeculative(speculativeTree.expr, resultInfo); 414 } 415 416 @Override 417 ArgumentType<JCParens> dup(JCParens tree, Env<AttrContext> env) { 418 return new ParensType(tree, env, speculativeTree, speculativeTypes); 419 } 420 } 421 422 /** 423 * Argument type for conditionals. 424 */ 425 class ConditionalType extends ArgumentType<JCConditional> { 426 ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) { 427 this(tree, env, speculativeCond, new HashMap<>()); 428 } 429 430 ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) { 431 super(tree, env, speculativeCond, speculativeTypes); 432 } 433 434 @Override 435 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 436 ResultInfo localInfo = resultInfo.dup(attr.conditionalContext(resultInfo.checkContext)); 437 if (speculativeTree.isStandalone()) { 438 return localInfo.check(speculativeTree, speculativeTree.type); 439 } else if (resultInfo.pt.hasTag(VOID)) { 440 //this means we are returning a poly conditional from void-compatible lambda expression 441 resultInfo.checkContext.report(tree, attr.diags.fragment("conditional.target.cant.be.void")); 442 return attr.types.createErrorType(resultInfo.pt); 443 } else { 444 //poly 445 checkSpeculative(speculativeTree.truepart, localInfo); 446 checkSpeculative(speculativeTree.falsepart, localInfo); 447 return localInfo.pt; 448 } 449 } 450 451 @Override 452 ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) { 453 return new ConditionalType(tree, env, speculativeTree, speculativeTypes); 454 } 455 } 456 457 /** 458 * Argument type for explicit lambdas. 459 */ 460 class ExplicitLambdaType extends ArgumentType<JCLambda> { 461 462 /** List of argument types (lazily populated). */ 463 Optional<List<Type>> argtypes = Optional.empty(); 464 465 /** List of return expressions (lazily populated). */ 466 Optional<List<JCReturn>> returnExpressions = Optional.empty(); 467 468 ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda) { 469 this(originalLambda, env, speculativeLambda, new HashMap<>()); 470 } 471 472 ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda, Map<ResultInfo, Type> speculativeTypes) { 473 super(originalLambda, env, speculativeLambda, speculativeTypes); 474 } 475 476 /** Compute argument types (if needed). */ 477 List<Type> argtypes() { 478 return argtypes.orElseGet(() -> { 479 List<Type> res = TreeInfo.types(speculativeTree.params); 480 argtypes = Optional.of(res); 481 return res; 482 }); 483 } 484 485 /** Compute return expressions (if needed). */ 486 List<JCReturn> returnExpressions() { 487 return returnExpressions.orElseGet(() -> { 488 final List<JCReturn> res; 489 if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION) { 490 res = List.of(attr.make.Return((JCExpression)speculativeTree.body)); 491 } else { 492 ListBuffer<JCReturn> returnExpressions = new ListBuffer<>(); 493 new LambdaReturnScanner() { 494 @Override 495 public void visitReturn(JCReturn tree) { 496 returnExpressions.add(tree); 497 } 498 }.scan(speculativeTree.body); 499 res = returnExpressions.toList(); 500 } 501 returnExpressions = Optional.of(res); 502 return res; 503 }); 504 } 505 506 @Override 507 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 508 try { 509 //compute target-type; this logic could be shared with Attr 510 TargetInfo targetInfo = attr.getTargetInfo(speculativeTree, resultInfo, argtypes()); 511 Type lambdaType = targetInfo.descriptor; 512 Type currentTarget = targetInfo.target; 513 //check compatibility 514 checkLambdaCompatible(lambdaType, resultInfo); 515 return currentTarget; 516 } catch (FunctionDescriptorLookupError ex) { 517 resultInfo.checkContext.report(null, ex.getDiagnostic()); 518 return null; //cannot get here 519 } 520 } 521 522 /** Check lambda against given target result */ 523 private void checkLambdaCompatible(Type descriptor, ResultInfo resultInfo) { 524 CheckContext checkContext = resultInfo.checkContext; 525 ResultInfo bodyResultInfo = attr.lambdaBodyResult(speculativeTree, descriptor, resultInfo); 526 for (JCReturn ret : returnExpressions()) { 527 Type t = getReturnType(ret); 528 if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION || !t.hasTag(VOID)) { 529 checkSpeculative(ret.expr, t, bodyResultInfo); 530 } 531 } 532 533 attr.checkLambdaCompatible(speculativeTree, descriptor, checkContext); 534 } 535 536 /** Get the type associated with given return expression. */ 537 Type getReturnType(JCReturn ret) { 538 if (ret.expr == null) { 539 return syms.voidType; 540 } else { 541 return ret.expr.type; 542 } 543 } 544 545 @Override 546 ArgumentType<JCLambda> dup(JCLambda tree, Env<AttrContext> env) { 547 return new ExplicitLambdaType(tree, env, speculativeTree, speculativeTypes); 548 } 549 } 550 551 /** 552 * Argument type for methods/constructors. 553 */ 554 abstract class ResolvedMemberType<E extends JCExpression> extends ArgumentType<E> { 555 556 public ResolvedMemberType(JCExpression tree, Env<AttrContext> env, E speculativeMethod, Map<ResultInfo, Type> speculativeTypes) { 557 super(tree, env, speculativeMethod, speculativeTypes); 558 } 559 560 @Override 561 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 562 Type mtype = methodType(); 563 ResultInfo localInfo = resultInfo(resultInfo); 564 if (mtype != null && mtype.hasTag(METHOD) && mtype.isPartial()) { 565 Type t = ((PartiallyInferredMethodType)mtype).check(localInfo); 566 if (!deferredAttrContext.inferenceContext.free(localInfo.pt)) { 567 speculativeTypes.put(localInfo, t); 568 return localInfo.check(tree.pos(), t); 569 } else { 570 return t; 571 } 572 } else { 573 Type t = localInfo.check(tree.pos(), speculativeTree.type); 574 speculativeTypes.put(localInfo, t); 575 return t; 576 } 577 } 578 579 /** 580 * Get the result info to be used for performing an overload check. 581 */ 582 abstract ResultInfo resultInfo(ResultInfo resultInfo); 583 584 /** 585 * Get the method type to be used for performing an overload check. 586 */ 587 abstract Type methodType(); 588 } 589 590 /** 591 * Argument type for methods. 592 */ 593 class ResolvedMethodType extends ResolvedMemberType<JCMethodInvocation> { 594 595 public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree) { 596 this(tree, env, speculativeTree, new HashMap<>()); 597 } 598 599 public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 600 super(tree, env, speculativeTree, speculativeTypes); 601 } 602 603 @Override 604 ResultInfo resultInfo(ResultInfo resultInfo) { 605 return resultInfo; 606 } 607 608 @Override 609 Type methodType() { 610 return speculativeTree.meth.type; 611 } 612 613 @Override 614 ArgumentType<JCMethodInvocation> dup(JCMethodInvocation tree, Env<AttrContext> env) { 615 return new ResolvedMethodType(tree, env, speculativeTree, speculativeTypes); 616 } 617 } 618 619 /** 620 * Argument type for constructors. 621 */ 622 class ResolvedConstructorType extends ResolvedMemberType<JCNewClass> { 623 624 public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree) { 625 this(tree, env, speculativeTree, new HashMap<>()); 626 } 627 628 public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 629 super(tree, env, speculativeTree, speculativeTypes); 630 } 631 632 @Override 633 ResultInfo resultInfo(ResultInfo resultInfo) { 634 return resultInfo.dup(attr.diamondContext(speculativeTree, speculativeTree.clazz.type.tsym, resultInfo.checkContext)); 635 } 636 637 @Override 638 Type methodType() { 639 return (speculativeTree.constructorType != null) ? 640 speculativeTree.constructorType.baseType() : syms.errType; 641 } 642 643 @Override 644 ArgumentType<JCNewClass> dup(JCNewClass tree, Env<AttrContext> env) { 645 return new ResolvedConstructorType(tree, env, speculativeTree, speculativeTypes); 646 } 647 } 648 649 /** 650 * An instance of this class represents a unique position in a compilation unit. A unique 651 * position is made up of (i) a unique position in a source file (char offset) and (ii) 652 * a source file info. 653 */ 654 class UniquePos { 655 656 /** Char offset. */ 657 int pos; 658 659 /** Source info. */ 660 DiagnosticSource source; 661 662 UniquePos(JCTree tree) { 663 this.pos = tree.pos; 664 this.source = log.currentSource(); 665 } 666 667 @Override 668 public int hashCode() { 669 return pos << 16 + source.hashCode(); 670 } 671 672 @Override 673 public boolean equals(Object obj) { 674 if (obj instanceof UniquePos) { 675 UniquePos that = (UniquePos)obj; 676 return pos == that.pos && source == that.source; 677 } else { 678 return false; 679 } 680 } 681 682 @Override 683 public String toString() { 684 return source.getFile().getName() + " @ " + source.getLineNumber(pos); 685 } 686 } 687} 688