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