src/share/classes/sun/rmi/rmic/iiop/ContextStack.java

Thu, 31 Aug 2017 18:10:36 +0800

author
aoqi
date
Thu, 31 Aug 2017 18:10:36 +0800
changeset 748
6845b95cba6b
parent 158
91006f157c46
parent 0
7ef37b2cdcad
permissions
-rw-r--r--

merge

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

mercurial