ArgumentAttr.java revision 3041:37b60162a2db
1157016Sdes/*
298937Sdes * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
398937Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
498937Sdes *
598937Sdes * This code is free software; you can redistribute it and/or modify it
698937Sdes * under the terms of the GNU General Public License version 2 only, as
798937Sdes * published by the Free Software Foundation.  Oracle designates this
898937Sdes * particular file as subject to the "Classpath" exception as provided
998937Sdes * by Oracle in the LICENSE file that accompanied this code.
1098937Sdes *
1198937Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
1298937Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1398937Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14124208Sdes * version 2 for more details (a copy is included in the LICENSE file that
1598937Sdes * accompanied this code).
1698937Sdes *
1798937Sdes * You should have received a copy of the GNU General Public License version
1898937Sdes * 2 along with this work; if not, write to the Free Software Foundation,
1998937Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2098937Sdes *
2198937Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2298937Sdes * or visit www.oracle.com if you need additional information or have any
2398937Sdes * questions.
2498937Sdes */
2598937Sdes
2698937Sdespackage com.sun.tools.javac.comp;
2798937Sdes
2898937Sdesimport com.sun.source.tree.LambdaExpressionTree.BodyKind;
2998937Sdesimport com.sun.tools.javac.code.Flags;
3098937Sdesimport com.sun.tools.javac.code.Symbol;
31157016Sdesimport com.sun.tools.javac.code.Symtab;
32157016Sdesimport com.sun.tools.javac.code.Type;
3398937Sdesimport com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
3498937Sdesimport com.sun.tools.javac.comp.Attr.ResultInfo;
3598937Sdesimport com.sun.tools.javac.comp.Attr.TargetInfo;
3698937Sdesimport com.sun.tools.javac.comp.Check.CheckContext;
37162852Sdesimport com.sun.tools.javac.comp.DeferredAttr.AttrMode;
38162852Sdesimport com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
39162852Sdesimport com.sun.tools.javac.comp.DeferredAttr.DeferredType;
40162852Sdesimport com.sun.tools.javac.comp.DeferredAttr.DeferredTypeCompleter;
41162852Sdesimport com.sun.tools.javac.comp.DeferredAttr.LambdaReturnScanner;
42162852Sdesimport com.sun.tools.javac.comp.Infer.PartiallyInferredMethodType;
43162852Sdesimport com.sun.tools.javac.comp.Resolve.MethodResolutionPhase;
44162852Sdesimport com.sun.tools.javac.tree.JCTree;
45162852Sdesimport com.sun.tools.javac.tree.JCTree.JCConditional;
46162852Sdesimport com.sun.tools.javac.tree.JCTree.JCExpression;
47162852Sdesimport com.sun.tools.javac.tree.JCTree.JCLambda;
48162852Sdesimport com.sun.tools.javac.tree.JCTree.JCLambda.ParameterKind;
49162852Sdesimport com.sun.tools.javac.tree.JCTree.JCMemberReference;
50162852Sdesimport com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
5198937Sdesimport com.sun.tools.javac.tree.JCTree.JCNewClass;
52124208Sdesimport com.sun.tools.javac.tree.JCTree.JCParens;
5398937Sdesimport com.sun.tools.javac.tree.JCTree.JCReturn;
5498937Sdesimport com.sun.tools.javac.tree.TreeCopier;
5598937Sdesimport com.sun.tools.javac.tree.TreeInfo;
5698937Sdesimport com.sun.tools.javac.util.Assert;
5798937Sdesimport com.sun.tools.javac.util.Context;
5898937Sdesimport com.sun.tools.javac.util.DiagnosticSource;
5998937Sdesimport com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
6098937Sdesimport com.sun.tools.javac.util.List;
6198937Sdesimport com.sun.tools.javac.util.ListBuffer;
6298937Sdesimport com.sun.tools.javac.util.Log;
6398937Sdes
6498937Sdesimport java.util.HashMap;
6598937Sdesimport java.util.LinkedHashMap;
6698937Sdesimport java.util.Map;
6798937Sdesimport java.util.Optional;
6898937Sdesimport java.util.function.Function;
6998937Sdesimport java.util.function.Supplier;
7098937Sdes
7198937Sdesimport static com.sun.tools.javac.code.TypeTag.DEFERRED;
7298937Sdesimport static com.sun.tools.javac.code.TypeTag.FORALL;
7398937Sdesimport static com.sun.tools.javac.code.TypeTag.METHOD;
7498937Sdesimport static com.sun.tools.javac.code.TypeTag.VOID;
7598937Sdes
7698937Sdes/**
7798937Sdes * This class performs attribution of method/constructor arguments when target-typing is enabled
7898937Sdes * (source >= 8); for each argument that is potentially a poly expression, this class builds
7998937Sdes * a rich representation (see {@link ArgumentType} which can then be used for performing fast overload
8098937Sdes * checks without requiring multiple attribution passes over the same code.
8198937Sdes *
8298937Sdes * 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 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())) {
277            tree.overloadKind = JCMemberReference.OverloadKind.OVERLOADED;
278        } else {
279            tree.overloadKind = 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            for (Map.Entry<ResultInfo, Type> _entry : speculativeTypes.entrySet()) {
366                DeferredAttrContext deferredAttrContext = _entry.getKey().checkContext.deferredAttrContext();
367                if (deferredAttrContext.phase == phase && deferredAttrContext.msym == msym) {
368                    return _entry.getValue();
369                }
370            }
371            return Type.noType;
372        }
373
374        @Override
375        JCTree speculativeTree(DeferredAttrContext deferredAttrContext) {
376            return speculativeTree;
377        }
378
379        /**
380         * Performs an overload check against a given target result.
381         */
382        abstract Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
383
384        /**
385         * Creates a copy of this argument type with given tree and environment.
386         */
387        abstract ArgumentType<T> dup(T tree, Env<AttrContext> env);
388    }
389
390    /**
391     * Argument type for parenthesized expression.
392     */
393    class ParensType extends ArgumentType<JCParens> {
394        ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens) {
395            this(tree, env, speculativeParens, new HashMap<>());
396        }
397
398        ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens, Map<ResultInfo, Type> speculativeTypes) {
399           super(tree, env, speculativeParens, speculativeTypes);
400        }
401
402        @Override
403        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
404            return checkSpeculative(speculativeTree.expr, resultInfo);
405        }
406
407        @Override
408        ArgumentType<JCParens> dup(JCParens tree, Env<AttrContext> env) {
409            return new ParensType(tree, env, speculativeTree, speculativeTypes);
410        }
411    }
412
413    /**
414     * Argument type for conditionals.
415     */
416    class ConditionalType extends ArgumentType<JCConditional> {
417        ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) {
418            this(tree, env, speculativeCond, new HashMap<>());
419        }
420
421        ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) {
422           super(tree, env, speculativeCond, speculativeTypes);
423        }
424
425        @Override
426        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
427            ResultInfo localInfo = resultInfo.dup(attr.conditionalContext(resultInfo.checkContext));
428            if (speculativeTree.isStandalone()) {
429                return localInfo.check(speculativeTree, speculativeTree.type);
430            } else if (resultInfo.pt.hasTag(VOID)) {
431                //this means we are returning a poly conditional from void-compatible lambda expression
432                resultInfo.checkContext.report(tree, attr.diags.fragment("conditional.target.cant.be.void"));
433                return attr.types.createErrorType(resultInfo.pt);
434            } else {
435                //poly
436                checkSpeculative(speculativeTree.truepart, localInfo);
437                checkSpeculative(speculativeTree.falsepart, localInfo);
438                return localInfo.pt;
439            }
440        }
441
442        @Override
443        ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) {
444            return new ConditionalType(tree, env, speculativeTree, speculativeTypes);
445        }
446    }
447
448    /**
449     * Argument type for explicit lambdas.
450     */
451    class ExplicitLambdaType extends ArgumentType<JCLambda> {
452
453        /** List of argument types (lazily populated). */
454        Optional<List<Type>> argtypes = Optional.empty();
455
456        /** List of return expressions (lazily populated). */
457        Optional<List<JCReturn>> returnExpressions = Optional.empty();
458
459        ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda) {
460            this(originalLambda, env, speculativeLambda, new HashMap<>());
461        }
462
463        ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda, Map<ResultInfo, Type> speculativeTypes) {
464            super(originalLambda, env, speculativeLambda, speculativeTypes);
465        }
466
467        /** Compute argument types (if needed). */
468        List<Type> argtypes() {
469            return argtypes.orElseGet(() -> {
470                List<Type> res = TreeInfo.types(speculativeTree.params);
471                argtypes = Optional.of(res);
472                return res;
473            });
474        }
475
476        /** Compute return expressions (if needed). */
477        List<JCReturn> returnExpressions() {
478            return returnExpressions.orElseGet(() -> {
479                final List<JCReturn> res;
480                if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION) {
481                    res = List.of(attr.make.Return((JCExpression)speculativeTree.body));
482                } else {
483                    ListBuffer<JCReturn> returnExpressions = new ListBuffer<>();
484                    new LambdaReturnScanner() {
485                        @Override
486                        public void visitReturn(JCReturn tree) {
487                            returnExpressions.add(tree);
488                        }
489                    }.scan(speculativeTree.body);
490                    res = returnExpressions.toList();
491                }
492                returnExpressions = Optional.of(res);
493                return res;
494            });
495        }
496
497        @Override
498        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
499            try {
500                //compute target-type; this logic could be shared with Attr
501                TargetInfo targetInfo = attr.getTargetInfo(speculativeTree, resultInfo, argtypes());
502                Type lambdaType = targetInfo.descriptor;
503                Type currentTarget = targetInfo.target;
504                //check compatibility
505                checkLambdaCompatible(lambdaType, resultInfo);
506                return currentTarget;
507            } catch (FunctionDescriptorLookupError ex) {
508                resultInfo.checkContext.report(null, ex.getDiagnostic());
509                return null; //cannot get here
510            }
511        }
512
513        /** Check lambda against given target result */
514        private void checkLambdaCompatible(Type descriptor, ResultInfo resultInfo) {
515            CheckContext checkContext = resultInfo.checkContext;
516            ResultInfo bodyResultInfo = attr.lambdaBodyResult(speculativeTree, descriptor, resultInfo);
517            for (JCReturn ret : returnExpressions()) {
518                Type t = getReturnType(ret);
519                if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION || !t.hasTag(VOID)) {
520                    checkSpeculative(ret.expr, t, bodyResultInfo);
521                }
522            }
523
524            attr.checkLambdaCompatible(speculativeTree, descriptor, checkContext);
525        }
526
527        /** Get the type associated with given return expression. */
528        Type getReturnType(JCReturn ret) {
529            if (ret.expr == null) {
530                return syms.voidType;
531            } else {
532                return ret.expr.type;
533            }
534        }
535
536        @Override
537        ArgumentType<JCLambda> dup(JCLambda tree, Env<AttrContext> env) {
538            return new ExplicitLambdaType(tree, env, speculativeTree, speculativeTypes);
539        }
540    }
541
542    /**
543     * Argument type for methods/constructors.
544     */
545    abstract class ResolvedMemberType<E extends JCExpression> extends ArgumentType<E> {
546
547        public ResolvedMemberType(JCExpression tree, Env<AttrContext> env, E speculativeMethod, Map<ResultInfo, Type> speculativeTypes) {
548            super(tree, env, speculativeMethod, speculativeTypes);
549        }
550
551        @Override
552        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
553            Type mtype = methodType();
554            ResultInfo localInfo = resultInfo(resultInfo);
555            if (mtype != null && mtype.hasTag(METHOD) && mtype.isPartial()) {
556                Type t = ((PartiallyInferredMethodType)mtype).check(localInfo);
557                if (!deferredAttrContext.inferenceContext.free(localInfo.pt)) {
558                    speculativeTypes.put(localInfo, t);
559                    return localInfo.check(tree.pos(), t);
560                } else {
561                    return t;
562                }
563            } else {
564                Type t = localInfo.check(tree.pos(), speculativeTree.type);
565                speculativeTypes.put(localInfo, t);
566                return t;
567            }
568        }
569
570        /**
571         * Get the result info to be used for performing an overload check.
572         */
573        abstract ResultInfo resultInfo(ResultInfo resultInfo);
574
575        /**
576         * Get the method type to be used for performing an overload check.
577         */
578        abstract Type methodType();
579    }
580
581    /**
582     * Argument type for methods.
583     */
584    class ResolvedMethodType extends ResolvedMemberType<JCMethodInvocation> {
585
586        public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree) {
587            this(tree, env, speculativeTree, new HashMap<>());
588        }
589
590        public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree, Map<ResultInfo, Type> speculativeTypes) {
591            super(tree, env, speculativeTree, speculativeTypes);
592        }
593
594        @Override
595        ResultInfo resultInfo(ResultInfo resultInfo) {
596            return resultInfo;
597        }
598
599        @Override
600        Type methodType() {
601            return speculativeTree.meth.type;
602        }
603
604        @Override
605        ArgumentType<JCMethodInvocation> dup(JCMethodInvocation tree, Env<AttrContext> env) {
606            return new ResolvedMethodType(tree, env, speculativeTree, speculativeTypes);
607        }
608    }
609
610    /**
611     * Argument type for constructors.
612     */
613    class ResolvedConstructorType extends ResolvedMemberType<JCNewClass> {
614
615        public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree) {
616            this(tree, env, speculativeTree, new HashMap<>());
617        }
618
619        public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree, Map<ResultInfo, Type> speculativeTypes) {
620            super(tree, env, speculativeTree, speculativeTypes);
621        }
622
623        @Override
624        ResultInfo resultInfo(ResultInfo resultInfo) {
625            return resultInfo.dup(attr.diamondContext(speculativeTree, speculativeTree.clazz.type.tsym, resultInfo.checkContext));
626        }
627
628        @Override
629        Type methodType() {
630            return (speculativeTree.constructorType != null) ?
631                    speculativeTree.constructorType.baseType() : syms.errType;
632        }
633
634        @Override
635        ArgumentType<JCNewClass> dup(JCNewClass tree, Env<AttrContext> env) {
636            return new ResolvedConstructorType(tree, env, speculativeTree, speculativeTypes);
637        }
638    }
639
640    /**
641     * An instance of this class represents a unique position in a compilation unit. A unique
642     * position is made up of (i) a unique position in a source file (char offset) and (ii)
643     * a source file info.
644     */
645    class UniquePos {
646
647        /** Char offset. */
648        int pos;
649
650        /** Source info. */
651        DiagnosticSource source;
652
653        UniquePos(JCTree tree) {
654            this.pos = tree.pos;
655            this.source = log.currentSource();
656        }
657
658        @Override
659        public int hashCode() {
660            return pos << 16 + source.hashCode();
661        }
662
663        @Override
664        public boolean equals(Object obj) {
665            if (obj instanceof UniquePos) {
666                UniquePos that = (UniquePos)obj;
667                return pos == that.pos && source == that.source;
668            } else {
669                return false;
670            }
671        }
672
673        @Override
674        public String toString() {
675            return source.getFile().getName() + " @ " + source.getLineNumber(pos);
676        }
677    }
678}
679