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