ArgumentAttr.java revision 3401:b00a838598ab
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            if (pertinentToApplicability) {
369                for (Map.Entry<ResultInfo, Type> _entry : speculativeTypes.entrySet()) {
370                    DeferredAttrContext deferredAttrContext = _entry.getKey().checkContext.deferredAttrContext();
371                    if (deferredAttrContext.phase == phase && deferredAttrContext.msym == msym) {
372                        return _entry.getValue();
373                    }
374                }
375                return Type.noType;
376            } else {
377                return super.speculativeType(msym, phase);
378            }
379        }
380
381        @Override
382        JCTree speculativeTree(DeferredAttrContext deferredAttrContext) {
383            return pertinentToApplicability ? speculativeTree : super.speculativeTree(deferredAttrContext);
384        }
385
386        /**
387         * Performs an overload check against a given target result.
388         */
389        abstract Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
390
391        /**
392         * Creates a copy of this argument type with given tree and environment.
393         */
394        abstract ArgumentType<T> dup(T tree, Env<AttrContext> env);
395    }
396
397    /**
398     * Argument type for parenthesized expression.
399     */
400    class ParensType extends ArgumentType<JCParens> {
401        ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens) {
402            this(tree, env, speculativeParens, new HashMap<>());
403        }
404
405        ParensType(JCExpression tree, Env<AttrContext> env, JCParens speculativeParens, Map<ResultInfo, Type> speculativeTypes) {
406           super(tree, env, speculativeParens, speculativeTypes);
407        }
408
409        @Override
410        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
411            return checkSpeculative(speculativeTree.expr, resultInfo);
412        }
413
414        @Override
415        ArgumentType<JCParens> dup(JCParens tree, Env<AttrContext> env) {
416            return new ParensType(tree, env, speculativeTree, speculativeTypes);
417        }
418    }
419
420    /**
421     * Argument type for conditionals.
422     */
423    class ConditionalType extends ArgumentType<JCConditional> {
424        ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) {
425            this(tree, env, speculativeCond, new HashMap<>());
426        }
427
428        ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) {
429           super(tree, env, speculativeCond, speculativeTypes);
430        }
431
432        @Override
433        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
434            ResultInfo localInfo = resultInfo.dup(attr.conditionalContext(resultInfo.checkContext));
435            if (speculativeTree.isStandalone()) {
436                return localInfo.check(speculativeTree, speculativeTree.type);
437            } else if (resultInfo.pt.hasTag(VOID)) {
438                //this means we are returning a poly conditional from void-compatible lambda expression
439                resultInfo.checkContext.report(tree, attr.diags.fragment("conditional.target.cant.be.void"));
440                return attr.types.createErrorType(resultInfo.pt);
441            } else {
442                //poly
443                checkSpeculative(speculativeTree.truepart, localInfo);
444                checkSpeculative(speculativeTree.falsepart, localInfo);
445                return localInfo.pt;
446            }
447        }
448
449        @Override
450        ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) {
451            return new ConditionalType(tree, env, speculativeTree, speculativeTypes);
452        }
453    }
454
455    /**
456     * Argument type for explicit lambdas.
457     */
458    class ExplicitLambdaType extends ArgumentType<JCLambda> {
459
460        /** List of argument types (lazily populated). */
461        Optional<List<Type>> argtypes = Optional.empty();
462
463        /** List of return expressions (lazily populated). */
464        Optional<List<JCReturn>> returnExpressions = Optional.empty();
465
466        ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda) {
467            this(originalLambda, env, speculativeLambda, new HashMap<>());
468        }
469
470        ExplicitLambdaType(JCLambda originalLambda, Env<AttrContext> env, JCLambda speculativeLambda, Map<ResultInfo, Type> speculativeTypes) {
471            super(originalLambda, env, speculativeLambda, speculativeTypes);
472        }
473
474        /** Compute argument types (if needed). */
475        List<Type> argtypes() {
476            return argtypes.orElseGet(() -> {
477                List<Type> res = TreeInfo.types(speculativeTree.params);
478                argtypes = Optional.of(res);
479                return res;
480            });
481        }
482
483        /** Compute return expressions (if needed). */
484        List<JCReturn> returnExpressions() {
485            return returnExpressions.orElseGet(() -> {
486                final List<JCReturn> res;
487                if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION) {
488                    res = List.of(attr.make.Return((JCExpression)speculativeTree.body));
489                } else {
490                    ListBuffer<JCReturn> returnExpressions = new ListBuffer<>();
491                    new LambdaReturnScanner() {
492                        @Override
493                        public void visitReturn(JCReturn tree) {
494                            returnExpressions.add(tree);
495                        }
496                    }.scan(speculativeTree.body);
497                    res = returnExpressions.toList();
498                }
499                returnExpressions = Optional.of(res);
500                return res;
501            });
502        }
503
504        @Override
505        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
506            try {
507                //compute target-type; this logic could be shared with Attr
508                TargetInfo targetInfo = attr.getTargetInfo(speculativeTree, resultInfo, argtypes());
509                Type lambdaType = targetInfo.descriptor;
510                Type currentTarget = targetInfo.target;
511                //check compatibility
512                checkLambdaCompatible(lambdaType, resultInfo);
513                return currentTarget;
514            } catch (FunctionDescriptorLookupError ex) {
515                resultInfo.checkContext.report(null, ex.getDiagnostic());
516                return null; //cannot get here
517            }
518        }
519
520        /** Check lambda against given target result */
521        private void checkLambdaCompatible(Type descriptor, ResultInfo resultInfo) {
522            CheckContext checkContext = resultInfo.checkContext;
523            ResultInfo bodyResultInfo = attr.lambdaBodyResult(speculativeTree, descriptor, resultInfo);
524            for (JCReturn ret : returnExpressions()) {
525                Type t = getReturnType(ret);
526                if (speculativeTree.getBodyKind() == BodyKind.EXPRESSION || !t.hasTag(VOID)) {
527                    checkSpeculative(ret.expr, t, bodyResultInfo);
528                }
529            }
530
531            attr.checkLambdaCompatible(speculativeTree, descriptor, checkContext);
532        }
533
534        /** Get the type associated with given return expression. */
535        Type getReturnType(JCReturn ret) {
536            if (ret.expr == null) {
537                return syms.voidType;
538            } else {
539                return ret.expr.type;
540            }
541        }
542
543        @Override
544        ArgumentType<JCLambda> dup(JCLambda tree, Env<AttrContext> env) {
545            return new ExplicitLambdaType(tree, env, speculativeTree, speculativeTypes);
546        }
547    }
548
549    /**
550     * Argument type for methods/constructors.
551     */
552    abstract class ResolvedMemberType<E extends JCExpression> extends ArgumentType<E> {
553
554        public ResolvedMemberType(JCExpression tree, Env<AttrContext> env, E speculativeMethod, Map<ResultInfo, Type> speculativeTypes) {
555            super(tree, env, speculativeMethod, speculativeTypes);
556        }
557
558        @Override
559        Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
560            Type mtype = methodType();
561            ResultInfo localInfo = resultInfo(resultInfo);
562            if (mtype != null && mtype.hasTag(METHOD) && mtype.isPartial()) {
563                Type t = ((PartiallyInferredMethodType)mtype).check(localInfo);
564                if (!deferredAttrContext.inferenceContext.free(localInfo.pt)) {
565                    speculativeTypes.put(localInfo, t);
566                    return localInfo.check(tree.pos(), t);
567                } else {
568                    return t;
569                }
570            } else {
571                Type t = localInfo.check(tree.pos(), speculativeTree.type);
572                speculativeTypes.put(localInfo, t);
573                return t;
574            }
575        }
576
577        /**
578         * Get the result info to be used for performing an overload check.
579         */
580        abstract ResultInfo resultInfo(ResultInfo resultInfo);
581
582        /**
583         * Get the method type to be used for performing an overload check.
584         */
585        abstract Type methodType();
586    }
587
588    /**
589     * Argument type for methods.
590     */
591    class ResolvedMethodType extends ResolvedMemberType<JCMethodInvocation> {
592
593        public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree) {
594            this(tree, env, speculativeTree, new HashMap<>());
595        }
596
597        public ResolvedMethodType(JCExpression tree, Env<AttrContext> env, JCMethodInvocation speculativeTree, Map<ResultInfo, Type> speculativeTypes) {
598            super(tree, env, speculativeTree, speculativeTypes);
599        }
600
601        @Override
602        ResultInfo resultInfo(ResultInfo resultInfo) {
603            return resultInfo;
604        }
605
606        @Override
607        Type methodType() {
608            return speculativeTree.meth.type;
609        }
610
611        @Override
612        ArgumentType<JCMethodInvocation> dup(JCMethodInvocation tree, Env<AttrContext> env) {
613            return new ResolvedMethodType(tree, env, speculativeTree, speculativeTypes);
614        }
615    }
616
617    /**
618     * Argument type for constructors.
619     */
620    class ResolvedConstructorType extends ResolvedMemberType<JCNewClass> {
621
622        public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree) {
623            this(tree, env, speculativeTree, new HashMap<>());
624        }
625
626        public ResolvedConstructorType(JCExpression tree, Env<AttrContext> env, JCNewClass speculativeTree, Map<ResultInfo, Type> speculativeTypes) {
627            super(tree, env, speculativeTree, speculativeTypes);
628        }
629
630        @Override
631        ResultInfo resultInfo(ResultInfo resultInfo) {
632            return resultInfo.dup(attr.diamondContext(speculativeTree, speculativeTree.clazz.type.tsym, resultInfo.checkContext));
633        }
634
635        @Override
636        Type methodType() {
637            return (speculativeTree.constructorType != null) ?
638                    speculativeTree.constructorType.baseType() : syms.errType;
639        }
640
641        @Override
642        ArgumentType<JCNewClass> dup(JCNewClass tree, Env<AttrContext> env) {
643            return new ResolvedConstructorType(tree, env, speculativeTree, speculativeTypes);
644        }
645    }
646
647    /**
648     * An instance of this class represents a unique position in a compilation unit. A unique
649     * position is made up of (i) a unique position in a source file (char offset) and (ii)
650     * a source file info.
651     */
652    class UniquePos {
653
654        /** Char offset. */
655        int pos;
656
657        /** Source info. */
658        DiagnosticSource source;
659
660        UniquePos(JCTree tree) {
661            this.pos = tree.pos;
662            this.source = log.currentSource();
663        }
664
665        @Override
666        public int hashCode() {
667            return pos << 16 + source.hashCode();
668        }
669
670        @Override
671        public boolean equals(Object obj) {
672            if (obj instanceof UniquePos) {
673                UniquePos that = (UniquePos)obj;
674                return pos == that.pos && source == that.source;
675            } else {
676                return false;
677            }
678        }
679
680        @Override
681        public String toString() {
682            return source.getFile().getName() + " @ " + source.getLineNumber(pos);
683        }
684    }
685}
686