1 /*
  2  * Copyright (c) 1999, 2019, 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 
 26 package com.sun.tools.javac.comp;
 27 
 28 import com.sun.tools.javac.tree.JCTree;
 29 import com.sun.tools.javac.util.*;
 30 import com.sun.tools.javac.code.*;
 31 import com.sun.tools.javac.code.Scope.WriteableScope;
 32 import com.sun.tools.javac.comp.DeferredAttr.AttributionMode;
 33 
 34 /** Contains information specific to the attribute and enter
 35  *  passes, to be used in place of the generic field in environments.
 36  *
 37  *  <p><b>This is NOT part of any supported API.
 38  *  If you write code that depends on this, you do so at your own risk.
 39  *  This code and its internal interfaces are subject to change or
 40  *  deletion without notice.</b>
 41  */
 42 public class AttrContext {
 43 
 44     /** The scope of local symbols.
 45      */
 46     WriteableScope scope = null;
 47 
 48     /** The number of enclosing `static' modifiers.
 49      */
 50     int staticLevel = 0;
 51 
 52     /** Is this an environment for a this(...) or super(...) call?
 53      */
 54     boolean isSelfCall = false;
 55 
 56     /** are we analyzing the arguments for a constructor invocation?
 57      */
 58     boolean constructorArgs = false;
 59 
 60     /** Are we evaluating the selector of a `super' or type name?
 61      */
 62     boolean selectSuper = false;
 63 
 64     /** Is the current target of lambda expression or method reference serializable or is this a
 65      *  serializable class?
 66      */
 67     boolean isSerializable = false;
 68 
 69     /** Is this a serializable lambda?
 70      */
 71     boolean isSerializableLambda = false;
 72 
 73     /** Is this a lambda environment?
 74      */
 75     boolean isLambda = false;
 76 
 77     /** Is this a speculative attribution environment?
 78      */
 79     AttributionMode attributionMode = AttributionMode.FULL;
 80 
 81     /**
 82      *  Is this an attribution environment for an anonymous class instantiated using <> ?
 83      */
 84     boolean isAnonymousDiamond = false;
 85 
 86     /**
 87      *  Is this an attribution environment for an instance creation expression?
 88      */
 89     boolean isNewClass = false;
 90 
 91     /**
 92      *  Is this an attribution environment for a withfield operation ?
 93      */
 94     boolean inWithField = false;
 95 
 96     /** Indicate if the type being visited is a service implementation
 97      */
 98     boolean visitingServiceImplementation = false;
 99 
100     /** Are arguments to current function applications boxed into an array for varargs?
101      */
102     Resolve.MethodResolutionPhase pendingResolutionPhase = null;
103 
104     /** A record of the lint/SuppressWarnings currently in effect
105      */
106     Lint lint;
107 
108     /** The variable whose initializer is being attributed
109      * useful for detecting self-references in variable initializers
110      */
111     Symbol enclVar = null;
112 
113     /** ResultInfo to be used for attributing 'return' statement expressions
114      * (set by Attr.visitMethod and Attr.visitLambda)
115      */
116     Attr.ResultInfo returnResult = null;
117 
118     /** ResultInfo to be used for attributing 'yield' statement expressions
119      * (set by Attr.visitSwitchExpression)
120      */
121     Attr.ResultInfo yieldResult = null;
122 
123     /** Symbol corresponding to the site of a qualified default super call
124      */
125     Type defaultSuperCallSite = null;
126 
127     /** Tree that when non null, is to be preferentially used in diagnostics.
128      *  Usually Env<AttrContext>.tree is the tree to be referred to in messages,
129      *  but this may not be true during the window a method is looked up in enclosing
130      *  contexts (JDK-8145466)
131      */
132     JCTree preferredTreeForDiagnostics;
133 
134     /** Duplicate this context, replacing scope field and copying all others.
135      */
136     AttrContext dup(WriteableScope scope) {
137         AttrContext info = new AttrContext();
138         info.scope = scope;
139         info.staticLevel = staticLevel;
140         info.isSelfCall = isSelfCall;
141         info.constructorArgs = constructorArgs;
142         info.selectSuper = selectSuper;
143         info.pendingResolutionPhase = pendingResolutionPhase;
144         info.lint = lint;
145         info.enclVar = enclVar;
146         info.returnResult = returnResult;
147         info.yieldResult = yieldResult;
148         info.defaultSuperCallSite = defaultSuperCallSite;
149         info.isSerializable = isSerializable;
150         info.isLambda = isLambda;
151         info.isSerializableLambda = isSerializableLambda;
152         info.attributionMode = attributionMode;
153         info.isAnonymousDiamond = isAnonymousDiamond;
154         info.isNewClass = isNewClass;
155         info.inWithField = inWithField;
156         info.preferredTreeForDiagnostics = preferredTreeForDiagnostics;
157         info.visitingServiceImplementation = visitingServiceImplementation;
158         return info;
159     }
160 
161     /** Duplicate this context, copying all fields.
162      */
163     AttrContext dup() {
164         return dup(scope);
165     }
166 
167     public Iterable<Symbol> getLocalElements() {
168         if (scope == null)
169             return List.nil();
170         return scope.getSymbols();
171     }
172 
173     boolean lastResolveVarargs() {
174         return pendingResolutionPhase != null &&
175                 pendingResolutionPhase.isVarargsRequired();
176     }
177 
178     @Override
179     public String toString() {
180         return "AttrContext[" + scope.toString() + "]";
181     }
182 }