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