src/share/classes/com/sun/tools/javac/comp/Env.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Env.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.comp;
    1.30 +
    1.31 +import com.sun.tools.javac.tree.*;
    1.32 +import java.util.Iterator;
    1.33 +import java.util.NoSuchElementException;
    1.34 +
    1.35 +/** A class for environments, instances of which are passed as
    1.36 + *  arguments to tree visitors.  Environments refer to important ancestors
    1.37 + *  of the subtree that's currently visited, such as the enclosing method,
    1.38 + *  the enclosing class, or the enclosing toplevel node. They also contain
    1.39 + *  a generic component, represented as a type parameter, to carry further
    1.40 + *  information specific to individual passes.
    1.41 + *
    1.42 + *  <p><b>This is NOT part of any supported API.
    1.43 + *  If you write code that depends on this, you do so at your own risk.
    1.44 + *  This code and its internal interfaces are subject to change or
    1.45 + *  deletion without notice.</b>
    1.46 + */
    1.47 +public class Env<A> implements Iterable<Env<A>> {
    1.48 +
    1.49 +    /** The next enclosing environment.
    1.50 +     */
    1.51 +    public Env<A> next;
    1.52 +
    1.53 +    /** The environment enclosing the current class.
    1.54 +     */
    1.55 +    public Env<A> outer;
    1.56 +
    1.57 +    /** The tree with which this environment is associated.
    1.58 +     */
    1.59 +    public JCTree tree;
    1.60 +
    1.61 +    /** The enclosing toplevel tree.
    1.62 +     */
    1.63 +    public JCTree.JCCompilationUnit toplevel;
    1.64 +
    1.65 +    /** The next enclosing class definition.
    1.66 +     */
    1.67 +    public JCTree.JCClassDecl enclClass;
    1.68 +
    1.69 +    /** The next enclosing method definition.
    1.70 +     */
    1.71 +    public JCTree.JCMethodDecl enclMethod;
    1.72 +
    1.73 +    /** A generic field for further information.
    1.74 +     */
    1.75 +    public A info;
    1.76 +
    1.77 +    /** Is this an environment for evaluating a base clause?
    1.78 +     */
    1.79 +    public boolean baseClause = false;
    1.80 +
    1.81 +    /** Create an outermost environment for a given (toplevel)tree,
    1.82 +     *  with a given info field.
    1.83 +     */
    1.84 +    public Env(JCTree tree, A info) {
    1.85 +        this.next = null;
    1.86 +        this.outer = null;
    1.87 +        this.tree = tree;
    1.88 +        this.toplevel = null;
    1.89 +        this.enclClass = null;
    1.90 +        this.enclMethod = null;
    1.91 +        this.info = info;
    1.92 +    }
    1.93 +
    1.94 +    /** Duplicate this environment, updating with given tree and info,
    1.95 +     *  and copying all other fields.
    1.96 +     */
    1.97 +    public Env<A> dup(JCTree tree, A info) {
    1.98 +        return dupto(new Env<A>(tree, info));
    1.99 +    }
   1.100 +
   1.101 +    /** Duplicate this environment into a given Environment,
   1.102 +     *  using its tree and info, and copying all other fields.
   1.103 +     */
   1.104 +    public Env<A> dupto(Env<A> that) {
   1.105 +        that.next = this;
   1.106 +        that.outer = this.outer;
   1.107 +        that.toplevel = this.toplevel;
   1.108 +        that.enclClass = this.enclClass;
   1.109 +        that.enclMethod = this.enclMethod;
   1.110 +        return that;
   1.111 +    }
   1.112 +
   1.113 +    /** Duplicate this environment, updating with given tree,
   1.114 +     *  and copying all other fields.
   1.115 +     */
   1.116 +    public Env<A> dup(JCTree tree) {
   1.117 +        return dup(tree, this.info);
   1.118 +    }
   1.119 +
   1.120 +    /** Return closest enclosing environment which points to a tree with given tag.
   1.121 +     */
   1.122 +    public Env<A> enclosing(JCTree.Tag tag) {
   1.123 +        Env<A> env1 = this;
   1.124 +        while (env1 != null && !env1.tree.hasTag(tag)) env1 = env1.next;
   1.125 +        return env1;
   1.126 +    }
   1.127 +
   1.128 +    @Override
   1.129 +    public String toString() {
   1.130 +        StringBuilder sb = new StringBuilder();
   1.131 +        sb.append("Env[").append(info);
   1.132 +//        if (enclMethod != null)
   1.133 +//            sb.append(",enclMethod=").append(Pretty.toSimpleString(enclMethod));
   1.134 +//        if (enclClass != null)
   1.135 +//            sb.append(",enclClass=").append(Pretty.toSimpleString(enclClass));
   1.136 +        if (outer != null)
   1.137 +            sb.append(",outer=").append(outer);
   1.138 +        sb.append("]");
   1.139 +        return sb.toString();
   1.140 +    }
   1.141 +
   1.142 +    public Iterator<Env<A>> iterator() {
   1.143 +        return new Iterator<Env<A>>() {
   1.144 +            Env<A> next = Env.this;
   1.145 +            public boolean hasNext() {
   1.146 +                return next.outer != null;
   1.147 +            }
   1.148 +            public Env<A> next() {
   1.149 +                if (hasNext()) {
   1.150 +                    Env<A> current = next;
   1.151 +                    next = current.outer;
   1.152 +                    return current;
   1.153 +                }
   1.154 +                throw new NoSuchElementException();
   1.155 +
   1.156 +            }
   1.157 +            public void remove() {
   1.158 +                throw new UnsupportedOperationException();
   1.159 +            }
   1.160 +        };
   1.161 +    }
   1.162 +}

mercurial