The environment of Javac

Source: Internet
Author: User

The source code for Env is as follows:

/** A class for environments, instances of which is passed as * arguments to tree visitors.  Environments refer to important ancestors * of the subtree that ' s currently visited, such as the enclosing method, * the Enclosing class, or the enclosing toplevel node. They also contain * a generic component, represented as a type parameter, to carry further * information specific to IND Ividual passes. * * An environment of a class whose instances will be passed as parameters of the tree visitor. The environment involves the ancestors of important subtrees, the subtree is currently visited. Examples are closed methods, closed classes, or closed top-level nodes. They also contain a generic component that is described as a type parameter, carrying more information for individual passes * */public class Environment<a> implements Iterable<environment<a     >> {/** the next enclosing env. * * The next closed environment, and the scope is different.    This is the same as the organizational nesting of the syntax tree. */Public environment<a> next;     /** the env enclosing the current class.    * * Enclosing the environment of the current class, just like the organization of the syntax tree, only the previous nested structure of the class is saved, possibly Block,class,jccompilationunit and jcnewclass * Because only these structures can define classes */    Public environment<a> outer; /** the tree with which this env is associated.    * * Syntax tree associated with the current environment */public jctree tree;     View environment The main is to look at the tree attribute, see this environment belongs to which syntax node can/** the enclosing toplevel tree.    * * Closed top-level syntax tree, syntax node type Jccompilationunit */public jccompilationunit toplevel;     /** the next enclosing class definition. * * Enclosing the class of the current environment, also the nearest class.    The enclosingclass of class is oneself * * public jcclassdeclaration EnclosingClass;     /** the next enclosing method definition.    * Next closed method definition */public jcmethoddeclaration Enclosingmethod;     /** A Generic field for further information. * * A common attribute that can carry future specified information.    Here is generally attrcontext */public A info;     /** is this an env for evaluating a base clause?    * This is an environment for evaluating an underlying clause, viewing the Baseenvironment () method in the Memberenter class */public Boolean baseclause = false;     /** Create an outermost env for a given (TopLevel) Tree,with a given info field. * * Creates an outermost environment for a given (top-level) node tree and a given property-related information */public environment (Jctree Jctree, a info) {This.next = Nu        ll THis.outer = null;        This.tree = Jctree;        This.toplevel = null;        This.enclosingclass = null;        This.enclosingmethod = null;    This.info = info;     }/** Duplicate This env, updating with given tree and info, * and copying all other fields. */Public environment<a> Duplicate (jctree jctree, A info) {environment<a> environment = new Environ        Ment<a> (Jctree, info);        environment<a> copy = Duplicateto (environment);    Return copy;//return Duplicateto (New environment<a> (tree, info));     }/** Duplicate This env to a given environment,using its tree and info, and copying all other fields.    */Public environment<a> Duplicateto (environment<a>) {that.next = this;        Assign this object to the Copy object's Next property That.outer = This.outer;        That.toplevel = This.toplevel;        That.enclosingclass = This.enclosingclass;        That.enclosingmethod = This.enclosingmethod; //Tree and info information with that own return that;     }/** Duplicate This env, updating with given Tree,and copying all other fields. */Public environment<a> Duplicate (Jctree jctree) {//update only the tree attribute, and all other information uses this environment's return duplicate (JC    Tree, This.info);     }/** Return closest enclosing environment which points to a tree with given Typetag.        */public environment<a> enclosing (int tags) {environment<a> environment1 = this;        while (environment1! = null && environment1.tree.getTag ()! = tag) {environment1 = Environment1.next;    } return environment1;    } public String toString () {return ' environment[' + info + (outer = = null? ":", outer= "+ outer) +"] "; } public iterator<environment<a>> Iterator () {iterator<environment<a>> ITR = new Iterat            Or<environment<a>> () {environment<a> next = environment.this; public Boolean HAsnext () {return next.outer! = NULL; } public environment<a> Next () {if (Hasnext ()) {environment<a>                    current = next;                    Next = Current.outer;                return current;            } throw new Nosuchelementexception ();            } public void Remove () {throw new unsupportedoperationexception ();        }        };    return ITR; }}

For example, the following:

Interface I{void M1 ();} public class test3{static void T () {}static I a = new I () {@Overridepublic void M1 () {T ();}};}

Then the environment of the T () env is structured as follows.

  

This is the outer property, and the next attribute and the syntax tree are closely aligned, as follows.

The info attribute is generally attrcontext and the source code is as follows:

/** Contains information specific to the attribute and enter * passes, to is used in place of the generic field in Enviro Nments.     * */public class Attrcontext {/** The scope of local symbols.    */Scope scope = NULL;     /** the number of enclosing ' static ' modifiers.    */int staticlevel = 0;     /** is the environment for a This (...) or super (...) call?    */Boolean isselfcall = false;     /** is we evaluating the selector of a ' super ' or type name?    */Boolean selectsuper = false;     /** is arguments to the current function applications boxed into an array for varargs?    */Boolean VarArgs = false;     /** A List of type variables that is all-quantifed in the current context.    */list<type> Tvars = List.nil ();    /** A Record of the lint/suppresswarnings currently in effect */lint lint;      /** the variable whose initializer is being attributed * useful for detecting self-references in variable initializers */Symbol Enclvar = nUll     /** Duplicate This context, replacing scopes field and copying all others.        */Attrcontext DUP (scope scope) {Attrcontext info = new Attrcontext ();        Info.scope = scope;        Info.staticlevel = Staticlevel;        Info.isselfcall = Isselfcall;        Info.selectsuper = Selectsuper;        Info.varargs = VarArgs;        Info.tvars = Tvars;        Info.lint = lint;        Info.enclvar = Enclvar;    return info;     }/** Duplicate This context, copying all fields.    */Attrcontext DUP () {return DUP (scope);        } public iterable<symbol> getlocalelements () {if (scope = = null) return List.nil ();    return scope.getelements ();    } public String toString () {return ' attrcontext[' + scope.tostring () + "]"; }}

  

In the example above, the staticlevel of the JCVARIABLEDECL node and its sub-nodes are 1, and the parent node and the above are 0. Let me give you an example, as follows:

public class test3{static{                      //Staticlevel = 1//No more static occurrences in the static block, such as//static int a = 1;//static class a{}//Clas s a{//public  static void M1 () {}//}int A = 1;               Staticlevel = 1}//The staticlevel of this class is 0, without any difference from the general class static class inner{          //staticlevel = 0 public static void M1 () {//S Taticlevel = 1int B = 1;           Staticlevel = 1}}

  

The environment of Javac

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.