ArgumentAttr.java revision 3224:4a4f58f3b344
1/* 2 * Copyright (c) 2015, 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 = (JCExpression)deferredAttr.attribSpeculative(tree.getQualifierExpression(), localEnv, 264 attr.memberReferenceQualifierResult(tree)); 265 JCMemberReference mref2 = new TreeCopier<Void>(attr.make).copy(tree); 266 mref2.expr = exprTree; 267 Symbol lhsSym = TreeInfo.symbol(exprTree); 268 localEnv.info.selectSuper = lhsSym != null && lhsSym.name == lhsSym.name.table.names._super; 269 Symbol res = 270 attr.rs.getMemberReference(tree, localEnv, mref2, 271 exprTree.type, tree.name); 272 if (!res.kind.isResolutionError()) { 273 tree.sym = res; 274 } 275 if (res.kind.isResolutionTargetError() || 276 res.type != null && res.type.hasTag(FORALL) || 277 (res.flags() & Flags.VARARGS) != 0 || 278 (TreeInfo.isStaticSelector(exprTree, tree.name.table.names) && 279 exprTree.type.isRaw() && !exprTree.type.hasTag(ARRAY))) { 280 tree.overloadKind = JCMemberReference.OverloadKind.OVERLOADED; 281 } else { 282 tree.overloadKind = JCMemberReference.OverloadKind.UNOVERLOADED; 283 } 284 //return a plain old deferred type for this 285 setResult(tree, deferredAttr.new DeferredType(tree, env)); 286 } 287 288 @Override 289 public void visitLambda(JCLambda that) { 290 if (that.paramKind == ParameterKind.EXPLICIT) { 291 //if lambda is explicit, we can save info in the corresponding argument type 292 processArg(that, () -> { 293 JCLambda speculativeLambda = 294 deferredAttr.attribSpeculativeLambda(that, env, attr.methodAttrInfo); 295 return new ExplicitLambdaType(that, env, speculativeLambda); 296 }); 297 } else { 298 //otherwise just use a deferred type 299 setResult(that, deferredAttr.new DeferredType(that, env)); 300 } 301 } 302 303 @Override 304 public void visitApply(JCMethodInvocation that) { 305 if (that.getTypeArguments().isEmpty()) { 306 processArg(that, speculativeTree -> new ResolvedMethodType(that, env, speculativeTree)); 307 } else { 308 //not a poly expression, just call Attr 309 setResult(that, attr.attribTree(that, env, attr.unknownExprInfo)); 310 } 311 } 312 313 @Override 314 public void visitNewClass(JCNewClass that) { 315 if (TreeInfo.isDiamond(that)) { 316 processArg(that, speculativeTree -> new ResolvedConstructorType(that, env, speculativeTree)); 317 } else { 318 //not a poly expression, just call Attr 319 setResult(that, attr.attribTree(that, env, attr.unknownExprInfo)); 320 } 321 } 322 323 /** 324 * An argument type is similar to a plain deferred type; the most important difference is that 325 * the completion logic associated with argument types allows speculative attribution to be skipped 326 * during overload resolution - that is, an argument type always has enough information to 327 * perform an overload check without the need of calling back to Attr. This extra information 328 * is typically stored in the form of a speculative tree. 329 */ 330 abstract class ArgumentType<T extends JCExpression> extends DeferredType implements DeferredTypeCompleter { 331 332 /** The speculative tree carrying type information. */ 333 T speculativeTree; 334 335 /** Types associated with this argument (one type per possible target result). */ 336 Map<ResultInfo, Type> speculativeTypes; 337 338 public ArgumentType(JCExpression tree, Env<AttrContext> env, T speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 339 deferredAttr.super(tree, env); 340 this.speculativeTree = speculativeTree; 341 this.speculativeTypes = speculativeTypes; 342 } 343 344 @Override 345 final DeferredTypeCompleter completer() { 346 return this; 347 } 348 349 @Override 350 final public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 351 Assert.check(dt == this); 352 if (deferredAttrContext.mode == AttrMode.SPECULATIVE) { 353 Type t = (resultInfo.pt == Type.recoveryType) ? 354 deferredAttr.basicCompleter.complete(dt, resultInfo, deferredAttrContext) : 355 overloadCheck(resultInfo, deferredAttrContext); 356 speculativeTypes.put(resultInfo, t); 357 return t; 358 } else { 359 if (!env.info.isSpeculative) { 360 argumentTypeCache.remove(new UniquePos(dt.tree)); 361 } 362 return deferredAttr.basicCompleter.complete(dt, resultInfo, deferredAttrContext); 363 } 364 } 365 366 @Override 367 Type speculativeType(Symbol msym, MethodResolutionPhase phase) { 368 for (Map.Entry<ResultInfo, Type> _entry : speculativeTypes.entrySet()) { 369 DeferredAttrContext deferredAttrContext = _entry.getKey().checkContext.deferredAttrContext(); 370 if (deferredAttrContext.phase == phase && deferredAttrContext.msym == msym) { 371 return _entry.getValue(); 372 } 373 } 374 return Type.noType; 375 } 376 377 @Override 378 JCTree speculativeTree(DeferredAttrContext deferredAttrContext) { 379 return speculativeTree; 380 } 381 382 /** 383 * Performs an overload check against a given target result. 384 */ 385 abstract Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext); 386 387 /** 388 * Creates a copy of this argument type with given tree and environment. 389 */ 390 abstract ArgumentType<T> dup(T tree, Env<AttrContext> env); 391 } 392 393 /** 394 * Argument type for parenthesized expression. 395 */ 396 class ParensType extends ArgumentType<JCParens> { 397 ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens) { 398 this(tree, env, speculativeParens, new HashMap<>()); 399 } 400 401 ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens, Map<ResultInfo, Type> speculativeTypes) { 402 super(tree, env, speculativeParens, speculativeTypes); 403 } 404 405 @Override 406 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 407 return checkSpeculative(speculativeTree.expr, resultInfo); 408 } 409 410 @Override 411 ArgumentType<JCParens> dup(JCParens tree, Env<AttrContext> env) { 412 return new ParensType(tree, env, speculativeTree, speculativeTypes); 413 } 414 } 415 416 /** 417 * Argument type for conditionals. 418 */ 419 class ConditionalType extends ArgumentType<JCConditional> { 420 ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) { 421 this(tree, env, speculativeCond, new HashMap<>()); 422 } 423 424 ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) { 425 super(tree, env, speculativeCond, speculativeTypes); 426 } 427 428 @Override 429 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 430 ResultInfo localInfo = resultInfo.dup(attr.conditionalContext(resultInfo.checkContext)); 431 if (speculativeTree.isStandalone()) { 432 return localInfo.check(speculativeTree, speculativeTree.type); 433 } else if (resultInfo.pt.hasTag(VOID)) { 434 //this means we are returning a poly conditional from void-compatible lambda expression 435 resultInfo.checkContext.report(tree, attr.diags.fragment("conditional.target.cant.be.void")); 436 return attr.types.createErrorType(resultInfo.pt); 437 } else { 438 //poly 439 checkSpeculative(speculativeTree.truepart, localInfo); 440 checkSpeculative(speculativeTree.falsepart, localInfo); 441 return localInfo.pt; 442 } 443 } 444 445 @Override 446 ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) { 447 return new ConditionalType(tree, env, speculativeTree, speculativeTypes); 448 } 449 } 450 451 /** 452 * Argument type for explicit lambdas. 453 */ 454 class ExplicitLambdaType extends ArgumentType<JCLambda> { 455 456 /** List of argument types (lazily populated). */ 457 Optional<List<Type>> argtypes = Optional.empty(); 458 459 /** List of return expressions (lazily populated). */ 460 Optional<List<JCReturn>> returnExpressions = Optional.empty(); 461 462 ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda) { 463 this(originalLambda, env, speculativeLambda, new HashMap<>()); 464 } 465 466 ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda, Map<ResultInfo, Type> speculativeTypes) { 467 super(originalLambda, env, speculativeLambda, speculativeTypes); 468 } 469 470 /** Compute argument types (if needed). */ 471 List<Type> argtypes() { 472 return argtypes.orElseGet(() -> { 473 List<Type> res = TreeInfo.types(speculativeTree.params); 474 argtypes = Optional.of(res); 475 return res; 476 }); 477 } 478 479 /** Compute return expressions (if needed). */ 480 List<JCReturn> returnExpressions() { 481 return returnExpressions.orElseGet(() -> { 482 final List<JCReturn> res; 483 if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION) { 484 res = List.of(attr.make.Return((JCExpression)speculativeTree.body)); 485 } else { 486 ListBuffer<JCReturn> returnExpressions = new ListBuffer<>(); 487 new LambdaReturnScanner() { 488 @Override 489 public void visitReturn(JCReturn tree) { 490 returnExpressions.add(tree); 491 } 492 }.scan(speculativeTree.body); 493 res = returnExpressions.toList(); 494 } 495 returnExpressions = Optional.of(res); 496 return res; 497 }); 498 } 499 500 @Override 501 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 502 try { 503 //compute target-type; this logic could be shared with Attr 504 TargetInfo targetInfo = attr.getTargetInfo(speculativeTree, resultInfo, argtypes()); 505 Type lambdaType = targetInfo.descriptor; 506 Type currentTarget = targetInfo.target; 507 //check compatibility 508 checkLambdaCompatible(lambdaType, resultInfo); 509 return currentTarget; 510 } catch (FunctionDescriptorLookupError ex) { 511 resultInfo.checkContext.report(null, ex.getDiagnostic()); 512 return null; //cannot get here 513 } 514 } 515 516 /** Check lambda against given target result */ 517 private void checkLambdaCompatible(Type descriptor, ResultInfo resultInfo) { 518 CheckContext checkContext = resultInfo.checkContext; 519 ResultInfo bodyResultInfo = attr.lambdaBodyResult(speculativeTree, descriptor, resultInfo); 520 for (JCReturn ret : returnExpressions()) { 521 Type t = getReturnType(ret); 522 if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION || !t.hasTag(VOID)) { 523 checkSpeculative(ret.expr, t, bodyResultInfo); 524 } 525 } 526 527 attr.checkLambdaCompatible(speculativeTree, descriptor, checkContext); 528 } 529 530 /** Get the type associated with given return expression. */ 531 Type getReturnType(JCReturn ret) { 532 if (ret.expr == null) { 533 return syms.voidType; 534 } else { 535 return ret.expr.type; 536 } 537 } 538 539 @Override 540 ArgumentType<JCLambda> dup(JCLambda tree, Env<AttrContext> env) { 541 return new ExplicitLambdaType(tree, env, speculativeTree, speculativeTypes); 542 } 543 } 544 545 /** 546 * Argument type for methods/constructors. 547 */ 548 abstract class ResolvedMemberType<E extends JCExpression> extends ArgumentType<E> { 549 550 public ResolvedMemberType(JCExpression tree, Env<AttrContext> env, E speculativeMethod, Map<ResultInfo, Type> speculativeTypes) { 551 super(tree, env, speculativeMethod, speculativeTypes); 552 } 553 554 @Override 555 Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { 556 Type mtype = methodType(); 557 ResultInfo localInfo = resultInfo(resultInfo); 558 if (mtype != null && mtype.hasTag(METHOD) && mtype.isPartial()) { 559 Type t = ((PartiallyInferredMethodType)mtype).check(localInfo); 560 if (!deferredAttrContext.inferenceContext.free(localInfo.pt)) { 561 speculativeTypes.put(localInfo, t); 562 return localInfo.check(tree.pos(), t); 563 } else { 564 return t; 565 } 566 } else { 567 Type t = localInfo.check(tree.pos(), speculativeTree.type); 568 speculativeTypes.put(localInfo, t); 569 return t; 570 } 571 } 572 573 /** 574 * Get the result info to be used for performing an overload check. 575 */ 576 abstract ResultInfo resultInfo(ResultInfo resultInfo); 577 578 /** 579 * Get the method type to be used for performing an overload check. 580 */ 581 abstract Type methodType(); 582 } 583 584 /** 585 * Argument type for methods. 586 */ 587 class ResolvedMethodType extends ResolvedMemberType<JCMethodInvocation> { 588 589 public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree) { 590 this(tree, env, speculativeTree, new HashMap<>()); 591 } 592 593 public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 594 super(tree, env, speculativeTree, speculativeTypes); 595 } 596 597 @Override 598 ResultInfo resultInfo(ResultInfo resultInfo) { 599 return resultInfo; 600 } 601 602 @Override 603 Type methodType() { 604 return speculativeTree.meth.type; 605 } 606 607 @Override 608 ArgumentType<JCMethodInvocation> dup(JCMethodInvocation tree, Env<AttrContext> env) { 609 return new ResolvedMethodType(tree, env, speculativeTree, speculativeTypes); 610 } 611 } 612 613 /** 614 * Argument type for constructors. 615 */ 616 class ResolvedConstructorType extends ResolvedMemberType<JCNewClass> { 617 618 public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree) { 619 this(tree, env, speculativeTree, new HashMap<>()); 620 } 621 622 public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree, Map<ResultInfo, Type> speculativeTypes) { 623 super(tree, env, speculativeTree, speculativeTypes); 624 } 625 626 @Override 627 ResultInfo resultInfo(ResultInfo resultInfo) { 628 return resultInfo.dup(attr.diamondContext(speculativeTree, speculativeTree.clazz.type.tsym, resultInfo.checkContext)); 629 } 630 631 @Override 632 Type methodType() { 633 return (speculativeTree.constructorType != null) ? 634 speculativeTree.constructorType.baseType() : syms.errType; 635 } 636 637 @Override 638 ArgumentType<JCNewClass> dup(JCNewClass tree, Env<AttrContext> env) { 639 return new ResolvedConstructorType(tree, env, speculativeTree, speculativeTypes); 640 } 641 } 642 643 /** 644 * An instance of this class represents a unique position in a compilation unit. A unique 645 * position is made up of (i) a unique position in a source file (char offset) and (ii) 646 * a source file info. 647 */ 648 class UniquePos { 649 650 /** Char offset. */ 651 int pos; 652 653 /** Source info. */ 654 DiagnosticSource source; 655 656 UniquePos(JCTree tree) { 657 this.pos = tree.pos; 658 this.source = log.currentSource(); 659 } 660 661 @Override 662 public int hashCode() { 663 return pos << 16 + source.hashCode(); 664 } 665 666 @Override 667 public boolean equals(Object obj) { 668 if (obj instanceof UniquePos) { 669 UniquePos that = (UniquePos)obj; 670 return pos == that.pos && source == that.source; 671 } else { 672 return false; 673 } 674 } 675 676 @Override 677 public String toString() { 678 return source.getFile().getName() + " @ " + source.getLineNumber(pos); 679 } 680 } 681} 682