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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import com.sun.tools.javac.tree.*;
duke@1 30 import java.util.Iterator;
duke@1 31 import java.util.NoSuchElementException;
duke@1 32
duke@1 33 /** A class for environments, instances of which are passed as
duke@1 34 * arguments to tree visitors. Environments refer to important ancestors
duke@1 35 * of the subtree that's currently visited, such as the enclosing method,
duke@1 36 * the enclosing class, or the enclosing toplevel node. They also contain
duke@1 37 * a generic component, represented as a type parameter, to carry further
duke@1 38 * information specific to individual passes.
duke@1 39 *
duke@1 40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 41 * you write code that depends on this, you do so at your own risk.
duke@1 42 * This code and its internal interfaces are subject to change or
duke@1 43 * deletion without notice.</b>
duke@1 44 */
duke@1 45 public class Env<A> implements Iterable<Env<A>> {
duke@1 46
duke@1 47 /** The next enclosing environment.
duke@1 48 */
duke@1 49 public Env<A> next;
duke@1 50
duke@1 51 /** The environment enclosing the current class.
duke@1 52 */
duke@1 53 public Env<A> outer;
duke@1 54
duke@1 55 /** The tree with which this environment is associated.
duke@1 56 */
duke@1 57 public JCTree tree;
duke@1 58
duke@1 59 /** The enclosing toplevel tree.
duke@1 60 */
duke@1 61 public JCTree.JCCompilationUnit toplevel;
duke@1 62
duke@1 63 /** The next enclosing class definition.
duke@1 64 */
duke@1 65 public JCTree.JCClassDecl enclClass;
duke@1 66
duke@1 67 /** The next enclosing method definition.
duke@1 68 */
duke@1 69 public JCTree.JCMethodDecl enclMethod;
duke@1 70
duke@1 71 /** A generic field for further information.
duke@1 72 */
duke@1 73 public A info;
duke@1 74
duke@1 75 /** Is this an environment for evaluating a base clause?
duke@1 76 */
duke@1 77 public boolean baseClause = false;
duke@1 78
duke@1 79 /** Create an outermost environment for a given (toplevel)tree,
duke@1 80 * with a given info field.
duke@1 81 */
duke@1 82 public Env(JCTree tree, A info) {
duke@1 83 this.next = null;
duke@1 84 this.outer = null;
duke@1 85 this.tree = tree;
duke@1 86 this.toplevel = null;
duke@1 87 this.enclClass = null;
duke@1 88 this.enclMethod = null;
duke@1 89 this.info = info;
duke@1 90 }
duke@1 91
duke@1 92 /** Duplicate this environment, updating with given tree and info,
duke@1 93 * and copying all other fields.
duke@1 94 */
duke@1 95 public Env<A> dup(JCTree tree, A info) {
duke@1 96 return dupto(new Env<A>(tree, info));
duke@1 97 }
duke@1 98
duke@1 99 /** Duplicate this environment into a given Environment,
duke@1 100 * using its tree and info, and copying all other fields.
duke@1 101 */
duke@1 102 public Env<A> dupto(Env<A> that) {
duke@1 103 that.next = this;
duke@1 104 that.outer = this.outer;
duke@1 105 that.toplevel = this.toplevel;
duke@1 106 that.enclClass = this.enclClass;
duke@1 107 that.enclMethod = this.enclMethod;
duke@1 108 return that;
duke@1 109 }
duke@1 110
duke@1 111 /** Duplicate this environment, updating with given tree,
duke@1 112 * and copying all other fields.
duke@1 113 */
duke@1 114 public Env<A> dup(JCTree tree) {
duke@1 115 return dup(tree, this.info);
duke@1 116 }
duke@1 117
duke@1 118 /** Return closest enclosing environment which points to a tree with given tag.
duke@1 119 */
duke@1 120 public Env<A> enclosing(int tag) {
duke@1 121 Env<A> env1 = this;
duke@1 122 while (env1 != null && env1.tree.getTag() != tag) env1 = env1.next;
duke@1 123 return env1;
duke@1 124 }
duke@1 125
duke@1 126 public String toString() {
duke@1 127 return "Env[" + info + (outer == null ? "" : ",outer=" + outer) + "]";
duke@1 128 }
duke@1 129
duke@1 130 public Iterator<Env<A>> iterator() {
duke@1 131 return new Iterator<Env<A>>() {
duke@1 132 Env<A> next = Env.this;
duke@1 133 public boolean hasNext() {
duke@1 134 return next.outer != null;
duke@1 135 }
duke@1 136 public Env<A> next() {
duke@1 137 if (hasNext()) {
duke@1 138 Env<A> current = next;
duke@1 139 next = current.outer;
duke@1 140 return current;
duke@1 141 }
duke@1 142 throw new NoSuchElementException();
duke@1 143
duke@1 144 }
duke@1 145 public void remove() {
duke@1 146 throw new UnsupportedOperationException();
duke@1 147 }
duke@1 148 };
duke@1 149 }
duke@1 150 }

mercurial