corba/src/share/classes/sun/rmi/rmic/iiop/ContextStack.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
package sun.rmi.rmic.iiop;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import sun.tools.java.CompilerError;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
 * ContextStack provides a mechanism to record parsing state.
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
 * @author      Bryan Atsatt
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
public class ContextStack {
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    // Context codes.
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    public static final int TOP = 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    public static final int METHOD = 2;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
    public static final int METHOD_RETURN = 3;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
    public static final int METHOD_ARGUMENT = 4;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
    public static final int METHOD_EXCEPTION = 5;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
    public static final int MEMBER = 6;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    public static final int MEMBER_CONSTANT = 7;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
    public static final int MEMBER_STATIC = 8;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    public static final int MEMBER_TRANSIENT = 9;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
    public static final int IMPLEMENTS = 10;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    public static final int EXTENDS = 11;
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
    // String versions of context codes.
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    private static final String[] CODE_NAMES = {
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        "UNKNOWN ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        "Top level type ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
        "Method ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
        "Return parameter ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
        "Parameter ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
        "Exception ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
        "Member ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
        "Constant member ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
        "Static member ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        "Transient member ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        "Implements ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        "Extends ",
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    };
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
    // Member data.
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    private int currentIndex = -1;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    private int maxIndex = 100;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
    private TypeContext[] stack = new TypeContext[maxIndex];
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
    private int newCode = TOP;
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
    private BatchEnvironment env = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
    private boolean trace = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
    private TypeContext tempContext = new TypeContext();
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    private static final String TRACE_INDENT = "   ";
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
     * Constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
    public ContextStack (BatchEnvironment env) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        this.env = env;
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        env.contextStack = this;
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
     * Return true if env.nerrors > 0.
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    public boolean anyErrors () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
        return env.nerrors > 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
     * Enable/disable tracing.
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
    public void setTrace(boolean trace) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        this.trace = trace;
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
     * Check trace flag.
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
    public boolean isTraceOn() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
        return trace;
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
     * Get the environment.
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
    public BatchEnvironment getEnv() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
        return env;
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
     * Set the new context.
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
    public void setNewContextCode(int code) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
        newCode = code;
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
     * Get the current context code.
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
    public int getCurrentContextCode() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
        return newCode;
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
     * If tracing on, write the current call stack (not the context stack) to
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
     * System.out.
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
    final void traceCallStack () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
        if (trace) dumpCallStack();
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
    public final static void dumpCallStack() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
        new Error().printStackTrace(System.out);
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
     * Print a line indented by stack depth.
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    final private void tracePrint (String text, boolean line) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
        int length = text.length() + (currentIndex * TRACE_INDENT.length());
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
        StringBuffer buffer = new StringBuffer(length);
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
        for (int i = 0; i < currentIndex; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
            buffer.append(TRACE_INDENT);
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        buffer.append(text);
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
        if (line) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
            buffer.append("\n");
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
        System.out.print(buffer.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
     * If tracing on, print a line.
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
    final void trace (String text) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
        if (trace) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
            tracePrint(text,false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
     * If tracing on, print a line followed by a '\n'.
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
    final void traceln (String text) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        if (trace) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
            tracePrint(text,true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
     * If tracing on, print a pre-mapped ContextElement.
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
    final void traceExistingType (Type type) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        if (trace) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
            tempContext.set(newCode,type);
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
            traceln(toResultString(tempContext,true,true));
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
     * Push a new element on the stack.
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
     * @return the new element.
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
    public TypeContext push (ContextElement element) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
        currentIndex++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
        // Grow array if need to...
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        if (currentIndex == maxIndex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
            int newMax = maxIndex * 2;
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
            TypeContext[] newStack = new TypeContext[newMax];
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
            System.arraycopy(stack,0,newStack,0,maxIndex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
            maxIndex = newMax;
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
            stack = newStack;
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
        // Make sure we have a context object to use at this position...
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
        TypeContext it = stack[currentIndex];
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        if (it == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
            it = new TypeContext();
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
            stack[currentIndex] = it;
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
        // Set the context object...
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        it.set(newCode,element);
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
        // Trace...
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
        traceln(toTrialString(it));
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
        // Return...
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
        return it;
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
     * Pop an element from the stack.
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
     * @return the new current element or null if top.
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
    public TypeContext pop (boolean wasValid) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
        if (currentIndex < 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
            throw new CompilerError("Nothing on stack!");
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
        newCode = stack[currentIndex].getCode();
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
        traceln(toResultString(stack[currentIndex],wasValid,false));
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        Type last = stack[currentIndex].getCandidateType();
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
        if (last != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
            // Set status...
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
            if (wasValid) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
                last.setStatus(Constants.STATUS_VALID);
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
                last.setStatus(Constants.STATUS_INVALID);
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
        currentIndex--;
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        if (currentIndex < 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
            // Done parsing, so update the invalid types
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
            // if this type was valid...
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
            if (wasValid) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
                Type.updateAllInvalidTypes(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
            return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
            return stack[currentIndex];
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
     * Get the current size.
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
    public int size () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
        return currentIndex + 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
     * Get a specific context.
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
    public TypeContext getContext (int index) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
        if (currentIndex < index) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
            throw new Error("Index out of range");
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
        return stack[index];
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
     * Get the current top context.
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
    public TypeContext getContext () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
        if (currentIndex < 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
            throw new Error("Nothing on stack!");
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
        return stack[currentIndex];
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
     * Is parent context a value type?
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
    public boolean isParentAValue () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
        if (currentIndex > 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
            return stack[currentIndex - 1].isValue();
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
            return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
     * Get parent context. Null if none.
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
    public TypeContext getParentContext () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        if (currentIndex > 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
            return stack[currentIndex - 1];
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
            return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
     * Get a string for the context name...
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
    public String getContextCodeString () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
        if (currentIndex >= 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
            return CODE_NAMES[newCode];
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
            return CODE_NAMES[0];
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
     * Get a string for the given context code...
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
    public static String getContextCodeString (int contextCode) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
        return CODE_NAMES[contextCode];
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
    private String toTrialString(TypeContext it) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
        int code = it.getCode();
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
        if (code != METHOD && code != MEMBER) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
            return it.toString() + " (trying " + it.getTypeDescription() + ")";
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
            return it.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
    private String toResultString (TypeContext it, boolean result, boolean preExisting) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
        int code = it.getCode();
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
        if (code != METHOD && code != MEMBER) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
            if (result) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
                String str = it.toString() + " --> " + it.getTypeDescription();
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
                if (preExisting) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
                    return str + " [Previously mapped]";
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
                    return str;
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
            if (result) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
                return it.toString() + " --> [Mapped]";
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
        return it.toString() + " [Did not map]";
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
    public void clear () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
        for (int i = 0; i < stack.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
            if (stack[i] != null) stack[i].destroy();
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
class TypeContext {
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
    public void set(int code, ContextElement element) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
        this.code = code;
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
        this.element = element;
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
        if (element instanceof ValueType) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
            isValue = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
            isValue = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
    public int getCode() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
        return code;
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
    public String getName() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
        return element.getElementName();
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
    public Type getCandidateType() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
        if (element instanceof Type) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
            return (Type) element;
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
            return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
public String getTypeDescription() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
    if (element instanceof Type) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
        return ((Type) element).getTypeDescription();
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
    } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
        return "[unknown type]";
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
public String toString () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
    if (element != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
        return ContextStack.getContextCodeString(code) + element.getElementName();
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
    } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
        return ContextStack.getContextCodeString(code) + "null";
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
public boolean isValue () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
    return isValue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
    public boolean isConstant () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
        return code == ContextStack.MEMBER_CONSTANT;
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
02bb8761fcce Initial load
duke
parents:
diff changeset
   437
    public void destroy() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   438
        if (element instanceof Type) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   439
            ((Type)element).destroy();
02bb8761fcce Initial load
duke
parents:
diff changeset
   440
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   441
        element = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   442
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   443
02bb8761fcce Initial load
duke
parents:
diff changeset
   444
    private int code = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   445
    private ContextElement element = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   446
    private boolean isValue = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   447
}