jdk/src/share/classes/sun/tools/javac/SourceClass.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 22584 eed64ee05369
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.javac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.tools.java.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.tools.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.tools.tree.CompoundStatement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.tools.asm.Assembler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.asm.ConstantPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class represents an Java class as it is read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * an Java source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
class SourceClass extends ClassDefinition {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The toplevel environment, shared with the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    Environment toplevelEnv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The default constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    SourceMember defConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    ConstantPool tab = new ConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The list of class dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
    73
    Hashtable<ClassDeclaration, ClassDeclaration> deps = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The field used to represent "this" in all of my code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    LocalMember thisArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Last token of class, as reported by parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    long endPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Access methods for constructors are distinguished from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the constructors themselves by a dummy first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * A unique type used for this purpose and shared by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * constructor access methods within a package-member class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * maintained here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * This field is null except in an outermost class containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * one or more classes needing such an access method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private Type dummyArgumentType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public SourceClass(Environment env, long where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                       ClassDeclaration declaration, String documentation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                       int modifiers, IdentifierToken superClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                       IdentifierToken interfaces[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                       SourceClass outerClass, Identifier localName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super(env.getSource(), where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
              declaration, modifiers, superClass, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        setOuterClass(outerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.toplevelEnv = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.documentation = documentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (ClassDefinition.containsDeprecated(documentation)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.modifiers |= M_DEPRECATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Check for a package level class which is declared static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (isStatic() && outerClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            env.error(where, "static.class", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            this.modifiers &=~ M_STATIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Inner classes cannot be static, nor can they be interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // (which are implicitly static).  Static classes and interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // can only occur as top-level entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // Note that we do not have to check for local classes declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // to be static (this is currently caught by the parser) but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // we check anyway in case the parser is modified to allow this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (isLocal() || (outerClass != null && !outerClass.isTopLevel())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                env.error(where, "inner.interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else if (isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                env.error(where, "static.inner.class", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                this.modifiers &=~ M_STATIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (innerClassMember != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    innerClassMember.subModifiers(M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (isPrivate() && outerClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            env.error(where, "private.class", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            this.modifiers &=~ M_PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (isProtected() && outerClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            env.error(where, "protected.class", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            this.modifiers &=~ M_PROTECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*----*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if ((isPublic() || isProtected()) && isInsideLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            env.error(where, "warn.public.local.class", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         *----*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // maybe define an uplevel "A.this" current instance field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!isTopLevel() && !isLocal()) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   157
            LocalMember outerArg = outerClass.getThisArgument();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            UplevelReference r = getReference(outerArg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            setOuterMember(r.getLocalField(env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Set simple, unmangled local name for a local or anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // NOTE: It would be OK to do this unconditionally, as null is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // correct value for a member (non-local) class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (localName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            setLocalName(localName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // Check for inner class with same simple name as one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // its enclosing classes.  Note that 'getLocalName' returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // the simple, unmangled source-level name of any class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // The previous version of this code was not careful to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // mangled local class names.  This version fixes 4047746.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Identifier thisName = getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (thisName != idNull) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // Test above suppresses error for nested anonymous classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // which have an internal "name", but are not named in source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (ClassDefinition scope = outerClass; scope != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                  scope = scope.getOuterClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                Identifier outerName = scope.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (thisName.equals(outerName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    env.error(where, "inner.redefined", thisName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Return last position in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see #getWhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public long getEndPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return endPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public void setEndPosition(long endPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.endPosition = endPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Return absolute name of source file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public String getAbsoluteName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        String AbsName = ((ClassFile)getSource()).getAbsoluteName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return AbsName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
//end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Return imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public Imports getImports() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return toplevelEnv.getImports();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Find or create my "this" argument, which is used for all methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public LocalMember getThisArgument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (thisArg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            thisArg = new LocalMember(where, this, 0, getType(), idThis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return thisArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Add a dependency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public void addDependency(ClassDeclaration c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (tab != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            tab.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // If doing -xdepend option, save away list of class dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //   making sure to NOT include duplicates or the class we are in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        //   (Hashtable's put() makes sure we don't have duplicates)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if ( toplevelEnv.print_dependencies() && c != getClassDeclaration() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            deps.put(c,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Add a field (check it first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void addMember(Environment env, MemberDefinition f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // Make sure the access permissions are self-consistent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        switch (f.getModifiers() & (M_PUBLIC | M_PRIVATE | M_PROTECTED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        case M_PUBLIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case M_PRIVATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        case M_PROTECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            env.error(f.getWhere(), "inconsistent.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // Cut out the more restrictive modifier(s):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (f.isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                f.subModifiers(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                f.subModifiers(M_PRIVATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // Note exemption for synthetic members below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (f.isStatic() && !isTopLevel() && !f.isSynthetic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (f.isMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                env.error(f.getWhere(), "static.inner.method", f, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                f.subModifiers(M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else if (f.isVariable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (!f.isFinal() || f.isBlankFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    env.error(f.getWhere(), "static.inner.field", f.getName(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    f.subModifiers(M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // Even if a static passes this test, there is still another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // check in 'SourceMember.check'.  The check is delayed so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // that the initializer may be inspected more closely, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // 'isConstant()'.  Part of fix for 4095568.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // Static inner classes are diagnosed in 'SourceClass.<init>'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                f.subModifiers(M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (f.isMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (f.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (f.getClassDefinition().isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    env.error(f.getWhere(), "intf.constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (f.isNative() || f.isAbstract() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                      f.isStatic() || f.isSynchronized() || f.isFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    env.error(f.getWhere(), "constr.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    f.subModifiers(M_NATIVE | M_ABSTRACT |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                   M_STATIC | M_SYNCHRONIZED | M_FINAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else if (f.isInitializer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (f.getClassDefinition().isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    env.error(f.getWhere(), "intf.initializer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // f is not allowed to return an array of void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if ((f.getType().getReturnType()).isVoidArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                env.error(f.getWhere(), "void.array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (f.getClassDefinition().isInterface() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                (f.isStatic() || f.isSynchronized() || f.isNative()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                 || f.isFinal() || f.isPrivate() || f.isProtected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                env.error(f.getWhere(), "intf.modifier.method", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                f.subModifiers(M_STATIC |  M_SYNCHRONIZED | M_NATIVE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                               M_FINAL | M_PRIVATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (f.isTransient()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                env.error(f.getWhere(), "transient.meth", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                f.subModifiers(M_TRANSIENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (f.isVolatile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                env.error(f.getWhere(), "volatile.meth", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                f.subModifiers(M_VOLATILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (f.isAbstract()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (f.isPrivate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    env.error(f.getWhere(), "abstract.private.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    f.subModifiers(M_PRIVATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (f.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    env.error(f.getWhere(), "abstract.static.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    f.subModifiers(M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (f.isFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    env.error(f.getWhere(), "abstract.final.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    f.subModifiers(M_FINAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (f.isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    env.error(f.getWhere(), "abstract.native.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    f.subModifiers(M_NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (f.isSynchronized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    env.error(f.getWhere(),"abstract.synchronized.modifier",f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    f.subModifiers(M_SYNCHRONIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (f.isAbstract() || f.isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (f.getValue() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    env.error(f.getWhere(), "invalid.meth.body", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    f.setValue(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (f.getValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (f.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        env.error(f.getWhere(), "no.constructor.body", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        env.error(f.getWhere(), "no.meth.body", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    f.addModifiers(M_ABSTRACT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
   360
            Vector<MemberDefinition> arguments = f.getArguments();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (arguments != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // arguments can be null if this is an implicit abstract method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                int argumentLength = arguments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                Type argTypes[] = f.getType().getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                for (int i = 0; i < argTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    Object arg = arguments.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    long where = f.getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    if (arg instanceof MemberDefinition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        where = ((MemberDefinition)arg).getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        arg = ((MemberDefinition)arg).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // (arg should be an Identifier now)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    if (argTypes[i].isType(TC_VOID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        || argTypes[i].isVoidArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        env.error(where, "void.argument", arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else if (f.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (f.isVolatile() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                f.isTransient() || f.isNative() || f.isSynchronized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                env.error(f.getWhere(), "inner.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                f.subModifiers(M_VOLATILE | M_TRANSIENT |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                               M_NATIVE | M_SYNCHRONIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // same check as for fields, below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (f.getClassDefinition().isInterface() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                  (f.isPrivate() || f.isProtected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                env.error(f.getWhere(), "intf.modifier.field", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                f.subModifiers(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                f.addModifiers(M_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // Fix up the class itself to agree with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // the inner-class member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                ClassDefinition c = f.getInnerClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                c.subModifiers(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                c.addModifiers(M_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (f.getType().isType(TC_VOID) || f.getType().isVoidArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                env.error(f.getWhere(), "void.inst.var", f.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                // REMIND: set type to error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (f.isSynchronized() || f.isAbstract() || f.isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                env.error(f.getWhere(), "var.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                f.subModifiers(M_SYNCHRONIZED | M_ABSTRACT | M_NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (f.isStrict()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                env.error(f.getWhere(), "var.floatmodifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                f.subModifiers(M_STRICTFP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (f.isTransient() && isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                env.error(f.getWhere(), "transient.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                f.subModifiers(M_TRANSIENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (f.isVolatile() && (isInterface() || f.isFinal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                env.error(f.getWhere(), "volatile.modifier", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                f.subModifiers(M_VOLATILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (f.isFinal() && (f.getValue() == null) && isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                env.error(f.getWhere(), "initializer.needed", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                f.subModifiers(M_FINAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (f.getClassDefinition().isInterface() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                  (f.isPrivate() || f.isProtected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                env.error(f.getWhere(), "intf.modifier.field", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                f.subModifiers(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                f.addModifiers(M_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // Do not check for repeated methods here:  Types are not yet resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (!f.isInitializer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            for (MemberDefinition f2 = getFirstMatch(f.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                         f2 != null; f2 = f2.getNextMatch()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (f.isVariable() && f2.isVariable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    env.error(f.getWhere(), "var.multidef", f, f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                } else if (f.isInnerClass() && f2.isInnerClass() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                           !f.getInnerClass().isLocal() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                           !f2.getInnerClass().isLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    // Found a duplicate inner-class member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    // Duplicate local classes are detected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    // 'VarDeclarationStatement.checkDeclaration'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    env.error(f.getWhere(), "inner.class.multidef", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        super.addMember(env, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Create an environment suitable for checking this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Make sure the source and imports are set right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Make sure the environment contains no context information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * (Actually, throw away env altogether and use toplevelEnv instead.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public Environment setupEnv(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // In some cases, we go to some trouble to create the 'env' argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // that is discarded.  We should remove the 'env' argument entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        // as well as the vestigial code that supports it.  See comments on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // 'newEnvironment' in 'checkInternal' below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return new Environment(toplevelEnv, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * A source class never reports deprecation, since the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * allows access to deprecated features that are being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * in the same job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public boolean reportDeprecated(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * See if the source file of this class is right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see ClassDefinition#noteUsedBy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void noteUsedBy(ClassDefinition ref, long where, Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // If this class is not public, watch for cross-file references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        super.noteUsedBy(ref, where, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        ClassDefinition def = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        while (def.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            def = def.getOuterClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (def.isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return;             // already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        while (ref.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            ref = ref.getOuterClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (def.getSource().equals(ref.getSource())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return;             // intra-file reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        ((SourceClass)def).checkSourceFile(env, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Check this class and all its fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public void check(Environment env) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (tracing) env.dtEnter("SourceClass.check: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (isInsideLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // An inaccessible class gets checked when the surrounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // block is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // QUERY: Should this case ever occur?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // What would invoke checking of a local class aside from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // checking the surrounding method body?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (tracing) env.dtEvent("SourceClass.check: INSIDE LOCAL " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                     getOuterClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            getOuterClass().check(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                if (tracing) env.dtEvent("SourceClass.check: INNER CLASS " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                         getOuterClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                // Make sure the outer is checked first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                ((SourceClass)getOuterClass()).maybeCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            Vset vset = new Vset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            Context ctx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                env.dtEvent("SourceClass.check: CHECK INTERNAL " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            vset = checkInternal(setupEnv(env), ctx, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            // drop vset here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (tracing) env.dtExit("SourceClass.check: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private void maybeCheck(Environment env) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (tracing) env.dtEvent("SourceClass.maybeCheck: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // Check this class now, if it has not yet been checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // Cf. Main.compile().  Perhaps this code belongs there somehow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        ClassDeclaration c = getClassDeclaration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (c.getStatus() == CS_PARSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // Set it first to avoid vicious circularity:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            c.setDefinition(this, CS_CHECKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            check(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private Vset checkInternal(Environment env, Context ctx, Vset vset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        Identifier nm = getClassDeclaration().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (env.verbose()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            env.output("[checking class " + nm + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // Save context enclosing class for later access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // by 'ClassDefinition.resolveName.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        classContext = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // At present, the call to 'newEnvironment' is not needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // The incoming environment to 'basicCheck' is always passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        // 'setupEnv', which discards it completely.  This is also the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        // only call to 'newEnvironment', which is now apparently dead code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        basicCheck(Context.newEnvironment(env, ctx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        // Validate access for all inner-class components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        // of a qualified name, not just the last one, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // is checked below.  Yes, this is a dirty hack...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // Much of this code was cribbed from 'checkSupers'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // Part of fix for 4094658.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        ClassDeclaration sup = getSuperClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (sup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            long where = getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            where = IdentifierToken.getWhere(superClassId, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            env.resolveExtendsByName(where, this, sup.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            ClassDeclaration intf = interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            long where = getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // Error localization fails here if interfaces were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // elided during error recovery from an invalid one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (interfaceIds != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                && interfaceIds.length == interfaces.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                where = IdentifierToken.getWhere(interfaceIds[i], where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            env.resolveExtendsByName(where, this, intf.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // Does the name already exist in an imported package?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // See JLS 8.1 for the precise rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (!isInnerClass() && !isInsideLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // Discard package qualification for the import checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            Identifier simpleName = nm.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                // We want this to throw a ClassNotFound exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                Imports imports = toplevelEnv.getImports();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                Identifier ID = imports.resolve(env, simpleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                if (ID != getName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    env.error(where, "class.multidef.import", simpleName, ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } catch (AmbiguousClass e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                // At least one of e.name1 and e.name2 must be different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                Identifier ID = (e.name1 != getName()) ? e.name1 : e.name2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                env.error(where, "class.multidef.import", simpleName, ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }  catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // we want this to happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            // Make sure that no package with the same fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // name exists.  This is required by JLS 7.1.  We only need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // to perform this check for top level classes -- it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            // necessary for inner classes.  (bug 4101529)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            // This change has been backed out because, on WIN32, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            // failed to distinguish between java.awt.event and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            // java.awt.Event when looking for a directory.  We will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // add this back in later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            //  if (env.getPackage(nm).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            //      env.error(where, "class.package.conflict", nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            //  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            // } catch (java.io.IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            //  env.error(where, "io.exception.package", nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            // Make sure it was defined in the right file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                checkSourceFile(env, getWhere());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        vset = checkMembers(env, ctx, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    private boolean sourceFileChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * See if the source file of this class is of the right name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public void checkSourceFile(Environment env, long where) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // one error per offending class is sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (sourceFileChecked)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        sourceFileChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        String fname = getName().getName() + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        String src = ((ClassFile)getSource()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (!src.equals(fname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                env.error(where, "public.class.file", this, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                env.error(where, "warn.package.class.file", this, src, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    // Set true if superclass (but not necessarily superinterfaces) have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    // been checked.  If the superclass is still unresolved, then an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    // message should have been issued, and we assume that no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    // resolution is possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    private boolean supersChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Overrides 'ClassDefinition.getSuperClass'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public ClassDeclaration getSuperClass(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (tracing) env.dtEnter("SourceClass.getSuperClass: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // Superclass may fail to be set because of error recovery,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // so resolve types here only if 'checkSupers' has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // completed its checks on the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // QUERY: Can we eliminate the need to resolve superclasses on demand?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        // See comments in 'checkSupers' and in 'ClassDefinition.getInnerClass'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (superClass == null && superClassId != null && !supersChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            // We used to report an error here if the superclass was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // resolved.  Having moved the call to 'checkSupers' from 'basicCheck'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            // into 'resolveTypeStructure', the errors reported here should have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            // already been reported.  Furthermore, error recovery can null out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            // the superclass, which would cause a spurious error from the test here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (tracing) env.dtExit("SourceClass.getSuperClass: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Check that all superclasses and superinterfaces are defined and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * well formed.  Among other checks, verify that the inheritance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * graph is acyclic.  Called from 'resolveTypeStructure'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private void checkSupers(Environment env) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        // *** DEBUG ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        supersCheckStarted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (tracing) env.dtEnter("SourceClass.checkSupers: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if (isFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                Identifier nm = getClassDeclaration().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                env.error(getWhere(), "final.intf", nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // Interfaces have no superclass.  Superinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                // are checked below, in code shared with the class case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            // Check superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            // Call to 'getSuperClass(env)' (note argument) attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            // 'resolveTypeStructure' if superclass has not successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            // been resolved.  Since we have just now called 'resolveSupers'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            // (see our call in 'resolveTypeStructure'), it is not clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // that this can do any good.  Why not 'getSuperClass()' here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (getSuperClass(env) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                long where = getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                where = IdentifierToken.getWhere(superClassId, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    ClassDefinition def =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        getSuperClass().getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    // Resolve superclass and its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    def.resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    // Access to the superclass should be checked relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    // to the surrounding context, not as if the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    // appeared within the class body. Changed 'canAccess'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    // to 'extendsCanAccess' to fix 4087314.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    if (!extendsCanAccess(env, getSuperClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        env.error(where, "cant.access.class", getSuperClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        // Might it be a better recovery to let the access go through?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    } else if (def.isFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        env.error(where, "super.is.final", getSuperClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        // Might it be a better recovery to let the access go through?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    } else if (def.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        env.error(where, "super.is.intf", getSuperClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    } else if (superClassOf(env, getSuperClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        env.error(where, "cyclic.super");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        def.noteUsedBy(this, where, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    if (superClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        def = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        // If we have a valid superclass, check its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        // supers as well, and so on up to root class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        // Call to 'enclosingClassOf' will raise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        // 'NullPointerException' if 'def' is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        // so omit this check as error recovery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        ClassDefinition sup = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                            if (enclosingClassOf(sup)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                // Do we need a similar test for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                // interfaces?  See bugid 4038529.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                                env.error(where, "super.is.inner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                            // Since we resolved the superclass and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            // ancestors above, we should not discover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                            // any unresolved classes on the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                            // chain.  It should thus be sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                            // call 'getSuperClass()' (no argument) here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                            ClassDeclaration s = sup.getSuperClass(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                // Superclass not resolved due to error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                            sup = s.getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    // Error is detected in call to 'getClassDefinition'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    // The class may actually exist but be ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    // Call env.resolve(e.name) to see if it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    // env.resolve(name) will definitely tell us if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    // class is ambiguous, but may not necessarily tell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    // us if the class is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    // (part of solution for 4059855)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                reportError: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                            env.resolve(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        } catch (AmbiguousClass ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            env.error(where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                      "ambig.class", ee.name1, ee.name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                            break reportError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                            // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                        env.error(where, "super.not.found", e.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    } // The break exits this block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                // Superclass was null on entry, after call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                // 'resolveSupers'.  This should normally not happen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                // as 'resolveSupers' sets 'superClass' to a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                // value for all named classes, except for one special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                // case: 'java.lang.Object', which has no superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                if (isAnonymous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // checker should have filled it in first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    throw new CompilerError("anonymous super");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                } else  if (!getName().equals(idJavaLangObject)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    throw new CompilerError("unresolved super");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // At this point, if 'superClass' is null due to an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // in the user program, a message should have been issued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        supersChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // Check interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            ClassDeclaration intf = interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            long where = getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            if (interfaceIds != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                && interfaceIds.length == interfaces.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                where = IdentifierToken.getWhere(interfaceIds[i], where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                ClassDefinition def = intf.getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                // Resolve superinterface and its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                def.resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                // Check superinterface access in the correct context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                // Changed 'canAccess' to 'extendsCanAccess' to fix 4087314.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                if (!extendsCanAccess(env, intf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    env.error(where, "cant.access.class", intf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                } else if (!intf.getClassDefinition(env).isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    env.error(where, "not.intf", intf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                } else if (isInterface() && implementedBy(env, intf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    env.error(where, "cyclic.intf", intf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    def.noteUsedBy(this, where, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    // Interface is OK, leave it in the interface list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                // The interface may actually exist but be ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                // Call env.resolve(e.name) to see if it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                // env.resolve(name) will definitely tell us if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                // interface is ambiguous, but may not necessarily tell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                // us if the interface is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                // (part of solution for 4059855)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            reportError2: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        env.resolve(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    } catch (AmbiguousClass ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        env.error(where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                  "ambig.class", ee.name1, ee.name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        break reportError2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    env.error(where, "intf.not.found", e.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                } // The break exits this block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // Remove this interface from the list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // as recovery from an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            ClassDeclaration newInterfaces[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                new ClassDeclaration[interfaces.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            System.arraycopy(interfaces, 0, newInterfaces, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            System.arraycopy(interfaces, i + 1, newInterfaces, i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                             newInterfaces.length - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            interfaces = newInterfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            --i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (tracing) env.dtExit("SourceClass.checkSupers: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Check all of the members of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Inner classes are checked in the following way.  Any class which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * is immediately contained in a block (anonymous and local classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * is checked along with its containing method; see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * SourceMember.check() method for more information.  Member classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * of this class are checked immediately after this class, unless this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * class is insideLocal(), in which case, they are checked with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * rest of the members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    private Vset checkMembers(Environment env, Context ctx, Vset vset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        // bail out if there were any errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if (getError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // Make sure that all of our member classes have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        // basicCheck'ed before we check the rest of our members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        // If our member classes haven't been basicCheck'ed, then they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        // may not have <init> methods.  It is important that they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        // have <init> methods so we can process NewInstanceExpressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // correctly.  This problem didn't occur before 1.2beta1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // This is a fix for bug 4082816.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                     f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (f.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                // System.out.println("Considering " + f + " in " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                SourceClass cdef = (SourceClass) f.getInnerClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                if (cdef.isMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    cdef.basicCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (isFinal() && isAbstract()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            env.error(where, "final.abstract", this.getName().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // This class should be abstract if there are any abstract methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // in our parent classes and interfaces which we do not override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // There are odd cases when, even though we cannot access some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // abstract method from our superclass, that abstract method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // still force this class to be abstract.  See the discussion in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        // bug id 1240831.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (!isInterface() && !isAbstract() && mustBeAbstract(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            // Set the class abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            modifiers |= M_ABSTRACT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // Tell the user which methods force this class to be abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            // First list all of the "unimplementable" abstract methods.
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
   924
            Iterator<MemberDefinition> iter = getPermanentlyAbstractMethods();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            while (iter.hasNext()) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
   926
                MemberDefinition method = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                // We couldn't override this method even if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                // wanted to.  Try to make the error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                // as non-confusing as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                env.error(where, "abstract.class.cannot.override",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                          getClassDeclaration(), method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                          method.getDefiningClassDeclaration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            // Now list all of the traditional abstract methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            iter = getMethods(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                // For each method, check if it is abstract.  If it is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                // output an appropriate error message.
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
   940
                MemberDefinition method = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                if (method.isAbstract()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    env.error(where, "abstract.class",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                              getClassDeclaration(), method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                              method.getDefiningClassDeclaration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // Check the instance variables in a pre-pass before any constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        // This lets constructors "in-line" any initializers directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        // It also lets us do some definite assignment checks on variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        Context ctxInit = new Context(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        Vset vsInst = vset.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        Vset vsClass = vset.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // Do definite assignment checking on blank finals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        // Other variables do not need such checks.  The simple textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // ordering constraints implemented by MemberDefinition.canReach()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        // are necessary and sufficient for the other variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // Note that within non-static code, all statics are always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        // definitely assigned, and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                     f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (f.isVariable() && f.isBlankFinal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                // The following allocates a LocalMember object as a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                // to represent the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                int number = ctxInit.declareFieldNumber(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (f.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    vsClass = vsClass.addVarUnassigned(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    vsInst = vsInst.addVar(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    vsInst = vsInst.addVarUnassigned(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    vsClass = vsClass.addVar(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // For instance variable checks, use a context with a "this" parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        Context ctxInst = new Context(ctxInit, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        LocalMember thisArg = getThisArgument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        int thisNumber = ctxInst.declare(env, thisArg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        vsInst = vsInst.addVar(thisNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        // Do all the initializers in order, checking the definite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        // assignment of blank finals.  Separate static from non-static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                     f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                if (f.isVariable() || f.isInitializer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    if (f.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        vsClass = f.check(env, ctxInit, vsClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                        vsInst = f.check(env, ctxInst, vsInst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                env.error(f.getWhere(), "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        checkBlankFinals(env, ctxInit, vsClass, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        // Check the rest of the field definitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // (Note:  Re-checking a field is a no-op.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                     f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                if (f.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    // When checking a constructor, an explicit call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    // 'this(...)' makes all blank finals definitely assigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    // See 'MethodExpression.checkValue'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    Vset vsCon = f.check(env, ctxInit, vsInst.copy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    // May issue multiple messages for the same variable!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    checkBlankFinals(env, ctxInit, vsCon, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                    // (drop vsCon here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    Vset vsFld = f.check(env, ctx, vset.copy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    // (drop vsFld here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                env.error(f.getWhere(), "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        // Must mark class as checked before visiting inner classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        // as they may in turn request checking of the current class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        // as an outer class.  Fix for bug id 4056774.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        getClassDeclaration().setDefinition(this, CS_CHECKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        // Also check other classes in the same nest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        // All checking of this nest must be finished before any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        // of its classes emit bytecode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // Otherwise, the inner classes might not have a chance to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        // add access or class literal fields to the outer class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                     f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (f.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                SourceClass cdef = (SourceClass) f.getInnerClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                if (!cdef.isInsideLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    cdef.maybeCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // Note:  Since inner classes cannot set up-level variables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        // the returned vset is always equal to the passed-in vset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        // Still, we'll return it for the sake of regularity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /** Make sure all my blank finals exist now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    private void checkBlankFinals(Environment env, Context ctxInit, Vset vset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                  boolean isStatic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        for (int i = 0; i < ctxInit.getVarNumber(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (!vset.testVar(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                MemberDefinition ff = ctxInit.getElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                if (ff != null && ff.isBlankFinal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    && ff.isStatic() == isStatic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    && ff.getClassDefinition() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    env.error(ff.getWhere(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                              "final.var.not.initialized", ff.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Check this class has its superclass and its interfaces.  Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * force it to have an <init> method (if it doesn't already have one)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * and to have all the abstract methods of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    private boolean basicChecking = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private boolean basicCheckDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    protected void basicCheck(Environment env) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (tracing) env.dtEnter("SourceClass.basicCheck: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        super.basicCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (basicChecking || basicCheckDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (tracing) env.dtExit("SourceClass.basicCheck: OK " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (tracing) env.dtEvent("SourceClass.basicCheck: CHECKING " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        basicChecking = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        env = setupEnv(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        Imports imports = env.getImports();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (imports != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            imports.resolve(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // Check the existence of the superclass and all interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        // Also responsible for breaking inheritance cycles.  This call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        // has been moved to 'resolveTypeStructure', just after the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        // to 'resolveSupers', as inheritance cycles must be broken before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        // resolving types within the members.  Fixes 4073739.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        //   checkSupers(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (!isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // Add implicit <init> method, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // QUERY:  What keeps us from adding an implicit constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            // when the user explicitly declares one?  Is it truly guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // that the declaration for such an explicit constructor will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            // been processed by the time we arrive here?  In general, 'basicCheck'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            // is called very early, prior to the normal member checking phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (!hasConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                Node code = new CompoundStatement(getWhere(), new Statement[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                Type t = Type.tMethod(Type.tVoid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                // Default constructors inherit the access modifiers of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                // class.  For non-inner classes, this follows from JLS 8.6.7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                // as the only possible modifier is 'public'.  For the sake of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                // robustness in the presence of errors, we ignore any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                // modifiers.  For inner classes, the rule needs to be extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                // in some way to account for the possibility of private and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                // protected classes.  We make the 'obvious' extension, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                // the inner classes spec is silent on this issue, and a definitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                // resolution is needed.  See bugid 4087421.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                // WORKAROUND: A private constructor might need an access method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                // but it is not possible to create one due to a restriction in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                // the verifier.  (This is a known problem -- see 4015397.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                // We therefore do not inherit the 'private' modifier from the class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                // allowing the default constructor to be package private.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                // workaround can be observed via reflection, but is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                // undetectable, as the constructor is always accessible within
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                // the class in which its containing (private) class appears.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                int accessModifiers = getModifiers() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    (isInnerClass() ? (M_PUBLIC | M_PROTECTED) : M_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                env.makeMemberDefinition(env, getWhere(), this, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                         accessModifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                                         t, idInit, null, null, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // Only do the inheritance/override checks if they are turned on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        // The idea here is that they will be done in javac, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        // in javadoc.  See the comment for turnOffChecks(), above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if (doInheritanceChecks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            // Verify the compatibility of all inherited method definitions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            // by collecting all of our inheritable methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            collectInheritedMethods(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        basicChecking = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        basicCheckDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (tracing) env.dtExit("SourceClass.basicCheck: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Add a group of methods to this class as miranda methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * For a definition of Miranda methods, see the comment above the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * method addMirandaMethods() in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * sun/tools/java/ClassDeclaration.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    protected void addMirandaMethods(Environment env,
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  1166
                                     Iterator<MemberDefinition> mirandas) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        while(mirandas.hasNext()) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  1169
            MemberDefinition method = mirandas.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            addMember(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            //System.out.println("adding miranda method " + newMethod +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            //                   " to " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <em>After parsing is complete</em>, resolve all names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * except those inside method bodies or initializers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * In particular, this is the point at which we find out what
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * kinds of variables and methods there are in the classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * and therefore what is each class's interface to the world.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * Also perform certain other transformations, such as inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * "this$C" arguments into constructors, and reorganizing structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * to flatten qualified member names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Do not perform type-based or name-based consistency checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * or normalizations (such as default nullary constructors),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * and do not attempt to compile code against this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * until after this phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    private boolean resolving = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public void resolveTypeStructure(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            env.dtEnter("SourceClass.resolveTypeStructure: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        // Resolve immediately enclosing type, which in turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        // forces resolution of all enclosing type declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        ClassDefinition oc = getOuterClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        if (oc != null && oc instanceof SourceClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            && !((SourceClass)oc).resolved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // Do the outer class first, always.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            ((SourceClass)oc).resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            // (Note:  this.resolved is probably true at this point.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        // Punt if we've already resolved this class, or are currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        // in the process of doing so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        if (resolved || resolving) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                env.dtExit("SourceClass.resolveTypeStructure: OK " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        // Previously, 'resolved' was set here, and served to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        // duplicate resolutions here as well as its function in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // 'ClassDefinition.addMember'.  Now, 'resolving' serves the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        // former purpose, distinct from that of 'resolved'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        resolving = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            env.dtEvent("SourceClass.resolveTypeStructure: RESOLVING " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        env = setupEnv(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        // Resolve superclass names to class declarations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        // for the immediate superclass and superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        resolveSupers(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        // Check all ancestor superclasses for various
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        // errors, verifying definition of all superclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        // and superinterfaces.  Also breaks inheritance cycles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // Calls 'resolveTypeStructure' recursively for ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // This call used to appear in 'basicCheck', but was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // performed early enough.  Most of the compiler will barf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        // on inheritance cycles!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            checkSupers(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            // Undefined classes should be reported by 'checkSupers'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            env.error(where, "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        for (MemberDefinition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                 f = getFirstMember() ; f != null ; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (f instanceof SourceMember)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                ((SourceMember)f).resolveTypeStructure(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        resolving = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // Mark class as resolved.  If new members are subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // added to the class, they will be resolved at that time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        // See 'ClassDefinition.addMember'.  Previously, this variable was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        // set prior to the calls to 'checkSupers' and 'resolveTypeStructure'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        // (which may engender further calls to 'checkSupers').  This could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        // lead to duplicate resolution of implicit constructors, as the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        // 'basicCheck' from 'checkSupers' could add the constructor while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        // its class is marked resolved, and thus would resolve the constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // believing it to be a "late addition".  It would then be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        // redundantly during the normal traversal of the members, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        // immediately follows in the code above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        resolved = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        // Now we have enough information to detect method repeats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        for (MemberDefinition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                 f = getFirstMember() ; f != null ; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            if (f.isInitializer())  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (!f.isMethod())  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            for (MemberDefinition f2 = f; (f2 = f2.getNextMatch()) != null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                if (!f2.isMethod())  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                if (f.getType().equals(f2.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    env.error(f.getWhere(), "meth.multidef", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                if (f.getType().equalArguments(f2.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    env.error(f.getWhere(), "meth.redef.rettype", f, f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            env.dtExit("SourceClass.resolveTypeStructure: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    protected void resolveSupers(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            env.dtEnter("SourceClass.resolveSupers: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        // Find the super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (superClassId != null && superClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            superClass = resolveSuper(env, superClassId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            // Special-case java.lang.Object here (not in the parser).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            // In all other cases, if we have a valid 'superClassId',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            // we return with a valid and non-null 'superClass' value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            if (superClass == getClassDeclaration()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                && getName().equals(idJavaLangObject)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                    superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    superClassId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        // Find interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        if (interfaceIds != null && interfaces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            interfaces = new ClassDeclaration[interfaceIds.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                interfaces[i] = resolveSuper(env, interfaceIds[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                for (int j = 0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    if (interfaces[i] == interfaces[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                        Identifier id = interfaceIds[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                        long where = interfaceIds[j].getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                        env.error(where, "intf.repeated", id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            env.dtExit("SourceClass.resolveSupers: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    private ClassDeclaration resolveSuper(Environment env, IdentifierToken t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        Identifier name = t.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            env.dtEnter("SourceClass.resolveSuper: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        if (isInnerClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            name = outerClass.resolveName(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            name = env.resolveName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        ClassDeclaration result = env.getClassDeclaration(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        // Result is never null, as a new 'ClassDeclaration' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        // created if one with the given name does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        if (tracing) env.dtExit("SourceClass.resolveSuper: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * During the type-checking of an outer method body or initializer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * this routine is called to check a local class body
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * in the proper context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @param   sup     the named super class or interface (if anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @param   args    the actual arguments (if anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    public Vset checkLocalClass(Environment env, Context ctx, Vset vset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                                ClassDefinition sup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                                Expression args[], Type argTypes[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                ) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        env = setupEnv(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if ((sup != null) != isAnonymous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            throw new CompilerError("resolveAnonymousStructure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if (isAnonymous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            resolveAnonymousStructure(env, sup, args, argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // Run the checks in the lexical context from the outer class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        vset = checkInternal(env, ctx, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        // This is now done by 'checkInternal' via its call to 'checkMembers'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        // getClassDeclaration().setDefinition(this, CS_CHECKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * As with checkLocalClass, run the inline phase for a local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    public void inlineLocalClass(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        for (MemberDefinition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                 f = getFirstMember(); f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if ((f.isVariable() || f.isInitializer()) && !f.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                continue;       // inlined inside of constructors only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                ((SourceMember)f).inline(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                env.error(f.getWhere(), "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        if (getReferencesFrozen() != null && !inlinedLocalClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            inlinedLocalClass = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            // add more constructor arguments for uplevel references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            for (MemberDefinition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                     f = getFirstMember(); f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                if (f.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    //((SourceMember)f).addUplevelArguments(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    ((SourceMember)f).addUplevelArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    private boolean inlinedLocalClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * Check a class which is inside a local class, but is not itself local.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    public Vset checkInsideClass(Environment env, Context ctx, Vset vset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (!isInsideLocal() || isLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            throw new CompilerError("checkInsideClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        return checkInternal(env, ctx, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * Just before checking an anonymous class, decide its true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * inheritance, and build its (sole, implicit) constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    private void resolveAnonymousStructure(Environment env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                           ClassDefinition sup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                                           Expression args[], Type argTypes[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                                           ) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (tracing) env.dtEvent("SourceClass.resolveAnonymousStructure: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                                 this + ", super " + sup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        // Decide now on the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // This check has been removed as part of the fix for 4055017.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        // In the anonymous class created to hold the 'class$' method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        // of an interface, 'superClassId' refers to 'java.lang.Object'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        /*---------------------*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        if (!(superClass == null && superClassId.getName() == idNull)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            throw new CompilerError("superclass "+superClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        *---------------------*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        if (sup.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            // allow an interface in the "super class" position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            int ni = (interfaces == null) ? 0 : interfaces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            ClassDeclaration i1[] = new ClassDeclaration[1+ni];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            if (ni > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                System.arraycopy(interfaces, 0, i1, 1, ni);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                if (interfaceIds != null && interfaceIds.length == ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                    IdentifierToken id1[] = new IdentifierToken[1+ni];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    System.arraycopy(interfaceIds, 0, id1, 1, ni);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                    id1[0] = new IdentifierToken(sup.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            i1[0] = sup.getClassDeclaration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            interfaces = i1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            sup = toplevelEnv.getClassDefinition(idJavaLangObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        superClass = sup.getClassDeclaration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        if (hasConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            throw new CompilerError("anonymous constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        // Synthesize an appropriate constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        Type t = Type.tMethod(Type.tVoid, argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        IdentifierToken names[] = new IdentifierToken[argTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            names[i] = new IdentifierToken(args[i].getWhere(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                           Identifier.lookup("$"+i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        int outerArg = (sup.isTopLevel() || sup.isLocal()) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        Expression superArgs[] = new Expression[-outerArg + args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        for (int i = outerArg ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            superArgs[-outerArg + i] = new IdentifierExpression(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        long where = getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        Expression superExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if (outerArg == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            superExp = new SuperExpression(where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            superExp = new SuperExpression(where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                                           new IdentifierExpression(names[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        Expression superCall = new MethodExpression(where,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                                                    superExp, idInit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                                                    superArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        Statement body[] = { new ExpressionStatement(where, superCall) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        Node code = new CompoundStatement(where, body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        int mod = M_SYNTHETIC; // ISSUE: make M_PRIVATE, with wrapper?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        env.makeMemberDefinition(env, where, this, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                                mod, t, idInit, names, null, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * Convert class modifiers to a string for diagnostic purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * Accepts modifiers applicable to inner classes and that appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * in the InnerClasses attribute only, as well as those that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * appear in the class modifier proper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    private static int classModifierBits[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        { ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
          ACC_INTERFACE, ACC_ABSTRACT, ACC_SUPER, M_ANONYMOUS, M_LOCAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
          M_STRICTFP, ACC_STRICT};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    private static String classModifierNames[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        { "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
          "INTERFACE", "ABSTRACT", "SUPER", "ANONYMOUS", "LOCAL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
          "STRICTFP", "STRICT"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    static String classModifierString(int mods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        String s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        for (int i = 0; i < classModifierBits.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            if ((mods & classModifierBits[i]) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                s = s + " " + classModifierNames[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                mods &= ~classModifierBits[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        if (mods != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            s = s + " ILLEGAL:" + Integer.toHexString(mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * Find or create an access method for a private member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * or return null if this is not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    public MemberDefinition getAccessMember(Environment env, Context ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                                          MemberDefinition field, boolean isSuper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        return getAccessMember(env, ctx, field, false, isSuper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    public MemberDefinition getUpdateMember(Environment env, Context ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                                          MemberDefinition field, boolean isSuper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        if (!field.isVariable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            throw new CompilerError("method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        return getAccessMember(env, ctx, field, true, isSuper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    private MemberDefinition getAccessMember(Environment env, Context ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                             MemberDefinition field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                             boolean isUpdate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                             boolean isSuper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        // The 'isSuper' argument is really only meaningful when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        // target member is a method, in which case an 'invokespecial'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        // is needed.  For fields, 'getfield' and 'putfield' instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        // are generated in either case, and 'isSuper' currently plays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        // no essential role.  Nonetheless, we maintain the distinction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        // consistently for the time being.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        boolean isStatic = field.isStatic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        boolean isMethod = field.isMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        // Find pre-existing access method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        // In the case of a field access method, we only look for the getter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // A getter is always created whenever a setter is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // QUERY: Why doesn't the 'MemberDefinition' object for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        // itself just have fields for its getter and setter?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        MemberDefinition af;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        for (af = getFirstMember(); af != null; af = af.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            if (af.getAccessMethodTarget() == field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                if (isMethod && af.isSuperAccessMethod() == isSuper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                // Distinguish the getter and the setter by the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                // arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                int nargs = af.getType().getArgumentTypes().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                // This was (nargs == (isStatic ? 0 : 1) + (isUpdate ? 1 : 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                // in order to find a setter as well as a getter.  This caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                // allocation of multiple getters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                if (nargs == (isStatic ? 0 : 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        if (af != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (!isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                return af;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                MemberDefinition uf = af.getAccessUpdateMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                if (uf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    return uf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        } else if (isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            // must find or create the getter before creating the setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            af = getAccessMember(env, ctx, field, false, isSuper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // If we arrive here, we are creating a new access member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        Identifier anm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        Type dummyType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        if (field.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            // For a constructor, we use the same name as for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            // constructors ("<init>"), but add a distinguishing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            // argument of an otherwise unused "dummy" type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            anm = idInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            // Get the dummy class, creating it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            SourceClass outerMostClass = (SourceClass)getTopClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            dummyType = outerMostClass.dummyArgumentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            if (dummyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                // Create dummy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                IdentifierToken sup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    new IdentifierToken(0, idJavaLangObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                IdentifierToken interfaces[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                IdentifierToken t = new IdentifierToken(0, idNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                int mod = M_ANONYMOUS | M_STATIC | M_SYNTHETIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                // If an interface has a public inner class, the dummy class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                // the constructor must always be accessible. Fix for 4221648.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                if (outerMostClass.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    mod |= M_PUBLIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                ClassDefinition dummyClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                    toplevelEnv.makeClassDefinition(toplevelEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                                                    0, t, null, mod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                                                    sup, interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                                                    outerMostClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                // Check the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                // It is likely that a full check is not really necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                // but it is essential that the class be marked as parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                dummyClass.getClassDeclaration().setDefinition(dummyClass, CS_PARSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                Expression argsX[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                Type argTypesX[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    ClassDefinition supcls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                        toplevelEnv.getClassDefinition(idJavaLangObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    dummyClass.checkLocalClass(toplevelEnv, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                                               new Vset(), supcls, argsX, argTypesX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                } catch (ClassNotFound ee) {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                // Get class type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                dummyType = dummyClass.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                outerMostClass.dummyArgumentType = dummyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            // Otherwise, we use the name "access$N", for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            // smallest value of N >= 0 yielding an unused name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                anm = Identifier.lookup(prefixAccess + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                if (getFirstMatch(anm) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        Type argTypes[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        Type t = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        if (isStatic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            if (!isMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                if (!isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    Type at[] = { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    argTypes = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                    t = Type.tMethod(t); // nullary getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                    Type at[] = { t };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    argTypes = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                    t = Type.tMethod(Type.tVoid, argTypes); // unary setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                // Since constructors are never static, we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                // have to worry about a dummy argument here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                argTypes = t.getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            // All access methods for non-static members get an explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            // 'this' pointer as an extra argument, as the access methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            // themselves must be static. EXCEPTION: Access methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            // constructors are non-static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            Type classType = this.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            if (!isMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                if (!isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    Type at[] = { classType };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    argTypes = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    t = Type.tMethod(t, argTypes); // nullary getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                    Type at[] = { classType, t };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    argTypes = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                    t = Type.tMethod(Type.tVoid, argTypes); // unary setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                // Target is a method, possibly a constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                Type at[] = t.getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                int nargs = at.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                if (field.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    // Access method is a constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    // Requires a dummy argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                    MemberDefinition outerThisArg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                        ((SourceMember)field).getOuterThisArg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    if (outerThisArg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                        // Outer instance link must be the first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                        // The following is a sanity check that will catch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                        // most cases in which in this requirement is violated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        if (at[0] != outerThisArg.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                            throw new CompilerError("misplaced outer this");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                        // Strip outer 'this' argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                        // It will be added back when the access method is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                        argTypes = new Type[nargs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                        argTypes[0] = dummyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                        for (int i = 1; i < nargs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                            argTypes[i] = at[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                        // There is no outer instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                        argTypes = new Type[nargs+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                        argTypes[0] = dummyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                        for (int i = 0; i < nargs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                            argTypes[i+1] = at[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                    // Access method is static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    // Requires an explicit 'this' argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    argTypes = new Type[nargs+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                    argTypes[0] = classType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    for (int i = 0; i < nargs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                        argTypes[i+1] = at[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                t = Type.tMethod(t.getReturnType(), argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        int nlen = argTypes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        long where = field.getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        IdentifierToken names[] = new IdentifierToken[nlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        for (int i = 0; i < nlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            names[i] = new IdentifierToken(where, Identifier.lookup("$"+i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        Expression access = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        Expression thisArg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        Expression args[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (isStatic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            args = new Expression[nlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            for (int i = 0 ; i < nlen ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                args[i] = new IdentifierExpression(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            if (field.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                // Constructor access method is non-static, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                // 'this' works normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                thisArg = new ThisExpression(where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                // Remove dummy argument, as it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                // passed to the target method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                args = new Expression[nlen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                for (int i = 1 ; i < nlen ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                    args[i-1] = new IdentifierExpression(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                // Non-constructor access method is static, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                // we use the first argument as 'this'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                thisArg = new IdentifierExpression(names[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                // Remove first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                args = new Expression[nlen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                for (int i = 1 ; i < nlen ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                    args[i-1] = new IdentifierExpression(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            access = thisArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        if (!isMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            access = new FieldExpression(where, access, field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            if (isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                access = new AssignExpression(where, access, args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            // If true, 'isSuper' forces a non-virtual call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            access = new MethodExpression(where, access, field, args, isSuper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        Statement code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        if (t.getReturnType().isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            code = new ExpressionStatement(where, access);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            code = new ReturnStatement(where, access);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        Statement body[] = { code };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        code = new CompoundStatement(where, body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        // Access methods are now static (constructors excepted), and no longer final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        // This change was mandated by the interaction of the access method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        // naming conventions and the restriction against overriding final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        // methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        int mod = M_SYNTHETIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        if (!field.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            mod |= M_STATIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        // Create the synthetic method within the class in which the referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        // private member appears.  The 'env' argument to 'makeMemberDefinition'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        // is suspect because it represents the environment at the point at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        // which a reference takes place, while it should represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        // environment in which the definition of the synthetic method appears.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        // We get away with this because 'env' is used only to access globals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        // such as 'Environment.error', and also as an argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        // 'resolveTypeStructure', which immediately discards it using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        // 'setupEnv'. Apparently, the current definition of 'setupEnv'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        // represents a design change that has not been thoroughly propagated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        // An access method is declared with same list of exceptions as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        // target. As the exceptions are simply listed by name, the correctness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        // of this approach requires that the access method be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        // (name-resolved) in the same context as its target method  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        // should always be the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        SourceMember newf = (SourceMember)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            env.makeMemberDefinition(env, where, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                                     null, mod, t, anm, names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                                     field.getExceptionIds(), code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        // Just to be safe, copy over the name-resolved exceptions from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        // target so that the context in which the access method is checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        // doesn't matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        newf.setExceptions(field.getExceptions(env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        newf.setAccessMethodTarget(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        if (isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            af.setAccessUpdateMember(newf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        newf.setIsSuperAccessMethod(isSuper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        // The call to 'check' is not needed, as the access method will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        // checked by the containing class after it is added.  This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        // idiom followed in the implementation of class literals. (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        // 'FieldExpression.java'.) In any case, the context is wrong in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        // call below.  The access method must be checked in the context in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        // which it is declared, i.e., the class containing the referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        // private member, not the (inner) class in which the original member
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        // reference occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        // try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        //     newf.check(env, ctx, new Vset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        // } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        //     env.error(where, "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        // The comment above is inaccurate.  While it is often the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        // that the containing class will check the access method, this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        // by no means guaranteed.  In fact, an access method may be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        // after the checking of its class is complete.  In this case, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        // the context in which the class was checked will have been saved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        // the class definition object (by the fix for 4095716), allowing us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        // to check the field now, and in the correct context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        // This fixes bug 4098093.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        Context checkContext = newf.getClassDefinition().getClassContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        if (checkContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            //System.out.println("checking late addition: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                newf.check(env, checkContext, new Vset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                env.error(where, "class.not.found", ee.name, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        //System.out.println("[Access member '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        //                      newf + "' created for field '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        //                      field +"' in class '" + this + "']");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        return newf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * Find an inner class of 'this', chosen arbitrarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * Result is always an actual class, never an interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * Returns null if none found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    SourceClass findLookupContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        // Look for an immediate inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
             f != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
             f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            if (f.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                SourceClass ic = (SourceClass)f.getInnerClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                if (!ic.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    return ic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        // Look for a class nested within an immediate inner interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        // At this point, we have given up on finding a minimally-nested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        // class (which would require a breadth-first traversal).  It doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        // really matter which inner class we find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        for (MemberDefinition f = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
             f != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
             f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            if (f.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                SourceClass lc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                    ((SourceClass)f.getInnerClass()).findLookupContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                if (lc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    return lc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        // No inner classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    private MemberDefinition lookup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * Get helper method for class literal lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    public MemberDefinition getClassLiteralLookup(long fwhere) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        // If we have already created a lookup method, reuse it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        if (lookup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            return lookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        // If the current class is a nested class, make sure we put the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        // lookup method in the outermost class.  Set 'lookup' for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        // intervening inner classes so we won't have to do the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        // again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        if (outerClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            lookup = outerClass.getClassLiteralLookup(fwhere);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            return lookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        // If we arrive here, there was no existing 'class$' method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        ClassDefinition c = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        boolean needNewClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            // The top-level type is an interface.  Try to find an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            // inner class in which to create the helper method.  Any will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            c = findLookupContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                // The interface has no inner classes.  Create an anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                // inner class to hold the helper method, as an interface must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                // not have any methods.  The tests above for prior creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                // of a 'class$' method assure that only one such class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                // allocated for each outermost class containing a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                // literal embedded somewhere within.  Part of fix for 4055017.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                needNewClass = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                IdentifierToken sup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                    new IdentifierToken(fwhere, idJavaLangObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                IdentifierToken interfaces[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                IdentifierToken t = new IdentifierToken(fwhere, idNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                int mod = M_PUBLIC | M_ANONYMOUS | M_STATIC | M_SYNTHETIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                c = (SourceClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                    toplevelEnv.makeClassDefinition(toplevelEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                                                    fwhere, t, null, mod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                                                    sup, interfaces, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        // The name of the class-getter stub is "class$"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        Identifier idDClass = Identifier.lookup(prefixClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        Type strarg[] = { Type.tString };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        // Some sanity checks of questionable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        // This check became useless after matchMethod() was modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        // to not return synthetic methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        //try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        //    lookup = c.matchMethod(toplevelEnv, c, idDClass, strarg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        //} catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        //    throw new CompilerError("unexpected missing class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        //} catch (AmbiguousMember ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        //    throw new CompilerError("synthetic name clash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        //if (lookup != null && lookup.getClassDefinition() == c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        //    // Error if method found was not inherited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        //    throw new CompilerError("unexpected duplicate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        // Some sanity checks of questionable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        /*  // The helper function looks like this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
         *  // It simply maps a checked exception to an unchecked one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         *  static Class class$(String class$) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         *    try { return Class.forName(class$); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         *    catch (ClassNotFoundException forName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         *      throw new NoClassDefFoundError(forName.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        long w = c.getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        IdentifierToken arg = new IdentifierToken(w, idDClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        Expression e = new IdentifierExpression(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        Expression a1[] = { e };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        Identifier idForName = Identifier.lookup("forName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        e = new MethodExpression(w, new TypeExpression(w, Type.tClassDesc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                                 idForName, a1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        Statement body = new ReturnStatement(w, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        // map the exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        Identifier idClassNotFound =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            Identifier.lookup("java.lang.ClassNotFoundException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        Identifier idNoClassDefFound =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            Identifier.lookup("java.lang.NoClassDefFoundError");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        Type ctyp = Type.tClass(idClassNotFound);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        Type exptyp = Type.tClass(idNoClassDefFound);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        Identifier idGetMessage = Identifier.lookup("getMessage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        e = new IdentifierExpression(w, idForName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        e = new MethodExpression(w, e, idGetMessage, new Expression[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        Expression a2[] = { e };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        e = new NewInstanceExpression(w, new TypeExpression(w, exptyp), a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        Statement handler = new CatchStatement(w, new TypeExpression(w, ctyp),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                                               new IdentifierToken(idForName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                                               new ThrowStatement(w, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        Statement handlers[] = { handler };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        body = new TryStatement(w, body, handlers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        Type mtype = Type.tMethod(Type.tClassDesc, strarg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        IdentifierToken args[] = { arg };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        // Use default (package) access.  If private, an access method would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        // be needed in the event that the class literal belonged to an interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        // Also, making it private tickles bug 4098316.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        lookup = toplevelEnv.makeMemberDefinition(toplevelEnv, w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                                                  c, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                                                  M_STATIC | M_SYNTHETIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                                                  mtype, idDClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                                                  args, null, body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        // If a new class was created to contain the helper method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        // check it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        if (needNewClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            if (c.getClassDeclaration().getStatus() == CS_CHECKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                throw new CompilerError("duplicate check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            c.getClassDeclaration().setDefinition(c, CS_PARSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            Expression argsX[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            Type argTypesX[] = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                ClassDefinition sup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    toplevelEnv.getClassDefinition(idJavaLangObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                c.checkLocalClass(toplevelEnv, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                                  new Vset(), sup, argsX, argTypesX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            } catch (ClassNotFound ee) {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        return lookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * A list of active ongoing compilations. This list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * is used to stop two compilations from saving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2042
    private static Vector<Object> active = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * Compile this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    public void compile(OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                throws InterruptedException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        Environment env = toplevelEnv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        synchronized (active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            while (active.contains(getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                active.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            active.addElement(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            compileClass(env, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            throw new CompilerError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            synchronized (active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                active.removeElement(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                active.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * Verify that the modifier bits included in 'required' are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * all present in 'mods', otherwise signal an internal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * Note that errors in the source program may corrupt the modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * thus we rely on the fact that 'CompilerError' exceptions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * silently ignored after an error message has been issued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    private static void assertModifiers(int mods, int required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        if ((mods & required) != required) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            throw new CompilerError("illegal class modifiers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    protected void compileClass(Environment env, OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                throws IOException, ClassNotFound {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2084
        Vector<CompilerMember> variables = new Vector<>();
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2085
        Vector<CompilerMember> methods = new Vector<>();
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2086
        Vector<ClassDefinition> innerClasses = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        CompilerMember init = new CompilerMember(new MemberDefinition(getWhere(), this, M_STATIC, Type.tMethod(Type.tVoid), idClassInit, null, null), new Assembler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        Context ctx = new Context((Context)null, init.field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        for (ClassDefinition def = this; def.isInnerClass(); def = def.getOuterClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            innerClasses.addElement(def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        // Reverse the order, so that outer levels come first:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        int ncsize = innerClasses.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        for (int i = ncsize; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            innerClasses.addElement(innerClasses.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        for (int i = ncsize; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            innerClasses.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        // System.out.println("compile class " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        boolean haveDeprecated = this.isDeprecated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        boolean haveSynthetic = this.isSynthetic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        boolean haveConstantValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        boolean haveExceptions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        // Generate code for all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        for (SourceMember field = (SourceMember)getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
             field != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
             field = (SourceMember)field.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            //System.out.println("compile field " + field.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            haveDeprecated |= field.isDeprecated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            haveSynthetic |= field.isSynthetic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                if (field.isMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                    haveExceptions |=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                        (field.getExceptions(env).length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    if (field.isInitializer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                        if (field.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                            field.code(env, init.asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                        CompilerMember f =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                            new CompilerMember(field, new Assembler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                        field.code(env, f.asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                        methods.addElement(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                } else if (field.isInnerClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                    innerClasses.addElement(field.getInnerClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                } else if (field.isVariable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                    field.inline(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                    CompilerMember f = new CompilerMember(field, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                    variables.addElement(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    if (field.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                        field.codeInit(env, ctx, init.asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    haveConstantValue |=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                        (field.getInitialValue() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            } catch (CompilerError ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                env.error(field, 0, "generic",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                          field.getClassDeclaration() + ":" + field +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                          "@" + ee.toString(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        if (!init.asm.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
           init.asm.add(getWhere(), opc_return, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            methods.addElement(init);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        // bail out if there were any errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        if (getNestError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        int nClassAttrs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        // Insert constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        if (methods.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            tab.put("Code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        if (haveConstantValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            tab.put("ConstantValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        String sourceFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        if (env.debug_source()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            sourceFile = ((ClassFile)getSource()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            tab.put("SourceFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            tab.put(sourceFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            nClassAttrs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        if (haveExceptions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            tab.put("Exceptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        if (env.debug_lines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            tab.put("LineNumberTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        if (haveDeprecated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            tab.put("Deprecated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            if (this.isDeprecated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                nClassAttrs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        if (haveSynthetic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            tab.put("Synthetic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            if (this.isSynthetic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                nClassAttrs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        if (env.coverage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            nClassAttrs += 2;           // AbsoluteSourcePath, TimeStamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            tab.put("AbsoluteSourcePath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            tab.put("TimeStamp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            tab.put("CoverageTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        if (env.debug_vars()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            tab.put("LocalVariableTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        if (innerClasses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            tab.put("InnerClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            nClassAttrs += 1;           // InnerClasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        String absoluteSourcePath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        long timeStamp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        if (env.coverage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                absoluteSourcePath = getAbsoluteName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                timeStamp = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                tab.put(absoluteSourcePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        tab.put(getClassDeclaration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        if (getSuperClass() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            tab.put(getSuperClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            tab.put(interfaces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        // Sort the methods in order to make sure both constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        // entries and methods are in a deterministic order from run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        // to run (this allows comparing class files for a fixed point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        // to validate the compiler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        CompilerMember[] ordered_methods =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            new CompilerMember[methods.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        methods.copyInto(ordered_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        java.util.Arrays.sort(ordered_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        for (int i=0; i<methods.size(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            methods.setElementAt(ordered_methods[i], i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        // Optimize Code and Collect method constants
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2245
        for (Enumeration<CompilerMember> e = methods.elements() ; e.hasMoreElements() ; ) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2246
            CompilerMember f = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                f.asm.optimize(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                f.asm.collect(env, f.field, tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                tab.put(f.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                tab.put(f.sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                ClassDeclaration exp[] = f.field.getExceptions(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                for (int i = 0 ; i < exp.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                    tab.put(exp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                env.error(f.field, -1, "generic", f.field.getName() + "@" + ee.toString(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                f.asm.listing(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        // Collect field constants
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2264
        for (Enumeration<CompilerMember> e = variables.elements() ; e.hasMoreElements() ; ) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2265
            CompilerMember f = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            tab.put(f.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            tab.put(f.sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            Object val = f.field.getInitialValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                tab.put((val instanceof String) ? new StringExpression(f.field.getWhere(), (String)val) : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        // Collect inner class constants
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2276
        for (Enumeration<ClassDefinition> e = innerClasses.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
             e.hasMoreElements() ; ) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2278
            ClassDefinition inner = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            tab.put(inner.getClassDeclaration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            // If the inner class is local, we do not need to add its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            // outer class here -- the outer_class_info_index is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            if (!inner.isLocal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                ClassDefinition outer = inner.getOuterClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                tab.put(outer.getClassDeclaration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            // If the local name of the class is idNull, don't bother to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            // add it to the constant pool.  We won't need it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            Identifier inner_local_name = inner.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            if (inner_local_name != idNull) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                tab.put(inner_local_name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        // Write header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        DataOutputStream data = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        data.writeInt(JAVA_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        data.writeShort(toplevelEnv.getMinorVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        data.writeShort(toplevelEnv.getMajorVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        tab.write(env, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        // Write class information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        int cmods = getModifiers() & MM_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        // Certain modifiers are implied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        // 1.  Any interface (nested or not) is implicitly deemed to be abstract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        //     whether it is explicitly marked so or not.  (Java 1.0.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        // 2.  A interface which is a member of a type is implicitly deemed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
        //     be static, whether it is explicitly marked so or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        // 3a. A type which is a member of an interface is implicitly deemed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        //     to be public, whether it is explicitly marked so or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        // 3b. A type which is a member of an interface is implicitly deemed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        //     to be static, whether it is explicitly marked so or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        // All of these rules are implemented in 'BatchParser.beginClass',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        // but the results are verified here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            // Rule 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            // The VM spec states that ACC_ABSTRACT must be set when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            // ACC_INTERFACE is; this was not done by javac prior to 1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
            // and the runtime compensates by setting it.  Making sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            // it is set here will allow the runtime hack to eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            // be removed. Rule 2 doesn't apply to transformed modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            assertModifiers(cmods, ACC_ABSTRACT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            // Contrary to the JVM spec, we only set ACC_SUPER for classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            // not interfaces.  This is a workaround for a bug in IE3.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            // which refuses interfaces with ACC_SUPER on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            cmods |= ACC_SUPER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        // If this is a nested class, transform access modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        if (outerClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            // If private, transform to default (package) access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            // If protected, transform to public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            // M_PRIVATE and M_PROTECTED are already masked off by MM_CLASS above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            // cmods &= ~(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            if (isProtected()) cmods |= M_PUBLIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            // Rule 3a.  Note that Rule 3b doesn't apply to transformed modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            if (outerClass.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                assertModifiers(cmods, M_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        data.writeShort(cmods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        if (env.dumpModifiers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            Identifier cn = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            Identifier nm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                Identifier.lookup(cn.getQualifier(), cn.getFlatName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            System.out.println("CLASSFILE  " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            System.out.println("---" + classModifierString(cmods));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        data.writeShort(tab.index(getClassDeclaration()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        data.writeShort((getSuperClass() != null) ? tab.index(getSuperClass()) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        data.writeShort(interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            data.writeShort(tab.index(interfaces[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        // write variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        ByteArrayOutputStream buf = new ByteArrayOutputStream(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        ByteArrayOutputStream attbuf = new ByteArrayOutputStream(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        DataOutputStream databuf = new DataOutputStream(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        data.writeShort(variables.size());
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2370
        for (Enumeration<CompilerMember> e = variables.elements() ; e.hasMoreElements() ; ) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2371
            CompilerMember f = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            Object val = f.field.getInitialValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            data.writeShort(f.field.getModifiers() & MM_FIELD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            data.writeShort(tab.index(f.name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            data.writeShort(tab.index(f.sig));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            int fieldAtts = (val != null ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            boolean dep = f.field.isDeprecated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            boolean syn = f.field.isSynthetic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            fieldAtts += (dep ? 1 : 0) + (syn ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            data.writeShort(fieldAtts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                data.writeShort(tab.index("ConstantValue"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                data.writeInt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                data.writeShort(tab.index((val instanceof String) ? new StringExpression(f.field.getWhere(), (String)val) : val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            if (dep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                data.writeShort(tab.index("Deprecated"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            if (syn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                data.writeShort(tab.index("Synthetic"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        // write methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        data.writeShort(methods.size());
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2402
        for (Enumeration<CompilerMember> e = methods.elements() ; e.hasMoreElements() ; ) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2403
            CompilerMember f = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            int xmods = f.field.getModifiers() & MM_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            // Transform floating point modifiers.  M_STRICTFP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            // of member + status of enclosing class turn into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            // ACC_STRICT bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            if (((xmods & M_STRICTFP)!=0) || ((cmods & M_STRICTFP)!=0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                xmods |= ACC_STRICT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                // Use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                if (env.strictdefault()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                    xmods |= ACC_STRICT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            data.writeShort(xmods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            data.writeShort(tab.index(f.name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            data.writeShort(tab.index(f.sig));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            ClassDeclaration exp[] = f.field.getExceptions(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            int methodAtts = ((exp.length > 0) ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            boolean dep = f.field.isDeprecated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            boolean syn = f.field.isSynthetic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            methodAtts += (dep ? 1 : 0) + (syn ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            if (!f.asm.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                data.writeShort(methodAtts+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                f.asm.write(env, databuf, f.field, tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                int natts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                if (env.debug_lines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                    natts++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                if (env.coverage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                    natts++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                if (env.debug_vars()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                    natts++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                databuf.writeShort(natts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                if (env.debug_lines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                    f.asm.writeLineNumberTable(env, new DataOutputStream(attbuf), tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                    databuf.writeShort(tab.index("LineNumberTable"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                    databuf.writeInt(attbuf.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                    attbuf.writeTo(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                    attbuf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
//JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                if (env.coverage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                    f.asm.writeCoverageTable(env, (ClassDefinition)this, new DataOutputStream(attbuf), tab, f.field.getWhere());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                    databuf.writeShort(tab.index("CoverageTable"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                    databuf.writeInt(attbuf.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                    attbuf.writeTo(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                    attbuf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                if (env.debug_vars()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                    f.asm.writeLocalVariableTable(env, f.field, new DataOutputStream(attbuf), tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                    databuf.writeShort(tab.index("LocalVariableTable"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                    databuf.writeInt(attbuf.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                    attbuf.writeTo(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                    attbuf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                data.writeShort(tab.index("Code"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                data.writeInt(buf.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                buf.writeTo(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                buf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
//JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                if ((env.coverage()) && ((f.field.getModifiers() & M_NATIVE) > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    f.asm.addNativeToJcovTab(env, (ClassDefinition)this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                data.writeShort(methodAtts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            if (exp.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                data.writeShort(tab.index("Exceptions"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                data.writeInt(2 + exp.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                data.writeShort(exp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                for (int i = 0 ; i < exp.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                    data.writeShort(tab.index(exp[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            if (dep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                data.writeShort(tab.index("Deprecated"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            if (syn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                data.writeShort(tab.index("Synthetic"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        // class attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
        data.writeShort(nClassAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        if (env.debug_source()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            data.writeShort(tab.index("SourceFile"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
            data.writeInt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            data.writeShort(tab.index(sourceFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        if (this.isDeprecated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            data.writeShort(tab.index("Deprecated"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        if (this.isSynthetic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            data.writeShort(tab.index("Synthetic"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            data.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        if (env.coverage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
            data.writeShort(tab.index("AbsoluteSourcePath"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            data.writeInt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            data.writeShort(tab.index(absoluteSourcePath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
            data.writeShort(tab.index("TimeStamp"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            data.writeInt(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
            data.writeLong(timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        if (innerClasses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            data.writeShort(tab.index("InnerClasses"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            data.writeInt(2 + 2*4*innerClasses.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            data.writeShort(innerClasses.size());
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2532
            for (Enumeration<ClassDefinition> e = innerClasses.elements() ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                 e.hasMoreElements() ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                // For each inner class name transformation, we have a record
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                // with the following fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                //    u2 inner_class_info_index;   // CONSTANT_Class_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                //    u2 outer_class_info_index;   // CONSTANT_Class_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                //    u2 inner_name_index;         // CONSTANT_Utf8_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                //    u2 inner_class_access_flags; // access_flags bitmask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                // The spec states that outer_class_info_index is 0 iff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                // the inner class is not a member of its enclosing class (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                // it is a local or anonymous class).  The spec also states
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                // that if a class is anonymous then inner_name_index should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                // be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                // See also the initInnerClasses() method in BinaryClass.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                // Generate inner_class_info_index.
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2551
                ClassDefinition inner = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                data.writeShort(tab.index(inner.getClassDeclaration()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                // Generate outer_class_info_index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                // Checking isLocal() should probably be enough here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                // but the check for isAnonymous is added for good
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                // measure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                if (inner.isLocal() || inner.isAnonymous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                    data.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                    // Query: what about if inner.isInsideLocal()?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                    // For now we continue to generate a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                    // outer_class_info_index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                    ClassDefinition outer = inner.getOuterClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                    data.writeShort(tab.index(outer.getClassDeclaration()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                // Generate inner_name_index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                Identifier inner_name = inner.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                if (inner_name == idNull) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                    if (!inner.isAnonymous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                        throw new CompilerError("compileClass(), anonymous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                    data.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                    data.writeShort(tab.index(inner_name.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                // Generate inner_class_access_flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                int imods = inner.getInnerClassMember().getModifiers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                            & ACCM_INNERCLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                // Certain modifiers are implied for nested types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                // See rules 1, 2, 3a, and 3b enumerated above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                // All of these rules are implemented in 'BatchParser.beginClass',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                // but are verified here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                if (inner.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                    // Rules 1 and 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                    assertModifiers(imods, M_ABSTRACT | M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                if (inner.getOuterClass().isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                    // Rules 3a and 3b.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    imods &= ~(M_PRIVATE | M_PROTECTED); // error recovery
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    assertModifiers(imods, M_PUBLIC | M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                data.writeShort(imods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                if (env.dumpModifiers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                    Identifier fn = inner.getInnerClassMember().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                    Identifier nm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                        Identifier.lookup(fn.getQualifier(), fn.getFlatName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                    System.out.println("INNERCLASS " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                    System.out.println("---" + classModifierString(imods));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        // Cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        data.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        tab = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
// JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        // generate coverage data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        if (env.covdata()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            Assembler CovAsm = new Assembler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            CovAsm.GenVecJCov(env, (ClassDefinition)this, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
// end JCOV
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * Print out the dependencies for this class (-xdepend) option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
    public void printClassDependencies(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        // Only do this if the -xdepend flag is on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        if ( toplevelEnv.print_dependencies() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            // Name of java source file this class was in (full path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            //    e.g. /home/ohair/Test.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            String src = ((ClassFile)getSource()).getAbsoluteName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            // Class name, fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            //   e.g. "java.lang.Object" or "FooBar" or "sun.tools.javac.Main"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            // Inner class names must be mangled, as ordinary '.' qualification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            // is used internally where the spec requires '$' separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            //   String className = getName().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            String className = Type.mangleInnerType(getName()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            // Line number where class starts in the src file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
            long startLine = getWhere() >> WHEREOFFSETBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            // Line number where class ends in the src file (not used yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            long endLine = getEndPosition() >> WHEREOFFSETBITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            // First line looks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            //    CLASS:src,startLine,endLine,className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
            System.out.println( "CLASS:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                    + src               + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                    + startLine         + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                    + endLine   + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                    + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            // For each class this class is dependent on:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            //    CLDEP:className1,className2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            //  where className1 is the name of the class we are in, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            //        classname2 is the name of the class className1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            //          is dependent on.
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2664
            for(Enumeration<ClassDeclaration> e = deps.elements();  e.hasMoreElements(); ) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 22584
diff changeset
  2665
                ClassDeclaration data = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                // Mangle name of class dependend on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                String depName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                    Type.mangleInnerType(data.getName()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                env.output("CLDEP:" + className + "," + depName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
}