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

Print this page




  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.*;
  29 import java.util.Set;
  30 import javax.tools.JavaFileObject;
  31 
  32 import com.sun.tools.javac.code.*;
  33 import com.sun.tools.javac.jvm.*;
  34 import com.sun.tools.javac.tree.*;
  35 import com.sun.tools.javac.util.*;
  36 import com.sun.tools.javac.util.List;
  37 
  38 import com.sun.tools.javac.code.Type.*;
  39 import com.sun.tools.javac.code.Symbol.*;
  40 import com.sun.tools.javac.tree.JCTree.*;
  41 
  42 import static com.sun.tools.javac.code.Flags.*;

  43 import static com.sun.tools.javac.code.Kinds.*;
  44 import static com.sun.tools.javac.code.TypeTags.*;
  45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  46 


  47 /** This is the second phase of Enter, in which classes are completed
  48  *  by entering their members into the class scope using
  49  *  MemberEnter.complete().  See Enter for an overview.
  50  *
  51  *  <p><b>This is NOT part of any supported API.
  52  *  If you write code that depends on this, you do so at your own risk.
  53  *  This code and its internal interfaces are subject to change or
  54  *  deletion without notice.</b>
  55  */
  56 public class MemberEnter extends JCTree.Visitor implements Completer {
  57     protected static final Context.Key<MemberEnter> memberEnterKey =
  58         new Context.Key<MemberEnter>();
  59 
  60     /** A switch to determine whether we check for package/class conflicts
  61      */
  62     final static boolean checkClash = true;
  63 
  64     private final Names names;
  65     private final Enter enter;
  66     private final Log log;


 627             attr.attribType(tree.vartype, localEnv);
 628         } finally {
 629             chk.setDeferredLintHandler(prevLintHandler);
 630         }
 631 
 632         if ((tree.mods.flags & VARARGS) != 0) {
 633             //if we are entering a varargs parameter, we need to replace its type
 634             //(a plain array type) with the more precise VarargsType --- we need
 635             //to do it this way because varargs is represented in the tree as a modifier
 636             //on the parameter declaration, and not as a distinct type of array node.
 637             ArrayType atype = (ArrayType)tree.vartype.type;
 638             tree.vartype.type = atype.makeVarargs();
 639         }
 640         Scope enclScope = enter.enterScope(env);
 641         VarSymbol v =
 642             new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
 643         v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
 644         tree.sym = v;
 645         if (tree.init != null) {
 646             v.flags_field |= HASINIT;
 647             if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
 648                 Env<AttrContext> initEnv = getInitEnv(tree, env);
 649                 initEnv.info.enclVar = v;
 650                 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree.init);
 651             }
 652         }
 653         if (chk.checkUnique(tree.pos(), v, enclScope)) {
 654             chk.checkTransparentVar(tree.pos(), v, enclScope);
 655             enclScope.enter(v);
 656         }
 657         annotateLater(tree.mods.annotations, localEnv, v);
 658         v.pos = tree.pos;
 659     }
 660 
 661     /** Create a fresh environment for a variable's initializer.
 662      *  If the variable is a field, the owner of the environment's scope
 663      *  is be the variable itself, otherwise the owner is the method
 664      *  enclosing the variable definition.
 665      *
 666      *  @param tree     The variable definition.
 667      *  @param env      The environment current outside of the variable definition.


 851         }
 852 
 853         ClassSymbol c = (ClassSymbol)sym;
 854         ClassType ct = (ClassType)c.type;
 855         Env<AttrContext> env = enter.typeEnvs.get(c);
 856         JCClassDecl tree = (JCClassDecl)env.tree;
 857         boolean wasFirst = isFirst;
 858         isFirst = false;
 859 
 860         JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
 861         try {
 862             // Save class environment for later member enter (2) processing.
 863             halfcompleted.append(env);
 864 
 865             // Mark class as not yet attributed.
 866             c.flags_field |= UNATTRIBUTED;
 867 
 868             // If this is a toplevel-class, make sure any preceding import
 869             // clauses have been seen.
 870             if (c.owner.kind == PCK) {
 871                 memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL));
 872                 todo.append(env);
 873             }
 874 
 875             if (c.owner.kind == TYP)
 876                 c.owner.complete();
 877 
 878             // create an environment for evaluating the base clauses
 879             Env<AttrContext> baseEnv = baseEnv(tree, env);
 880 
 881             // Determine supertype.
 882             Type supertype =
 883                 (tree.extending != null)
 884                 ? attr.attribBase(tree.extending, baseEnv, true, false, true)
 885                 : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c))
 886                 ? attr.attribBase(enumBase(tree.pos, c), baseEnv,
 887                                   true, false, false)
 888                 : (c.fullname == names.java_lang_Object)
 889                 ? Type.noType
 890                 : syms.objectType;
 891             ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);




  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.*;
  29 import java.util.Set;
  30 import javax.tools.JavaFileObject;
  31 
  32 import com.sun.tools.javac.code.*;
  33 import com.sun.tools.javac.jvm.*;
  34 import com.sun.tools.javac.tree.*;
  35 import com.sun.tools.javac.util.*;
  36 import com.sun.tools.javac.util.List;
  37 
  38 import com.sun.tools.javac.code.Type.*;
  39 import com.sun.tools.javac.code.Symbol.*;
  40 import com.sun.tools.javac.tree.JCTree.*;
  41 
  42 import static com.sun.tools.javac.code.Flags.*;
  43 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  44 import static com.sun.tools.javac.code.Kinds.*;
  45 import static com.sun.tools.javac.code.TypeTags.*;
  46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  47 
  48 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  49 
  50 /** This is the second phase of Enter, in which classes are completed
  51  *  by entering their members into the class scope using
  52  *  MemberEnter.complete().  See Enter for an overview.
  53  *
  54  *  <p><b>This is NOT part of any supported API.
  55  *  If you write code that depends on this, you do so at your own risk.
  56  *  This code and its internal interfaces are subject to change or
  57  *  deletion without notice.</b>
  58  */
  59 public class MemberEnter extends JCTree.Visitor implements Completer {
  60     protected static final Context.Key<MemberEnter> memberEnterKey =
  61         new Context.Key<MemberEnter>();
  62 
  63     /** A switch to determine whether we check for package/class conflicts
  64      */
  65     final static boolean checkClash = true;
  66 
  67     private final Names names;
  68     private final Enter enter;
  69     private final Log log;


 630             attr.attribType(tree.vartype, localEnv);
 631         } finally {
 632             chk.setDeferredLintHandler(prevLintHandler);
 633         }
 634 
 635         if ((tree.mods.flags & VARARGS) != 0) {
 636             //if we are entering a varargs parameter, we need to replace its type
 637             //(a plain array type) with the more precise VarargsType --- we need
 638             //to do it this way because varargs is represented in the tree as a modifier
 639             //on the parameter declaration, and not as a distinct type of array node.
 640             ArrayType atype = (ArrayType)tree.vartype.type;
 641             tree.vartype.type = atype.makeVarargs();
 642         }
 643         Scope enclScope = enter.enterScope(env);
 644         VarSymbol v =
 645             new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
 646         v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
 647         tree.sym = v;
 648         if (tree.init != null) {
 649             v.flags_field |= HASINIT;
 650             if ((v.flags_field & FINAL) != 0 && !tree.init.hasTag(NEWCLASS)) {
 651                 Env<AttrContext> initEnv = getInitEnv(tree, env);
 652                 initEnv.info.enclVar = v;
 653                 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree.init);
 654             }
 655         }
 656         if (chk.checkUnique(tree.pos(), v, enclScope)) {
 657             chk.checkTransparentVar(tree.pos(), v, enclScope);
 658             enclScope.enter(v);
 659         }
 660         annotateLater(tree.mods.annotations, localEnv, v);
 661         v.pos = tree.pos;
 662     }
 663 
 664     /** Create a fresh environment for a variable's initializer.
 665      *  If the variable is a field, the owner of the environment's scope
 666      *  is be the variable itself, otherwise the owner is the method
 667      *  enclosing the variable definition.
 668      *
 669      *  @param tree     The variable definition.
 670      *  @param env      The environment current outside of the variable definition.


 854         }
 855 
 856         ClassSymbol c = (ClassSymbol)sym;
 857         ClassType ct = (ClassType)c.type;
 858         Env<AttrContext> env = enter.typeEnvs.get(c);
 859         JCClassDecl tree = (JCClassDecl)env.tree;
 860         boolean wasFirst = isFirst;
 861         isFirst = false;
 862 
 863         JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
 864         try {
 865             // Save class environment for later member enter (2) processing.
 866             halfcompleted.append(env);
 867 
 868             // Mark class as not yet attributed.
 869             c.flags_field |= UNATTRIBUTED;
 870 
 871             // If this is a toplevel-class, make sure any preceding import
 872             // clauses have been seen.
 873             if (c.owner.kind == PCK) {
 874                 memberEnter(env.toplevel, env.enclosing(TOPLEVEL));
 875                 todo.append(env);
 876             }
 877 
 878             if (c.owner.kind == TYP)
 879                 c.owner.complete();
 880 
 881             // create an environment for evaluating the base clauses
 882             Env<AttrContext> baseEnv = baseEnv(tree, env);
 883 
 884             // Determine supertype.
 885             Type supertype =
 886                 (tree.extending != null)
 887                 ? attr.attribBase(tree.extending, baseEnv, true, false, true)
 888                 : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c))
 889                 ? attr.attribBase(enumBase(tree.pos, c), baseEnv,
 890                                   true, false, false)
 891                 : (c.fullname == names.java_lang_Object)
 892                 ? Type.noType
 893                 : syms.objectType;
 894             ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);