jdk/src/share/classes/sun/tools/tree/LocalMember.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 5506 202f599c92aa
child 25799 1afc4675dc75
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2003, 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.tree;
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 java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A local Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class LocalMember extends MemberDefinition {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The number of the variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int number = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Some statistics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    int readcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int writecount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * An indication of which block the variable comes from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Helps identify uplevel references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    int scopeNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Return current nesting level, i.e., the value of 'scopeNumber'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Made public for the benefit of 'ClassDefinition.resolveName'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public int getScopeNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return scopeNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Used by copyInline to record the original of this copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    LocalMember originalOfCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The previous local variable, this list is used to build a nested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * context of local variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    LocalMember prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public LocalMember(long where, ClassDefinition clazz, int modifiers, Type type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                      Identifier name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        super(where, clazz, modifiers, type, name, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Constructor for a block-inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public LocalMember(ClassDefinition innerClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        super(innerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // The class's "real" name is something like "foo$1$bar", but locally:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        name = innerClass.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Constructor for a proxy to an instance or class variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    LocalMember(MemberDefinition field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this(0, null, 0, field.getType(), idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // use this random slot to store the info:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        accessPeer = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Is this a proxy for the given field?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    final MemberDefinition getMember() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return (name == idClass) ? accessPeer : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Special checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public boolean isLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Make a copy of this field, which is an argument to a method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * or constructor.  Arrange so that when occurrences of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * are encountered in an immediately following copyInline() operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the expression nodes will replace the original argument by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * fresh copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public LocalMember copyInline(Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        LocalMember copy = new LocalMember(where, clazz, modifiers, type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        copy.readcount = this.readcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        copy.writecount = this.writecount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        copy.originalOfCopy = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Make a temporary link from the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // It only stays valid through the next call to copyInline().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // (This means that recursive inlining won't work.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // To stay honest, we mark these inline copies:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        copy.addModifiers(M_LOCAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (this.accessPeer != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            && (this.accessPeer.getModifiers() & M_LOCAL) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new CompilerError("local copyInline");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.accessPeer = copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the previous result of copyInline(ctx).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Must be called in the course of an Expression.copyInline()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * operation that immediately follows the LocalMember.copyInline().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Return "this" if there is no such copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public LocalMember getCurrentInlineCopy(Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        MemberDefinition accessPeer = this.accessPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (accessPeer != null && (accessPeer.getModifiers() & M_LOCAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            LocalMember copy = (LocalMember)accessPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * May inline copies of all the arguments of the given method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static public LocalMember[] copyArguments(Context ctx, MemberDefinition field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Vector v = field.getArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        LocalMember res[] = new LocalMember[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        v.copyInto(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        for (int i = 0; i < res.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            res[i] = res[i].copyInline(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Call this when finished with the result of a copyArguments() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static public void doneWithArguments(Context ctx, LocalMember res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (int i = 0; i < res.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (res[i].originalOfCopy.accessPeer == res[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                res[i].originalOfCopy.accessPeer = null;
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
     * Is this local variable's value stable and simple enough to be directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * substituted for occurrences of the variable itself?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * (This decision is made by VarDeclarationStatement.inline().)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public boolean isInlineable(Environment env, boolean fromFinal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (getModifiers() & M_INLINEABLE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Check if used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public boolean isUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return (readcount != 0) || (writecount != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // Used by class Context, only on members of MemberDefinition.available:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    LocalMember getAccessVar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return (LocalMember)accessPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    void setAccessVar(LocalMember f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        accessPeer = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // Used by class Context, only on "AccessVar" constructor args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    MemberDefinition getAccessVarMember() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return accessPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void setAccessVarMember(MemberDefinition f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        accessPeer = f;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public Node getValue(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return (Expression)getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Value number for vsets, or -1 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public int getNumber(Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}