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