001: /*
002: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.code;
027:
028: import java.util.*;
029:
030: import com.sun.tools.javac.util.*;
031: import com.sun.tools.javac.util.List;
032: import com.sun.tools.javac.code.Symbol.*;
033: import com.sun.tools.javac.code.Type.*;
034: import com.sun.tools.javac.jvm.*;
035:
036: import static com.sun.tools.javac.jvm.ByteCodes.*;
037: import static com.sun.tools.javac.code.Flags.*;
038:
039: /** A class that defines all predefined constants and operators
040: * as well as special classes such as java.lang.Object, which need
041: * to be known to the compiler. All symbols are held in instance
042: * fields. This makes it possible to work in multiple concurrent
043: * projects, which might use different class files for library classes.
044: *
045: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
046: * you write code that depends on this, you do so at your own risk.
047: * This code and its internal interfaces are subject to change or
048: * deletion without notice.</b>
049: */
050: @Version("@(#)Symtab.java 1.72 07/05/05")
051: public class Symtab {
052: /** The context key for the symbol table. */
053: protected static final Context.Key<Symtab> symtabKey = new Context.Key<Symtab>();
054:
055: /** Get the symbol table instance. */
056: public static Symtab instance(Context context) {
057: Symtab instance = context.get(symtabKey);
058: if (instance == null)
059: instance = new Symtab(context);
060: return instance;
061: }
062:
063: /** Builtin types.
064: */
065: public final Type byteType = new Type(TypeTags.BYTE, null);
066: public final Type charType = new Type(TypeTags.CHAR, null);
067: public final Type shortType = new Type(TypeTags.SHORT, null);
068: public final Type intType = new Type(TypeTags.INT, null);
069: public final Type longType = new Type(TypeTags.LONG, null);
070: public final Type floatType = new Type(TypeTags.FLOAT, null);
071: public final Type doubleType = new Type(TypeTags.DOUBLE, null);
072: public final Type booleanType = new Type(TypeTags.BOOLEAN, null);
073: public final Type botType = new BottomType();
074: public final JCNoType voidType = new JCNoType(TypeTags.VOID);
075:
076: private final Name.Table names;
077: private final ClassReader reader;
078:
079: /** A symbol for the root package.
080: */
081: public final PackageSymbol rootPackage;
082:
083: /** A symbol for the unnamed package.
084: */
085: public final PackageSymbol unnamedPackage;
086:
087: /** A symbol that stands for a missing symbol.
088: */
089: public final TypeSymbol noSymbol;
090:
091: /** The error symbol.
092: */
093: public final ClassSymbol errSymbol;
094:
095: /** An instance of the error type.
096: */
097: public final Type errType;
098:
099: /** A value for the unknown type. */
100: public final Type unknownType;
101:
102: /** The builtin type of all arrays. */
103: public final ClassSymbol arrayClass;
104: public final MethodSymbol arrayCloneMethod;
105:
106: /** VGJ: The (singleton) type of all bound types. */
107: public final ClassSymbol boundClass;
108:
109: /** The builtin type of all methods. */
110: public final ClassSymbol methodClass;
111:
112: /** Predefined types.
113: */
114: public final Type objectType;
115: public final Type classType;
116: public final Type classLoaderType;
117: public final Type stringType;
118: public final Type stringBufferType;
119: public final Type stringBuilderType;
120: public final Type cloneableType;
121: public final Type serializableType;
122: public final Type throwableType;
123: public final Type errorType;
124: public final Type illegalArgumentExceptionType;
125: public final Type exceptionType;
126: public final Type runtimeExceptionType;
127: public final Type classNotFoundExceptionType;
128: public final Type noClassDefFoundErrorType;
129: public final Type noSuchFieldErrorType;
130: public final Type assertionErrorType;
131: public final Type cloneNotSupportedExceptionType;
132: public final Type annotationType;
133: public final TypeSymbol enumSym;
134: public final Type listType;
135: public final Type collectionsType;
136: public final Type comparableType;
137: public final Type arraysType;
138: public final Type iterableType;
139: public final Type iteratorType;
140: public final Type annotationTargetType;
141: public final Type overrideType;
142: public final Type retentionType;
143: public final Type deprecatedType;
144: public final Type suppressWarningsType;
145: public final Type inheritedType;
146: public final Type proprietaryType;
147:
148: /** The symbol representing the length field of an array.
149: */
150: public final VarSymbol lengthVar;
151:
152: /** The null check operator. */
153: public final OperatorSymbol nullcheck;
154:
155: /** The symbol representing the final finalize method on enums */
156: public final MethodSymbol enumFinalFinalize;
157:
158: /** The predefined type that belongs to a tag.
159: */
160: public final Type[] typeOfTag = new Type[TypeTags.TypeTagCount];
161:
162: /** The name of the class that belongs to a basix type tag.
163: */
164: public final Name[] boxedName = new Name[TypeTags.TypeTagCount];
165:
166: /** A hashtable containing the encountered top-level and member classes,
167: * indexed by flat names. The table does not contain local classes.
168: * It should be updated from the outside to reflect classes defined
169: * by compiled source files.
170: */
171: public final Map<Name, ClassSymbol> classes = new HashMap<Name, ClassSymbol>();
172:
173: /** A hashtable containing the encountered packages.
174: * the table should be updated from outside to reflect packages defined
175: * by compiled source files.
176: */
177: public final Map<Name, PackageSymbol> packages = new HashMap<Name, PackageSymbol>();
178:
179: public void initType(Type type, ClassSymbol c) {
180: type.tsym = c;
181: typeOfTag[type.tag] = type;
182: }
183:
184: public void initType(Type type, String name) {
185: initType(type, new ClassSymbol(PUBLIC, names.fromString(name),
186: type, rootPackage));
187: }
188:
189: public void initType(Type type, String name, String bname) {
190: initType(type, name);
191: boxedName[type.tag] = names.fromString("java.lang." + bname);
192: }
193:
194: /** The class symbol that owns all predefined symbols.
195: */
196: public final ClassSymbol predefClass;
197:
198: /** Enter a constant into symbol table.
199: * @param name The constant's name.
200: * @param type The constant's type.
201: */
202: private VarSymbol enterConstant(String name, Type type) {
203: VarSymbol c = new VarSymbol(PUBLIC | STATIC | FINAL, names
204: .fromString(name), type, predefClass);
205: c.setData(type.constValue());
206: predefClass.members().enter(c);
207: return c;
208: }
209:
210: /** Enter a binary operation into symbol table.
211: * @param name The name of the operator.
212: * @param left The type of the left operand.
213: * @param right The type of the left operand.
214: * @param res The operation's result type.
215: * @param opcode The operation's bytecode instruction.
216: */
217: private void enterBinop(String name, Type left, Type right,
218: Type res, int opcode) {
219: predefClass.members().enter(
220: new OperatorSymbol(names.fromString(name),
221: new MethodType(List.of(left, right), res, List
222: .<Type> nil(), methodClass), opcode,
223: predefClass));
224: }
225:
226: /** Enter a binary operation, as above but with two opcodes,
227: * which get encoded as (opcode1 << ByteCodeTags.preShift) + opcode2.
228: * @param opcode1 First opcode.
229: * @param opcode2 Second opcode.
230: */
231: private void enterBinop(String name, Type left, Type right,
232: Type res, int opcode1, int opcode2) {
233: enterBinop(name, left, right, res,
234: (opcode1 << ByteCodes.preShift) | opcode2);
235: }
236:
237: /** Enter a unary operation into symbol table.
238: * @param name The name of the operator.
239: * @param arg The type of the operand.
240: * @param res The operation's result type.
241: * @param opcode The operation's bytecode instruction.
242: */
243: private OperatorSymbol enterUnop(String name, Type arg, Type res,
244: int opcode) {
245: OperatorSymbol sym = new OperatorSymbol(names.fromString(name),
246: new MethodType(List.of(arg), res, List.<Type> nil(),
247: methodClass), opcode, predefClass);
248: predefClass.members().enter(sym);
249: return sym;
250: }
251:
252: /** Enter a class into symbol table.
253: * @param The name of the class.
254: */
255: private Type enterClass(String s) {
256: return reader.enterClass(names.fromString(s)).type;
257: }
258:
259: /** Constructor; enters all predefined identifiers and operators
260: * into symbol table.
261: */
262: protected Symtab(Context context) throws CompletionFailure {
263: context.put(symtabKey, this );
264:
265: names = Name.Table.instance(context);
266:
267: // Create the unknown type
268: unknownType = new Type(TypeTags.UNKNOWN, null);
269:
270: // create the basic builtin symbols
271: rootPackage = new PackageSymbol(names.empty, null);
272: final Messages messages = Messages.instance(context);
273: unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
274: public String toString() {
275: return messages
276: .getLocalizedString("compiler.misc.unnamed.package");
277: }
278: };
279: noSymbol = new TypeSymbol(0, names.empty, Type.noType,
280: rootPackage);
281: noSymbol.kind = Kinds.NIL;
282:
283: // create the error symbols
284: errSymbol = new ClassSymbol(PUBLIC | STATIC | ACYCLIC,
285: names.any, null, rootPackage);
286: errType = new ErrorType(errSymbol);
287:
288: // initialize builtin types
289: initType(byteType, "byte", "Byte");
290: initType(shortType, "short", "Short");
291: initType(charType, "char", "Character");
292: initType(intType, "int", "Integer");
293: initType(longType, "long", "Long");
294: initType(floatType, "float", "Float");
295: initType(doubleType, "double", "Double");
296: initType(booleanType, "boolean", "Boolean");
297: initType(voidType, "void", "Void");
298: initType(botType, "<nulltype>");
299: initType(errType, errSymbol);
300: initType(unknownType, "<any?>");
301:
302: // the builtin class of all arrays
303: arrayClass = new ClassSymbol(PUBLIC | ACYCLIC, names.Array,
304: noSymbol);
305:
306: // VGJ
307: boundClass = new ClassSymbol(PUBLIC | ACYCLIC, names.Bound,
308: noSymbol);
309:
310: // the builtin class of all methods
311: methodClass = new ClassSymbol(PUBLIC | ACYCLIC, names.Method,
312: noSymbol);
313:
314: // Create class to hold all predefined constants and operations.
315: predefClass = new ClassSymbol(PUBLIC | ACYCLIC, names.empty,
316: rootPackage);
317: Scope scope = new Scope(predefClass);
318: predefClass.members_field = scope;
319:
320: // Enter symbols for basic types.
321: scope.enter(byteType.tsym);
322: scope.enter(shortType.tsym);
323: scope.enter(charType.tsym);
324: scope.enter(intType.tsym);
325: scope.enter(longType.tsym);
326: scope.enter(floatType.tsym);
327: scope.enter(doubleType.tsym);
328: scope.enter(booleanType.tsym);
329: scope.enter(errType.tsym);
330:
331: classes.put(predefClass.fullname, predefClass);
332:
333: reader = ClassReader.instance(context);
334: reader.init(this );
335:
336: // Enter predefined classes.
337: objectType = enterClass("java.lang.Object");
338: classType = enterClass("java.lang.Class");
339: stringType = enterClass("java.lang.String");
340: stringBufferType = enterClass("java.lang.StringBuffer");
341: stringBuilderType = enterClass("java.lang.StringBuilder");
342: cloneableType = enterClass("java.lang.Cloneable");
343: throwableType = enterClass("java.lang.Throwable");
344: serializableType = enterClass("java.io.Serializable");
345: errorType = enterClass("java.lang.Error");
346: illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
347: exceptionType = enterClass("java.lang.Exception");
348: runtimeExceptionType = enterClass("java.lang.RuntimeException");
349: classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
350: noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
351: noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
352: assertionErrorType = enterClass("java.lang.AssertionError");
353: cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
354: annotationType = enterClass("java.lang.annotation.Annotation");
355: classLoaderType = enterClass("java.lang.ClassLoader");
356: enumSym = reader.enterClass(names.java_lang_Enum);
357: enumFinalFinalize = new MethodSymbol(PROTECTED | FINAL
358: | HYPOTHETICAL, names.finalize,
359: new MethodType(List.<Type> nil(), voidType, List
360: .<Type> nil(), methodClass), enumSym);
361: listType = enterClass("java.util.List");
362: collectionsType = enterClass("java.util.Collections");
363: comparableType = enterClass("java.lang.Comparable");
364: arraysType = enterClass("java.util.Arrays");
365: iterableType = Target.instance(context).hasIterable() ? enterClass("java.lang.Iterable")
366: : enterClass("java.util.Collection");
367: iteratorType = enterClass("java.util.Iterator");
368: annotationTargetType = enterClass("java.lang.annotation.Target");
369: overrideType = enterClass("java.lang.Override");
370: retentionType = enterClass("java.lang.annotation.Retention");
371: deprecatedType = enterClass("java.lang.Deprecated");
372: suppressWarningsType = enterClass("java.lang.SuppressWarnings");
373: inheritedType = enterClass("java.lang.annotation.Inherited");
374:
375: // Enter a synthetic class that is used to mark Sun
376: // proprietary classes in ct.sym. This class does not have a
377: // class file.
378: ClassType proprietaryType = (ClassType) enterClass("sun.Proprietary+Annotation");
379: this .proprietaryType = proprietaryType;
380: ClassSymbol proprietarySymbol = (ClassSymbol) proprietaryType.tsym;
381: proprietarySymbol.completer = null;
382: proprietarySymbol.flags_field = PUBLIC | ACYCLIC | ANNOTATION
383: | INTERFACE;
384: proprietarySymbol.erasure_field = proprietaryType;
385: proprietarySymbol.members_field = new Scope(proprietarySymbol);
386: proprietaryType.typarams_field = List.nil();
387: proprietaryType.allparams_field = List.nil();
388: proprietaryType.super type_field = annotationType;
389: proprietaryType.interfaces_field = List.nil();
390:
391: // Enter a class for arrays.
392: // The class implements java.lang.Cloneable and java.io.Serializable.
393: // It has a final length field and a clone method.
394: ClassType arrayClassType = (ClassType) arrayClass.type;
395: arrayClassType.super type_field = objectType;
396: arrayClassType.interfaces_field = List.of(cloneableType,
397: serializableType);
398: arrayClass.members_field = new Scope(arrayClass);
399: lengthVar = new VarSymbol(PUBLIC | FINAL, names.length,
400: intType, arrayClass);
401: arrayClass.members().enter(lengthVar);
402: arrayCloneMethod = new MethodSymbol(PUBLIC, names.clone,
403: new MethodType(List.<Type> nil(), objectType, List
404: .<Type> nil(), methodClass), arrayClass);
405: arrayClass.members().enter(arrayCloneMethod);
406:
407: // Enter operators.
408: enterUnop("+", doubleType, doubleType, nop);
409: enterUnop("+", floatType, floatType, nop);
410: enterUnop("+", longType, longType, nop);
411: enterUnop("+", intType, intType, nop);
412:
413: enterUnop("-", doubleType, doubleType, dneg);
414: enterUnop("-", floatType, floatType, fneg);
415: enterUnop("-", longType, longType, lneg);
416: enterUnop("-", intType, intType, ineg);
417:
418: enterUnop("~", longType, longType, lxor);
419: enterUnop("~", intType, intType, ixor);
420:
421: enterUnop("++", doubleType, doubleType, dadd);
422: enterUnop("++", floatType, floatType, fadd);
423: enterUnop("++", longType, longType, ladd);
424: enterUnop("++", intType, intType, iadd);
425: enterUnop("++", charType, charType, iadd);
426: enterUnop("++", shortType, shortType, iadd);
427: enterUnop("++", byteType, byteType, iadd);
428:
429: enterUnop("--", doubleType, doubleType, dsub);
430: enterUnop("--", floatType, floatType, fsub);
431: enterUnop("--", longType, longType, lsub);
432: enterUnop("--", intType, intType, isub);
433: enterUnop("--", charType, charType, isub);
434: enterUnop("--", shortType, shortType, isub);
435: enterUnop("--", byteType, byteType, isub);
436:
437: enterUnop("!", booleanType, booleanType, bool_not);
438: nullcheck = enterUnop("<*nullchk*>", objectType, objectType,
439: nullchk);
440:
441: // string concatenation
442: enterBinop("+", stringType, objectType, stringType, string_add);
443: enterBinop("+", objectType, stringType, stringType, string_add);
444: enterBinop("+", stringType, stringType, stringType, string_add);
445: enterBinop("+", stringType, intType, stringType, string_add);
446: enterBinop("+", stringType, longType, stringType, string_add);
447: enterBinop("+", stringType, floatType, stringType, string_add);
448: enterBinop("+", stringType, doubleType, stringType, string_add);
449: enterBinop("+", stringType, booleanType, stringType, string_add);
450: enterBinop("+", stringType, botType, stringType, string_add);
451: enterBinop("+", intType, stringType, stringType, string_add);
452: enterBinop("+", longType, stringType, stringType, string_add);
453: enterBinop("+", floatType, stringType, stringType, string_add);
454: enterBinop("+", doubleType, stringType, stringType, string_add);
455: enterBinop("+", booleanType, stringType, stringType, string_add);
456: enterBinop("+", botType, stringType, stringType, string_add);
457:
458: // these errors would otherwise be matched as string concatenation
459: enterBinop("+", botType, botType, botType, error);
460: enterBinop("+", botType, intType, botType, error);
461: enterBinop("+", botType, longType, botType, error);
462: enterBinop("+", botType, floatType, botType, error);
463: enterBinop("+", botType, doubleType, botType, error);
464: enterBinop("+", botType, booleanType, botType, error);
465: enterBinop("+", botType, objectType, botType, error);
466: enterBinop("+", intType, botType, botType, error);
467: enterBinop("+", longType, botType, botType, error);
468: enterBinop("+", floatType, botType, botType, error);
469: enterBinop("+", doubleType, botType, botType, error);
470: enterBinop("+", booleanType, botType, botType, error);
471: enterBinop("+", objectType, botType, botType, error);
472:
473: enterBinop("+", doubleType, doubleType, doubleType, dadd);
474: enterBinop("+", floatType, floatType, floatType, fadd);
475: enterBinop("+", longType, longType, longType, ladd);
476: enterBinop("+", intType, intType, intType, iadd);
477:
478: enterBinop("-", doubleType, doubleType, doubleType, dsub);
479: enterBinop("-", floatType, floatType, floatType, fsub);
480: enterBinop("-", longType, longType, longType, lsub);
481: enterBinop("-", intType, intType, intType, isub);
482:
483: enterBinop("*", doubleType, doubleType, doubleType, dmul);
484: enterBinop("*", floatType, floatType, floatType, fmul);
485: enterBinop("*", longType, longType, longType, lmul);
486: enterBinop("*", intType, intType, intType, imul);
487:
488: enterBinop("/", doubleType, doubleType, doubleType, ddiv);
489: enterBinop("/", floatType, floatType, floatType, fdiv);
490: enterBinop("/", longType, longType, longType, ldiv);
491: enterBinop("/", intType, intType, intType, idiv);
492:
493: enterBinop("%", doubleType, doubleType, doubleType, dmod);
494: enterBinop("%", floatType, floatType, floatType, fmod);
495: enterBinop("%", longType, longType, longType, lmod);
496: enterBinop("%", intType, intType, intType, imod);
497:
498: enterBinop("&", booleanType, booleanType, booleanType, iand);
499: enterBinop("&", longType, longType, longType, land);
500: enterBinop("&", intType, intType, intType, iand);
501:
502: enterBinop("|", booleanType, booleanType, booleanType, ior);
503: enterBinop("|", longType, longType, longType, lor);
504: enterBinop("|", intType, intType, intType, ior);
505:
506: enterBinop("^", booleanType, booleanType, booleanType, ixor);
507: enterBinop("^", longType, longType, longType, lxor);
508: enterBinop("^", intType, intType, intType, ixor);
509:
510: enterBinop("<<", longType, longType, longType, lshll);
511: enterBinop("<<", intType, longType, intType, ishll);
512: enterBinop("<<", longType, intType, longType, lshl);
513: enterBinop("<<", intType, intType, intType, ishl);
514:
515: enterBinop(">>", longType, longType, longType, lshrl);
516: enterBinop(">>", intType, longType, intType, ishrl);
517: enterBinop(">>", longType, intType, longType, lshr);
518: enterBinop(">>", intType, intType, intType, ishr);
519:
520: enterBinop(">>>", longType, longType, longType, lushrl);
521: enterBinop(">>>", intType, longType, intType, iushrl);
522: enterBinop(">>>", longType, intType, longType, lushr);
523: enterBinop(">>>", intType, intType, intType, iushr);
524:
525: enterBinop("<", doubleType, doubleType, booleanType, dcmpg,
526: iflt);
527: enterBinop("<", floatType, floatType, booleanType, fcmpg, iflt);
528: enterBinop("<", longType, longType, booleanType, lcmp, iflt);
529: enterBinop("<", intType, intType, booleanType, if_icmplt);
530:
531: enterBinop(">", doubleType, doubleType, booleanType, dcmpl,
532: ifgt);
533: enterBinop(">", floatType, floatType, booleanType, fcmpl, ifgt);
534: enterBinop(">", longType, longType, booleanType, lcmp, ifgt);
535: enterBinop(">", intType, intType, booleanType, if_icmpgt);
536:
537: enterBinop("<=", doubleType, doubleType, booleanType, dcmpg,
538: ifle);
539: enterBinop("<=", floatType, floatType, booleanType, fcmpg, ifle);
540: enterBinop("<=", longType, longType, booleanType, lcmp, ifle);
541: enterBinop("<=", intType, intType, booleanType, if_icmple);
542:
543: enterBinop(">=", doubleType, doubleType, booleanType, dcmpl,
544: ifge);
545: enterBinop(">=", floatType, floatType, booleanType, fcmpl, ifge);
546: enterBinop(">=", longType, longType, booleanType, lcmp, ifge);
547: enterBinop(">=", intType, intType, booleanType, if_icmpge);
548:
549: enterBinop("==", objectType, objectType, booleanType, if_acmpeq);
550: enterBinop("==", booleanType, booleanType, booleanType,
551: if_icmpeq);
552: enterBinop("==", doubleType, doubleType, booleanType, dcmpl,
553: ifeq);
554: enterBinop("==", floatType, floatType, booleanType, fcmpl, ifeq);
555: enterBinop("==", longType, longType, booleanType, lcmp, ifeq);
556: enterBinop("==", intType, intType, booleanType, if_icmpeq);
557:
558: enterBinop("!=", objectType, objectType, booleanType, if_acmpne);
559: enterBinop("!=", booleanType, booleanType, booleanType,
560: if_icmpne);
561: enterBinop("!=", doubleType, doubleType, booleanType, dcmpl,
562: ifne);
563: enterBinop("!=", floatType, floatType, booleanType, fcmpl, ifne);
564: enterBinop("!=", longType, longType, booleanType, lcmp, ifne);
565: enterBinop("!=", intType, intType, booleanType, if_icmpne);
566:
567: enterBinop("&&", booleanType, booleanType, booleanType,
568: bool_and);
569: enterBinop("||", booleanType, booleanType, booleanType, bool_or);
570: }
571: }
|