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.code.Symbol.MethodSymbol;
  29 import com.sun.tools.javac.tree.JCTree;
  30 import com.sun.tools.javac.util.*;
  31 import com.sun.tools.javac.code.*;
  32 import com.sun.tools.javac.code.Scope.WriteableScope;
  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 evaluating the selector of a `super' or type name?
  57      */
  58     boolean selectSuper = false;
  59 
  60     /** Is the current target of lambda expression or method reference serializable or is this a
  61      *  serializable class?
  62      */
  63     boolean isSerializable = false;
  64 
  65     /** Is this a lambda environment?
  66      */
  67     boolean isLambda = false;
  68 
  69     /** Is this a speculative attribution environment?
  70      */
  71     boolean isSpeculative = false;
  72 
  73     /**
  74      *  Is this an attribution environment for an anonymous class instantiated using <> ?
  75      */
  76     boolean isAnonymousDiamond = false;
  77 
  78     /**
  79      *  Is this an attribution environment for an instance creation expression?
  80      */
  81     boolean isNewClass = false;
  82 
  83     /** Indicate if the type being visited is a service implementation
  84      */
  85     boolean visitingServiceImplementation = false;
  86 
  87     /** Are arguments to current function applications boxed into an array for varargs?
  88      */
  89     Resolve.MethodResolutionPhase pendingResolutionPhase = null;
  90 
  91     /** A record of the lint/SuppressWarnings currently in effect
  92      */
  93     Lint lint;
  94 
  95     /** The variable whose initializer is being attributed
  96      * useful for detecting self-references in variable initializers
  97      */
  98     Symbol enclVar = null;
  99 
 100     /** ResultInfo to be used for attributing 'return' statement expressions
 101      * (set by Attr.visitMethod and Attr.visitLambda)
 102      */
 103     Attr.ResultInfo returnResult = null;
 104 
 105     /** ResultInfo to be used for attributing 'yield' statement expressions
 106      * (set by Attr.visitSwitchExpression)
 107      */
 108     Attr.ResultInfo yieldResult = null;
 109 
 110     /** Symbol corresponding to the site of a qualified default super call
 111      */
 112     Type defaultSuperCallSite = null;
 113 
 114     /** Tree that when non null, is to be preferentially used in diagnostics.
 115      *  Usually Env<AttrContext>.tree is the tree to be referred to in messages,
 116      *  but this may not be true during the window a method is looked up in enclosing
 117      *  contexts (JDK-8145466)
 118      */
 119     JCTree preferredTreeForDiagnostics;
 120 
 121     MethodSymbol recordImplicitConstructor;
 122 
 123     /** Duplicate this context, replacing scope field and copying all others.
 124      */
 125     AttrContext dup(WriteableScope scope) {
 126         AttrContext info = new AttrContext();
 127         info.scope = scope;
 128         info.staticLevel = staticLevel;
 129         info.isSelfCall = isSelfCall;
 130         info.selectSuper = selectSuper;
 131         info.pendingResolutionPhase = pendingResolutionPhase;
 132         info.lint = lint;
 133         info.enclVar = enclVar;
 134         info.returnResult = returnResult;
 135         info.yieldResult = yieldResult;
 136         info.defaultSuperCallSite = defaultSuperCallSite;
 137         info.isSerializable = isSerializable;
 138         info.isLambda = isLambda;
 139         info.isSpeculative = isSpeculative;
 140         info.isAnonymousDiamond = isAnonymousDiamond;
 141         info.isNewClass = isNewClass;
 142         info.preferredTreeForDiagnostics = preferredTreeForDiagnostics;
 143         info.visitingServiceImplementation = visitingServiceImplementation;
 144         info.recordImplicitConstructor = recordImplicitConstructor;
 145         return info;
 146     }
 147 
 148     /** Duplicate this context, copying all fields.
 149      */
 150     AttrContext dup() {
 151         return dup(scope);
 152     }
 153 
 154     public Iterable<Symbol> getLocalElements() {
 155         if (scope == null)
 156             return List.nil();
 157         return scope.getSymbols();
 158     }
 159 
 160     boolean lastResolveVarargs() {
 161         return pendingResolutionPhase != null &&
 162                 pendingResolutionPhase.isVarargsRequired();
 163     }
 164 
 165     @Override
 166     public String toString() {
 167         return "AttrContext[" + scope.toString() + "]";
 168     }
 169 }