Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/javac/BatchParser.java
38918 views
/*1* Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.javac;2627import sun.tools.java.*;28import sun.tools.tree.*;2930import java.io.IOException;31import java.io.InputStream;32import java.util.Vector;33import java.util.Enumeration;3435/**36* Batch file parser, this needs more work.37*38* WARNING: The contents of this source file are not part of any39* supported API. Code that depends on them does so at its own risk:40* they are subject to change or removal without notice.41*/42@Deprecated43public44class BatchParser extends Parser {45/**46* The current package47*/48protected Identifier pkg;4950/**51* The current imports52*/53protected Imports imports;5455/**56* The classes defined in this file57*/58protected Vector classes;596061/**62* The current class63*/64protected SourceClass sourceClass;6566/**67* The toplevel environment68*/69protected Environment toplevelEnv;7071/**72* Create a batch file parser73*/74public BatchParser(Environment env, InputStream in) throws IOException {75super(env, in);7677imports = new Imports(env);78classes = new Vector();79toplevelEnv = imports.newEnvironment(env);80}8182/**83* Package declaration84*/85public void packageDeclaration(long where, IdentifierToken t) {86Identifier nm = t.getName();87//System.out.println("package " + nm);88if (pkg == null) {89// This code has been changed to pass an IdentifierToken,90// rather than an Identifier, to setCurrentPackage(). Imports91// now needs the location of the token.92pkg = t.getName();93imports.setCurrentPackage(t);94} else {95env.error(where, "package.repeated");96}97}9899/**100* Import class101*/102public void importClass(long pos, IdentifierToken t) {103//System.out.println("import class " + t);104imports.addClass(t);105}106107/**108* Import package109*/110public void importPackage(long pos, IdentifierToken t) {111//System.out.println("import package " + t);112imports.addPackage(t);113}114115/**116* Define class117*/118public ClassDefinition beginClass(long where, String doc, int mod,119IdentifierToken t,120IdentifierToken sup,121IdentifierToken interfaces[]) {122123// If this class is nested, the modifier bits set here will124// be copied into the 'SourceMember' object for the inner class125// created during the call to 'makeClassDefinition' below.126// When writing the class file, we will look there for the127// 'untransformed' modifiers. The modifiers in the ClassDefinition128// object will end up as the 'transformed' modifiers. Note that129// there are some bits set here that are not legal class modifiers130// according to the JVMS, e.g., M_PRIVATE and M_STATIC. These are131// masked off while writing the class file, but are preserved in132// the InnerClasses attributes.133134if (tracing) toplevelEnv.dtEnter("beginClass: " + sourceClass);135136SourceClass outerClass = sourceClass;137138if (outerClass == null && pkg != null) {139t = new IdentifierToken(t.getWhere(),140Identifier.lookup(pkg, t.getName()));141}142143// The defaults for anonymous and local classes should be documented!144145if ((mod & M_ANONYMOUS) != 0) {146mod |= (M_FINAL | M_PRIVATE);147}148if ((mod & M_LOCAL) != 0) {149mod |= M_PRIVATE;150}151152// Certain modifiers are implied as follows:153//154// 1. Any interface (nested or not) is implicitly deemed to be abstract,155// whether it is explicitly marked so or not. (Java 1.0.)156// 2. A interface which is a member of a type is implicitly deemed to157// be static, whether it is explicitly marked so or not. (InnerClasses)158// 3a. A type which is a member of an interface is implicitly deemed159// to be public, whether it is explicitly marked so or not. (InnerClasses)160// 3b. A type which is a member of an interface is implicitly deemed161// to be static, whether it is explicitly marked so or not. (InnerClasses)162163if ((mod & M_INTERFACE) != 0) {164// Rule 1.165mod |= M_ABSTRACT;166if (outerClass != null) {167// Rule 2.168mod |= M_STATIC;169}170}171172if (outerClass != null && outerClass.isInterface()) {173// Rule 3a.174// For interface members, neither 'private' nor 'protected'175// are legal modifiers. We avoid setting M_PUBLIC in some176// cases in order to avoid interfering with error detection177// and reporting. This is patched up, after reporting an178// error, by 'SourceClass.addMember'.179if ((mod & (M_PRIVATE | M_PROTECTED)) == 0)180mod |= M_PUBLIC;181// Rule 3b.182mod |= M_STATIC;183}184185// For nested classes, we must transform 'protected' to 'public'186// and 'private' to package scope. This must be done later,187// because any modifiers set here will be copied into the188// 'MemberDefinition' for the nested class, which must represent189// the original untransformed modifiers. Also, compile-time190// checks should be performed against the actual, untransformed191// modifiers. This is in contrast to transformations that implement192// implicit modifiers, such as M_STATIC and M_FINAL for fields193// of interfaces.194195sourceClass = (SourceClass)196toplevelEnv.makeClassDefinition(toplevelEnv, where, t,197doc, mod, sup,198interfaces, outerClass);199200sourceClass.getClassDeclaration().setDefinition(sourceClass, CS_PARSED);201env = new Environment(toplevelEnv, sourceClass);202203if (tracing) toplevelEnv.dtEvent("beginClass: SETTING UP DEPENDENCIES");204205// The code which adds artificial dependencies between206// classes in the same source file has been moved to207// BatchEnvironment#parseFile().208209if (tracing) toplevelEnv.dtEvent("beginClass: ADDING TO CLASS LIST");210211classes.addElement(sourceClass);212213if (tracing) toplevelEnv.dtExit("beginClass: " + sourceClass);214215return sourceClass;216}217218/**219* Report the current class under construction.220*/221public ClassDefinition getCurrentClass() {222return sourceClass;223}224225/**226* End class227*/228public void endClass(long where, ClassDefinition c) {229230if (tracing) toplevelEnv.dtEnter("endClass: " + sourceClass);231232// c == sourceClass; don't bother to check233sourceClass.setEndPosition(where);234SourceClass outerClass = (SourceClass) sourceClass.getOuterClass();235sourceClass = outerClass;236env = toplevelEnv;237if (sourceClass != null)238env = new Environment(env, sourceClass);239240if (tracing) toplevelEnv.dtExit("endClass: " + sourceClass);241}242243/**244* Define a method245*/246public void defineField(long where, ClassDefinition c,247String doc, int mod, Type t,248IdentifierToken name, IdentifierToken args[],249IdentifierToken exp[], Node val) {250// c == sourceClass; don't bother to check251Identifier nm = name.getName();252// Members that are nested classes are not created with 'defineField',253// so these transformations do not apply to them. See 'beginClass' above.254if (sourceClass.isInterface()) {255// Members of interfaces are implicitly public.256if ((mod & (M_PRIVATE | M_PROTECTED)) == 0)257// For interface members, neither 'private' nor 'protected'258// are legal modifiers. Avoid setting M_PUBLIC in some cases259// to avoid interfering with later error detection. This will260// be fixed up after the error is reported.261mod |= M_PUBLIC;262// Methods of interfaces are implicitly abstract.263// Fields of interfaces are implicitly static and final.264if (t.isType(TC_METHOD)) {265mod |= M_ABSTRACT;266} else {267mod |= M_STATIC | M_FINAL;268}269}270if (nm.equals(idInit)) {271// The parser reports "idInit" when in reality it has found272// that there is no method name at all present.273// So, decide if it's really a constructor, or a syntax error.274Type rt = t.getReturnType();275Identifier retname = !rt.isType(TC_CLASS) ? idStar /*no match*/276: rt.getClassName();277Identifier clsname = sourceClass.getLocalName();278if (clsname.equals(retname)) {279t = Type.tMethod(Type.tVoid, t.getArgumentTypes());280} else if (clsname.equals(retname.getFlatName().getName())) {281// It appears to be a constructor with spurious qualification.282t = Type.tMethod(Type.tVoid, t.getArgumentTypes());283env.error(where, "invalid.method.decl.qual");284} else if (retname.isQualified() || retname.equals(idStar)) {285// It appears to be a type name with no method name.286env.error(where, "invalid.method.decl.name");287return;288} else {289// We assume the type name is missing, even though the290// simple name that's present might have been intended291// to be a type: "String (){}" vs. "toString(){}".292env.error(where, "invalid.method.decl");293return;294}295}296297if (args == null && t.isType(TC_METHOD)) {298args = new IdentifierToken[0];299}300301if (exp == null && t.isType(TC_METHOD)) {302exp = new IdentifierToken[0];303}304305MemberDefinition f = env.makeMemberDefinition(env, where, sourceClass,306doc, mod, t, nm,307args, exp, val);308if (env.dump()) {309f.print(System.out);310}311}312}313314315