All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.ceylon.langtools.tools.javac.comp.Enter Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.redhat.ceylon.langtools.tools.javac.comp;

import static com.redhat.ceylon.langtools.tools.javac.code.Flags.*;
import static com.redhat.ceylon.langtools.tools.javac.code.Kinds.*;

import java.util.*;

import com.redhat.ceylon.javax.tools.JavaFileManager;
import com.redhat.ceylon.javax.tools.JavaFileObject;
import com.redhat.ceylon.langtools.tools.javac.code.*;
import com.redhat.ceylon.langtools.tools.javac.code.Scope.*;
import com.redhat.ceylon.langtools.tools.javac.code.Symbol.*;
import com.redhat.ceylon.langtools.tools.javac.code.Type.*;
import com.redhat.ceylon.langtools.tools.javac.jvm.*;
import com.redhat.ceylon.langtools.tools.javac.main.RecognizedOptions.PkgInfo;
import com.redhat.ceylon.langtools.tools.javac.tree.*;
import com.redhat.ceylon.langtools.tools.javac.tree.JCTree.*;
import com.redhat.ceylon.langtools.tools.javac.util.*;
import com.redhat.ceylon.langtools.tools.javac.util.List;
import com.redhat.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticPosition;

/** This class enters symbols for all encountered definitions into
 *  the symbol table. The pass consists of two phases, organized as
 *  follows:
 *
 *  

In the first phase, all class symbols are intered into their * enclosing scope, descending recursively down the tree for classes * which are members of other classes. The class symbols are given a * MemberEnter object as completer. * *

In the second phase classes are completed using * MemberEnter.complete(). Completion might occur on demand, but * any classes that are not completed that way will be eventually * completed by processing the `uncompleted' queue. Completion * entails (1) determination of a class's parameters, supertype and * interfaces, as well as (2) entering all symbols defined in the * class into its scope, with the exception of class symbols which * have been entered in phase 1. (2) depends on (1) having been * completed for a class and all its superclasses and enclosing * classes. That's why, after doing (1), we put classes in a * `halfcompleted' queue. Only when we have performed (1) for a class * and all it's superclasses and enclosing classes, we proceed to * (2). * *

Whereas the first phase is organized as a sweep through all * compiled syntax trees, the second phase is demand. Members of a * class are entered when the contents of a class are first * accessed. This is accomplished by installing completer objects in * class symbols for compiled classes which invoke the member-enter * phase for the corresponding class tree. * *

Classes migrate from one phase to the next via queues: * *

 *  class enter -> (Enter.uncompleted)         --> member enter (1)
 *              -> (MemberEnter.halfcompleted) --> member enter (2)
 *              -> (Todo)                      --> attribute
 *                                              (only for toplevel classes)
 *  
* *

This is NOT part of any supported API. * If you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice. */ public class Enter extends JCTree.Visitor { protected static final Context.Key enterKey = new Context.Key(); Log log; Symtab syms; Check chk; TreeMaker make; ClassReader reader; SourceLanguage sourceLanguage; Annotate annotate; MemberEnter memberEnter; Types types; Lint lint; Names names; JavaFileManager fileManager; PkgInfo pkginfoOpt; private Todo todo; public static Enter instance(Context context) { Enter instance = context.get(enterKey); if (instance == null){ instance = new Enter(context); instance.init(context); } return instance; } protected Enter(Context context) { context.put(enterKey, this); } protected void init(Context context){ log = Log.instance(context); sourceLanguage = SourceLanguage.instance(context); reader = ClassReader.instance(context); make = TreeMaker.instance(context); syms = Symtab.instance(context); chk = Check.instance(context); memberEnter = MemberEnter.instance(context); types = Types.instance(context); annotate = Annotate.instance(context); lint = Lint.instance(context); names = Names.instance(context); predefClassDef = make.ClassDef( make.Modifiers(PUBLIC), syms.predefClass.name, null, null, null, null); predefClassDef.sym = syms.predefClass; todo = Todo.instance(context); fileManager = context.get(JavaFileManager.class); Options options = Options.instance(context); pkginfoOpt = PkgInfo.get(options); } /** A hashtable mapping classes and packages to the environments current * at the points of their definitions. */ Map> typeEnvs = new HashMap>(); /** Accessor for typeEnvs */ public Env getEnv(TypeSymbol sym) { return typeEnvs.get(sym); } public Env getClassEnv(TypeSymbol sym) { Env localEnv = getEnv(sym); Env lintEnv = localEnv; while (lintEnv.info.lint == null) lintEnv = lintEnv.next; localEnv.info.lint = lintEnv.info.lint.augment(sym.attributes_field, sym.flags()); return localEnv; } /** The queue of all classes that might still need to be completed; * saved and initialized by main(). */ ListBuffer uncompleted; /** A dummy class to serve as enclClass for toplevel environments. */ private JCClassDecl predefClassDef; /* ************************************************************************ * environment construction *************************************************************************/ /** Create a fresh environment for class bodies. * This will create a fresh scope for local symbols of a class, referred * to by the environments info.scope field. * This scope will contain * - symbols for this and super * - symbols for any type parameters * In addition, it serves as an anchor for scopes of methods and initializers * which are nested in this scope via Scope.dup(). * This scope should not be confused with the members scope of a class. * * @param tree The class definition. * @param env The environment current outside of the class definition. */ public Env classEnv(JCClassDecl tree, Env env) { Env localEnv = env.dup(tree, env.info.dup(new Scope(tree.sym))); localEnv.enclClass = tree; localEnv.outer = env; localEnv.info.isSelfCall = false; localEnv.info.lint = null; // leave this to be filled in by Attr, // when annotations have been processed return localEnv; } /** Create a fresh environment for toplevels. * @param tree The toplevel tree. */ Env topLevelEnv(JCCompilationUnit tree) { Env localEnv = new Env(tree, new AttrContext()); localEnv.toplevel = tree; localEnv.enclClass = predefClassDef; tree.namedImportScope = new ImportScope(tree.packge); tree.starImportScope = new StarImportScope(tree.packge); localEnv.info.scope = tree.namedImportScope; localEnv.info.lint = lint; return localEnv; } public Env getTopLevelEnv(JCCompilationUnit tree) { Env localEnv = new Env(tree, new AttrContext()); localEnv.toplevel = tree; localEnv.enclClass = predefClassDef; localEnv.info.scope = tree.namedImportScope; localEnv.info.lint = lint; return localEnv; } /** The scope in which a member definition in environment env is to be entered * This is usually the environment's scope, except for class environments, * where the local scope is for type variables, and the this and super symbol * only, and members go into the class member scope. */ Scope enterScope(Env env) { return (env.tree.getTag() == JCTree.CLASSDEF) ? ((JCClassDecl) env.tree).sym.members_field : env.info.scope; } /* ************************************************************************ * Visitor methods for phase 1: class enter *************************************************************************/ /** Visitor argument: the current environment. */ protected Env env; /** Visitor result: the computed type. */ Type result; /** Visitor method: enter all classes in given tree, catching any * completion failure exceptions. Return the tree's type. * * @param tree The tree to be visited. * @param env The environment visitor argument. */ protected Type classEnter(JCTree tree, Env env) { Env prevEnv = this.env; try { this.env = env; tree.accept(this); return result; } catch (CompletionFailure ex) { return chk.completionError(tree.pos(), ex); } finally { this.env = prevEnv; } } /** Visitor method: enter classes of a list of trees, returning a list of types. */ List classEnter(List trees, Env env) { ListBuffer ts = new ListBuffer(); for (List l = trees; l.nonEmpty(); l = l.tail) { Type t = classEnter(l.head, env); if (t != null) ts.append(t); } return ts.toList(); } @Override public void visitTopLevel(JCCompilationUnit tree) { JavaFileObject prev = log.useSource(tree.sourcefile); boolean addEnv = false; boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); if (tree.pid != null) { tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid)); if (tree.packageAnnotations.nonEmpty() || pkginfoOpt == PkgInfo.ALWAYS) { if (isPkgInfo) { addEnv = true; } else { log.error(tree.packageAnnotations.head.pos(), "pkg.annotations.sb.in.package-info.java"); } } } else { tree.packge = syms.unnamedPackage; } tree.packge.complete(); // Find all classes in package. Env topEnv = topLevelEnv(tree); // Save environment of package-info.java file. if (isPkgInfo) { Env env0 = typeEnvs.get(tree.packge); if (env0 == null) { typeEnvs.put(tree.packge, topEnv); } else { JCCompilationUnit tree0 = env0.toplevel; if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) { log.warning(tree.pid != null ? tree.pid.pos() : null, "pkg-info.already.seen", tree.packge); if (addEnv || (tree0.packageAnnotations.isEmpty() && tree.docComments != null && tree.docComments.get(tree) != null)) { typeEnvs.put(tree.packge, topEnv); } } } for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner) q.flags_field |= EXISTS; Name name = names.package_info; ClassSymbol c = reader.enterClass(name, tree.packge); c.flatname = names.fromString(tree.packge + "." + name); c.sourcefile = tree.sourcefile; c.completer = null; c.members_field = new Scope(c); tree.packge.package_info = c; } classEnter(tree.defs, topEnv); if (addEnv) { todo.append(topEnv); } log.useSource(prev); result = null; } @Override public void visitClassDef(JCClassDecl tree) { Symbol owner = env.info.scope.owner; Scope enclScope = enterScope(env); ClassSymbol c; if (owner.kind == PCK) { // We are seeing a toplevel class. PackageSymbol packge = (PackageSymbol)owner; for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner) q.flags_field |= EXISTS; c = reader.enterClass(tree.name, packge); packge.members().enterIfAbsent(c); if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env) && !sourceLanguage.isCeylon()) { log.error(tree.pos(), "class.public.should.be.in.file", tree.name); } } else { if (!tree.name.isEmpty() && !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) { result = null; return; } if (owner.kind == TYP) { // We are seeing a member class. c = reader.enterClass(tree.name, (TypeSymbol)owner); if ((owner.flags_field & INTERFACE) != 0) { tree.mods.flags |= PUBLIC | STATIC; } } else { // We are seeing a local class. c = reader.defineClass(tree.name, owner); c.flatname = chk.localClassName(c); if (!c.name.isEmpty()) chk.checkTransparentClass(tree.pos(), c, env.info.scope); } } tree.sym = c; // Enter class into `compiled' table and enclosing scope. if (chk.compiled.get(c.flatname) != null) { duplicateClass(tree.pos(), c); result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType); tree.sym = (ClassSymbol)result.tsym; return; } chk.compiled.put(c.flatname, c); // CEYLON(stef): don't add anonymous classes to the environment if(tree.name.length() != 0) enclScope.enter(c); // Set up an environment for class block and store in `typeEnvs' // table, to be retrieved later in memberEnter and attribution. Env localEnv = classEnv(tree, env); typeEnvs.put(c, localEnv); // Fill out class fields. c.completer = memberEnter; c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree); c.sourcefile = env.toplevel.sourcefile; c.members_field = new Scope(c); // Ceylon: this is code that used to sit in Attr.visitClassDef() but it would be set // after this method is called, and this method needs this flag further down to // determine the enclosing type if(sourceLanguage.isCeylon()){ // If this class appears as an anonymous class // in a superclass constructor call where // no explicit outer instance is given, // disable implicit outer instance from being passed. // (This would be an illegal access to "this before super"). if (env.info.isSelfCall && (isNewAnonymousClass(env.tree) || isNewLetClass(env.tree))) { c.flags_field |= CEYLON_NOOUTERTHIS; } } ClassType ct = (ClassType)c.type; // Ceylon: make sure everything is reset if we Enter twice for bootstrap ct.interfaces_field = null; ct.allparams_field = null; ct.supertype_field = null; ct.all_interfaces_field = null; // End Ceylon if (owner.kind != PCK && (c.flags_field & STATIC) == 0) { // We are seeing a local or inner class. // Set outer_field of this class to closest enclosing class // which contains this class in a non-static context // (its "enclosing instance class"), provided such a class exists. Symbol owner1 = owner; // Ceylon: make sure we skip outer classes if required boolean skip = c.skipOuterClass(); while (skip || ((owner1.kind & (VAR | MTH)) != 0 && (owner1.flags_field & STATIC) == 0)) { // Ceylon: we only take new outer class skip orders from types, never from methods/vars if(owner1.kind == TYP) skip = owner1.skipOuterClass(); owner1 = owner1.owner; } if (owner1.kind == TYP) { ct.setEnclosingType(owner1.type); } } // Enter type parameters. ct.typarams_field = classEnter(tree.typarams, localEnv); // Add non-local class to uncompleted, to make sure it will be // completed later. if (!c.isLocal() && uncompleted != null) uncompleted.append(c); // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG // Recursively enter all member classes. classEnter(tree.defs, localEnv); result = c.type; } /** * Ceylon: return true if env.tree is a new unqualified anonymous class */ private boolean isNewAnonymousClass(JCTree tree) { return tree.getTag() == JCTree.NEWCLASS && ((JCNewClass) tree).encl == null; } /** * Ceylon: return true if env.tree is a let with a statement containing a class * and an expression which is an unqualified instanciation of that class */ private boolean isNewLetClass(JCTree tree) { if(tree.getTag() != JCTree.LETEXPR) return false; JCTree.LetExpr let = (JCTree.LetExpr)tree; return let.stats.size() == 1 && let.stats.head.getTag() == JCTree.CLASSDEF && let.expr != null && isNewAnonymousClass(let.expr); } //where /** Does class have the same name as the file it appears in? */ private static boolean classNameMatchesFileName(ClassSymbol c, Env env) { return env.toplevel.sourcefile.isNameCompatible(c.name.toString(), JavaFileObject.Kind.SOURCE); } /** Complain about a duplicate class. */ protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) { log.error(pos, "duplicate.class", c.fullname); } /** Class enter visitor method for type parameters. * Enter a symbol for type parameter in local scope, after checking that it * is unique. */ @Override public void visitTypeParameter(JCTypeParameter tree) { TypeVar a = (tree.type != null) ? (TypeVar)tree.type : new TypeVar(tree.name, env.info.scope.owner, syms.botType); tree.type = a; if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) { env.info.scope.enter(a.tsym); } result = a; } /** Default class enter visitor method: do nothing. */ @Override public void visitTree(JCTree tree) { result = null; } /** Main method: enter all classes in a list of toplevel trees. * @param trees The list of trees to be processed. */ public void main(List trees) { complete(trees, null); } /** Main method: enter one class from a list of toplevel trees and * place the rest on uncompleted for later processing. * @param trees The list of trees to be processed. * @param c The class symbol to be processed. */ public void complete(List trees, ClassSymbol c) { annotate.enterStart(); ListBuffer prevUncompleted = uncompleted; if (memberEnter.completionEnabled) uncompleted = new ListBuffer(); try { // enter all classes, and construct uncompleted list classEnter(trees, null); // complete all uncompleted classes in memberEnter if (memberEnter.completionEnabled) { while (uncompleted.nonEmpty()) { ClassSymbol clazz = uncompleted.next(); if (c == null || c == clazz || prevUncompleted == null) clazz.complete(); else // defer prevUncompleted.append(clazz); } // if there remain any unimported toplevels (these must have // no classes at all), process their import statements as well. for (JCCompilationUnit tree : trees) { if (tree.starImportScope.elems == null) { JavaFileObject prev = log.useSource(tree.sourcefile); Env topEnv = topLevelEnv(tree); memberEnter.memberEnter(tree, topEnv); log.useSource(prev); } } } } finally { uncompleted = prevUncompleted; annotate.enterDone(); } } // Ceylon public void reset(){ typeEnvs.clear(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy