Symtab.java revision 2601:8e638f046bf0
1/*
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.code;
27
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Map;
31import java.util.Set;
32
33import javax.lang.model.element.ElementVisitor;
34import javax.tools.JavaFileObject;
35
36
37import com.sun.tools.javac.code.Scope.WriteableScope;
38import com.sun.tools.javac.code.Symbol.ClassSymbol;
39import com.sun.tools.javac.code.Symbol.Completer;
40import com.sun.tools.javac.code.Symbol.CompletionFailure;
41import com.sun.tools.javac.code.Symbol.MethodSymbol;
42import com.sun.tools.javac.code.Symbol.OperatorSymbol;
43import com.sun.tools.javac.code.Symbol.PackageSymbol;
44import com.sun.tools.javac.code.Symbol.TypeSymbol;
45import com.sun.tools.javac.code.Symbol.VarSymbol;
46import com.sun.tools.javac.code.Type.BottomType;
47import com.sun.tools.javac.code.Type.ClassType;
48import com.sun.tools.javac.code.Type.ErrorType;
49import com.sun.tools.javac.code.Type.JCPrimitiveType;
50import com.sun.tools.javac.code.Type.JCVoidType;
51import com.sun.tools.javac.code.Type.MethodType;
52import com.sun.tools.javac.code.Type.UnknownType;
53import com.sun.tools.javac.jvm.ByteCodes;
54import com.sun.tools.javac.jvm.Target;
55import com.sun.tools.javac.util.Assert;
56import com.sun.tools.javac.util.Context;
57import com.sun.tools.javac.util.Convert;
58import com.sun.tools.javac.util.DefinedBy;
59import com.sun.tools.javac.util.DefinedBy.Api;
60import com.sun.tools.javac.util.JavacMessages;
61import com.sun.tools.javac.util.List;
62import com.sun.tools.javac.util.Log;
63import com.sun.tools.javac.util.Name;
64import com.sun.tools.javac.util.Names;
65
66import static com.sun.tools.javac.code.Flags.*;
67import static com.sun.tools.javac.code.Kinds.PCK;
68import static com.sun.tools.javac.code.Kinds.TYP;
69import static com.sun.tools.javac.jvm.ByteCodes.*;
70import static com.sun.tools.javac.code.TypeTag.*;
71
72/** A class that defines all predefined constants and operators
73 *  as well as special classes such as java.lang.Object, which need
74 *  to be known to the compiler. All symbols are held in instance
75 *  fields. This makes it possible to work in multiple concurrent
76 *  projects, which might use different class files for library classes.
77 *
78 *  <p><b>This is NOT part of any supported API.
79 *  If you write code that depends on this, you do so at your own risk.
80 *  This code and its internal interfaces are subject to change or
81 *  deletion without notice.</b>
82 */
83public class Symtab {
84    /** The context key for the symbol table. */
85    protected static final Context.Key<Symtab> symtabKey = new Context.Key<>();
86
87    /** Get the symbol table instance. */
88    public static Symtab instance(Context context) {
89        Symtab instance = context.get(symtabKey);
90        if (instance == null)
91            instance = new Symtab(context);
92        return instance;
93    }
94
95    /** Builtin types.
96     */
97    public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null);
98    public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null);
99    public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null);
100    public final JCPrimitiveType intType = new JCPrimitiveType(INT, null);
101    public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null);
102    public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null);
103    public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null);
104    public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null);
105    public final Type botType = new BottomType();
106    public final JCVoidType voidType = new JCVoidType();
107
108    private final Names names;
109    private final Completer initialCompleter;
110    private final Target target;
111
112    /** A symbol for the root package.
113     */
114    public final PackageSymbol rootPackage;
115
116    /** A symbol for the unnamed package.
117     */
118    public final PackageSymbol unnamedPackage;
119
120    /** A symbol that stands for a missing symbol.
121     */
122    public final TypeSymbol noSymbol;
123
124    /** The error symbol.
125     */
126    public final ClassSymbol errSymbol;
127
128    /** The unknown symbol.
129     */
130    public final ClassSymbol unknownSymbol;
131
132    /** A value for the errType, with a originalType of noType */
133    public final Type errType;
134
135    /** A value for the unknown type. */
136    public final Type unknownType;
137
138    /** The builtin type of all arrays. */
139    public final ClassSymbol arrayClass;
140    public final MethodSymbol arrayCloneMethod;
141
142    /** VGJ: The (singleton) type of all bound types. */
143    public final ClassSymbol boundClass;
144
145    /** The builtin type of all methods. */
146    public final ClassSymbol methodClass;
147
148    /** Predefined types.
149     */
150    public final Type objectType;
151    public final Type classType;
152    public final Type classLoaderType;
153    public final Type stringType;
154    public final Type stringBufferType;
155    public final Type stringBuilderType;
156    public final Type cloneableType;
157    public final Type serializableType;
158    public final Type serializedLambdaType;
159    public final Type methodHandleType;
160    public final Type methodHandleLookupType;
161    public final Type methodTypeType;
162    public final Type nativeHeaderType;
163    public final Type throwableType;
164    public final Type errorType;
165    public final Type interruptedExceptionType;
166    public final Type illegalArgumentExceptionType;
167    public final Type exceptionType;
168    public final Type runtimeExceptionType;
169    public final Type classNotFoundExceptionType;
170    public final Type noClassDefFoundErrorType;
171    public final Type noSuchFieldErrorType;
172    public final Type assertionErrorType;
173    public final Type cloneNotSupportedExceptionType;
174    public final Type annotationType;
175    public final TypeSymbol enumSym;
176    public final Type listType;
177    public final Type collectionsType;
178    public final Type comparableType;
179    public final Type comparatorType;
180    public final Type arraysType;
181    public final Type iterableType;
182    public final Type iteratorType;
183    public final Type annotationTargetType;
184    public final Type overrideType;
185    public final Type retentionType;
186    public final Type deprecatedType;
187    public final Type suppressWarningsType;
188    public final Type inheritedType;
189    public final Type profileType;
190    public final Type proprietaryType;
191    public final Type systemType;
192    public final Type autoCloseableType;
193    public final Type trustMeType;
194    public final Type lambdaMetafactory;
195    public final Type repeatableType;
196    public final Type documentedType;
197    public final Type elementTypeType;
198    public final Type functionalInterfaceType;
199
200    /** The symbol representing the length field of an array.
201     */
202    public final VarSymbol lengthVar;
203
204    /** The null check operator. */
205    public final OperatorSymbol nullcheck;
206
207    /** The symbol representing the final finalize method on enums */
208    public final MethodSymbol enumFinalFinalize;
209
210    /** The symbol representing the close method on TWR AutoCloseable type */
211    public final MethodSymbol autoCloseableClose;
212
213    /** The predefined type that belongs to a tag.
214     */
215    public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
216
217    /** The name of the class that belongs to a basix type tag.
218     */
219    public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()];
220
221    /** A set containing all operator names.
222     */
223    public final Set<Name> operatorNames = new HashSet<>();
224
225    /** A hashtable containing the encountered top-level and member classes,
226     *  indexed by flat names. The table does not contain local classes.
227     *  It should be updated from the outside to reflect classes defined
228     *  by compiled source files.
229     */
230    public final Map<Name, ClassSymbol> classes = new HashMap<>();
231
232    /** A hashtable containing the encountered packages.
233     *  the table should be updated from outside to reflect packages defined
234     *  by compiled source files.
235     */
236    public final Map<Name, PackageSymbol> packages = new HashMap<>();
237
238    public void initType(Type type, ClassSymbol c) {
239        type.tsym = c;
240        typeOfTag[type.getTag().ordinal()] = type;
241    }
242
243    public void initType(Type type, String name) {
244        initType(
245            type,
246            new ClassSymbol(
247                PUBLIC, names.fromString(name), type, rootPackage));
248    }
249
250    public void initType(Type type, String name, String bname) {
251        initType(type, name);
252            boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname);
253    }
254
255    /** The class symbol that owns all predefined symbols.
256     */
257    public final ClassSymbol predefClass;
258
259    /** Enter a constant into symbol table.
260     *  @param name   The constant's name.
261     *  @param type   The constant's type.
262     */
263    private VarSymbol enterConstant(String name, Type type) {
264        VarSymbol c = new VarSymbol(
265            PUBLIC | STATIC | FINAL,
266            names.fromString(name),
267            type,
268            predefClass);
269        c.setData(type.constValue());
270        predefClass.members().enter(c);
271        return c;
272    }
273
274    /** Enter a binary operation into symbol table.
275     *  @param name     The name of the operator.
276     *  @param left     The type of the left operand.
277     *  @param right    The type of the left operand.
278     *  @param res      The operation's result type.
279     *  @param opcode   The operation's bytecode instruction.
280     */
281    private void enterBinop(String name,
282                            Type left, Type right, Type res,
283                            int opcode) {
284        predefClass.members().enter(
285            new OperatorSymbol(
286                makeOperatorName(name),
287                new MethodType(List.of(left, right), res,
288                               List.<Type>nil(), methodClass),
289                opcode,
290                predefClass));
291    }
292
293    /** Enter a binary operation, as above but with two opcodes,
294     *  which get encoded as
295     *  {@code (opcode1 << ByteCodeTags.preShift) + opcode2 }.
296     *  @param opcode1     First opcode.
297     *  @param opcode2     Second opcode.
298     */
299    private void enterBinop(String name,
300                            Type left, Type right, Type res,
301                            int opcode1, int opcode2) {
302        enterBinop(
303            name, left, right, res, (opcode1 << ByteCodes.preShift) | opcode2);
304    }
305
306    /** Enter a unary operation into symbol table.
307     *  @param name     The name of the operator.
308     *  @param arg      The type of the operand.
309     *  @param res      The operation's result type.
310     *  @param opcode   The operation's bytecode instruction.
311     */
312    private OperatorSymbol enterUnop(String name,
313                                     Type arg,
314                                     Type res,
315                                     int opcode) {
316        OperatorSymbol sym =
317            new OperatorSymbol(makeOperatorName(name),
318                               new MethodType(List.of(arg),
319                                              res,
320                                              List.<Type>nil(),
321                                              methodClass),
322                               opcode,
323                               predefClass);
324        predefClass.members().enter(sym);
325        return sym;
326    }
327
328    /**
329     * Create a new operator name from corresponding String representation
330     * and add the name to the set of known operator names.
331     */
332    private Name makeOperatorName(String name) {
333        Name opName = names.fromString(name);
334        operatorNames.add(opName);
335        return opName;
336    }
337
338    /** Enter a class into symbol table.
339     *  @param s The name of the class.
340     */
341    private Type enterClass(String s) {
342        return enterClass(names.fromString(s)).type;
343    }
344
345    public void synthesizeEmptyInterfaceIfMissing(final Type type) {
346        final Completer completer = type.tsym.completer;
347        if (completer != null) {
348            type.tsym.completer = new Completer() {
349                public void complete(Symbol sym) throws CompletionFailure {
350                    try {
351                        completer.complete(sym);
352                    } catch (CompletionFailure e) {
353                        sym.flags_field |= (PUBLIC | INTERFACE);
354                        ((ClassType) sym.type).supertype_field = objectType;
355                    }
356                }
357            };
358        }
359    }
360
361    public void synthesizeBoxTypeIfMissing(final Type type) {
362        ClassSymbol sym = enterClass(boxedName[type.getTag().ordinal()]);
363        final Completer completer = sym.completer;
364        if (completer != null) {
365            sym.completer = new Completer() {
366                public void complete(Symbol sym) throws CompletionFailure {
367                    try {
368                        completer.complete(sym);
369                    } catch (CompletionFailure e) {
370                        sym.flags_field |= PUBLIC;
371                        ((ClassType) sym.type).supertype_field = objectType;
372                        MethodSymbol boxMethod =
373                            new MethodSymbol(PUBLIC | STATIC, names.valueOf,
374                                             new MethodType(List.of(type), sym.type,
375                                    List.<Type>nil(), methodClass),
376                                sym);
377                        sym.members().enter(boxMethod);
378                        MethodSymbol unboxMethod =
379                            new MethodSymbol(PUBLIC,
380                                type.tsym.name.append(names.Value), // x.intValue()
381                                new MethodType(List.<Type>nil(), type,
382                                    List.<Type>nil(), methodClass),
383                                sym);
384                        sym.members().enter(unboxMethod);
385                    }
386                }
387            };
388        }
389
390    }
391
392    // Enter a synthetic class that is used to mark classes in ct.sym.
393    // This class does not have a class file.
394    private Type enterSyntheticAnnotation(String name) {
395        ClassType type = (ClassType)enterClass(name);
396        ClassSymbol sym = (ClassSymbol)type.tsym;
397        sym.completer = null;
398        sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
399        sym.erasure_field = type;
400        sym.members_field = WriteableScope.create(sym);
401        type.typarams_field = List.nil();
402        type.allparams_field = List.nil();
403        type.supertype_field = annotationType;
404        type.interfaces_field = List.nil();
405        return type;
406    }
407
408    /** Constructor; enters all predefined identifiers and operators
409     *  into symbol table.
410     */
411    protected Symtab(Context context) throws CompletionFailure {
412        context.put(symtabKey, this);
413
414        names = Names.instance(context);
415        target = Target.instance(context);
416
417        // Create the unknown type
418        unknownType = new UnknownType();
419
420        // create the basic builtin symbols
421        rootPackage = new PackageSymbol(names.empty, null);
422        packages.put(names.empty, rootPackage);
423        final JavacMessages messages = JavacMessages.instance(context);
424        unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
425                public String toString() {
426                    return messages.getLocalizedString("compiler.misc.unnamed.package");
427                }
428            };
429        noSymbol = new TypeSymbol(Kinds.NIL, 0, names.empty, Type.noType, rootPackage) {
430            @DefinedBy(Api.LANGUAGE_MODEL)
431            public <R, P> R accept(ElementVisitor<R, P> v, P p) {
432                return v.visitUnknown(this, p);
433            }
434        };
435
436        // create the error symbols
437        errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
438        errType = new ErrorType(errSymbol, Type.noType);
439
440        unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
441        unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
442        unknownSymbol.type = unknownType;
443
444        // initialize builtin types
445        initType(byteType, "byte", "Byte");
446        initType(shortType, "short", "Short");
447        initType(charType, "char", "Character");
448        initType(intType, "int", "Integer");
449        initType(longType, "long", "Long");
450        initType(floatType, "float", "Float");
451        initType(doubleType, "double", "Double");
452        initType(booleanType, "boolean", "Boolean");
453        initType(voidType, "void", "Void");
454        initType(botType, "<nulltype>");
455        initType(errType, errSymbol);
456        initType(unknownType, unknownSymbol);
457
458        // the builtin class of all arrays
459        arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol);
460
461        // VGJ
462        boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol);
463        boundClass.members_field = new Scope.ErrorScope(boundClass);
464
465        // the builtin class of all methods
466        methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol);
467        methodClass.members_field = new Scope.ErrorScope(boundClass);
468
469        // Create class to hold all predefined constants and operations.
470        predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
471        WriteableScope scope = WriteableScope.create(predefClass);
472        predefClass.members_field = scope;
473
474        // Get the initial completer for Symbols from the ClassFinder
475        initialCompleter = ClassFinder.instance(context).getCompleter();
476        rootPackage.completer = initialCompleter;
477        unnamedPackage.completer = initialCompleter;
478
479        // Enter symbols for basic types.
480        scope.enter(byteType.tsym);
481        scope.enter(shortType.tsym);
482        scope.enter(charType.tsym);
483        scope.enter(intType.tsym);
484        scope.enter(longType.tsym);
485        scope.enter(floatType.tsym);
486        scope.enter(doubleType.tsym);
487        scope.enter(booleanType.tsym);
488        scope.enter(errType.tsym);
489
490        // Enter symbol for the errSymbol
491        scope.enter(errSymbol);
492
493        classes.put(predefClass.fullname, predefClass);
494
495        // Enter predefined classes.
496        objectType = enterClass("java.lang.Object");
497        classType = enterClass("java.lang.Class");
498        stringType = enterClass("java.lang.String");
499        stringBufferType = enterClass("java.lang.StringBuffer");
500        stringBuilderType = enterClass("java.lang.StringBuilder");
501        cloneableType = enterClass("java.lang.Cloneable");
502        throwableType = enterClass("java.lang.Throwable");
503        serializableType = enterClass("java.io.Serializable");
504        serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda");
505        methodHandleType = enterClass("java.lang.invoke.MethodHandle");
506        methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup");
507        methodTypeType = enterClass("java.lang.invoke.MethodType");
508        errorType = enterClass("java.lang.Error");
509        illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
510        interruptedExceptionType = enterClass("java.lang.InterruptedException");
511        exceptionType = enterClass("java.lang.Exception");
512        runtimeExceptionType = enterClass("java.lang.RuntimeException");
513        classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
514        noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
515        noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
516        assertionErrorType = enterClass("java.lang.AssertionError");
517        cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
518        annotationType = enterClass("java.lang.annotation.Annotation");
519        classLoaderType = enterClass("java.lang.ClassLoader");
520        enumSym = enterClass(names.java_lang_Enum);
521        enumFinalFinalize =
522            new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL,
523                             names.finalize,
524                             new MethodType(List.<Type>nil(), voidType,
525                                            List.<Type>nil(), methodClass),
526                             enumSym);
527        listType = enterClass("java.util.List");
528        collectionsType = enterClass("java.util.Collections");
529        comparableType = enterClass("java.lang.Comparable");
530        comparatorType = enterClass("java.util.Comparator");
531        arraysType = enterClass("java.util.Arrays");
532        iterableType = enterClass("java.lang.Iterable");
533        iteratorType = enterClass("java.util.Iterator");
534        annotationTargetType = enterClass("java.lang.annotation.Target");
535        overrideType = enterClass("java.lang.Override");
536        retentionType = enterClass("java.lang.annotation.Retention");
537        deprecatedType = enterClass("java.lang.Deprecated");
538        suppressWarningsType = enterClass("java.lang.SuppressWarnings");
539        inheritedType = enterClass("java.lang.annotation.Inherited");
540        repeatableType = enterClass("java.lang.annotation.Repeatable");
541        documentedType = enterClass("java.lang.annotation.Documented");
542        elementTypeType = enterClass("java.lang.annotation.ElementType");
543        systemType = enterClass("java.lang.System");
544        autoCloseableType = enterClass("java.lang.AutoCloseable");
545        autoCloseableClose = new MethodSymbol(PUBLIC,
546                             names.close,
547                             new MethodType(List.<Type>nil(), voidType,
548                                            List.of(exceptionType), methodClass),
549                             autoCloseableType.tsym);
550        trustMeType = enterClass("java.lang.SafeVarargs");
551        nativeHeaderType = enterClass("java.lang.annotation.Native");
552        lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
553        functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
554
555        synthesizeEmptyInterfaceIfMissing(autoCloseableType);
556        synthesizeEmptyInterfaceIfMissing(cloneableType);
557        synthesizeEmptyInterfaceIfMissing(serializableType);
558        synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
559        synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
560        synthesizeBoxTypeIfMissing(doubleType);
561        synthesizeBoxTypeIfMissing(floatType);
562        synthesizeBoxTypeIfMissing(voidType);
563
564        // Enter a synthetic class that is used to mark internal
565        // proprietary classes in ct.sym.  This class does not have a
566        // class file.
567        proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
568
569        // Enter a synthetic class that is used to provide profile info for
570        // classes in ct.sym.  This class does not have a class file.
571        profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
572        MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
573        profileType.tsym.members().enter(m);
574
575        // Enter a class for arrays.
576        // The class implements java.lang.Cloneable and java.io.Serializable.
577        // It has a final length field and a clone method.
578        ClassType arrayClassType = (ClassType)arrayClass.type;
579        arrayClassType.supertype_field = objectType;
580        arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
581        arrayClass.members_field = WriteableScope.create(arrayClass);
582        lengthVar = new VarSymbol(
583            PUBLIC | FINAL,
584            names.length,
585            intType,
586            arrayClass);
587        arrayClass.members().enter(lengthVar);
588        arrayCloneMethod = new MethodSymbol(
589            PUBLIC,
590            names.clone,
591            new MethodType(List.<Type>nil(), objectType,
592                           List.<Type>nil(), methodClass),
593            arrayClass);
594        arrayClass.members().enter(arrayCloneMethod);
595
596        // Enter operators.
597        /*  Internally we use +++, --- for unary +, - to reduce +, - operators
598         *  overloading
599         */
600        enterUnop("+++", doubleType, doubleType, nop);
601        enterUnop("+++", floatType, floatType, nop);
602        enterUnop("+++", longType, longType, nop);
603        enterUnop("+++", intType, intType, nop);
604
605        enterUnop("---", doubleType, doubleType, dneg);
606        enterUnop("---", floatType, floatType, fneg);
607        enterUnop("---", longType, longType, lneg);
608        enterUnop("---", intType, intType, ineg);
609
610        enterUnop("~", longType, longType, lxor);
611        enterUnop("~", intType, intType, ixor);
612
613        enterUnop("++", doubleType, doubleType, dadd);
614        enterUnop("++", floatType, floatType, fadd);
615        enterUnop("++", longType, longType, ladd);
616        enterUnop("++", intType, intType, iadd);
617        enterUnop("++", charType, charType, iadd);
618        enterUnop("++", shortType, shortType, iadd);
619        enterUnop("++", byteType, byteType, iadd);
620
621        enterUnop("--", doubleType, doubleType, dsub);
622        enterUnop("--", floatType, floatType, fsub);
623        enterUnop("--", longType, longType, lsub);
624        enterUnop("--", intType, intType, isub);
625        enterUnop("--", charType, charType, isub);
626        enterUnop("--", shortType, shortType, isub);
627        enterUnop("--", byteType, byteType, isub);
628
629        enterUnop("!", booleanType, booleanType, bool_not);
630        nullcheck = enterUnop("<*nullchk*>", objectType, objectType, nullchk);
631
632        // string concatenation
633        enterBinop("+", stringType, objectType, stringType, string_add);
634        enterBinop("+", objectType, stringType, stringType, string_add);
635        enterBinop("+", stringType, stringType, stringType, string_add);
636        enterBinop("+", stringType, intType, stringType, string_add);
637        enterBinop("+", stringType, longType, stringType, string_add);
638        enterBinop("+", stringType, floatType, stringType, string_add);
639        enterBinop("+", stringType, doubleType, stringType, string_add);
640        enterBinop("+", stringType, booleanType, stringType, string_add);
641        enterBinop("+", stringType, botType, stringType, string_add);
642        enterBinop("+", intType, stringType, stringType, string_add);
643        enterBinop("+", longType, stringType, stringType, string_add);
644        enterBinop("+", floatType, stringType, stringType, string_add);
645        enterBinop("+", doubleType, stringType, stringType, string_add);
646        enterBinop("+", booleanType, stringType, stringType, string_add);
647        enterBinop("+", botType, stringType, stringType, string_add);
648
649        // these errors would otherwise be matched as string concatenation
650        enterBinop("+", botType, botType, botType, error);
651        enterBinop("+", botType, intType, botType, error);
652        enterBinop("+", botType, longType, botType, error);
653        enterBinop("+", botType, floatType, botType, error);
654        enterBinop("+", botType, doubleType, botType, error);
655        enterBinop("+", botType, booleanType, botType, error);
656        enterBinop("+", botType, objectType, botType, error);
657        enterBinop("+", intType, botType, botType, error);
658        enterBinop("+", longType, botType, botType, error);
659        enterBinop("+", floatType, botType, botType, error);
660        enterBinop("+", doubleType, botType, botType, error);
661        enterBinop("+", booleanType, botType, botType, error);
662        enterBinop("+", objectType, botType, botType, error);
663
664        enterBinop("+", doubleType, doubleType, doubleType, dadd);
665        enterBinop("+", floatType, floatType, floatType, fadd);
666        enterBinop("+", longType, longType, longType, ladd);
667        enterBinop("+", intType, intType, intType, iadd);
668
669        enterBinop("-", doubleType, doubleType, doubleType, dsub);
670        enterBinop("-", floatType, floatType, floatType, fsub);
671        enterBinop("-", longType, longType, longType, lsub);
672        enterBinop("-", intType, intType, intType, isub);
673
674        enterBinop("*", doubleType, doubleType, doubleType, dmul);
675        enterBinop("*", floatType, floatType, floatType, fmul);
676        enterBinop("*", longType, longType, longType, lmul);
677        enterBinop("*", intType, intType, intType, imul);
678
679        enterBinop("/", doubleType, doubleType, doubleType, ddiv);
680        enterBinop("/", floatType, floatType, floatType, fdiv);
681        enterBinop("/", longType, longType, longType, ldiv);
682        enterBinop("/", intType, intType, intType, idiv);
683
684        enterBinop("%", doubleType, doubleType, doubleType, dmod);
685        enterBinop("%", floatType, floatType, floatType, fmod);
686        enterBinop("%", longType, longType, longType, lmod);
687        enterBinop("%", intType, intType, intType, imod);
688
689        enterBinop("&", booleanType, booleanType, booleanType, iand);
690        enterBinop("&", longType, longType, longType, land);
691        enterBinop("&", intType, intType, intType, iand);
692
693        enterBinop("|", booleanType, booleanType, booleanType, ior);
694        enterBinop("|", longType, longType, longType, lor);
695        enterBinop("|", intType, intType, intType, ior);
696
697        enterBinop("^", booleanType, booleanType, booleanType, ixor);
698        enterBinop("^", longType, longType, longType, lxor);
699        enterBinop("^", intType, intType, intType, ixor);
700
701        enterBinop("<<", longType, longType, longType, lshll);
702        enterBinop("<<", intType, longType, intType, ishll);
703        enterBinop("<<", longType, intType, longType, lshl);
704        enterBinop("<<", intType, intType, intType, ishl);
705
706        enterBinop(">>", longType, longType, longType, lshrl);
707        enterBinop(">>", intType, longType, intType, ishrl);
708        enterBinop(">>", longType, intType, longType, lshr);
709        enterBinop(">>", intType, intType, intType, ishr);
710
711        enterBinop(">>>", longType, longType, longType, lushrl);
712        enterBinop(">>>", intType, longType, intType, iushrl);
713        enterBinop(">>>", longType, intType, longType, lushr);
714        enterBinop(">>>", intType, intType, intType, iushr);
715
716        enterBinop("<", doubleType, doubleType, booleanType, dcmpg, iflt);
717        enterBinop("<", floatType, floatType, booleanType, fcmpg, iflt);
718        enterBinop("<", longType, longType, booleanType, lcmp, iflt);
719        enterBinop("<", intType, intType, booleanType, if_icmplt);
720
721        enterBinop(">", doubleType, doubleType, booleanType, dcmpl, ifgt);
722        enterBinop(">", floatType, floatType, booleanType, fcmpl, ifgt);
723        enterBinop(">", longType, longType, booleanType, lcmp, ifgt);
724        enterBinop(">", intType, intType, booleanType, if_icmpgt);
725
726        enterBinop("<=", doubleType, doubleType, booleanType, dcmpg, ifle);
727        enterBinop("<=", floatType, floatType, booleanType, fcmpg, ifle);
728        enterBinop("<=", longType, longType, booleanType, lcmp, ifle);
729        enterBinop("<=", intType, intType, booleanType, if_icmple);
730
731        enterBinop(">=", doubleType, doubleType, booleanType, dcmpl, ifge);
732        enterBinop(">=", floatType, floatType, booleanType, fcmpl, ifge);
733        enterBinop(">=", longType, longType, booleanType, lcmp, ifge);
734        enterBinop(">=", intType, intType, booleanType, if_icmpge);
735
736        enterBinop("==", objectType, objectType, booleanType, if_acmpeq);
737        enterBinop("==", booleanType, booleanType, booleanType, if_icmpeq);
738        enterBinop("==", doubleType, doubleType, booleanType, dcmpl, ifeq);
739        enterBinop("==", floatType, floatType, booleanType, fcmpl, ifeq);
740        enterBinop("==", longType, longType, booleanType, lcmp, ifeq);
741        enterBinop("==", intType, intType, booleanType, if_icmpeq);
742
743        enterBinop("!=", objectType, objectType, booleanType, if_acmpne);
744        enterBinop("!=", booleanType, booleanType, booleanType, if_icmpne);
745        enterBinop("!=", doubleType, doubleType, booleanType, dcmpl, ifne);
746        enterBinop("!=", floatType, floatType, booleanType, fcmpl, ifne);
747        enterBinop("!=", longType, longType, booleanType, lcmp, ifne);
748        enterBinop("!=", intType, intType, booleanType, if_icmpne);
749
750        enterBinop("&&", booleanType, booleanType, booleanType, bool_and);
751        enterBinop("||", booleanType, booleanType, booleanType, bool_or);
752    }
753
754    /** Define a new class given its name and owner.
755     */
756    public ClassSymbol defineClass(Name name, Symbol owner) {
757        ClassSymbol c = new ClassSymbol(0, name, owner);
758        if (owner.kind == PCK)
759            Assert.checkNull(classes.get(c.flatname), c);
760        c.completer = initialCompleter;
761        return c;
762    }
763
764    /** Create a new toplevel or member class symbol with given name
765     *  and owner and enter in `classes' unless already there.
766     */
767    public ClassSymbol enterClass(Name name, TypeSymbol owner) {
768        Name flatname = TypeSymbol.formFlatName(name, owner);
769        ClassSymbol c = classes.get(flatname);
770        if (c == null) {
771            c = defineClass(name, owner);
772            classes.put(flatname, c);
773        } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
774            // reassign fields of classes that might have been loaded with
775            // their flat names.
776            c.owner.members().remove(c);
777            c.name = name;
778            c.owner = owner;
779            c.fullname = ClassSymbol.formFullName(name, owner);
780        }
781        return c;
782    }
783
784    /**
785     * Creates a new toplevel class symbol with given flat name and
786     * given class (or source) file.
787     *
788     * @param flatName a fully qualified binary class name
789     * @param classFile the class file or compilation unit defining
790     * the class (may be {@code null})
791     * @return a newly created class symbol
792     * @throws AssertionError if the class symbol already exists
793     */
794    public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) {
795        ClassSymbol cs = classes.get(flatName);
796        if (cs != null) {
797            String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
798                                    cs.fullname,
799                                    cs.completer,
800                                    cs.classfile,
801                                    cs.sourcefile);
802            throw new AssertionError(msg);
803        }
804        Name packageName = Convert.packagePart(flatName);
805        PackageSymbol owner = packageName.isEmpty()
806                                ? unnamedPackage
807                                : enterPackage(packageName);
808        cs = defineClass(Convert.shortName(flatName), owner);
809        cs.classfile = classFile;
810        classes.put(flatName, cs);
811        return cs;
812    }
813
814    /** Create a new member or toplevel class symbol with given flat name
815     *  and enter in `classes' unless already there.
816     */
817    public ClassSymbol enterClass(Name flatname) {
818        ClassSymbol c = classes.get(flatname);
819        if (c == null)
820            return enterClass(flatname, (JavaFileObject)null);
821        else
822            return c;
823    }
824
825    /** Check to see if a package exists, given its fully qualified name.
826     */
827    public boolean packageExists(Name fullname) {
828        return enterPackage(fullname).exists();
829    }
830
831    /** Make a package, given its fully qualified name.
832     */
833    public PackageSymbol enterPackage(Name fullname) {
834        PackageSymbol p = packages.get(fullname);
835        if (p == null) {
836            Assert.check(!fullname.isEmpty(), "rootPackage missing!");
837            p = new PackageSymbol(
838                Convert.shortName(fullname),
839                enterPackage(Convert.packagePart(fullname)));
840            p.completer = initialCompleter;
841            packages.put(fullname, p);
842        }
843        return p;
844    }
845
846    /** Make a package, given its unqualified name and enclosing package.
847     */
848    public PackageSymbol enterPackage(Name name, PackageSymbol owner) {
849        return enterPackage(TypeSymbol.formFullName(name, owner));
850    }
851}
852