src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
author Dusan Balek <dbalek@netbeans.org>
Mon, 31 Jul 2017 11:07:41 +0200
changeset 5955 f54cccaf6e6c
parent 5950 993a3fed49b2
permissions -rw-r--r--
Mergin jlahoda's fix of #8182450: javac aborts when generating ct.sym intermittently - Initialize the module system model even in presence of missing/broken module-infos; BadClassFiles should not immediatelly abort compilation anymore, but should be handled as if the classfile did not exist.
duke@0
     1
/*
jjg@5674
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
duke@0
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0
     4
 *
duke@0
     5
 * This code is free software; you can redistribute it and/or modify it
duke@0
     6
 * under the terms of the GNU General Public License version 2 only, as
ohair@961
     7
 * published by the Free Software Foundation.  Oracle designates this
duke@0
     8
 * particular file as subject to the "Classpath" exception as provided
ohair@961
     9
 * by Oracle in the LICENSE file that accompanied this code.
duke@0
    10
 *
duke@0
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
duke@0
    14
 * version 2 for more details (a copy is included in the LICENSE file that
duke@0
    15
 * accompanied this code).
duke@0
    16
 *
duke@0
    17
 * You should have received a copy of the GNU General Public License version
duke@0
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0
    20
 *
ohair@961
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@961
    22
 * or visit www.oracle.com if you need additional information or have any
ohair@961
    23
 * questions.
duke@0
    24
 */
duke@0
    25
duke@0
    26
package com.sun.tools.javac.jvm;
duke@0
    27
duke@0
    28
import java.io.*;
duke@0
    29
import java.net.URI;
jjg@690
    30
import java.net.URISyntaxException;
duke@0
    31
import java.nio.CharBuffer;
jjg@5674
    32
import java.nio.file.ClosedFileSystemException;
jjg@718
    33
import java.util.Arrays;
duke@0
    34
import java.util.EnumSet;
duke@0
    35
import java.util.HashMap;
jjg@1217
    36
import java.util.HashSet;
duke@0
    37
import java.util.Map;
duke@0
    38
import java.util.Set;
jjg@4877
    39
jjg@4877
    40
import javax.lang.model.element.Modifier;
jjg@4877
    41
import javax.lang.model.element.NestingKind;
jjg@4309
    42
import javax.tools.JavaFileManager;
jjg@4250
    43
import javax.tools.JavaFileObject;
jjg@4877
    44
jjg@4454
    45
import com.sun.tools.javac.comp.Annotate;
jjg@4454
    46
import com.sun.tools.javac.comp.Annotate.AnnotationTypeCompleter;
duke@0
    47
import com.sun.tools.javac.code.*;
alanb@5016
    48
import com.sun.tools.javac.code.Directive.*;
jjg@1217
    49
import com.sun.tools.javac.code.Lint.LintCategory;
jlahoda@4103
    50
import com.sun.tools.javac.code.Scope.WriteableScope;
duke@0
    51
import com.sun.tools.javac.code.Symbol.*;
duke@0
    52
import com.sun.tools.javac.code.Symtab;
jjg@4309
    53
import com.sun.tools.javac.code.Type.*;
jjg@4454
    54
import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
jjg@4877
    55
import com.sun.tools.javac.file.BaseFileManager;
jjg@4877
    56
import com.sun.tools.javac.file.PathFileObject;
jjg@4309
    57
import com.sun.tools.javac.jvm.ClassFile.NameAndType;
jjg@4309
    58
import com.sun.tools.javac.jvm.ClassFile.Version;
jjg@5307
    59
import com.sun.tools.javac.main.Option;
duke@0
    60
import com.sun.tools.javac.util.*;
jlahoda@4176
    61
import com.sun.tools.javac.util.DefinedBy.Api;
jjg@1217
    62
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@0
    63
duke@0
    64
import static com.sun.tools.javac.code.Flags.*;
emc@4248
    65
import static com.sun.tools.javac.code.Kinds.Kind.*;
alanb@5550
    66
jlahoda@5513
    67
import com.sun.tools.javac.code.Scope.LookupKind;
alanb@5550
    68
sadayapalam@4429
    69
import static com.sun.tools.javac.code.TypeTag.ARRAY;
jjg@2213
    70
import static com.sun.tools.javac.code.TypeTag.CLASS;
vromero@3056
    71
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
jjg@546
    72
import static com.sun.tools.javac.jvm.ClassFile.*;
jjg@546
    73
import static com.sun.tools.javac.jvm.ClassFile.Version.*;
duke@0
    74
jjg@5307
    75
import static com.sun.tools.javac.main.Option.PARAMETERS;
jjg@1132
    76
duke@0
    77
/** This class provides operations to read a classfile into an internal
duke@0
    78
 *  representation. The internal representation is anchored in a
duke@0
    79
 *  ClassSymbol which contains in its scope symbol representations
duke@0
    80
 *  for all other definitions in the classfile. Top-level Classes themselves
duke@0
    81
 *  appear as members of the scopes of PackageSymbols.
duke@0
    82
 *
jjg@971
    83
 *  <p><b>This is NOT part of any supported API.
jjg@971
    84
 *  If you write code that depends on this, you do so at your own risk.
duke@0
    85
 *  This code and its internal interfaces are subject to change or
duke@0
    86
 *  deletion without notice.</b>
duke@0
    87
 */
jfranck@3023
    88
public class ClassReader {
duke@0
    89
    /** The context key for the class reader. */
dbalek@4426
    90
    public static final Context.Key<ClassReader> classReaderKey = new Context.Key<>();
duke@0
    91
ksrini@1265
    92
    public static final int INITIAL_BUFFER_SIZE = 0x0fff0;
ksrini@1265
    93
jjg@4454
    94
    private final Annotate annotate;
duke@0
    95
duke@0
    96
    /** Switch: verbose output.
duke@0
    97
     */
duke@0
    98
    boolean verbose;
duke@0
    99
duke@0
   100
    /** Switch: read constant pool and code sections. This switch is initially
duke@0
   101
     *  set to false but can be turned on from outside.
duke@0
   102
     */
duke@0
   103
    public boolean readAllOfClassFile = false;
duke@0
   104
mcimadamore@1244
   105
    /** Switch: allow simplified varargs.
mcimadamore@1244
   106
     */
mcimadamore@1244
   107
    boolean allowSimplifiedVarargs;
mcimadamore@1244
   108
alanb@5016
   109
    /** Switch: allow modules.
alanb@5016
   110
     */
alanb@5016
   111
    boolean allowModules;
alanb@5016
   112
mcimadamore@1244
   113
   /** Lint option: warn about classfile issues
jjg@1217
   114
     */
jjg@1217
   115
    boolean lintClassfile;
jjg@1217
   116
duke@0
   117
    /** Switch: preserve parameter names from the variable table.
duke@0
   118
     */
duke@0
   119
    public boolean saveParameterNames;
duke@0
   120
duke@0
   121
    /**
jjg@2470
   122
     * The currently selected profile.
jjg@2470
   123
     */
jjg@2470
   124
    public final Profile profile;
jjg@2470
   125
duke@0
   126
    /** The log to use for verbose output
duke@0
   127
     */
duke@0
   128
    final Log log;
duke@0
   129
duke@0
   130
    /** The symbol table. */
duke@0
   131
    Symtab syms;
duke@0
   132
duke@0
   133
    Types types;
duke@0
   134
duke@0
   135
    /** The name table. */
jjg@294
   136
    final Names names;
duke@0
   137
duke@0
   138
    /** Access to files
duke@0
   139
     */
duke@0
   140
    private final JavaFileManager fileManager;
duke@0
   141
jjg@194
   142
    /** Factory for diagnostics
jjg@194
   143
     */
jjg@194
   144
    JCDiagnostic.Factory diagFactory;
jjg@194
   145
dbalek@163
   146
    private final boolean ideMode;
duke@0
   147
duke@0
   148
    /** The current scope where type variables are entered.
duke@0
   149
     */
jlahoda@4103
   150
    protected WriteableScope typevars;
duke@0
   151
alanb@5016
   152
    private List<InterimUsesDirective> interimUses = List.nil();
alanb@5016
   153
    private List<InterimProvidesDirective> interimProvides = List.nil();
alanb@5016
   154
duke@0
   155
    /** The path name of the class file currently being read.
duke@0
   156
     */
duke@0
   157
    protected JavaFileObject currentClassFile = null;
duke@0
   158
duke@0
   159
    /** The class or method currently being read.
duke@0
   160
     */
duke@0
   161
    protected Symbol currentOwner = null;
duke@0
   162
alanb@5016
   163
    /** The module containing the class currently being read.
alanb@5016
   164
     */
alanb@5016
   165
    protected ModuleSymbol currentModule = null;
alanb@5016
   166
duke@0
   167
    /** The buffer containing the currently read class file.
duke@0
   168
     */
ksrini@1265
   169
    byte[] buf = new byte[INITIAL_BUFFER_SIZE];
duke@0
   170
duke@0
   171
    /** The current input pointer.
duke@0
   172
     */
ksrini@2152
   173
    protected int bp;
duke@0
   174
duke@0
   175
    /** The objects of the constant pool.
duke@0
   176
     */
duke@0
   177
    Object[] poolObj;
duke@0
   178
duke@0
   179
    /** For every constant pool entry, an index into buf where the
duke@0
   180
     *  defining section of the entry is found.
duke@0
   181
     */
duke@0
   182
    int[] poolIdx;
duke@0
   183
jjg@546
   184
    /** The major version number of the class file being read. */
jjg@546
   185
    int majorVersion;
jjg@546
   186
    /** The minor version number of the class file being read. */
jjg@546
   187
    int minorVersion;
jjg@546
   188
jjg@718
   189
    /** A table to hold the constant pool indices for method parameter
jjg@718
   190
     * names, as given in LocalVariableTable attributes.
jjg@718
   191
     */
jjg@718
   192
    int[] parameterNameIndices;
jjg@718
   193
jjg@718
   194
    /**
jjg@718
   195
     * Whether or not any parameter names have been found.
jjg@718
   196
     */
jjg@718
   197
    boolean haveParameterNameIndices;
jjg@718
   198
jjg@2312
   199
    /** Set this to false every time we start reading a method
jjg@2312
   200
     * and are saving parameter names.  Set it to true when we see
jjg@2312
   201
     * MethodParameters, if it's set when we see a LocalVariableTable,
jjg@2312
   202
     * then we ignore the parameter names from the LVT.
jjg@2312
   203
     */
jjg@2312
   204
    boolean sawMethodParameters;
jjg@2312
   205
jjg@1217
   206
    /**
jjg@1217
   207
     * The set of attribute names for which warnings have been generated for the current class
jjg@1217
   208
     */
briangoetz@3808
   209
    Set<Name> warnedAttrs = new HashSet<>();
jjg@1217
   210
jjg@4454
   211
    /**
jjg@4454
   212
     * The prototype @Target Attribute.Compound if this class is an annotation annotated with
jjg@4454
   213
     * @Target
jjg@4454
   214
     */
jjg@4454
   215
    CompoundAnnotationProxy target;
jjg@4454
   216
jjg@4454
   217
    /**
jjg@4454
   218
     * The prototype @Repetable Attribute.Compound if this class is an annotation annotated with
jjg@4454
   219
     * @Repeatable
jjg@4454
   220
     */
jjg@4454
   221
    CompoundAnnotationProxy repeatable;
jjg@4454
   222
duke@0
   223
    /** Get the ClassReader instance for this invocation. */
duke@0
   224
    public static ClassReader instance(Context context) {
duke@0
   225
        ClassReader instance = context.get(classReaderKey);
duke@0
   226
        if (instance == null)
jfranck@3822
   227
            instance = new ClassReader(context);
duke@0
   228
        return instance;
duke@0
   229
    }
duke@0
   230
jfranck@3822
   231
    /** Construct a new class reader. */
jfranck@3822
   232
    protected ClassReader(Context context) {
jfranck@3822
   233
        context.put(classReaderKey, this);
jjg@4454
   234
        annotate = Annotate.instance(context);
jjg@294
   235
        names = Names.instance(context);
duke@0
   236
        syms = Symtab.instance(context);
duke@0
   237
        types = Types.instance(context);
duke@0
   238
        fileManager = context.get(JavaFileManager.class);
duke@0
   239
        if (fileManager == null)
duke@0
   240
            throw new AssertionError("FileManager initialization error");
jjg@194
   241
        diagFactory = JCDiagnostic.Factory.instance(context);
duke@0
   242
duke@0
   243
        log = Log.instance(context);
duke@0
   244
duke@0
   245
        Options options = Options.instance(context);
jjg@5307
   246
        verbose         = options.isSet(Option.VERBOSE);
jjg@2470
   247
dbalek@163
   248
        ideMode = options.get("ide") != null;
duke@0
   249
        Source source = Source.instance(context);
mcimadamore@1244
   250
        allowSimplifiedVarargs = source.allowSimplifiedVarargs();
alanb@5016
   251
        allowModules     = source.allowModules();
jjg@2470
   252
vromero@5238
   253
        saveParameterNames = options.isSet(PARAMETERS);
duke@0
   254
jjg@2470
   255
        profile = Profile.instance(context);
jjg@2470
   256
jlahoda@4103
   257
        typevars = WriteableScope.create(syms.noSymbol);
jjg@546
   258
jjg@1217
   259
        lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
jjg@1217
   260
jjg@546
   261
        initAttributeReaders();
duke@0
   262
    }
duke@0
   263
duke@0
   264
    /** Add member to class unless it is synthetic.
duke@0
   265
     */
duke@0
   266
    private void enterMember(ClassSymbol c, Symbol sym) {
rfield@2496
   267
        // Synthetic members are not entered -- reason lost to history (optimization?).
rfield@2496
   268
        // Lambda methods must be entered because they may have inner classes (which reference them)
rfield@2496
   269
        if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC || sym.name.startsWith(names.lambda))
duke@0
   270
            c.members_field.enter(sym);
duke@0
   271
    }
duke@0
   272
duke@0
   273
/************************************************************************
duke@0
   274
 * Error Diagnoses
duke@0
   275
 ***********************************************************************/
duke@0
   276
jjg@4002
   277
    public ClassFinder.BadClassFile badClassFile(String key, Object... args) {
jjg@4002
   278
        return new ClassFinder.BadClassFile (
duke@0
   279
            currentOwner.enclClass(),
duke@0
   280
            currentClassFile,
jjg@4002
   281
            diagFactory.fragment(key, args),
jjg@4002
   282
            diagFactory);
duke@0
   283
    }
duke@0
   284
vromero@5051
   285
    public ClassFinder.BadEnclosingMethodAttr badEnclosingMethod(Object... args) {
vromero@5051
   286
        return new ClassFinder.BadEnclosingMethodAttr (
vromero@5051
   287
            currentOwner.enclClass(),
vromero@5051
   288
            currentClassFile,
vromero@5051
   289
            diagFactory.fragment("bad.enclosing.method", args),
vromero@5051
   290
            diagFactory);
vromero@5051
   291
    }
vromero@5051
   292
duke@0
   293
/************************************************************************
duke@0
   294
 * Buffer Access
duke@0
   295
 ***********************************************************************/
duke@0
   296
duke@0
   297
    /** Read a character.
duke@0
   298
     */
dbalek@2400
   299
    protected char nextChar() {
duke@0
   300
        return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
duke@0
   301
    }
duke@0
   302
jjg@598
   303
    /** Read a byte.
jjg@598
   304
     */
jjg@2372
   305
    int nextByte() {
jjg@2372
   306
        return buf[bp++] & 0xFF;
jjg@598
   307
    }
jjg@598
   308
duke@0
   309
    /** Read an integer.
duke@0
   310
     */
duke@0
   311
    int nextInt() {
duke@0
   312
        return
duke@0
   313
            ((buf[bp++] & 0xFF) << 24) +
duke@0
   314
            ((buf[bp++] & 0xFF) << 16) +
duke@0
   315
            ((buf[bp++] & 0xFF) << 8) +
duke@0
   316
            (buf[bp++] & 0xFF);
duke@0
   317
    }
duke@0
   318
duke@0
   319
    /** Extract a character at position bp from buf.
duke@0
   320
     */
duke@0
   321
    char getChar(int bp) {
duke@0
   322
        return
duke@0
   323
            (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
duke@0
   324
    }
duke@0
   325
duke@0
   326
    /** Extract an integer at position bp from buf.
duke@0
   327
     */
duke@0
   328
    int getInt(int bp) {
duke@0
   329
        return
duke@0
   330
            ((buf[bp] & 0xFF) << 24) +
duke@0
   331
            ((buf[bp+1] & 0xFF) << 16) +
duke@0
   332
            ((buf[bp+2] & 0xFF) << 8) +
duke@0
   333
            (buf[bp+3] & 0xFF);
duke@0
   334
    }
duke@0
   335
duke@0
   336
duke@0
   337
    /** Extract a long integer at position bp from buf.
duke@0
   338
     */
duke@0
   339
    long getLong(int bp) {
duke@0
   340
        DataInputStream bufin =
duke@0
   341
            new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
duke@0
   342
        try {
duke@0
   343
            return bufin.readLong();
duke@0
   344
        } catch (IOException e) {
duke@0
   345
            throw new AssertionError(e);
duke@0
   346
        }
duke@0
   347
    }
duke@0
   348
duke@0
   349
    /** Extract a float at position bp from buf.
duke@0
   350
     */
duke@0
   351
    float getFloat(int bp) {
duke@0
   352
        DataInputStream bufin =
duke@0
   353
            new DataInputStream(new ByteArrayInputStream(buf, bp, 4));
duke@0
   354
        try {
duke@0
   355
            return bufin.readFloat();
duke@0
   356
        } catch (IOException e) {
duke@0
   357
            throw new AssertionError(e);
duke@0
   358
        }
duke@0
   359
    }
duke@0
   360
duke@0
   361
    /** Extract a double at position bp from buf.
duke@0
   362
     */
duke@0
   363
    double getDouble(int bp) {
duke@0
   364
        DataInputStream bufin =
duke@0
   365
            new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
duke@0
   366
        try {
duke@0
   367
            return bufin.readDouble();
duke@0
   368
        } catch (IOException e) {
duke@0
   369
            throw new AssertionError(e);
duke@0
   370
        }
duke@0
   371
    }
duke@0
   372
duke@0
   373
/************************************************************************
duke@0
   374
 * Constant Pool Access
duke@0
   375
 ***********************************************************************/
duke@0
   376
duke@0
   377
    /** Index all constant pool entries, writing their start addresses into
duke@0
   378
     *  poolIdx.
duke@0
   379
     */
duke@0
   380
    void indexPool() {
duke@0
   381
        poolIdx = new int[nextChar()];
duke@0
   382
        poolObj = new Object[poolIdx.length];
duke@0
   383
        int i = 1;
duke@0
   384
        while (i < poolIdx.length) {
duke@0
   385
            poolIdx[i++] = bp;
duke@0
   386
            byte tag = buf[bp++];
duke@0
   387
            switch (tag) {
duke@0
   388
            case CONSTANT_Utf8: case CONSTANT_Unicode: {
duke@0
   389
                int len = nextChar();
duke@0
   390
                bp = bp + len;
duke@0
   391
                break;
duke@0
   392
            }
duke@0
   393
            case CONSTANT_Class:
duke@0
   394
            case CONSTANT_String:
ksrini@1291
   395
            case CONSTANT_MethodType:
alanb@5580
   396
            case CONSTANT_Module:
alanb@5580
   397
            case CONSTANT_Package:
duke@0
   398
                bp = bp + 2;
duke@0
   399
                break;
ksrini@1291
   400
            case CONSTANT_MethodHandle:
ksrini@1291
   401
                bp = bp + 3;
ksrini@1291
   402
                break;
duke@0
   403
            case CONSTANT_Fieldref:
duke@0
   404
            case CONSTANT_Methodref:
duke@0
   405
            case CONSTANT_InterfaceMethodref:
duke@0
   406
            case CONSTANT_NameandType:
duke@0
   407
            case CONSTANT_Integer:
duke@0
   408
            case CONSTANT_Float:
ksrini@1291
   409
            case CONSTANT_InvokeDynamic:
duke@0
   410
                bp = bp + 4;
duke@0
   411
                break;
duke@0
   412
            case CONSTANT_Long:
duke@0
   413
            case CONSTANT_Double:
duke@0
   414
                bp = bp + 8;
duke@0
   415
                i++;
duke@0
   416
                break;
duke@0
   417
            default:
duke@0
   418
                throw badClassFile("bad.const.pool.tag.at",
duke@0
   419
                                   Byte.toString(tag),
duke@0
   420
                                   Integer.toString(bp -1));
duke@0
   421
            }
duke@0
   422
        }
duke@0
   423
    }
duke@0
   424
duke@0
   425
    /** Read constant pool entry at start address i, use pool as a cache.
duke@0
   426
     */
duke@0
   427
    Object readPool(int i) {
duke@0
   428
        Object result = poolObj[i];
duke@0
   429
        if (result != null) return result;
duke@0
   430
duke@0
   431
        int index = poolIdx[i];
duke@0
   432
        if (index == 0) return null;
duke@0
   433
duke@0
   434
        byte tag = buf[index];
duke@0
   435
        switch (tag) {
duke@0
   436
        case CONSTANT_Utf8:
duke@0
   437
            poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1));
duke@0
   438
            break;
duke@0
   439
        case CONSTANT_Unicode:
duke@0
   440
            throw badClassFile("unicode.str.not.supported");
duke@0
   441
        case CONSTANT_Class:
duke@0
   442
            poolObj[i] = readClassOrType(getChar(index + 1));
duke@0
   443
            break;
duke@0
   444
        case CONSTANT_String:
duke@0
   445
            // FIXME: (footprint) do not use toString here
duke@0
   446
            poolObj[i] = readName(getChar(index + 1)).toString();
duke@0
   447
            break;
duke@0
   448
        case CONSTANT_Fieldref: {
duke@0
   449
            ClassSymbol owner = readClassSymbol(getChar(index + 1));
pgovereau@3953
   450
            NameAndType nt = readNameAndType(getChar(index + 3));
vromero@2290
   451
            poolObj[i] = new VarSymbol(0, nt.name, nt.uniqueType.type, owner);
duke@0
   452
            break;
duke@0
   453
        }
duke@0
   454
        case CONSTANT_Methodref:
duke@0
   455
        case CONSTANT_InterfaceMethodref: {
duke@0
   456
            ClassSymbol owner = readClassSymbol(getChar(index + 1));
pgovereau@3953
   457
            NameAndType nt = readNameAndType(getChar(index + 3));
vromero@2290
   458
            poolObj[i] = new MethodSymbol(0, nt.name, nt.uniqueType.type, owner);
duke@0
   459
            break;
duke@0
   460
        }
duke@0
   461
        case CONSTANT_NameandType:
duke@0
   462
            poolObj[i] = new NameAndType(
duke@0
   463
                readName(getChar(index + 1)),
vromero@2290
   464
                readType(getChar(index + 3)), types);
duke@0
   465
            break;
duke@0
   466
        case CONSTANT_Integer:
duke@0
   467
            poolObj[i] = getInt(index + 1);
duke@0
   468
            break;
duke@0
   469
        case CONSTANT_Float:
smarks@5070
   470
            poolObj[i] = Float.valueOf(getFloat(index + 1));
duke@0
   471
            break;
duke@0
   472
        case CONSTANT_Long:
smarks@5070
   473
            poolObj[i] = Long.valueOf(getLong(index + 1));
duke@0
   474
            break;
duke@0
   475
        case CONSTANT_Double:
smarks@5070
   476
            poolObj[i] = Double.valueOf(getDouble(index + 1));
duke@0
   477
            break;
ksrini@1291
   478
        case CONSTANT_MethodHandle:
ksrini@1291
   479
            skipBytes(4);
ksrini@1291
   480
            break;
ksrini@1291
   481
        case CONSTANT_MethodType:
ksrini@1291
   482
            skipBytes(3);
ksrini@1291
   483
            break;
ksrini@1291
   484
        case CONSTANT_InvokeDynamic:
ksrini@1291
   485
            skipBytes(5);
ksrini@1291
   486
            break;
alanb@5580
   487
        case CONSTANT_Module:
alanb@5580
   488
        case CONSTANT_Package:
alanb@5580
   489
            // this is temporary for now: treat as a simple reference to the underlying Utf8.
alanb@5580
   490
            poolObj[i] = readName(getChar(index + 1));
alanb@5580
   491
            break;
duke@0
   492
        default:
duke@0
   493
            throw badClassFile("bad.const.pool.tag", Byte.toString(tag));
duke@0
   494
        }
duke@0
   495
        return poolObj[i];
duke@0
   496
    }
duke@0
   497
duke@0
   498
    /** Read signature and convert to type.
duke@0
   499
     */
dbalek@2400
   500
    protected Type readType(int i) {
duke@0
   501
        int index = poolIdx[i];
duke@0
   502
        return sigToType(buf, index + 3, getChar(index + 1));
duke@0
   503
    }
duke@0
   504
duke@0
   505
    /** If name is an array type or class signature, return the
duke@0
   506
     *  corresponding type; otherwise return a ClassSymbol with given name.
duke@0
   507
     */
duke@0
   508
    Object readClassOrType(int i) {
duke@0
   509
        int index =  poolIdx[i];
duke@0
   510
        int len = getChar(index + 1);
duke@0
   511
        int start = index + 3;
jjg@1281
   512
        Assert.check(buf[start] == '[' || buf[start + len - 1] != ';');
duke@0
   513
        // by the above assertion, the following test can be
duke@0
   514
        // simplified to (buf[start] == '[')
duke@0
   515
        return (buf[start] == '[' || buf[start + len - 1] == ';')
duke@0
   516
            ? (Object)sigToType(buf, start, len)
alanb@5016
   517
            : (Object)enterClass(names.fromUtf(internalize(buf, start,
duke@0
   518
                                                           len)));
duke@0
   519
    }
duke@0
   520
duke@0
   521
    /** Read signature and convert to type parameters.
duke@0
   522
     */
duke@0
   523
    List<Type> readTypeParams(int i) {
duke@0
   524
        int index = poolIdx[i];
duke@0
   525
        return sigToTypeParams(buf, index + 3, getChar(index + 1));
duke@0
   526
    }
duke@0
   527
duke@0
   528
    /** Read class entry.
duke@0
   529
     */
duke@0
   530
    ClassSymbol readClassSymbol(int i) {
pgovereau@3953
   531
        Object obj = readPool(i);
pgovereau@3953
   532
        if (obj != null && !(obj instanceof ClassSymbol))
pgovereau@3953
   533
            throw badClassFile("bad.const.pool.entry",
pgovereau@3953
   534
                               currentClassFile.toString(),
pgovereau@3953
   535
                               "CONSTANT_Class_info", i);
pgovereau@3953
   536
        return (ClassSymbol)obj;
duke@0
   537
    }
duke@0
   538
alanb@5016
   539
    Name readClassName(int i) {
alanb@5016
   540
        int index = poolIdx[i];
alanb@5016
   541
        if (index == 0) return null;
alanb@5016
   542
        byte tag = buf[index];
alanb@5016
   543
        if (tag != CONSTANT_Class) {
alanb@5016
   544
            throw badClassFile("bad.const.pool.entry",
alanb@5016
   545
                               currentClassFile.toString(),
alanb@5016
   546
                               "CONSTANT_Class_info", i);
alanb@5016
   547
        }
alanb@5016
   548
        int nameIndex =  poolIdx[getChar(index + 1)];
alanb@5016
   549
        int len = getChar(nameIndex + 1);
alanb@5016
   550
        int start = nameIndex + 3;
alanb@5016
   551
        if (buf[start] == '[' || buf[start + len - 1] == ';')
alanb@5016
   552
            throw badClassFile("wrong class name"); //TODO: proper diagnostics
alanb@5016
   553
        return names.fromUtf(internalize(buf, start, len));
alanb@5016
   554
    }
alanb@5016
   555
duke@0
   556
    /** Read name.
duke@0
   557
     */
dbalek@2400
   558
    protected Name readName(int i) {
pgovereau@3953
   559
        Object obj = readPool(i);
pgovereau@3953
   560
        if (obj != null && !(obj instanceof Name))
pgovereau@3953
   561
            throw badClassFile("bad.const.pool.entry",
pgovereau@3953
   562
                               currentClassFile.toString(),
pgovereau@3953
   563
                               "CONSTANT_Utf8_info or CONSTANT_String_info", i);
pgovereau@3953
   564
        return (Name)obj;
pgovereau@3953
   565
    }
pgovereau@3953
   566
pgovereau@3953
   567
    /** Read name and type.
pgovereau@3953
   568
     */
pgovereau@3953
   569
    NameAndType readNameAndType(int i) {
pgovereau@3953
   570
        Object obj = readPool(i);
pgovereau@3953
   571
        if (obj != null && !(obj instanceof NameAndType))
pgovereau@3953
   572
            throw badClassFile("bad.const.pool.entry",
pgovereau@3953
   573
                               currentClassFile.toString(),
pgovereau@3953
   574
                               "CONSTANT_NameAndType_info", i);
pgovereau@3953
   575
        return (NameAndType)obj;
duke@0
   576
    }
duke@0
   577
alanb@5550
   578
    /** Read the name of a module.
alanb@5580
   579
     * The name is stored in a CONSTANT_Module entry, in
alanb@5580
   580
     * JVMS 4.2 binary form (using ".", not "/")
alanb@5016
   581
     */
alanb@5550
   582
    Name readModuleName(int i) {
alanb@5580
   583
        return readName(i);
alanb@5550
   584
    }
alanb@5550
   585
alanb@5550
   586
    /** Read module_flags.
alanb@5550
   587
     */
alanb@5550
   588
    Set<ModuleFlags> readModuleFlags(int flags) {
alanb@5550
   589
        Set<ModuleFlags> set = EnumSet.noneOf(ModuleFlags.class);
alanb@5550
   590
        for (ModuleFlags f : ModuleFlags.values()) {
alanb@5550
   591
            if ((flags & f.value) != 0)
alanb@5550
   592
                set.add(f);
sadayapalam@5328
   593
        }
alanb@5550
   594
        return set;
alanb@5550
   595
    }
alanb@5550
   596
alanb@5580
   597
    /** Read resolution_flags.
alanb@5580
   598
     */
alanb@5580
   599
    Set<ModuleResolutionFlags> readModuleResolutionFlags(int flags) {
alanb@5580
   600
        Set<ModuleResolutionFlags> set = EnumSet.noneOf(ModuleResolutionFlags.class);
alanb@5580
   601
        for (ModuleResolutionFlags f : ModuleResolutionFlags.values()) {
alanb@5580
   602
            if ((flags & f.value) != 0)
alanb@5580
   603
                set.add(f);
alanb@5016
   604
        }
alanb@5580
   605
        return set;
alanb@5580
   606
    }
alanb@5580
   607
alanb@5550
   608
    /** Read exports_flags.
alanb@5550
   609
     */
alanb@5550
   610
    Set<ExportsFlag> readExportsFlags(int flags) {
alanb@5550
   611
        Set<ExportsFlag> set = EnumSet.noneOf(ExportsFlag.class);
alanb@5550
   612
        for (ExportsFlag f: ExportsFlag.values()) {
alanb@5550
   613
            if ((flags & f.value) != 0)
alanb@5550
   614
                set.add(f);
alanb@5550
   615
        }
alanb@5550
   616
        return set;
alanb@5550
   617
    }
alanb@5550
   618
alanb@5550
   619
    /** Read opens_flags.
alanb@5550
   620
     */
alanb@5550
   621
    Set<OpensFlag> readOpensFlags(int flags) {
alanb@5550
   622
        Set<OpensFlag> set = EnumSet.noneOf(OpensFlag.class);
alanb@5550
   623
        for (OpensFlag f: OpensFlag.values()) {
alanb@5550
   624
            if ((flags & f.value) != 0)
alanb@5550
   625
                set.add(f);
alanb@5550
   626
        }
alanb@5550
   627
        return set;
alanb@5016
   628
    }
alanb@5016
   629
alanb@5016
   630
    /** Read requires_flags.
alanb@5016
   631
     */
alanb@5016
   632
    Set<RequiresFlag> readRequiresFlags(int flags) {
alanb@5016
   633
        Set<RequiresFlag> set = EnumSet.noneOf(RequiresFlag.class);
alanb@5016
   634
        for (RequiresFlag f: RequiresFlag.values()) {
alanb@5016
   635
            if ((flags & f.value) != 0)
alanb@5016
   636
                set.add(f);
alanb@5016
   637
        }
alanb@5016
   638
        return set;
alanb@5016
   639
    }
alanb@5016
   640
duke@0
   641
/************************************************************************
duke@0
   642
 * Reading Types
duke@0
   643
 ***********************************************************************/
duke@0
   644
duke@0
   645
    /** The unread portion of the currently read type is
duke@0
   646
     *  signature[sigp..siglimit-1].
duke@0
   647
     */
duke@0
   648
    byte[] signature;
duke@0
   649
    int sigp;
duke@0
   650
    int siglimit;
duke@0
   651
    boolean sigEnterPhase = false;
duke@0
   652
duke@0
   653
    /** Convert signature to type, where signature is a byte array segment.
duke@0
   654
     */
duke@0
   655
    Type sigToType(byte[] sig, int offset, int len) {
duke@0
   656
        signature = sig;
duke@0
   657
        sigp = offset;
duke@0
   658
        siglimit = offset + len;
duke@0
   659
        return sigToType();
duke@0
   660
    }
duke@0
   661
duke@0
   662
    /** Convert signature to type, where signature is implicit.
duke@0
   663
     */
duke@0
   664
    Type sigToType() {
duke@0
   665
        switch ((char) signature[sigp]) {
duke@0
   666
        case 'T':
duke@0
   667
            sigp++;
duke@0
   668
            int start = sigp;
duke@0
   669
            while (signature[sigp] != ';') sigp++;
duke@0
   670
            sigp++;
duke@0
   671
            return sigEnterPhase
duke@0
   672
                ? Type.noType
duke@0
   673
                : findTypeVar(names.fromUtf(signature, start, sigp - 1 - start));
duke@0
   674
        case '+': {
duke@0
   675
            sigp++;
duke@0
   676
            Type t = sigToType();
emc@4221
   677
            return new WildcardType(t, BoundKind.EXTENDS, syms.boundClass);
duke@0
   678
        }
duke@0
   679
        case '*':
duke@0
   680
            sigp++;
duke@0
   681
            return new WildcardType(syms.objectType, BoundKind.UNBOUND,
emc@4221
   682
                                    syms.boundClass);
duke@0
   683
        case '-': {
duke@0
   684
            sigp++;
duke@0
   685
            Type t = sigToType();
emc@4221
   686
            return new WildcardType(t, BoundKind.SUPER, syms.boundClass);
duke@0
   687
        }
duke@0
   688
        case 'B':
duke@0
   689
            sigp++;
duke@0
   690
            return syms.byteType;
duke@0
   691
        case 'C':
duke@0
   692
            sigp++;
duke@0
   693
            return syms.charType;
duke@0
   694
        case 'D':
duke@0
   695
            sigp++;
duke@0
   696
            return syms.doubleType;
duke@0
   697
        case 'F':
duke@0
   698
            sigp++;
duke@0
   699
            return syms.floatType;
duke@0
   700
        case 'I':
duke@0
   701
            sigp++;
duke@0
   702
            return syms.intType;
duke@0
   703
        case 'J':
duke@0
   704
            sigp++;
duke@0
   705
            return syms.longType;
dbalek@46
   706
        case 'R':
duke@0
   707
        case 'L':
duke@0
   708
            {
duke@0
   709
                // int oldsigp = sigp;
duke@0
   710
                Type t = classSigToType();
duke@0
   711
                if (sigp < siglimit && signature[sigp] == '.')
duke@0
   712
                    throw badClassFile("deprecated inner class signature syntax " +
duke@0
   713
                                       "(please recompile from source)");
duke@0
   714
                /*
duke@0
   715
                System.err.println(" decoded " +
duke@0
   716
                                   new String(signature, oldsigp, sigp-oldsigp) +
duke@0
   717
                                   " => " + t + " outer " + t.outer());
duke@0
   718
                */
duke@0
   719
                return t;
duke@0
   720
            }
duke@0
   721
        case 'S':
duke@0
   722
            sigp++;
duke@0
   723
            return syms.shortType;
duke@0
   724
        case 'V':
duke@0
   725
            sigp++;
duke@0
   726
            return syms.voidType;
duke@0
   727
        case 'Z':
duke@0
   728
            sigp++;
duke@0
   729
            return syms.booleanType;
duke@0
   730
        case '[':
duke@0
   731
            sigp++;
emc@4221
   732
            return new ArrayType(sigToType(), syms.arrayClass);
duke@0
   733
        case '(':
duke@0
   734
            sigp++;
duke@0
   735
            List<Type> argtypes = sigToTypes(')');
duke@0
   736
            Type restype = sigToType();
duke@0
   737
            List<Type> thrown = List.nil();
duke@0
   738
            while (signature[sigp] == '^') {
duke@0
   739
                sigp++;
duke@0
   740
                thrown = thrown.prepend(sigToType());
duke@0
   741
            }
vromero@3056
   742
            // if there is a typevar in the throws clause we should state it.
vromero@3056
   743
            for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) {
vromero@3056
   744
                if (l.head.hasTag(TYPEVAR)) {
vromero@3056
   745
                    l.head.tsym.flags_field |= THROWS;
vromero@3056
   746
                }
vromero@3056
   747
            }
duke@0
   748
            return new MethodType(argtypes,
duke@0
   749
                                  restype,
duke@0
   750
                                  thrown.reverse(),
duke@0
   751
                                  syms.methodClass);
duke@0
   752
        case '<':
duke@0
   753
            typevars = typevars.dup(currentOwner);
duke@0
   754
            Type poly = new ForAll(sigToTypeParams(), sigToType());
duke@0
   755
            typevars = typevars.leave();
duke@0
   756
            return poly;
duke@0
   757
        default:
duke@0
   758
            throw badClassFile("bad.signature",
duke@0
   759
                               Convert.utf2string(signature, sigp, 10));
duke@0
   760
        }
duke@0
   761
    }
duke@0
   762
duke@0
   763
    byte[] signatureBuffer = new byte[0];
duke@0
   764
    int sbp = 0;
duke@0
   765
    /** Convert class signature to type, where signature is implicit.
duke@0
   766
     */
duke@0
   767
    Type classSigToType() {
dbalek@46
   768
        if (signature[sigp] != 'L' && signature[sigp] != 'R')
duke@0
   769
            throw badClassFile("bad.class.signature",
duke@0
   770
                               Convert.utf2string(signature, sigp, 10));
dbalek@46
   771
        boolean err = signature[sigp] == 'R';
duke@0
   772
        sigp++;
duke@0
   773
        Type outer = Type.noType;
duke@0
   774
        int startSbp = sbp;
duke@0
   775
duke@0
   776
        while (true) {
duke@0
   777
            final byte c = signature[sigp++];
duke@0
   778
            switch (c) {
duke@0
   779
duke@0
   780
            case ';': {         // end
alanb@5016
   781
                ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
duke@0
   782
                                                         startSbp,
duke@0
   783
                                                         sbp - startSbp));
vromero@3017
   784
vromero@3017
   785
                try {
dbalek@4426
   786
                    return err ? new ErrorType(Type.noType, t) :
dbalek@3084
   787
                            (outer == Type.noType) ?
vromero@3017
   788
                            t.erasure(types) :
mcimadamore@5586
   789
                        new ClassType(outer, List.nil(), t);
vromero@3017
   790
                } finally {
vromero@3017
   791
                    sbp = startSbp;
vromero@3017
   792
                }
duke@0
   793
            }
duke@0
   794
duke@0
   795
            case '<':           // generic arguments
alanb@5016
   796
                ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
duke@0
   797
                                                         startSbp,
duke@0
   798
                                                         sbp - startSbp));
emc@4221
   799
                outer = new ClassType(outer, sigToTypes('>'), t) {
duke@0
   800
                        boolean completed = false;
jlahoda@4176
   801
                        @Override @DefinedBy(Api.LANGUAGE_MODEL)
duke@0
   802
                        public Type getEnclosingType() {
duke@0
   803
                            if (!completed) {
duke@0
   804
                                completed = true;
dbalek@1624
   805
                                try {
dbalek@1624
   806
                                    tsym.complete();
dbalek@1624
   807
                                } catch (CompletionFailure cf) {}
duke@0
   808
                                Type enclosingType = tsym.type.getEnclosingType();
duke@0
   809
                                if (enclosingType != Type.noType) {
duke@0
   810
                                    List<Type> typeArgs =
duke@0
   811
                                        super.getEnclosingType().allparams();
duke@0
   812
                                    List<Type> typeParams =
duke@0
   813
                                        enclosingType.allparams();
duke@0
   814
                                    if (typeParams.length() != typeArgs.length()) {
duke@0
   815
                                        // no "rare" types
duke@0
   816
                                        super.setEnclosingType(types.erasure(enclosingType));
duke@0
   817
                                    } else {
duke@0
   818
                                        super.setEnclosingType(types.subst(enclosingType,
duke@0
   819
                                                                           typeParams,
duke@0
   820
                                                                           typeArgs));
duke@0
   821
                                    }
duke@0
   822
                                } else {
duke@0
   823
                                    super.setEnclosingType(Type.noType);
duke@0
   824
                                }
duke@0
   825
                            }
duke@0
   826
                            return super.getEnclosingType();
duke@0
   827
                        }
jjg@546
   828
                        @Override
duke@0
   829
                        public void setEnclosingType(Type outer) {
duke@0
   830
                            throw new UnsupportedOperationException();
duke@0
   831
                        }
duke@0
   832
                    };
duke@0
   833
                switch (signature[sigp++]) {
duke@0
   834
                case ';':
duke@0
   835
                    if (sigp < signature.length && signature[sigp] == '.') {
duke@0
   836
                        // support old-style GJC signatures
duke@0
   837
                        // The signature produced was
duke@0
   838
                        // Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>;
duke@0
   839
                        // rather than say
duke@0
   840
                        // Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>;
duke@0
   841
                        // so we skip past ".Lfoo/Outer$"
duke@0
   842
                        sigp += (sbp - startSbp) + // "foo/Outer"
duke@0
   843
                            3;  // ".L" and "$"
duke@0
   844
                        signatureBuffer[sbp++] = (byte)'$';
duke@0
   845
                        break;
duke@0
   846
                    } else {
duke@0
   847
                        sbp = startSbp;
duke@0
   848
                        return outer;
duke@0
   849
                    }
duke@0
   850
                case '.':
duke@0
   851
                    signatureBuffer[sbp++] = (byte)'$';
duke@0
   852
                    break;
duke@0
   853
                default:
duke@0
   854
                    throw new AssertionError(signature[sigp-1]);
duke@0
   855
                }
duke@0
   856
                continue;
duke@0
   857
duke@0
   858
            case '.':
vromero@3017
   859
                //we have seen an enclosing non-generic class
vromero@3017
   860
                if (outer != Type.noType) {
alanb@5016
   861
                    t = enterClass(names.fromUtf(signatureBuffer,
vromero@3017
   862
                                                 startSbp,
vromero@3017
   863
                                                 sbp - startSbp));
mcimadamore@5586
   864
                    outer = new ClassType(outer, List.nil(), t);
vromero@3017
   865
                }
duke@0
   866
                signatureBuffer[sbp++] = (byte)'$';
duke@0
   867
                continue;
duke@0
   868
            case '/':
duke@0
   869
                signatureBuffer[sbp++] = (byte)'.';
duke@0
   870
                continue;
duke@0
   871
            default:
duke@0
   872
                signatureBuffer[sbp++] = c;
duke@0
   873
                continue;
duke@0
   874
            }
duke@0
   875
        }
duke@0
   876
    }
duke@0
   877
duke@0
   878
    /** Convert (implicit) signature to list of types
duke@0
   879
     *  until `terminator' is encountered.
duke@0
   880
     */
duke@0
   881
    List<Type> sigToTypes(char terminator) {
duke@0
   882
        List<Type> head = List.of(null);
duke@0
   883
        List<Type> tail = head;
duke@0
   884
        while (signature[sigp] != terminator)
duke@0
   885
            tail = tail.setTail(List.of(sigToType()));
duke@0
   886
        sigp++;
duke@0
   887
        return head.tail;
duke@0
   888
    }
duke@0
   889
duke@0
   890
    /** Convert signature to type parameters, where signature is a byte
duke@0
   891
     *  array segment.
duke@0
   892
     */
duke@0
   893
    List<Type> sigToTypeParams(byte[] sig, int offset, int len) {
duke@0
   894
        signature = sig;
duke@0
   895
        sigp = offset;
duke@0
   896
        siglimit = offset + len;
duke@0
   897
        return sigToTypeParams();
duke@0
   898
    }
duke@0
   899
duke@0
   900
    /** Convert signature to type parameters, where signature is implicit.
duke@0
   901
     */
duke@0
   902
    List<Type> sigToTypeParams() {
duke@0
   903
        List<Type> tvars = List.nil();
duke@0
   904
        if (signature[sigp] == '<') {
duke@0
   905
            sigp++;
duke@0
   906
            int start = sigp;
duke@0
   907
            sigEnterPhase = true;
duke@0
   908
            while (signature[sigp] != '>')
duke@0
   909
                tvars = tvars.prepend(sigToTypeParam());
duke@0
   910
            sigEnterPhase = false;
duke@0
   911
            sigp = start;
duke@0
   912
            while (signature[sigp] != '>')
duke@0
   913
                sigToTypeParam();
duke@0
   914
            sigp++;
duke@0
   915
        }
duke@0
   916
        return tvars.reverse();
duke@0
   917
    }
duke@0
   918
duke@0
   919
    /** Convert (implicit) signature to type parameter.
duke@0
   920
     */
duke@0
   921
    Type sigToTypeParam() {
duke@0
   922
        int start = sigp;
duke@0
   923
        while (signature[sigp] != ':') sigp++;
duke@0
   924
        Name name = names.fromUtf(signature, start, sigp - start);
duke@0
   925
        TypeVar tvar;
duke@0
   926
        if (sigEnterPhase) {
emc@4221
   927
            tvar = new TypeVar(name, currentOwner, syms.botType);
duke@0
   928
            typevars.enter(tvar.tsym);
duke@0
   929
        } else {
duke@0
   930
            tvar = (TypeVar)findTypeVar(name);
duke@0
   931
        }
duke@0
   932
        List<Type> bounds = List.nil();
mcimadamore@2276
   933
        boolean allInterfaces = false;
duke@0
   934
        if (signature[sigp] == ':' && signature[sigp+1] == ':') {
duke@0
   935
            sigp++;
mcimadamore@2276
   936
            allInterfaces = true;
duke@0
   937
        }
duke@0
   938
        while (signature[sigp] == ':') {
duke@0
   939
            sigp++;
duke@0
   940
            bounds = bounds.prepend(sigToType());
duke@0
   941
        }
duke@0
   942
        if (!sigEnterPhase) {
mcimadamore@2276
   943
            types.setBounds(tvar, bounds.reverse(), allInterfaces);
duke@0
   944
        }
duke@0
   945
        return tvar;
duke@0
   946
    }
duke@0
   947
duke@0
   948
    /** Find type variable with given name in `typevars' scope.
duke@0
   949
     */
duke@0
   950
    Type findTypeVar(Name name) {
jlahoda@4103
   951
        Symbol s = typevars.findFirst(name);
jlahoda@4103
   952
        if (s != null) {
jlahoda@4103
   953
            return s.type;
duke@0
   954
        } else {
duke@0
   955
            if (readingClassAttr) {
duke@0
   956
                // While reading the class attribute, the supertypes
duke@0
   957
                // might refer to a type variable from an enclosing element
duke@0
   958
                // (method or class).
duke@0
   959
                // If the type variable is defined in the enclosing class,
duke@0
   960
                // we can actually find it in
duke@0
   961
                // currentOwner.owner.type.getTypeArguments()
duke@0
   962
                // However, until we have read the enclosing method attribute
duke@0
   963
                // we don't know for sure if this owner is correct.  It could
duke@0
   964
                // be a method and there is no way to tell before reading the
duke@0
   965
                // enclosing method attribute.
emc@4221
   966
                TypeVar t = new TypeVar(name, currentOwner, syms.botType);
duke@0
   967
                missingTypeVariables = missingTypeVariables.prepend(t);
duke@0
   968
                // System.err.println("Missing type var " + name);
duke@0
   969
                return t;
duke@0
   970
            }
duke@0
   971
            throw badClassFile("undecl.type.var", name);
duke@0
   972
        }
duke@0
   973
    }
duke@0
   974
duke@0
   975
/************************************************************************
duke@0
   976
 * Reading Attributes
duke@0
   977
 ***********************************************************************/
duke@0
   978
briangoetz@3808
   979
    protected enum AttributeKind { CLASS, MEMBER }
briangoetz@3808
   980
jjg@546
   981
    protected abstract class AttributeReader {
ksrini@2152
   982
        protected AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) {
jjg@546
   983
            this.name = name;
jjg@546
   984
            this.version = version;
jjg@546
   985
            this.kinds = kinds;
jjg@546
   986
        }
jjg@546
   987
ksrini@2152
   988
        protected boolean accepts(AttributeKind kind) {
jjg@1217
   989
            if (kinds.contains(kind)) {
jjg@1217
   990
                if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor))
jjg@1217
   991
                    return true;
jjg@1217
   992
jjg@1217
   993
                if (lintClassfile && !warnedAttrs.contains(name)) {
jjg@1217
   994
                    JavaFileObject prev = log.useSource(currentClassFile);
jjg@1217
   995
                    try {
jjg@1217
   996
                        log.warning(LintCategory.CLASSFILE, (DiagnosticPosition) null, "future.attr",
jjg@1217
   997
                                name, version.major, version.minor, majorVersion, minorVersion);
jjg@1217
   998
                    } finally {
jjg@1217
   999
                        log.useSource(prev);
jjg@1217
  1000
                    }
jjg@1217
  1001
                    warnedAttrs.add(name);
jjg@1217
  1002
                }
dbalek@1220
  1003
                
dbalek@1220
  1004
                return ideMode;
jjg@1217
  1005
            }
jjg@1217
  1006
            return false;
jjg@546
  1007
        }
jjg@546
  1008
ksrini@2152
  1009
        protected abstract void read(Symbol sym, int attrLen);
jjg@546
  1010
ksrini@2152
  1011
        protected final Name name;
ksrini@2152
  1012
        protected final ClassFile.Version version;
ksrini@2152
  1013
        protected final Set<AttributeKind> kinds;
jjg@546
  1014
    }
jjg@546
  1015
jjg@546
  1016
    protected Set<AttributeKind> CLASS_ATTRIBUTE =
jjg@546
  1017
            EnumSet.of(AttributeKind.CLASS);
jjg@546
  1018
    protected Set<AttributeKind> MEMBER_ATTRIBUTE =
jjg@546
  1019
            EnumSet.of(AttributeKind.MEMBER);
jjg@546
  1020
    protected Set<AttributeKind> CLASS_OR_MEMBER_ATTRIBUTE =
jjg@546
  1021
            EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER);
jjg@546
  1022
briangoetz@3808
  1023
    protected Map<Name, AttributeReader> attributeReaders = new HashMap<>();
jjg@546
  1024
jjg@1217
  1025
    private void initAttributeReaders() {
jjg@546
  1026
        AttributeReader[] readers = {
jjg@546
  1027
            // v45.3 attributes
jjg@546
  1028
jjg@546
  1029
            new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
ksrini@2152
  1030
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1031
                    if (readAllOfClassFile || saveParameterNames)
jjg@546
  1032
                        ((MethodSymbol)sym).code = readCode(sym);
jjg@546
  1033
                    else
jjg@546
  1034
                        bp = bp + attrLen;
jjg@546
  1035
                }
jjg@546
  1036
            },
jjg@546
  1037
jjg@546
  1038
            new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
ksrini@2152
  1039
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1040
                    Object v = readPool(nextChar());
jjg@546
  1041
                    // Ignore ConstantValue attribute if field not final.
cushon@5625
  1042
                    if ((sym.flags() & FINAL) == 0) {
cushon@5625
  1043
                        return;
cushon@5625
  1044
                    }
cushon@5625
  1045
                    VarSymbol var = (VarSymbol) sym;
cushon@5625
  1046
                    switch (var.type.getTag()) {
cushon@5625
  1047
                       case BOOLEAN:
cushon@5625
  1048
                       case BYTE:
cushon@5625
  1049
                       case CHAR:
cushon@5625
  1050
                       case SHORT:
cushon@5625
  1051
                       case INT:
cushon@5625
  1052
                           checkType(var, Integer.class, v);
cushon@5625
  1053
                           break;
cushon@5625
  1054
                       case LONG:
cushon@5625
  1055
                           checkType(var, Long.class, v);
cushon@5625
  1056
                           break;
cushon@5625
  1057
                       case FLOAT:
cushon@5625
  1058
                           checkType(var, Float.class, v);
cushon@5625
  1059
                           break;
cushon@5625
  1060
                       case DOUBLE:
cushon@5625
  1061
                           checkType(var, Double.class, v);
cushon@5625
  1062
                           break;
cushon@5625
  1063
                       case CLASS:
cushon@5625
  1064
                           Assert.check(var.type.tsym == syms.stringType.tsym);
cushon@5625
  1065
                           checkType(var, String.class, v);
cushon@5625
  1066
                           break;
cushon@5625
  1067
                       default:
cushon@5625
  1068
                           // ignore ConstantValue attribute if type is not primitive or String
cushon@5625
  1069
                           return;
cushon@5625
  1070
                    }
cushon@5625
  1071
                    if (v instanceof Integer && !var.type.getTag().checkRange((Integer) v)) {
cushon@5625
  1072
                        throw badClassFile("bad.constant.range", v, var, var.type);
cushon@5625
  1073
                    }
cushon@5625
  1074
                    var.setData(v);
cushon@5625
  1075
                }
cushon@5625
  1076
cushon@5625
  1077
                void checkType(Symbol var, Class<?> clazz, Object value) {
cushon@5625
  1078
                    if (!clazz.isInstance(value)) {
cushon@5625
  1079
                        throw badClassFile("bad.constant.value", value, var, clazz.getSimpleName());
cushon@5625
  1080
                    }
jjg@546
  1081
                }
jjg@546
  1082
            },
jjg@546
  1083
jjg@546
  1084
            new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1085
                protected void read(Symbol sym, int attrLen) {
alanb@5550
  1086
                    Symbol s = sym.owner.kind == MDL ? sym.owner : sym;
alanb@5550
  1087
alanb@5550
  1088
                    s.flags_field |= DEPRECATED;
jjg@546
  1089
                }
jjg@546
  1090
            },
jjg@546
  1091
jjg@546
  1092
            new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1093
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1094
                    int nexceptions = nextChar();
jjg@546
  1095
                    List<Type> thrown = List.nil();
jjg@546
  1096
                    for (int j = 0; j < nexceptions; j++)
jjg@546
  1097
                        thrown = thrown.prepend(readClassSymbol(nextChar()).type);
jjg@546
  1098
                    if (sym.type.getThrownTypes().isEmpty())
jjg@546
  1099
                        sym.type.asMethodType().thrown = thrown.reverse();
jjg@546
  1100
                }
jjg@546
  1101
            },
jjg@546
  1102
jjg@546
  1103
            new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
ksrini@2152
  1104
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1105
                    ClassSymbol c = (ClassSymbol) sym;
alanb@5016
  1106
                    if (currentModule.module_info == c) {
alanb@5016
  1107
                        //prevent entering the classes too soon:
alanb@5016
  1108
                        skipInnerClasses();
alanb@5016
  1109
                    } else {
alanb@5016
  1110
                        readInnerClasses(c);
alanb@5016
  1111
                    }
jjg@546
  1112
                }
jjg@546
  1113
            },
jjg@546
  1114
jjg@546
  1115
            new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1116
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1117
                    int newbp = bp + attrLen;
jjg@2312
  1118
                    if (saveParameterNames && !sawMethodParameters) {
jjg@718
  1119
                        // Pick up parameter names from the variable table.
jjg@718
  1120
                        // Parameter names are not explicitly identified as such,
jjg@718
  1121
                        // but all parameter name entries in the LocalVariableTable
jjg@718
  1122
                        // have a start_pc of 0.  Therefore, we record the name
jjg@718
  1123
                        // indicies of all slots with a start_pc of zero in the
jjg@718
  1124
                        // parameterNameIndicies array.
jjg@718
  1125
                        // Note that this implicitly honors the JVMS spec that
jjg@718
  1126
                        // there may be more than one LocalVariableTable, and that
jjg@718
  1127
                        // there is no specified ordering for the entries.
jjg@546
  1128
                        int numEntries = nextChar();
jjg@718
  1129
                        for (int i = 0; i < numEntries; i++) {
jjg@546
  1130
                            int start_pc = nextChar();
jjg@546
  1131
                            int length = nextChar();
jjg@546
  1132
                            int nameIndex = nextChar();
jjg@546
  1133
                            int sigIndex = nextChar();
jjg@546
  1134
                            int register = nextChar();
jjg@718
  1135
                            if (start_pc == 0) {
jjg@718
  1136
                                // ensure array large enough
jjg@718
  1137
                                if (register >= parameterNameIndices.length) {
jlahoda@5218
  1138
                                    int newSize =
jlahoda@5218
  1139
                                            Math.max(register + 1, parameterNameIndices.length + 8);
jjg@718
  1140
                                    parameterNameIndices =
jjg@718
  1141
                                            Arrays.copyOf(parameterNameIndices, newSize);
jjg@546
  1142
                                }
jjg@718
  1143
                                parameterNameIndices[register] = nameIndex;
jjg@718
  1144
                                haveParameterNameIndices = true;
jjg@546
  1145
                            }
jjg@546
  1146
                        }
jjg@546
  1147
                    }
jjg@546
  1148
                    bp = newbp;
jjg@546
  1149
                }
jjg@546
  1150
            },
jjg@546
  1151
jjg@546
  1152
            new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
ksrini@2152
  1153
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1154
                    ClassSymbol c = (ClassSymbol) sym;
jjg@546
  1155
                    Name n = readName(nextChar());
jjg@546
  1156
                    c.sourcefile = new SourceFileObject(n, c.flatname);
ohrstrom@2227
  1157
                    // If the class is a toplevel class, originating from a Java source file,
ohrstrom@2227
  1158
                    // but the class name does not match the file name, then it is
ohrstrom@2227
  1159
                    // an auxiliary class.
ohrstrom@2227
  1160
                    String sn = n.toString();
emc@4248
  1161
                    if (c.owner.kind == PCK &&
ohrstrom@2227
  1162
                        sn.endsWith(".java") &&
ohrstrom@2227
  1163
                        !sn.equals(c.name.toString()+".java")) {
ohrstrom@2227
  1164
                        c.flags_field |= AUXILIARY;
ohrstrom@2227
  1165
                    }
jjg@546
  1166
                }
jjg@546
  1167
            },
jjg@546
  1168
jjg@546
  1169
            new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1170
                protected void read(Symbol sym, int attrLen) {
ntoda@4105
  1171
                    sym.flags_field |= SYNTHETIC;
jjg@546
  1172
                }
jjg@546
  1173
            },
jjg@546
  1174
jjg@546
  1175
            // standard v49 attributes
jjg@546
  1176
jjg@546
  1177
            new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
ksrini@2152
  1178
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1179
                    int newbp = bp + attrLen;
jjg@546
  1180
                    readEnclosingMethodAttr(sym);
jjg@546
  1181
                    bp = newbp;
jjg@546
  1182
                }
jjg@546
  1183
            },
jjg@546
  1184
jjg@546
  1185
            new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1186
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1187
                    if (sym.kind == TYP) {
jjg@546
  1188
                        ClassSymbol c = (ClassSymbol) sym;
jjg@546
  1189
                        readingClassAttr = true;
jjg@546
  1190
                        try {
jjg@546
  1191
                            ClassType ct1 = (ClassType)c.type;
jjg@1281
  1192
                            Assert.check(c == currentOwner);
jjg@546
  1193
                            ct1.typarams_field = readTypeParams(nextChar());
jjg@546
  1194
                            ct1.supertype_field = sigToType();
briangoetz@3808
  1195
                            ListBuffer<Type> is = new ListBuffer<>();
jjg@546
  1196
                            while (sigp != siglimit) is.append(sigToType());
jjg@546
  1197
                            ct1.interfaces_field = is.toList();
jjg@546
  1198
                        } finally {
jjg@546
  1199
                            readingClassAttr = false;
jjg@546
  1200
                        }
jjg@546
  1201
                    } else {
jjg@546
  1202
                        List<Type> thrown = sym.type.getThrownTypes();
jjg@546
  1203
                        sym.type = readType(nextChar());
jjg@546
  1204
                        //- System.err.println(" # " + sym.type);
jjg@546
  1205
                        if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
jjg@546
  1206
                            sym.type.asMethodType().thrown = thrown;
jjg@546
  1207
jjg@546
  1208
                    }
jjg@546
  1209
                }
jjg@546
  1210
            },
jjg@546
  1211
jjg@546
  1212
            // v49 annotation attributes
jjg@546
  1213
jjg@546
  1214
            new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1215
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1216
                    attachAnnotationDefault(sym);
jjg@546
  1217
                }
jjg@546
  1218
            },
jjg@546
  1219
jjg@546
  1220
            new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1221
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1222
                    attachAnnotations(sym);
jjg@546
  1223
                }
jjg@546
  1224
            },
jjg@546
  1225
jjg@546
  1226
            new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1227
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1228
                    attachParameterAnnotations(sym);
jjg@546
  1229
                }
jjg@546
  1230
            },
jjg@546
  1231
jjg@546
  1232
            new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1233
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1234
                    attachAnnotations(sym);
jjg@546
  1235
                }
jjg@546
  1236
            },
jjg@546
  1237
jjg@546
  1238
            new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1239
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1240
                    attachParameterAnnotations(sym);
jjg@546
  1241
                }
jjg@546
  1242
            },
jjg@546
  1243
jjg@546
  1244
            // additional "legacy" v49 attributes, superceded by flags
jjg@546
  1245
jjg@546
  1246
            new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1247
                protected void read(Symbol sym, int attrLen) {
ntoda@4105
  1248
                    sym.flags_field |= ANNOTATION;
jjg@546
  1249
                }
jjg@546
  1250
            },
jjg@546
  1251
jjg@546
  1252
            new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
ksrini@2152
  1253
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1254
                    sym.flags_field |= BRIDGE;
jjg@546
  1255
                }
jjg@546
  1256
            },
jjg@546
  1257
jjg@546
  1258
            new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1259
                protected void read(Symbol sym, int attrLen) {
jjg@546
  1260
                    sym.flags_field |= ENUM;
jjg@546
  1261
                }
jjg@546
  1262
            },
jjg@546
  1263
jjg@546
  1264
            new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
ksrini@2152
  1265
                protected void read(Symbol sym, int attrLen) {
ntoda@4105
  1266
                    sym.flags_field |= VARARGS;
jjg@546
  1267
                }
jjg@598
  1268
            },
jjg@598
  1269
jjg@2372
  1270
            new AttributeReader(names.RuntimeVisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@2372
  1271
                protected void read(Symbol sym, int attrLen) {
jjg@2372
  1272
                    attachTypeAnnotations(sym);
jjg@2372
  1273
                }
jjg@2372
  1274
            },
jjg@2372
  1275
jjg@2372
  1276
            new AttributeReader(names.RuntimeInvisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@2372
  1277
                protected void read(Symbol sym, int attrLen) {
jjg@2372
  1278
                    attachTypeAnnotations(sym);
jjg@2372
  1279
                }
jjg@2372
  1280
            },
jjg@2372
  1281
jjg@546
  1282
            // The following attributes for a Code attribute are not currently handled
jjg@546
  1283
            // StackMapTable
jjg@546
  1284
            // SourceDebugExtension
jjg@546
  1285
            // LineNumberTable
jjg@546
  1286
            // LocalVariableTypeTable
alanb@5016
  1287
alanb@5016
  1288
            // standard v52 attributes
alanb@5016
  1289
alanb@5016
  1290
            new AttributeReader(names.MethodParameters, V52, MEMBER_ATTRIBUTE) {
alanb@5016
  1291
                protected void read(Symbol sym, int attrlen) {
alanb@5016
  1292
                    int newbp = bp + attrlen;
alanb@5016
  1293
                    if (saveParameterNames) {
alanb@5016
  1294
                        sawMethodParameters = true;
alanb@5016
  1295
                        int numEntries = nextByte();
alanb@5016
  1296
                        parameterNameIndices = new int[numEntries];
alanb@5016
  1297
                        haveParameterNameIndices = true;
alanb@5016
  1298
                        for (int i = 0; i < numEntries; i++) {
alanb@5016
  1299
                            int nameIndex = nextChar();
alanb@5016
  1300
                            int flags = nextChar();
alanb@5016
  1301
                            parameterNameIndices[i] = nameIndex;
alanb@5016
  1302
                        }
alanb@5016
  1303
                    }
alanb@5016
  1304
                    bp = newbp;
alanb@5016
  1305
                }
alanb@5016
  1306
            },
alanb@5016
  1307
alanb@5016
  1308
            // standard v53 attributes
alanb@5016
  1309
alanb@5016
  1310
            new AttributeReader(names.Module, V53, CLASS_ATTRIBUTE) {
alanb@5016
  1311
                @Override
alanb@5016
  1312
                protected boolean accepts(AttributeKind kind) {
alanb@5016
  1313
                    return super.accepts(kind) && allowModules;
alanb@5016
  1314
                }
alanb@5016
  1315
                protected void read(Symbol sym, int attrLen) {
alanb@5016
  1316
                    if (sym.kind == TYP && sym.owner.kind == MDL) {
alanb@5016
  1317
                        ModuleSymbol msym = (ModuleSymbol) sym.owner;
alanb@5016
  1318
                        ListBuffer<Directive> directives = new ListBuffer<>();
alanb@5016
  1319
alanb@5580
  1320
                        Name moduleName = readModuleName(nextChar());
alanb@5580
  1321
                        if (currentModule.name != moduleName) {
alanb@5580
  1322
                            throw badClassFile("module.name.mismatch", moduleName, currentModule.name);
alanb@5550
  1323
                        }
alanb@5550
  1324
alanb@5550
  1325
                        msym.flags.addAll(readModuleFlags(nextChar()));
alanb@5580
  1326
                        msym.version = readName(nextChar());
alanb@5550
  1327
alanb@5016
  1328
                        ListBuffer<RequiresDirective> requires = new ListBuffer<>();
alanb@5016
  1329
                        int nrequires = nextChar();
alanb@5016
  1330
                        for (int i = 0; i < nrequires; i++) {
alanb@5550
  1331
                            ModuleSymbol rsym = syms.enterModule(readModuleName(nextChar()));
alanb@5016
  1332
                            Set<RequiresFlag> flags = readRequiresFlags(nextChar());
alanb@5580
  1333
                            nextChar(); // skip compiled version
alanb@5016
  1334
                            requires.add(new RequiresDirective(rsym, flags));
alanb@5016
  1335
                        }
alanb@5016
  1336
                        msym.requires = requires.toList();
alanb@5016
  1337
                        directives.addAll(msym.requires);
alanb@5016
  1338
alanb@5016
  1339
                        ListBuffer<ExportsDirective> exports = new ListBuffer<>();
alanb@5016
  1340
                        int nexports = nextChar();
alanb@5016
  1341
                        for (int i = 0; i < nexports; i++) {
alanb@5016
  1342
                            Name n = readName(nextChar());
alanb@5016
  1343
                            PackageSymbol p = syms.enterPackage(currentModule, names.fromUtf(internalize(n)));
alanb@5550
  1344
                            Set<ExportsFlag> flags = readExportsFlags(nextChar());
alanb@5016
  1345
                            int nto = nextChar();
alanb@5016
  1346
                            List<ModuleSymbol> to;
alanb@5016
  1347
                            if (nto == 0) {
alanb@5016
  1348
                                to = null;
alanb@5016
  1349
                            } else {
alanb@5016
  1350
                                ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
alanb@5016
  1351
                                for (int t = 0; t < nto; t++)
alanb@5550
  1352
                                    lb.append(syms.enterModule(readModuleName(nextChar())));
alanb@5016
  1353
                                to = lb.toList();
alanb@5016
  1354
                            }
alanb@5550
  1355
                            exports.add(new ExportsDirective(p, to, flags));
alanb@5016
  1356
                        }
alanb@5016
  1357
                        msym.exports = exports.toList();
alanb@5016
  1358
                        directives.addAll(msym.exports);
alanb@5550
  1359
                        ListBuffer<OpensDirective> opens = new ListBuffer<>();
alanb@5550
  1360
                        int nopens = nextChar();
alanb@5550
  1361
                        if (nopens != 0 && msym.flags.contains(ModuleFlags.OPEN)) {
alanb@5550
  1362
                            throw badClassFile("module.non.zero.opens", currentModule.name);
alanb@5550
  1363
                        }
alanb@5550
  1364
                        for (int i = 0; i < nopens; i++) {
alanb@5550
  1365
                            Name n = readName(nextChar());
alanb@5550
  1366
                            PackageSymbol p = syms.enterPackage(currentModule, names.fromUtf(internalize(n)));
alanb@5550
  1367
                            Set<OpensFlag> flags = readOpensFlags(nextChar());
alanb@5550
  1368
                            int nto = nextChar();
alanb@5550
  1369
                            List<ModuleSymbol> to;
alanb@5550
  1370
                            if (nto == 0) {
alanb@5550
  1371
                                to = null;
alanb@5550
  1372
                            } else {
alanb@5550
  1373
                                ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
alanb@5550
  1374
                                for (int t = 0; t < nto; t++)
alanb@5550
  1375
                                    lb.append(syms.enterModule(readModuleName(nextChar())));
alanb@5550
  1376
                                to = lb.toList();
alanb@5550
  1377
                            }
alanb@5550
  1378
                            opens.add(new OpensDirective(p, to, flags));
alanb@5550
  1379
                        }
alanb@5550
  1380
                        msym.opens = opens.toList();
alanb@5550
  1381
                        directives.addAll(msym.opens);
alanb@5016
  1382
alanb@5016
  1383
                        msym.directives = directives.toList();
alanb@5016
  1384
alanb@5016
  1385
                        ListBuffer<InterimUsesDirective> uses = new ListBuffer<>();
alanb@5016
  1386
                        int nuses = nextChar();
alanb@5016
  1387
                        for (int i = 0; i < nuses; i++) {
alanb@5016
  1388
                            Name srvc = readClassName(nextChar());
alanb@5016
  1389
                            uses.add(new InterimUsesDirective(srvc));
alanb@5016
  1390
                        }
alanb@5016
  1391
                        interimUses = uses.toList();
alanb@5016
  1392
alanb@5016
  1393
                        ListBuffer<InterimProvidesDirective> provides = new ListBuffer<>();
alanb@5016
  1394
                        int nprovides = nextChar();
alanb@5550
  1395
                        for (int p = 0; p < nprovides; p++) {
alanb@5016
  1396
                            Name srvc = readClassName(nextChar());
alanb@5550
  1397
                            int nimpls = nextChar();
alanb@5550
  1398
                            ListBuffer<Name> impls = new ListBuffer<>();
alanb@5550
  1399
                            for (int i = 0; i < nimpls; i++) {
alanb@5550
  1400
                                impls.append(readClassName(nextChar()));
alanb@5550
  1401
                            provides.add(new InterimProvidesDirective(srvc, impls.toList()));
alanb@5550
  1402
                            }
alanb@5016
  1403
                        }
alanb@5016
  1404
                        interimProvides = provides.toList();
alanb@5016
  1405
                    }
alanb@5016
  1406
                }
alanb@5016
  1407
            },
alanb@5016
  1408
alanb@5580
  1409
            new AttributeReader(names.ModuleResolution, V53, CLASS_ATTRIBUTE) {
alanb@5016
  1410
                @Override
alanb@5016
  1411
                protected boolean accepts(AttributeKind kind) {
alanb@5016
  1412
                    return super.accepts(kind) && allowModules;
alanb@5016
  1413
                }
alanb@5016
  1414
                protected void read(Symbol sym, int attrLen) {
alanb@5016
  1415
                    if (sym.kind == TYP && sym.owner.kind == MDL) {
alanb@5016
  1416
                        ModuleSymbol msym = (ModuleSymbol) sym.owner;
alanb@5580
  1417
                        msym.resolutionFlags.addAll(readModuleResolutionFlags(nextChar()));
alanb@5016
  1418
                    }
alanb@5016
  1419
                }
alanb@5016
  1420
            },
jjg@546
  1421
        };
jjg@546
  1422
jjg@546
  1423
        for (AttributeReader r: readers)
jjg@546
  1424
            attributeReaders.put(r.name, r);
jjg@546
  1425
    }
jjg@546
  1426
ksrini@2152
  1427
    protected void readEnclosingMethodAttr(Symbol sym) {
duke@0
  1428
        // sym is a nested class with an "Enclosing Method" attribute
duke@0
  1429
        // remove sym from it's current owners scope and place it in
duke@0
  1430
        // the scope specified by the attribute
dbalek@5950
  1431
        if (sym.owner.members() != null) {
dbalek@5950
  1432
            sym.owner.members().remove(sym);
dbalek@5950
  1433
        }
duke@0
  1434
        ClassSymbol self = (ClassSymbol)sym;
duke@0
  1435
        ClassSymbol c = readClassSymbol(nextChar());
pgovereau@3953
  1436
        NameAndType nt = readNameAndType(nextChar());
duke@0
  1437
dbalek@5955
  1438
        if (c.members_field == null || c.kind != TYP)
jjg@1439
  1439
            throw badClassFile("bad.enclosing.class", self, c);
jjg@1439
  1440
duke@0
  1441
        MethodSymbol m = findMethod(nt, c.members_field, self.flags());
duke@0
  1442
        if (nt != null && m == null)
vromero@5051
  1443
            throw badEnclosingMethod(self);
duke@0
  1444
duke@0
  1445
        self.name = simpleBinaryName(self.flatname, c.flatname) ;
duke@0
  1446
        self.owner = m != null ? m : c;
jjg@294
  1447
        if (self.name.isEmpty())
darcy@908
  1448
            self.fullname = names.empty;
duke@0
  1449
        else
duke@0
  1450
            self.fullname = ClassSymbol.formFullName(self.name, self.owner);
duke@0
  1451
dbalek@1869
  1452
        if (c.classfile != null && c.classfile.getKind() == JavaFileObject.Kind.SOURCE)
dbalek@1779
  1453
            throw new Abort();
dbalek@1779
  1454
duke@0
  1455
        if (m != null) {
duke@0
  1456
            ((ClassType)sym.type).setEnclosingType(m.type);
duke@0
  1457
        } else if ((self.flags_field & STATIC) == 0) {
duke@0
  1458
            ((ClassType)sym.type).setEnclosingType(c.type);
duke@0
  1459
        } else {
duke@0
  1460
            ((ClassType)sym.type).setEnclosingType(Type.noType);
duke@0
  1461
        }
jlahoda@5502
  1462
        enterTypevars(self, self.type);
duke@0
  1463
        if (!missingTypeVariables.isEmpty()) {
briangoetz@3808
  1464
            ListBuffer<Type> typeVars =  new ListBuffer<>();
duke@0
  1465
            for (Type typevar : missingTypeVariables) {
duke@0
  1466
                typeVars.append(findTypeVar(typevar.tsym.name));
duke@0
  1467
            }
duke@0
  1468
            foundTypeVariables = typeVars.toList();
duke@0
  1469
        } else {
duke@0
  1470
            foundTypeVariables = List.nil();
duke@0
  1471
        }
duke@0
  1472
    }
duke@0
  1473
duke@0
  1474
    // See java.lang.Class
jlahoda@5
  1475
    protected Name simpleBinaryName(Name self, Name enclosing) {
dbalek@172
  1476
        String selfStr = self.toString();
dbalek@172
  1477
        String enclStr = enclosing.toString();
dbalek@172
  1478
        if (selfStr.length() <= enclStr.length())
dbalek@172
  1479
            throw badClassFile("bad.enclosing.method", self);
dbalek@172
  1480
        String simpleBinaryName = selfStr.substring(enclStr.length());
duke@0
  1481
        if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
duke@0
  1482
            throw badClassFile("bad.enclosing.method", self);
duke@0
  1483
        int index = 1;
duke@0
  1484
        while (index < simpleBinaryName.length() &&
duke@0
  1485
               isAsciiDigit(simpleBinaryName.charAt(index)))
duke@0
  1486
            index++;
duke@0
  1487
        return names.fromString(simpleBinaryName.substring(index));
duke@0
  1488
    }
duke@0
  1489
duke@0
  1490
    private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
duke@0
  1491
        if (nt == null)
duke@0
  1492
            return null;
duke@0
  1493
vromero@2290
  1494
        MethodType type = nt.uniqueType.type.asMethodType();
duke@0
  1495
jlahoda@4103
  1496
        for (Symbol sym : scope.getSymbolsByName(nt.name)) {
jlahoda@4103
  1497
            if (sym.kind == MTH && isSameBinaryType(sym.type.asMethodType(), type))
jlahoda@4103
  1498
                return (MethodSymbol)sym;
jlahoda@4103
  1499
        }
duke@0
  1500
duke@0
  1501
        if (nt.name != names.init)
duke@0
  1502
            // not a constructor
duke@0
  1503
            return null;
duke@0
  1504
        if ((flags & INTERFACE) != 0)
duke@0
  1505
            // no enclosing instance
duke@0
  1506
            return null;
vromero@2290
  1507
        if (nt.uniqueType.type.getParameterTypes().isEmpty())
duke@0
  1508
            // no parameters
duke@0
  1509
            return null;
duke@0
  1510
duke@0
  1511
        // A constructor of an inner class.
duke@0
  1512
        // Remove the first argument (the enclosing instance)
vromero@2290
  1513
        nt.setType(new MethodType(nt.uniqueType.type.getParameterTypes().tail,
vromero@2290
  1514
                                 nt.uniqueType.type.getReturnType(),
vromero@2290
  1515
                                 nt.uniqueType.type.getThrownTypes(),
vromero@2290
  1516
                                 syms.methodClass));
duke@0
  1517
        // Try searching again
duke@0
  1518
        return findMethod(nt, scope, flags);
duke@0
  1519
    }
duke@0
  1520
duke@0
  1521
    /** Similar to Types.isSameType but avoids completion */
jlahoda@5
  1522
    protected boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
duke@0
  1523
        List<Type> types1 = types.erasure(mt1.getParameterTypes())
duke@0
  1524
            .prepend(types.erasure(mt1.getReturnType()));
duke@0
  1525
        List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
duke@0
  1526
        while (!types1.isEmpty() && !types2.isEmpty()) {
duke@0
  1527
            if (types1.head.tsym != types2.head.tsym)
duke@0
  1528
                return false;
duke@0
  1529
            types1 = types1.tail;
duke@0
  1530
            types2 = types2.tail;
duke@0
  1531
        }
duke@0
  1532
        return types1.isEmpty() && types2.isEmpty();
duke@0
  1533
    }
duke@0
  1534
duke@0
  1535
    /**
duke@0
  1536
     * Character.isDigit answers <tt>true</tt> to some non-ascii
duke@0
  1537
     * digits.  This one does not.  <b>copied from java.lang.Class</b>
duke@0
  1538
     */
duke@0
  1539
    private static boolean isAsciiDigit(char c) {
duke@0
  1540
        return '0' <= c && c <= '9';
duke@0
  1541
    }
duke@0
  1542
duke@0
  1543
    /** Read member attributes.
duke@0
  1544
     */
duke@0
  1545
    void readMemberAttrs(Symbol sym) {
jjg@546
  1546
        readAttrs(sym, AttributeKind.MEMBER);
jjg@546
  1547
    }
jjg@546
  1548
jjg@546
  1549
    void readAttrs(Symbol sym, AttributeKind kind) {
duke@0
  1550
        char ac = nextChar();
duke@0
  1551
        for (int i = 0; i < ac; i++) {
duke@0
  1552
            Name attrName = readName(nextChar());
duke@0
  1553
            int attrLen = nextInt();
jjg@546
  1554
            AttributeReader r = attributeReaders.get(attrName);
jjg@546
  1555
            if (r != null && r.accepts(kind))
jjg@546
  1556
                r.read(sym, attrLen);
jjg@546
  1557
            else  {
jjg@546
  1558
                bp = bp + attrLen;
jjg@546
  1559
            }
duke@0
  1560
        }
duke@0
  1561
    }
duke@0
  1562
duke@0
  1563
    private boolean readingClassAttr = false;
duke@0
  1564
    private List<Type> missingTypeVariables = List.nil();
duke@0
  1565
    private List<Type> foundTypeVariables = List.nil();
duke@0
  1566
duke@0
  1567
    /** Read class attributes.
duke@0
  1568
     */
duke@0
  1569
    void readClassAttrs(ClassSymbol c) {
jjg@546
  1570
        readAttrs(c, AttributeKind.CLASS);
duke@0
  1571
    }
duke@0
  1572
duke@0
  1573
    /** Read code block.
duke@0
  1574
     */
duke@0
  1575
    Code readCode(Symbol owner) {
duke@0
  1576
        nextChar(); // max_stack
duke@0
  1577
        nextChar(); // max_locals
duke@0
  1578
        final int  code_length = nextInt();
duke@0
  1579
        bp += code_length;
duke@0
  1580
        final char exception_table_length = nextChar();
duke@0
  1581
        bp += exception_table_length * 8;
duke@0
  1582
        readMemberAttrs(owner);
duke@0
  1583
        return null;
duke@0
  1584
    }
duke@0
  1585
duke@0
  1586
/************************************************************************
duke@0
  1587
 * Reading Java-language annotations
duke@0
  1588
 ***********************************************************************/
duke@0
  1589
duke@0
  1590
    /** Attach annotations.
duke@0
  1591
     */
dbalek@2400
  1592
    protected void attachAnnotations(final Symbol sym) {
duke@0
  1593
        int numAttributes = nextChar();
duke@0
  1594
        if (numAttributes != 0) {
briangoetz@3808
  1595
            ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
duke@0
  1596
            for (int i = 0; i<numAttributes; i++) {
duke@0
  1597
                CompoundAnnotationProxy proxy = readCompoundAnnotation();
jlahoda@4550
  1598
                if (proxy.type.tsym == syms.proprietaryType.tsym)
jlahoda@4550
  1599
                    sym.flags_field |= PROPRIETARY;
jlahoda@4550
  1600
                else if (proxy.type.tsym == syms.profileType.tsym) {
jlahoda@4550
  1601
                    if (profile != Profile.DEFAULT) {
jlahoda@4550
  1602
                        for (Pair<Name,Attribute> v: proxy.values) {
jlahoda@4550
  1603
                            if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
jlahoda@4550
  1604
                                Attribute.Constant c = (Attribute.Constant) v.snd;
jlahoda@4550
  1605
                                if (c.type == syms.intType && ((Integer) c.value) > profile.value) {
jlahoda@4550
  1606
                                    sym.flags_field |= NOT_IN_PROFILE;
jlahoda@4550
  1607
                                }
jlahoda@4550
  1608
                            }
jlahoda@4550
  1609
                        }
jlahoda@4550
  1610
                    }
jlahoda@4550
  1611
                } else {
jlahoda@4550
  1612
                    if (proxy.type.tsym == syms.annotationTargetType.tsym) {
jlahoda@4550
  1613
                        target = proxy;
jlahoda@4550
  1614
                    } else if (proxy.type.tsym == syms.repeatableType.tsym) {
jlahoda@4550
  1615
                        repeatable = proxy;
jjg@5468
  1616
                    } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
alanb@5550
  1617
                        sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
jjg@5468
  1618
                        for (Pair<Name, Attribute> v : proxy.values) {
jjg@5468
  1619
                            if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
jjg@5468
  1620
                                Attribute.Constant c = (Attribute.Constant) v.snd;
jjg@5468
  1621
                                if (c.type == syms.booleanType && ((Integer) c.value) != 0) {
jjg@5468
  1622
                                    sym.flags_field |= DEPRECATED_REMOVAL;
jjg@5468
  1623
                                }
jjg@5468
  1624
                            }
jjg@5468
  1625
                        }
jlahoda@4550
  1626
                    }
jlahoda@4550
  1627
jlahoda@4550
  1628
                    proxies.append(proxy);
jjg@4454
  1629
                }
duke@0
  1630
            }
jfranck@2156
  1631
            annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
duke@0
  1632
        }
duke@0
  1633
    }
duke@0
  1634
duke@0
  1635
    /** Attach parameter annotations.
duke@0
  1636
     */
dbalek@2400
  1637
    protected void attachParameterAnnotations(final Symbol method) {
duke@0
  1638
        final MethodSymbol meth = (MethodSymbol)method;
duke@0
  1639
        int numParameters = buf[bp++] & 0xFF;
duke@0
  1640
        List<VarSymbol> parameters = meth.params();
duke@0
  1641
        int pnum = 0;
duke@0
  1642
        while (parameters.tail != null) {
duke@0
  1643
            attachAnnotations(parameters.head);
duke@0
  1644
            parameters = parameters.tail;
duke@0
  1645
            pnum++;
duke@0
  1646
        }
duke@0
  1647
        if (pnum != numParameters) {
duke@0
  1648
            throw badClassFile("bad.runtime.invisible.param.annotations", meth);
duke@0
  1649
        }
duke@0
  1650
    }
duke@0
  1651
dbalek@2402
  1652
    protected void attachTypeAnnotations(final Symbol sym) {
jjg@2372
  1653
        int numAttributes = nextChar();
jjg@2372
  1654
        if (numAttributes != 0) {
alundblad@3063
  1655
            ListBuffer<TypeAnnotationProxy> proxies = new ListBuffer<>();
jjg@2372
  1656
            for (int i = 0; i < numAttributes; i++)
jjg@2372
  1657
                proxies.append(readTypeAnnotation());
jjg@2372
  1658
            annotate.normal(new TypeAnnotationCompleter(sym, proxies.toList()));
jjg@2372
  1659
        }
jjg@2372
  1660
    }
jjg@2372
  1661
duke@0
  1662
    /** Attach the default value for an annotation element.
duke@0
  1663
     */
duke@0
  1664
    void attachAnnotationDefault(final Symbol sym) {
duke@0
  1665
        final MethodSymbol meth = (MethodSymbol)sym; // only on methods
duke@0
  1666
        final Attribute value = readAttributeValue();
jfranck@2288
  1667
jfranck@2288
  1668
        // The default value is set later during annotation. It might
jfranck@2288
  1669
        // be the case that the Symbol sym is annotated _after_ the
jfranck@2288
  1670
        // repeating instances that depend on this default value,
jfranck@2288
  1671
        // because of this we set an interim value that tells us this
jfranck@2288
  1672
        // element (most likely) has a default.
jfranck@2288
  1673
        //
jfranck@2288
  1674
        // Set interim value for now, reset just before we do this
jfranck@2288
  1675
        // properly at annotate time.
jfranck@2288
  1676
        meth.defaultValue = value;
jfranck@2156
  1677
        annotate.normal(new AnnotationDefaultCompleter(meth, value));
duke@0
  1678
    }
duke@0
  1679
duke@0
  1680
    Type readTypeOrClassSymbol(int i) {
duke@0
  1681
        // support preliminary jsr175-format class files
duke@0
  1682
        if (buf[poolIdx[i]] == CONSTANT_Class)
duke@0
  1683
            return readClassSymbol(i).type;
alanb@5550
  1684
        return readTypeToProxy(i);
duke@0
  1685
    }
duke@0
  1686
    Type readEnumType(int i) {
duke@0
  1687
        // support preliminary jsr175-format class files
duke@0
  1688
        int index = poolIdx[i];
duke@0
  1689
        int length = getChar(index + 1);
duke@0
  1690
        if (buf[index + length + 2] != ';')
alanb@5016
  1691
            return enterClass(readName(i)).type;
alanb@5550
  1692
        return readTypeToProxy(i);
alanb@5550
  1693
    }
alanb@5550
  1694
    Type readTypeToProxy(int i) {
alanb@5550
  1695
        if (currentModule.module_info == currentOwner) {
alanb@5550
  1696
            int index = poolIdx[i];
alanb@5550
  1697
            return new ProxyType(Arrays.copyOfRange(buf, index + 3, index + 3 + getChar(index + 1)));
alanb@5550
  1698
        } else {
alanb@5550
  1699
            return readType(i);
alanb@5550
  1700
        }
duke@0
  1701
    }
duke@0
  1702
duke@0
  1703
    CompoundAnnotationProxy readCompoundAnnotation() {
alanb@5550
  1704
        Type t;
alanb@5550
  1705
        if (currentModule.module_info == currentOwner) {
alanb@5550
  1706
            int index = poolIdx[nextChar()];
alanb@5550
  1707
            t = new ProxyType(Arrays.copyOfRange(buf, index + 3, index + 3 + getChar(index + 1)));
alanb@5550
  1708
        } else {
alanb@5550
  1709
            t = readTypeOrClassSymbol(nextChar());
alanb@5550
  1710
        }
duke@0
  1711
        int numFields = nextChar();
briangoetz@3808
  1712
        ListBuffer<Pair<Name,Attribute>> pairs = new ListBuffer<>();
duke@0
  1713
        for (int i=0; i<numFields; i++) {
duke@0
  1714
            Name name = readName(nextChar());
duke@0
  1715
            Attribute value = readAttributeValue();
briangoetz@3808
  1716
            pairs.append(new Pair<>(name, value));
duke@0
  1717
        }
duke@0
  1718
        return new CompoundAnnotationProxy(t, pairs.toList());
duke@0
  1719
    }
duke@0
  1720
jjg@2372
  1721
    TypeAnnotationProxy readTypeAnnotation() {
jjg@2372
  1722
        TypeAnnotationPosition position = readPosition();
jjg@2372
  1723
        CompoundAnnotationProxy proxy = readCompoundAnnotation();
jjg@2372
  1724
jjg@2372
  1725
        return new TypeAnnotationProxy(proxy, position);
jjg@2372
  1726
    }
jjg@2372
  1727
jjg@2372
  1728
    TypeAnnotationPosition readPosition() {
jjg@2372
  1729
        int tag = nextByte(); // TargetType tag is a byte
jjg@2372
  1730
jjg@2372
  1731
        if (!TargetType.isValidTargetTypeValue(tag))
jjg@4002
  1732
            throw badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
jjg@2372
  1733
jjg@2372
  1734
        TargetType type = TargetType.fromTargetTypeValue(tag);
jjg@2372
  1735
jjg@2372
  1736
        switch (type) {
jjg@2372
  1737
        // instanceof
emc@3853
  1738
        case INSTANCEOF: {
emc@3853
  1739
            final int offset = nextChar();
emc@3853
  1740
            final TypeAnnotationPosition position =
emc@3853
  1741
                TypeAnnotationPosition.instanceOf(readTypePath());
emc@3853
  1742
            position.offset = offset;
emc@3853
  1743
            return position;
emc@3853
  1744
        }
jjg@2372
  1745
        // new expression
emc@3853
  1746
        case NEW: {
emc@3853
  1747
            final int offset = nextChar();
emc@3853
  1748
            final TypeAnnotationPosition position =
emc@3853
  1749
                TypeAnnotationPosition.newObj(readTypePath());
emc@3853
  1750
            position.offset = offset;
emc@3853
  1751
            return position;
emc@3853
  1752
        }
jjg@2444
  1753
        // constructor/method reference receiver
emc@3853
  1754
        case CONSTRUCTOR_REFERENCE: {
emc@3853
  1755
            final int offset = nextChar();
emc@3853
  1756
            final TypeAnnotationPosition position =
emc@3853
  1757
                TypeAnnotationPosition.constructorRef(readTypePath());
emc@3853
  1758
            position.offset = offset;
emc@3853
  1759
            return position;
emc@3853
  1760
        }
emc@3853
  1761
        case METHOD_REFERENCE: {
emc@3853
  1762
            final int offset = nextChar();
emc@3853
  1763
            final TypeAnnotationPosition position =
emc@3853
  1764
                TypeAnnotationPosition.methodRef(readTypePath());
emc@3853
  1765
            position.offset = offset;
emc@3853
  1766
            return position;
emc@3853
  1767
        }
jjg@2372
  1768
        // local variable
emc@3853
  1769
        case LOCAL_VARIABLE: {
emc@3853
  1770
            final int table_length = nextChar();
emc@3995
  1771
            final int[] newLvarOffset = new int[table_length];
emc@3995
  1772
            final int[] newLvarLength = new int[table_length];
emc@3995
  1773
            final int[] newLvarIndex = new int[table_length];
jjg@2372
  1774
jjg@2372
  1775
            for (int i = 0; i < table_length; ++i) {
emc@3995
  1776
                newLvarOffset[i] = nextChar();
emc@3995
  1777
                newLvarLength[i] = nextChar();
emc@3995
  1778
                newLvarIndex[i] = nextChar();
jjg@2372
  1779
            }
emc@3995
  1780
emc@3995
  1781
            final TypeAnnotationPosition position =
emc@3995
  1782
                    TypeAnnotationPosition.localVariable(readTypePath());
emc@3995
  1783
            position.lvarOffset = newLvarOffset;
emc@3995
  1784
            position.lvarLength = newLvarLength;
emc@3995
  1785
            position.lvarIndex = newLvarIndex;
emc@3853
  1786
            return position;
emc@3853
  1787
        }
emc@3853
  1788
        // resource variable
emc@3853
  1789
        case RESOURCE_VARIABLE: {
emc@3853
  1790
            final int table_length = nextChar();
emc@3995
  1791
            final int[] newLvarOffset = new int[table_length];
emc@3995
  1792
            final int[] newLvarLength = new int[table_length];
emc@3995
  1793
            final int[] newLvarIndex = new int[table_length];
emc@3853
  1794
emc@3853
  1795
            for (int i = 0; i < table_length; ++i) {
emc@3995
  1796
                newLvarOffset[i] = nextChar();
emc@3995
  1797
                newLvarLength[i] = nextChar();
emc@3995
  1798
                newLvarIndex[i] = nextChar();
emc@3853
  1799
            }
emc@3995
  1800
emc@3995
  1801
            final TypeAnnotationPosition position =
emc@3995
  1802
                    TypeAnnotationPosition.resourceVariable(readTypePath());
emc@3995
  1803
            position.lvarOffset = newLvarOffset;
emc@3995
  1804
            position.lvarLength = newLvarLength;
emc@3995
  1805
            position.lvarIndex = newLvarIndex;
emc@3853
  1806
            return position;
emc@3853
  1807
        }
jjg@2372
  1808
        // exception parameter
emc@3853
  1809
        case EXCEPTION_PARAMETER: {
emc@3853
  1810
            final int exception_index = nextChar();
emc@3853
  1811
            final TypeAnnotationPosition position =
emc@3853
  1812
                TypeAnnotationPosition.exceptionParameter(readTypePath());
emc@3944
  1813
            position.setExceptionIndex(exception_index);
emc@3853
  1814
            return position;
emc@3853
  1815
        }
jjg@2372
  1816
        // method receiver
jjg@2372
  1817
        case METHOD_RECEIVER:
emc@3853
  1818
            return TypeAnnotationPosition.methodReceiver(readTypePath());
jjg@2372
  1819
        // type parameter
emc@3853
  1820
        case CLASS_TYPE_PARAMETER: {
emc@3853
  1821
            final int parameter_index = nextByte();
emc@3853
  1822
            return TypeAnnotationPosition
emc@3853
  1823
                .typeParameter(readTypePath(), parameter_index);
emc@3853
  1824
        }
emc@3853
  1825
        case METHOD_TYPE_PARAMETER: {
emc@3853
  1826
            final int parameter_index = nextByte();
emc@3853
  1827
            return TypeAnnotationPosition
emc@3853
  1828
                .methodTypeParameter(readTypePath(), parameter_index);
emc@3853
  1829
        }
jjg@2372
  1830
        // type parameter bound
emc@3853
  1831
        case CLASS_TYPE_PARAMETER_BOUND: {
emc@3853
  1832
            final int parameter_index = nextByte();
emc@3853
  1833
            final int bound_index = nextByte();
emc@3853
  1834
            return TypeAnnotationPosition
emc@3853
  1835
                .typeParameterBound(readTypePath(), parameter_index,
emc@3853
  1836
                                    bound_index);
emc@3853
  1837
        }
emc@3853
  1838
        case METHOD_TYPE_PARAMETER_BOUND: {
emc@3853
  1839
            final int parameter_index = nextByte();
emc@3853
  1840
            final int bound_index = nextByte();
emc@3853
  1841
            return TypeAnnotationPosition
emc@3853
  1842
                .methodTypeParameterBound(readTypePath(), parameter_index,
emc@3853
  1843
                                          bound_index);
emc@3853
  1844
        }
jjg@2372
  1845
        // class extends or implements clause
emc@3853
  1846
        case CLASS_EXTENDS: {
emc@3853
  1847
            final int type_index = nextChar();
emc@3853
  1848
            return TypeAnnotationPosition.classExtends(readTypePath(),
emc@3853
  1849
                                                       type_index);
emc@3853
  1850
        }
jjg@2372
  1851
        // throws
emc@3853
  1852
        case THROWS: {
emc@3853
  1853
            final int type_index = nextChar();
emc@3853
  1854
            return TypeAnnotationPosition.methodThrows(readTypePath(),
emc@3853
  1855
                                                       type_index);
emc@3853
  1856
        }
jjg@2372
  1857
        // method parameter
emc@3853
  1858
        case METHOD_FORMAL_PARAMETER: {
emc@3853
  1859
            final int parameter_index = nextByte();
emc@3853
  1860
            return TypeAnnotationPosition.methodParameter(readTypePath(),
emc@3853
  1861
                                                          parameter_index);
emc@3853
  1862
        }
jjg@2444
  1863
        // type cast
emc@3853
  1864
        case CAST: {
emc@3853
  1865
            final int offset = nextChar();
emc@3853
  1866
            final int type_index = nextByte();
emc@3853
  1867
            final TypeAnnotationPosition position =
emc@3853
  1868
                TypeAnnotationPosition.typeCast(readTypePath(), type_index);
emc@3853
  1869
            position.offset = offset;
emc@3853
  1870
            return position;
emc@3853
  1871
        }
jjg@2372
  1872
        // method/constructor/reference type argument
emc@3853
  1873
        case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: {
emc@3853
  1874
            final int offset = nextChar();
emc@3853
  1875
            final int type_index = nextByte();
emc@3853
  1876
            final TypeAnnotationPosition position = TypeAnnotationPosition
emc@3853
  1877
                .constructorInvocationTypeArg(readTypePath(), type_index);
emc@3853
  1878
            position.offset = offset;
emc@3853
  1879
            return position;
emc@3853
  1880
        }
emc@3853
  1881
        case METHOD_INVOCATION_TYPE_ARGUMENT: {
emc@3853
  1882
            final int offset = nextChar();
emc@3853
  1883
            final int type_index = nextByte();
emc@3853
  1884
            final TypeAnnotationPosition position = TypeAnnotationPosition
emc@3853
  1885
                .methodInvocationTypeArg(readTypePath(), type_index);
emc@3853
  1886
            position.offset = offset;
emc@3853
  1887
            return position;
emc@3853
  1888
        }
emc@3853
  1889
        case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: {
emc@3853
  1890
            final int offset = nextChar();
emc@3853
  1891
            final int type_index = nextByte();
emc@3853
  1892
            final TypeAnnotationPosition position = TypeAnnotationPosition
emc@3853
  1893
                .constructorRefTypeArg(readTypePath(), type_index);
emc@3853
  1894
            position.offset = offset;
emc@3853
  1895
            return position;
emc@3853
  1896
        }
emc@3853
  1897
        case METHOD_REFERENCE_TYPE_ARGUMENT: {
emc@3853
  1898
            final int offset = nextChar();
emc@3853
  1899
            final int type_index = nextByte();
emc@3853
  1900
            final TypeAnnotationPosition position = TypeAnnotationPosition
emc@3853
  1901
                .methodRefTypeArg(readTypePath(), type_index);
emc@3853
  1902
            position.offset = offset;
emc@3853
  1903
            return position;
emc@3853
  1904
        }
jjg@2372
  1905
        // We don't need to worry about these
jjg@2372
  1906
        case METHOD_RETURN:
emc@3853
  1907
            return TypeAnnotationPosition.methodReturn(readTypePath());
jjg@2372
  1908
        case FIELD:
emc@3853
  1909
            return TypeAnnotationPosition.field(readTypePath());
jfranck@4203
  1910
        case UNKNOWN:
jfranck@4203
  1911
            throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
jjg@2372
  1912
        default:
emc@3853
  1913
            throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + type);
jjg@2372
  1914
        }
emc@3853
  1915
    }
jjg@2372
  1916
emc@3853
  1917
    List<TypeAnnotationPosition.TypePathEntry> readTypePath() {
emc@3853
  1918
        int len = nextByte();
emc@3853
  1919
        ListBuffer<Integer> loc = new ListBuffer<>();
emc@3853
  1920
        for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
emc@3853
  1921
            loc = loc.append(nextByte());
jjg@2372
  1922
emc@3853
  1923
        return TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
emc@3853
  1924
jjg@2372
  1925
    }
jjg@2372
  1926
duke@0
  1927
    Attribute readAttributeValue() {
duke@0
  1928
        char c = (char) buf[bp++];
duke@0
  1929
        switch (c) {
duke@0
  1930
        case 'B':
duke@0
  1931
            return new Attribute.Constant(syms.byteType, readPool(nextChar()));
duke@0
  1932
        case 'C':
duke@0
  1933
            return new Attribute.Constant(syms.charType, readPool(nextChar()));
duke@0
  1934
        case 'D':
duke@0
  1935
            return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
duke@0
  1936
        case 'F':
duke@0
  1937
            return new Attribute.Constant(syms.floatType, readPool(nextChar()));
duke@0
  1938
        case 'I':
duke@0
  1939
            return new Attribute.Constant(syms.intType, readPool(nextChar()));
duke@0
  1940
        case 'J':
duke@0
  1941
            return new Attribute.Constant(syms.longType, readPool(nextChar()));
duke@0
  1942
        case 'S':
duke@0
  1943
            return new Attribute.Constant(syms.shortType, readPool(nextChar()));
duke@0
  1944
        case 'Z':
duke@0
  1945
            return new Attribute.Constant(syms.booleanType, readPool(nextChar()));
duke@0
  1946
        case 's':
duke@0
  1947
            return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString());
duke@0
  1948
        case 'e':
duke@0
  1949
            return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar()));
duke@0
  1950
        case 'c':
alanb@5550
  1951
            return new ClassAttributeProxy(readTypeOrClassSymbol(nextChar()));
duke@0
  1952
        case '[': {
duke@0
  1953
            int n = nextChar();
briangoetz@3808
  1954
            ListBuffer<Attribute> l = new ListBuffer<>();
duke@0
  1955
            for (int i=0; i<n; i++)
duke@0
  1956
                l.append(readAttributeValue());
duke@0
  1957
            return new ArrayAttributeProxy(l.toList());
duke@0
  1958
        }
duke@0
  1959
        case '@':
duke@0
  1960
            return readCompoundAnnotation();
duke@0
  1961
        default:
duke@0
  1962
            throw new AssertionError("unknown annotation tag '" + c + "'");
duke@0
  1963
        }
duke@0
  1964
    }
duke@0
  1965
duke@0
  1966
    interface ProxyVisitor extends Attribute.Visitor {
duke@0
  1967
        void visitEnumAttributeProxy(EnumAttributeProxy proxy);
alanb@5550
  1968
        void visitClassAttributeProxy(ClassAttributeProxy proxy);
duke@0
  1969
        void visitArrayAttributeProxy(ArrayAttributeProxy proxy);
duke@0
  1970
        void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy);
duke@0
  1971
    }
duke@0
  1972
jlahoda@5
  1973
    protected static class EnumAttributeProxy extends Attribute {
duke@0
  1974
        Type enumType;
duke@0
  1975
        Name enumerator;
duke@0
  1976
        public EnumAttributeProxy(Type enumType, Name enumerator) {
duke@0
  1977
            super(null);
duke@0
  1978
            this.enumType = enumType;
duke@0
  1979
            this.enumerator = enumerator;
duke@0
  1980
        }
duke@0
  1981
        public void accept(Visitor v) { ((ProxyVisitor)v).visitEnumAttributeProxy(this); }
jlahoda@4176
  1982
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
duke@0
  1983
        public String toString() {
duke@0
  1984
            return "/*proxy enum*/" + enumType + "." + enumerator;
duke@0
  1985
        }
duke@0
  1986
    }
duke@0
  1987
dbalek@5603
  1988
    protected static class ClassAttributeProxy extends Attribute {
alanb@5550
  1989
        Type classType;
alanb@5550
  1990
        public ClassAttributeProxy(Type classType) {
alanb@5550
  1991
            super(null);
alanb@5550
  1992
            this.classType = classType;
alanb@5550
  1993
        }
alanb@5550
  1994
        public void accept(Visitor v) { ((ProxyVisitor)v).visitClassAttributeProxy(this); }
alanb@5550
  1995
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
alanb@5550
  1996
        public String toString() {
alanb@5550
  1997
            return "/*proxy class*/" + classType + ".class";
alanb@5550
  1998
        }
alanb@5550
  1999
    }
alanb@5550
  2000
jlahoda@5
  2001
    protected static class ArrayAttributeProxy extends Attribute {
duke@0
  2002
        List<Attribute> values;
jlahoda@5
  2003
        public ArrayAttributeProxy(List<Attribute> values) {
duke@0
  2004
            super(null);
duke@0
  2005
            this.values = values;
duke@0
  2006
        }
duke@0
  2007
        public void accept(Visitor v) { ((ProxyVisitor)v).visitArrayAttributeProxy(this); }
jlahoda@4176
  2008
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
duke@0
  2009
        public String toString() {
duke@0
  2010
            return "{" + values + "}";
duke@0
  2011
        }
duke@0
  2012
    }
duke@0
  2013
duke@0
  2014
    /** A temporary proxy representing a compound attribute.
duke@0
  2015
     */
jlahoda@5
  2016
    protected static class CompoundAnnotationProxy extends Attribute {
duke@0
  2017
        final List<Pair<Name,Attribute>> values;
duke@0
  2018
        public CompoundAnnotationProxy(Type type,
duke@0
  2019
                                      List<Pair<Name,Attribute>> values) {
duke@0
  2020
            super(type);
duke@0
  2021
            this.values = values;
duke@0
  2022
        }
duke@0
  2023
        public void accept(Visitor v) { ((ProxyVisitor)v).visitCompoundAnnotationProxy(this); }
jlahoda@4176
  2024
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
duke@0
  2025
        public String toString() {
jjg@1217
  2026
            StringBuilder buf = new StringBuilder();
duke@0
  2027
            buf.append("@");
duke@0
  2028
            buf.append(type.tsym.getQualifiedName());
duke@0
  2029
            buf.append("/*proxy*/{");
duke@0
  2030
            boolean first = true;
duke@0
  2031
            for (List<Pair<Name,Attribute>> v = values;
duke@0
  2032
                 v.nonEmpty(); v = v.tail) {
duke@0
  2033
                Pair<Name,Attribute> value = v.head;
duke@0
  2034
                if (!first) buf.append(",");
duke@0
  2035
                first = false;
duke@0
  2036
                buf.append(value.fst);
duke@0
  2037
                buf.append("=");
duke@0
  2038
                buf.append(value.snd);
duke@0
  2039
            }
duke@0
  2040
            buf.append("}");
duke@0
  2041
            return buf.toString();
duke@0
  2042
        }
duke@0
  2043
    }
duke@0
  2044
jjg@598
  2045
    /** A temporary proxy representing a type annotation.
jjg@598
  2046
     */
jjg@598
  2047
    static class TypeAnnotationProxy {
jjg@598
  2048
        final CompoundAnnotationProxy compound;
jjg@598
  2049
        final TypeAnnotationPosition position;
jjg@598
  2050
        public TypeAnnotationProxy(CompoundAnnotationProxy compound,
jjg@598
  2051
                TypeAnnotationPosition position) {
jjg@598
  2052
            this.compound = compound;
jjg@598
  2053
            this.position = position;
jjg@598
  2054
        }
jjg@598
  2055
    }
jjg@598
  2056
duke@0
  2057
    class AnnotationDeproxy implements ProxyVisitor {
jjg@4454
  2058
        private ClassSymbol requestingOwner;
jjg@4454
  2059
jjg@4454
  2060
        AnnotationDeproxy(ClassSymbol owner) {
jjg@4454
  2061
            this.requestingOwner = owner;
jjg@4454
  2062
        }
duke@0
  2063
duke@0
  2064
        List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) {
duke@0
  2065
            // also must fill in types!!!!
briangoetz@3808
  2066
            ListBuffer<Attribute.Compound> buf = new ListBuffer<>();
duke@0
  2067
            for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) {
duke@0
  2068
                buf.append(deproxyCompound(l.head));
duke@0
  2069
            }
duke@0
  2070
            return buf.toList();
duke@0
  2071
        }
duke@0
  2072
duke@0
  2073
        Attribute.Compound deproxyCompound(CompoundAnnotationProxy a) {
alanb@5550
  2074
            Type annotationType = resolvePossibleProxyType(a.type);
briangoetz@3808
  2075
            ListBuffer<Pair<Symbol.MethodSymbol,Attribute>> buf = new ListBuffer<>();
duke@0
  2076
            for (List<Pair<Name,Attribute>> l = a.values;
duke@0
  2077
                 l.nonEmpty();
duke@0
  2078
                 l = l.tail) {
alanb@5550
  2079
                MethodSymbol meth = findAccessMethod(annotationType, l.head.fst);
briangoetz@3808
  2080
                buf.append(new Pair<>(meth, deproxy(meth.type.getReturnType(), l.head.snd)));
duke@0
  2081
            }
alanb@5550
  2082
            return new Attribute.Compound(annotationType, buf.toList());
duke@0
  2083
        }
duke@0
  2084
duke@0
  2085
        MethodSymbol findAccessMethod(Type container, Name name) {
duke@0
  2086
            CompletionFailure failure = null;
duke@0
  2087
            try {
jlahoda@4103
  2088
                for (Symbol sym : container.tsym.members().getSymbolsByName(name)) {
duke@0
  2089
                    if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0)
duke@0
  2090
                        return (MethodSymbol) sym;
duke@0
  2091
                }
duke@0
  2092
            } catch (CompletionFailure ex) {
duke@0
  2093
                failure = ex;
duke@0
  2094
            }
duke@0
  2095
            // The method wasn't found: emit a warning and recover
duke@0
  2096
            JavaFileObject prevSource = log.useSource(requestingOwner.classfile);
duke@0
  2097
            try {
darcy@4350
  2098
                if (lintClassfile) {
darcy@4350
  2099
                    if (failure == null) {
darcy@4350
  2100
                        log.warning("annotation.method.not.found",
darcy@4350
  2101
                                    container,
darcy@4350
  2102
                                    name);
darcy@4350
  2103
                    } else {
darcy@4350
  2104
                        log.warning("annotation.method.not.found.reason",
darcy@4350
  2105
                                    container,
darcy@4350
  2106
                                    name,
darcy@4350
  2107
                                    failure.getDetailValue());//diagnostic, if present
darcy@4350
  2108
                    }
duke@0
  2109
                }
duke@0
  2110
            } finally {
duke@0
  2111
                log.useSource(prevSource);
duke@0
  2112
            }
duke@0
  2113
            // Construct a new method type and symbol.  Use bottom
duke@0
  2114
            // type (typeof null) as return type because this type is
duke@0
  2115
            // a subtype of all reference types and can be converted
duke@0
  2116
            // to primitive types by unboxing.
mcimadamore@5586
  2117
            MethodType mt = new MethodType(List.nil(),
duke@0
  2118
                                           syms.botType,
mcimadamore@5586
  2119
                                           List.nil(),
duke@0
  2120
                                           syms.methodClass);
duke@0
  2121
            return new MethodSymbol(PUBLIC | ABSTRACT, name, mt, container.tsym);
duke@0
  2122
        }
duke@0
  2123
duke@0
  2124
        Attribute result;
duke@0
  2125
        Type type;
duke@0
  2126
        Attribute deproxy(Type t, Attribute a) {
duke@0
  2127
            Type oldType = type;
duke@0
  2128
            try {
duke@0
  2129
                type = t;
duke@0
  2130
                a.accept(this);
duke@0
  2131
                return result;
duke@0
  2132
            } finally {
duke@0
  2133
                type = oldType;
duke@0
  2134
            }
duke@0
  2135
        }
duke@0
  2136
duke@0
  2137
        // implement Attribute.Visitor below
duke@0
  2138
duke@0
  2139
        public void visitConstant(Attribute.Constant value) {
duke@0
  2140
            // assert value.type == type;
duke@0
  2141
            result = value;
duke@0
  2142
        }
duke@0
  2143
duke@0
  2144
        public void visitClass(Attribute.Class clazz) {
duke@0
  2145
            result = clazz;
duke@0
  2146
        }
duke@0
  2147
duke@0
  2148
        public void visitEnum(Attribute.Enum e) {
duke@0
  2149
            throw new AssertionError(); // shouldn't happen
duke@0
  2150
        }
duke@0
  2151
duke@0
  2152
        public void visitCompound(Attribute.Compound compound) {
duke@0
  2153
            throw new AssertionError(); // shouldn't happen
duke@0
  2154
        }
duke@0
  2155
duke@0
  2156
        public void visitArray(Attribute.Array array) {
duke@0
  2157
            throw new AssertionError(); // shouldn't happen
duke@0
  2158
        }
duke@0
  2159
duke@0
  2160
        public void visitError(Attribute.Error e) {
duke@0
  2161
            throw new AssertionError(); // shouldn't happen
duke@0
  2162
        }
duke@0
  2163
duke@0
  2164
        public void visitEnumAttributeProxy(EnumAttributeProxy proxy) {
duke@0
  2165
            // type.tsym.flatName() should == proxy.enumFlatName
alanb@5550
  2166
            Type enumType = resolvePossibleProxyType(proxy.enumType);
alanb@5550
  2167
            TypeSymbol enumTypeSym = enumType.tsym;
duke@0
  2168
            VarSymbol enumerator = null;
mcimadamore@1486
  2169
            CompletionFailure failure = null;
mcimadamore@1486
  2170
            try {
jlahoda@4103
  2171
                for (Symbol sym : enumTypeSym.members().getSymbolsByName(proxy.enumerator)) {
jlahoda@4103
  2172
                    if (sym.kind == VAR) {
jlahoda@4103
  2173
                        enumerator = (VarSymbol)sym;
mcimadamore@1486
  2174
                        break;
mcimadamore@1486
  2175
                    }
duke@0
  2176
                }
duke@0
  2177
            }
mcimadamore@1486
  2178
            catch (CompletionFailure ex) {
mcimadamore@1486
  2179
                failure = ex;
mcimadamore@1486
  2180
            }
duke@0
  2181
            if (enumerator == null) {
mcimadamore@1486
  2182
                if (failure != null) {
mcimadamore@1486
  2183
                    log.warning("unknown.enum.constant.reason",
mcimadamore@1486
  2184
                              currentClassFile, enumTypeSym, proxy.enumerator,
mcimadamore@1486
  2185
                              failure.getDiagnostic());
mcimadamore@1486
  2186
                } else {
mcimadamore@1486
  2187
                    log.warning("unknown.enum.constant",
mcimadamore@1486
  2188
                              currentClassFile, enumTypeSym, proxy.enumerator);
mcimadamore@1486
  2189
                }
mcimadamore@1486
  2190
                result = new Attribute.Enum(enumTypeSym.type,
mcimadamore@1486
  2191
                        new VarSymbol(0, proxy.enumerator, syms.botType, enumTypeSym));
duke@0
  2192
            } else {
duke@0
  2193
                result = new Attribute.Enum(enumTypeSym.type, enumerator);
duke@0
  2194
            }
duke@0
  2195
        }
duke@0
  2196
alanb@5550
  2197
        @Override
alanb@5550
  2198
        public void visitClassAttributeProxy(ClassAttributeProxy proxy) {
alanb@5550
  2199
            Type classType = resolvePossibleProxyType(proxy.classType);
alanb@5550
  2200
            result = new Attribute.Class(types, classType);
alanb@5550
  2201
        }
alanb@5550
  2202
duke@0
  2203
        public void visitArrayAttributeProxy(ArrayAttributeProxy proxy) {
duke@0
  2204
            int length = proxy.values.length();
duke@0
  2205
            Attribute[] ats = new Attribute[length];
duke@0
  2206
            Type elemtype = types.elemtype(type);
duke@0
  2207
            int i = 0;
duke@0
  2208
            for (List<Attribute> p = proxy.values; p.nonEmpty(); p = p.tail) {
duke@0
  2209
                ats[i++] = deproxy(elemtype, p.head);
duke@0
  2210
            }
duke@0
  2211
            result = new Attribute.Array(type, ats);
duke@0
  2212
        }
duke@0
  2213
duke@0
  2214
        public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) {
duke@0
  2215
            result = deproxyCompound(proxy);
duke@0
  2216
        }
alanb@5550
  2217
alanb@5550
  2218
        Type resolvePossibleProxyType(Type t) {
alanb@5550
  2219
            if (t instanceof ProxyType) {
alanb@5550
  2220
                Assert.check(requestingOwner.owner.kind == MDL);
alanb@5550
  2221
                ModuleSymbol prevCurrentModule = currentModule;
alanb@5550
  2222
                currentModule = (ModuleSymbol) requestingOwner.owner;
alanb@5550
  2223
                try {
alanb@5550
  2224
                    return ((ProxyType) t).resolve();
alanb@5550
  2225
                } finally {
alanb@5550
  2226
                    currentModule = prevCurrentModule;
alanb@5550
  2227
                }
alanb@5550
  2228
            } else {
alanb@5550
  2229
                return t;
alanb@5550
  2230
            }
alanb@5550
  2231
        }
duke@0
  2232
    }
duke@0
  2233
dbalek@4654
  2234
    protected class AnnotationDefaultCompleter extends AnnotationDeproxy implements Runnable {
duke@0
  2235
        final MethodSymbol sym;
duke@0
  2236
        final Attribute value;
duke@0
  2237
        final JavaFileObject classFile = currentClassFile;
jjg@4454
  2238
jlahoda@5
  2239
        public AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) {
jjg@4454
  2240
            super(currentOwner.kind == MTH
jjg@4454
  2241
                    ? currentOwner.enclClass() : (ClassSymbol)currentOwner);
duke@0
  2242
            this.sym = sym;
duke@0
  2243
            this.value = value;
duke@0
  2244
        }
jjg@4454
  2245
jjg@4454
  2246
        @Override
jlahoda@3157
  2247
        public void run() {
duke@0
  2248
            JavaFileObject previousClassFile = currentClassFile;
duke@0
  2249
            try {
jfranck@2288
  2250
                // Reset the interim value set earlier in
jfranck@2288
  2251
                // attachAnnotationDefault().
jfranck@2288
  2252
                sym.defaultValue = null;
duke@0
  2253
                currentClassFile = classFile;
duke@0
  2254
                sym.defaultValue = deproxy(sym.type.getReturnType(), value);
duke@0
  2255
            } finally {
duke@0
  2256
                currentClassFile = previousClassFile;
duke@0
  2257
            }
duke@0
  2258
        }
jjg@4454
  2259
jjg@4454
  2260
        @Override
jjg@4454
  2261
        public String toString() {
jjg@4454
  2262
            return " ClassReader store default for " + sym.owner + "." + sym + " is " + value;
jjg@4454
  2263
        }
duke@0
  2264
    }
duke@0
  2265
dbalek@4654
  2266
    protected class AnnotationCompleter extends AnnotationDeproxy implements Runnable {
duke@0
  2267
        final Symbol sym;
duke@0
  2268
        final List<CompoundAnnotationProxy> l;
duke@0
  2269
        final JavaFileObject classFile;
jjg@4454
  2270
jlahoda@5
  2271
        public AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) {
jjg@4454
  2272
            super(currentOwner.kind == MTH
jjg@4454
  2273
                    ? currentOwner.enclClass() : (ClassSymbol)currentOwner);
alanb@5550
  2274
            if (sym.kind == TYP && sym.owner.kind == MDL) {
alanb@5550
  2275
                this.sym = sym.owner;
alanb@5550
  2276
            } else {
alanb@5550
  2277
                this.sym = sym;
alanb@5550
  2278
            }
duke@0
  2279
            this.l = l;
duke@0
  2280
            this.classFile = currentClassFile;
duke@0
  2281
        }
jjg@4454
  2282
jjg@4454
  2283
        @Override
jlahoda@3157
  2284
        public void run() {
jlahoda@2449
  2285
            if ((sym.flags_field & FROMCLASS) == 0 && ((sym.flags_field & PARAMETER) == 0 || (sym.owner.flags_field & FROMCLASS) == 0))
dbalek@2405
  2286
                return;
duke@0
  2287
            JavaFileObject previousClassFile = currentClassFile;
duke@0
  2288
            try {
duke@0
  2289
                currentClassFile = classFile;
duke@0
  2290
                List<Attribute.Compound> newList = deproxyCompoundList(l);
alanb@5550
  2291
                for (Attribute.Compound attr : newList) {
alanb@5550
  2292
                    if (attr.type.tsym == syms.deprecatedType.tsym) {
alanb@5550
  2293
                        sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
alanb@5550
  2294
                        Attribute forRemoval = attr.member(names.forRemoval);
alanb@5550
  2295
                        if (forRemoval instanceof Attribute.Constant) {
alanb@5550
  2296
                            Attribute.Constant c = (Attribute.Constant) forRemoval;
alanb@5550
  2297
                            if (c.type == syms.booleanType && ((Integer) c.value) != 0) {
alanb@5550
  2298
                                sym.flags_field |= DEPRECATED_REMOVAL;
alanb@5550
  2299
                            }
alanb@5550
  2300
                        }
alanb@5550
  2301
                    }
alanb@5550
  2302
                }
jjg@2786
  2303
                if (sym.annotationsPendingCompletion()) {
jjg@2786
  2304
                    sym.setDeclarationAttributes(newList);
jfranck@2156
  2305
                } else {
jjg@2786
  2306
                    sym.appendAttributes(newList);
jfranck@2156
  2307
                }
duke@0
  2308
            } finally {
duke@0
  2309
                currentClassFile = previousClassFile;
duke@0
  2310
            }
duke@0
  2311
        }
jjg@4454
  2312
jjg@4454
  2313
        @Override
jjg@4454
  2314
        public String toString() {
jjg@4454
  2315
            return " ClassReader annotate " + sym.owner + "." + sym + " with " + l;
jjg@4454
  2316
        }
duke@0
  2317
    }
duke@0
  2318
jjg@2372
  2319
    class TypeAnnotationCompleter extends AnnotationCompleter {
jjg@2372
  2320
jjg@2372
  2321
        List<TypeAnnotationProxy> proxies;
jjg@2372
  2322
jjg@2372
  2323
        TypeAnnotationCompleter(Symbol sym,
jjg@2372
  2324
                List<TypeAnnotationProxy> proxies) {
mcimadamore@5586
  2325
            super(sym, List.nil());
jjg@2372
  2326
            this.proxies = proxies;
jjg@2372
  2327
        }
jjg@2372
  2328
jjg@2372
  2329
        List<Attribute.TypeCompound> deproxyTypeCompoundList(List<TypeAnnotationProxy> proxies) {
alundblad@3063
  2330
            ListBuffer<Attribute.TypeCompound> buf = new ListBuffer<>();
jjg@2372
  2331
            for (TypeAnnotationProxy proxy: proxies) {
jjg@2372
  2332
                Attribute.Compound compound = deproxyCompound(proxy.compound);
jjg@2372
  2333
                Attribute.TypeCompound typeCompound = new Attribute.TypeCompound(compound, proxy.position);
jjg@2372
  2334
                buf.add(typeCompound);
jjg@2372
  2335
            }
jjg@2372
  2336
            return buf.toList();
jjg@2372
  2337
        }
jjg@2372
  2338
jjg@2372
  2339
        @Override
jlahoda@3157
  2340
        public void run() {
jjg@2372
  2341
            JavaFileObject previousClassFile = currentClassFile;
jjg@2372
  2342
            try {
jjg@2372
  2343
                currentClassFile = classFile;
jjg@2372
  2344
                List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
jjg@2786
  2345
                sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
jjg@2372
  2346
            } finally {
jjg@2372
  2347
                currentClassFile = previousClassFile;
jjg@2372
  2348
            }
jjg@2372
  2349
        }
jjg@2372
  2350
    }
jjg@2372
  2351
duke@0
  2352
duke@0
  2353
/************************************************************************
duke@0
  2354
 * Reading Symbols
duke@0
  2355
 ***********************************************************************/
duke@0
  2356
duke@0
  2357
    /** Read a field.
duke@0
  2358
     */
duke@0
  2359
    VarSymbol readField() {
duke@0
  2360
        long flags = adjustFieldFlags(nextChar());
duke@0
  2361
        Name name = readName(nextChar());
duke@0
  2362
        Type type = readType(nextChar());
duke@0
  2363
        VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
duke@0
  2364
        readMemberAttrs(v);
duke@0
  2365
        return v;
duke@0
  2366
    }
duke@0
  2367
duke@0
  2368
    /** Read a method.
duke@0
  2369
     */
duke@0
  2370
    MethodSymbol readMethod() {
duke@0
  2371
        long flags = adjustMethodFlags(nextChar());
duke@0
  2372
        Name name = readName(nextChar());
duke@0
  2373
        Type type = readType(nextChar());
mcimadamore@2260
  2374
        if (currentOwner.isInterface() &&
mcimadamore@2260
  2375
                (flags & ABSTRACT) == 0 && !name.equals(names.clinit)) {
ntoda@4004
  2376
            if (majorVersion > Version.V52.major ||
ntoda@4004
  2377
                    (majorVersion == Version.V52.major && minorVersion >= Version.V52.minor)) {
sadayapalam@5812
  2378
                if ((flags & (STATIC | PRIVATE)) == 0) {
jlahoda@3123
  2379
                    currentOwner.flags_field |= DEFAULT;
jlahoda@3123
  2380
                    flags |= DEFAULT | ABSTRACT;
jlahoda@3123
  2381
                }
mcimadamore@2260
  2382
            } else {
mcimadamore@2260
  2383
                //protect against ill-formed classfiles
jlahoda@3123
  2384
                throw badClassFile((flags & STATIC) == 0 ? "invalid.default.interface" : "invalid.static.interface",
jlahoda@3123
  2385
                                   Integer.toString(majorVersion),
jlahoda@3123
  2386
                                   Integer.toString(minorVersion));
mcimadamore@2260
  2387
            }
mcimadamore@2260
  2388
        }
duke@0
  2389
        if (name == names.init && currentOwner.hasOuterInstance()) {
duke@0
  2390
            // Sometimes anonymous classes don't have an outer
duke@0
  2391
            // instance, however, there is no reliable way to tell so
duke@0
  2392
            // we never strip this$n
jlahoda@5513
  2393
            // ditto for local classes. Local classes that have an enclosing method set
jlahoda@5513
  2394
            // won't pass the "hasOuterInstance" check above, but those that don't have an
jlahoda@5513
  2395
            // enclosing method (i.e. from initializers) will pass that check.
jlahoda@5513
  2396
            boolean local = !currentOwner.owner.members().includes(currentOwner, LookupKind.NON_RECURSIVE);
jlahoda@5513
  2397
            if (!currentOwner.name.isEmpty() && !local)
mcimadamore@1244
  2398
                type = new MethodType(adjustMethodParams(flags, type.getParameterTypes()),
duke@0
  2399
                                      type.getReturnType(),
duke@0
  2400
                                      type.getThrownTypes(),
duke@0
  2401
                                      syms.methodClass);
duke@0
  2402
        }
duke@0
  2403
        MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
vromero@2804
  2404
        if (types.isSignaturePolymorphic(m)) {
vromero@2804
  2405
            m.flags_field |= SIGNATURE_POLYMORPHIC;
vromero@2804
  2406
        }
jjg@718
  2407
        if (saveParameterNames)
jjg@718
  2408
            initParameterNames(m);
duke@0
  2409
        Symbol prevOwner = currentOwner;
duke@0
  2410
        currentOwner = m;
duke@0
  2411
        try {
duke@0
  2412
            readMemberAttrs(m);
duke@0
  2413
        } finally {
duke@0
  2414
            currentOwner = prevOwner;
duke@0
  2415
        }
jjg@718
  2416
        if (saveParameterNames)
jjg@718
  2417
            setParameterNames(m, type);
sadayapalam@4429
  2418
sadayapalam@4429
  2419
        if ((flags & VARARGS) != 0) {
sadayapalam@4429
  2420
            final Type last = type.getParameterTypes().last();
sadayapalam@4429
  2421
            if (last == null || !last.hasTag(ARRAY)) {
sadayapalam@4429
  2422
                m.flags_field &= ~VARARGS;
sadayapalam@4429
  2423
                throw badClassFile("malformed.vararg.method", m);
sadayapalam@4429
  2424
            }
sadayapalam@4429
  2425
        }
sadayapalam@4429
  2426
duke@0
  2427
        return m;
duke@0
  2428
    }
duke@0
  2429
mcimadamore@1244
  2430
    private List<Type> adjustMethodParams(long flags, List<Type> args) {
mcimadamore@1244
  2431
        boolean isVarargs = (flags & VARARGS) != 0;
mcimadamore@1244
  2432
        if (isVarargs) {
mcimadamore@1244
  2433
            Type varargsElem = args.last();
alundblad@3063
  2434
            ListBuffer<Type> adjustedArgs = new ListBuffer<>();
mcimadamore@1244
  2435
            for (Type t : args) {
mcimadamore@1244
  2436
                adjustedArgs.append(t != varargsElem ?
mcimadamore@1244
  2437
                    t :
mcimadamore@1244
  2438
                    ((ArrayType)t).makeVarargs());
mcimadamore@1244
  2439
            }
mcimadamore@1244
  2440
            args = adjustedArgs.toList();
mcimadamore@1244
  2441
        }
mcimadamore@1244
  2442
        return args.tail;
mcimadamore@1244
  2443
    }
mcimadamore@1244
  2444
jjg@718
  2445
    /**
jjg@718
  2446
     * Init the parameter names array.
jjg@718
  2447
     * Parameter names are currently inferred from the names in the
jjg@718
  2448
     * LocalVariableTable attributes of a Code attribute.
jjg@718
  2449
     * (Note: this means parameter names are currently not available for
jjg@718
  2450
     * methods without a Code attribute.)
jjg@718
  2451
     * This method initializes an array in which to store the name indexes
jjg@718
  2452
     * of parameter names found in LocalVariableTable attributes. It is
jjg@718
  2453
     * slightly supersized to allow for additional slots with a start_pc of 0.
jjg@718
  2454
     */
jjg@718
  2455
    void initParameterNames(MethodSymbol sym) {
jjg@718
  2456
        // make allowance for synthetic parameters.
jjg@718
  2457
        final int excessSlots = 4;
jjg@718
  2458
        int expectedParameterSlots =
jjg@718
  2459
                Code.width(sym.type.getParameterTypes()) + excessSlots;
jjg@718
  2460
        if (parameterNameIndices == null
jjg@718
  2461
                || parameterNameIndices.length < expectedParameterSlots) {
jjg@718
  2462
            parameterNameIndices = new int[expectedParameterSlots];
jjg@718
  2463
        } else
jjg@718
  2464
            Arrays.fill(parameterNameIndices, 0);
jjg@718
  2465
        haveParameterNameIndices = false;
jjg@2312
  2466
        sawMethodParameters = false;
jjg@718
  2467
    }
jjg@718
  2468
jjg@718
  2469
    /**
jjg@718
  2470
     * Set the parameter names for a symbol from the name index in the
jjg@718
  2471
     * parameterNameIndicies array. The type of the symbol may have changed
jjg@718
  2472
     * while reading the method attributes (see the Signature attribute).
jjg@718
  2473
     * This may be because of generic information or because anonymous
jjg@718
  2474
     * synthetic parameters were added.   The original type (as read from
jjg@718
  2475
     * the method descriptor) is used to help guess the existence of
jjg@718
  2476
     * anonymous synthetic parameters.
jjg@718
  2477
     * On completion, sym.savedParameter names will either be null (if
jjg@718
  2478
     * no parameter names were found in the class file) or will be set to a
jjg@718
  2479
     * list of names, one per entry in sym.type.getParameterTypes, with
jjg@718
  2480
     * any missing names represented by the empty name.
jjg@718
  2481
     */
jjg@718
  2482
    void setParameterNames(MethodSymbol sym, Type jvmType) {
jjg@718
  2483
        // if no names were found in the class file, there's nothing more to do
dbalek@799
  2484
        if (!haveParameterNameIndices || sym.type == null || sym.type.getParameterTypes() == null)
jjg@718
  2485
            return;
jjg@2312
  2486
        // If we get parameter names from MethodParameters, then we
jjg@2312
  2487
        // don't need to skip.
jjg@2312
  2488
        int firstParam = 0;
jjg@2312
  2489
        if (!sawMethodParameters) {
jjg@2312
  2490
            firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
jjg@2312
  2491
            // the code in readMethod may have skipped the first
jjg@2312
  2492
            // parameter when setting up the MethodType. If so, we
jjg@2312
  2493
            // make a corresponding allowance here for the position of
jjg@2312
  2494
            // the first parameter.  Note that this assumes the
jjg@2312
  2495
            // skipped parameter has a width of 1 -- i.e. it is not
jjg@718
  2496
        // a double width type (long or double.)
jjg@718
  2497
        if (sym.name == names.init && currentOwner.hasOuterInstance()) {
jjg@718
  2498
            // Sometimes anonymous classes don't have an outer
jjg@718
  2499
            // instance, however, there is no reliable way to tell so
jjg@718
  2500
            // we never strip this$n
jjg@718
  2501
            if (!currentOwner.name.isEmpty())
jjg@718
  2502
                firstParam += 1;
jjg@718
  2503
        }
jjg@718
  2504
jjg@718
  2505
        if (sym.type != jvmType) {
jjg@2312
  2506
                // reading the method attributes has caused the
jjg@2312
  2507
                // symbol's type to be changed. (i.e. the Signature
jjg@2312
  2508
                // attribute.)  This may happen if there are hidden
jjg@2312
  2509
                // (synthetic) parameters in the descriptor, but not
jjg@2312
  2510
                // in the Signature.  The position of these hidden
jjg@2312
  2511
                // parameters is unspecified; for now, assume they are
jjg@2312
  2512
                // at the beginning, and so skip over them. The
jjg@2312
  2513
                // primary case for this is two hidden parameters
jjg@2312
  2514
                // passed into Enum constructors.
jjg@718
  2515
            int skip = Code.width(jvmType.getParameterTypes())
jjg@718
  2516
                    - Code.width(sym.type.getParameterTypes());
jjg@718
  2517
            firstParam += skip;
jjg@718
  2518
        }
jjg@2312
  2519
        }
jjg@718
  2520
        List<Name> paramNames = List.nil();
jjg@718
  2521
        int index = firstParam;
jjg@718
  2522
        for (Type t: sym.type.getParameterTypes()) {
dbalek@2748
  2523
            int nameIdx = (index >= 0 && index < parameterNameIndices.length
jjg@718
  2524
                    ? parameterNameIndices[index] : 0);
jjg@718
  2525
            Name name = nameIdx == 0 ? names.empty : readName(nameIdx);
jjg@718
  2526
            paramNames = paramNames.prepend(name);
cushon@5566
  2527
            index += sawMethodParameters ? 1 : Code.width(t);
jjg@718
  2528
        }
jjg@718
  2529
        sym.savedParameterNames = paramNames.reverse();
jjg@718
  2530
    }
jjg@718
  2531
ksrini@1291
  2532
    /**
ksrini@1291
  2533
     * skip n bytes
ksrini@1291
  2534
     */
ksrini@1291
  2535
    void skipBytes(int n) {
ksrini@1291
  2536
        bp = bp + n;
ksrini@1291
  2537
    }
ksrini@1291
  2538
duke@0
  2539
    /** Skip a field or method
duke@0
  2540
     */
duke@0
  2541
    void skipMember() {
duke@0
  2542
        bp = bp + 6;
duke@0
  2543
        char ac = nextChar();
duke@0
  2544
        for (int i = 0; i < ac; i++) {
duke@0
  2545
            bp = bp + 2;
duke@0
  2546
            int attrLen = nextInt();
duke@0
  2547
            bp = bp + attrLen;
duke@0
  2548
        }
duke@0
  2549
    }
duke@0
  2550
alanb@5016
  2551
    void skipInnerClasses() {
alanb@5016
  2552
        int n = nextChar();
alanb@5016
  2553
        for (int i = 0; i < n; i++) {
alanb@5016
  2554
            nextChar();
alanb@5016
  2555
            nextChar();
alanb@5016
  2556
            nextChar();
alanb@5016
  2557
            nextChar();
alanb@5016
  2558
        }
alanb@5016
  2559
    }
alanb@5016
  2560
duke@0
  2561
    /** Enter type variables of this classtype and all enclosing ones in
duke@0
  2562
     *  `typevars'.
duke@0
  2563
     */
jlahoda@5502
  2564
    protected void enterTypevars(Symbol sym, Type t) {
jlahoda@5502
  2565
        if (t.getEnclosingType() != null) {
jlahoda@5502
  2566
            if (!t.getEnclosingType().hasTag(TypeTag.NONE)) {
jlahoda@5502
  2567
                enterTypevars(sym.owner, t.getEnclosingType());
jlahoda@5502
  2568
            }
jlahoda@5502
  2569
        } else if (sym.kind == MTH && !sym.isStatic()) {
jlahoda@5502
  2570
            enterTypevars(sym.owner, sym.owner.type);
jlahoda@5502
  2571
        }
jlahoda@5502
  2572
        for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail) {
duke@0
  2573
            typevars.enter(xs.head.tsym);
duke@0
  2574
        }
duke@0
  2575
    }
duke@0
  2576
alanb@5016
  2577
    protected ClassSymbol enterClass(Name name) {
alanb@5016
  2578
        return syms.enterClass(currentModule, name);
alanb@5016
  2579
    }
alanb@5016
  2580
alanb@5016
  2581
    protected ClassSymbol enterClass(Name name, TypeSymbol owner) {
alanb@5016
  2582
        return syms.enterClass(currentModule, name, owner);
alanb@5016
  2583
    }
alanb@5016
  2584
duke@0
  2585
    /** Read contents of a given class symbol `c'. Both external and internal
duke@0
  2586
     *  versions of an inner class are read.
duke@0
  2587
     */
duke@0
  2588
    void readClass(ClassSymbol c) {
duke@0
  2589
        ClassType ct = (ClassType)c.type;
duke@0
  2590
duke@0
  2591
        // allocate scope for members
jlahoda@4103
  2592
        c.members_field = WriteableScope.create(c);
duke@0
  2593
duke@0
  2594
        // prepare type variable table
duke@0
  2595
        typevars = typevars.dup(currentOwner);
jjg@2213
  2596
        if (ct.getEnclosingType().hasTag(CLASS))
jlahoda@5502
  2597
            enterTypevars(c.owner, ct.getEnclosingType());
duke@0
  2598
duke@0
  2599
        // read flags, or skip if this is an inner class
alanb@5016
  2600
        long f = nextChar();
alanb@5016
  2601
        long flags = adjustClassFlags(f);
alanb@5016
  2602
        if ((flags & MODULE) == 0) {
alanb@5016
  2603
            if (c.owner.kind == PCK) c.flags_field = flags;
alanb@5016
  2604
            // read own class name and check that it matches
alanb@5016
  2605
            currentModule = c.packge().modle;
alanb@5016
  2606
            ClassSymbol self = readClassSymbol(nextChar());
alanb@5016
  2607
            if (c != self) {
alanb@5016
  2608
                throw badClassFile("class.file.wrong.class",
alanb@5016
  2609
                                   self.flatname);
alanb@5016
  2610
            }
alanb@5016
  2611
        } else {
alanb@5580
  2612
            if (majorVersion < Version.V53.major) {
alanb@5580
  2613
                throw badClassFile("anachronistic.module.info",
alanb@5580
  2614
                        Integer.toString(majorVersion),
alanb@5580
  2615
                        Integer.toString(minorVersion));
alanb@5580
  2616
            }
alanb@5016
  2617
            c.flags_field = flags;
alanb@5016
  2618
            currentModule = (ModuleSymbol) c.owner;
dbalek@5322
  2619
            currentModule.flags_field |= FROMCLASS;
alanb@5550
  2620
            int this_class = nextChar();
alanb@5580
  2621
            // temp, no check on this_class
alanb@5016
  2622
        }
duke@0
  2623
duke@0
  2624
        // class attributes must be read before class
duke@0
  2625
        // skip ahead to read class attributes
duke@0
  2626
        int startbp = bp;
duke@0
  2627
        nextChar();
duke@0
  2628
        char interfaceCount = nextChar();
duke@0
  2629
        bp += interfaceCount * 2;
duke@0
  2630
        char fieldCount = nextChar();
duke@0
  2631
        for (int i = 0; i < fieldCount; i++) skipMember();
duke@0
  2632
        char methodCount = nextChar();
duke@0
  2633
        for (int i = 0; i < methodCount; i++) skipMember();
duke@0
  2634
        readClassAttrs(c);
duke@0
  2635
duke@0
  2636
        if (readAllOfClassFile) {
duke@0
  2637
            for (int i = 1; i < poolObj.length; i++) readPool(i);
vromero@2290
  2638
            c.pool = new Pool(poolObj.length, poolObj, types);
duke@0
  2639
        }
duke@0
  2640
duke@0
  2641
        // reset and read rest of classinfo
duke@0
  2642
        bp = startbp;
duke@0
  2643
        int n = nextChar();
vromero@5224
  2644
        if ((flags & MODULE) != 0 && n > 0) {
vromero@5224
  2645
            throw badClassFile("module.info.invalid.super.class");
vromero@5224
  2646
        }
duke@0
  2647
        if (ct.supertype_field == null)
duke@0
  2648
            ct.supertype_field = (n == 0)
duke@0
  2649
                ? Type.noType
duke@0
  2650
                : readClassSymbol(n).erasure(types);
duke@0
  2651
        n = nextChar();
duke@0
  2652
        List<Type> is = List.nil();
duke@0
  2653
        for (int i = 0; i < n; i++) {
duke@0
  2654
            Type _inter = readClassSymbol(nextChar()).erasure(types);
duke@0
  2655
            is = is.prepend(_inter);
duke@0
  2656
        }
duke@0
  2657
        if (ct.interfaces_field == null)
duke@0
  2658
            ct.interfaces_field = is.reverse();
duke@0
  2659
jjg@1281
  2660
        Assert.check(fieldCount == nextChar());
duke@0
  2661
        for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
jjg@1281
  2662
        Assert.check(methodCount == nextChar());
duke@0
  2663
        for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
duke@0
  2664
duke@0
  2665
        typevars = typevars.leave();
duke@0
  2666
    }
duke@0
  2667
duke@0
  2668
    /** Read inner class info. For each inner/outer pair allocate a
duke@0
  2669
     *  member class.
duke@0
  2670
     */
duke@0
  2671
    void readInnerClasses(ClassSymbol c) {
duke@0
  2672
        int n = nextChar();
duke@0
  2673
        for (int i = 0; i < n; i++) {
duke@0
  2674
            nextChar(); // skip inner class symbol
duke@0
  2675
            ClassSymbol outer = readClassSymbol(nextChar());
duke@0
  2676
            Name name = readName(nextChar());
duke@0
  2677
            if (name == null) name = names.empty;
duke@0
  2678
            long flags = adjustClassFlags(nextChar());
duke@0
  2679
            if (outer != null) { // we have a member class
duke@0
  2680
                if (name == names.empty)
duke@0
  2681
                    name = names.one;
alanb@5016
  2682
                ClassSymbol member = enterClass(name, outer);
duke@0
  2683
                if ((flags & STATIC) == 0) {
duke@0
  2684
                    ((ClassType)member.type).setEnclosingType(outer.type);
duke@0
  2685
                    if (member.erasure_field != null)
duke@0
  2686
                        ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
duke@0
  2687
                }
duke@0
  2688
                if (c == outer) {
duke@0
  2689
                    member.flags_field = flags;
duke@0
  2690
                    enterMember(c, member);
duke@0
  2691
                }
duke@0
  2692
            }
duke@0
  2693
        }
duke@0
  2694
    }
duke@0
  2695
jjg@4002
  2696
    /** Read a class definition from the bytes in buf.
duke@0
  2697
     */
jjg@4002
  2698
    private void readClassBuffer(ClassSymbol c) throws IOException {
duke@0
  2699
        int magic = nextInt();
duke@0
  2700
        if (magic != JAVA_MAGIC)
duke@0
  2701
            throw badClassFile("illegal.start.of.class.file");
duke@0
  2702
jjg@546
  2703
        minorVersion = nextChar();
jjg@546
  2704
        majorVersion = nextChar();
alanb@5550
  2705
        int maxMajor = 53; // Version.MAX().major;  //******* TEMPORARY *******
ntoda@4004
  2706
        int maxMinor = Version.MAX().minor;
duke@0
  2707
        if (majorVersion > maxMajor ||
duke@0
  2708
            majorVersion * 1000 + minorVersion <
vromero@4965
  2709
            Version.MIN().major * 1000 + Version.MIN().minor) {
duke@0
  2710
            if (majorVersion == (maxMajor + 1))
duke@0
  2711
                log.warning("big.major.version",
duke@0
  2712
                            currentClassFile,
duke@0
  2713
                            majorVersion,
duke@0
  2714
                            maxMajor);
duke@0
  2715
            else
duke@0
  2716
                throw badClassFile("wrong.version",
duke@0
  2717
                                   Integer.toString(majorVersion),
duke@0
  2718
                                   Integer.toString(minorVersion),
duke@0
  2719
                                   Integer.toString(maxMajor),
duke@0
  2720
                                   Integer.toString(maxMinor));
duke@0
  2721
        }
vromero@4965
  2722
duke@0
  2723
        indexPool();
duke@0
  2724
        if (signatureBuffer.length < bp) {
duke@0
  2725
            int ns = Integer.highestOneBit(bp) << 1;
duke@0
  2726
            signatureBuffer = new byte[ns];
duke@0
  2727
        }
duke@0
  2728
        readClass(c);
duke@0
  2729
    }
duke@0
  2730
jjg@4002
  2731
    public void readClassFile(ClassSymbol c) {
jjg@4002
  2732
        currentOwner = c;
jjg@4002
  2733
        currentClassFile = c.classfile;
jjg@4002
  2734
        warnedAttrs.clear();
jjg@4002
  2735
        filling = true;
jjg@4454
  2736
        target = null;
jjg@4454
  2737
        repeatable = null;
jjg@4002
  2738
        try {
jjg@4002
  2739
            bp = 0;
jjg@4002
  2740
            buf = readInputStream(buf, c.classfile.openInputStream());
jjg@4002
  2741
            readClassBuffer(c);
jjg@4002
  2742
            if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
jjg@4002
  2743
                List<Type> missing = missingTypeVariables;
jjg@4002
  2744
                List<Type> found = foundTypeVariables;
jjg@4002
  2745
                missingTypeVariables = List.nil();
jjg@4002
  2746
                foundTypeVariables = List.nil();
alanb@5550
  2747
                interimUses = List.nil();
alanb@5550
  2748
                interimProvides = List.nil();
jjg@4002
  2749
                filling = false;
jjg@4002
  2750
                ClassType ct = (ClassType)currentOwner.type;
jjg@4002
  2751
                ct.supertype_field =
jjg@4002
  2752
                    types.subst(ct.supertype_field, missing, found);
jjg@4002
  2753
                ct.interfaces_field =
jjg@4002
  2754
                    types.subst(ct.interfaces_field, missing, found);
jlahoda@5502
  2755
                ct.typarams_field =
jlahoda@5502
  2756
                    types.substBounds(ct.typarams_field, missing, found);
jlahoda@5502
  2757
                for (List<Type> types = ct.typarams_field; types.nonEmpty(); types = types.tail) {
jlahoda@5502
  2758
                    types.head.tsym.type = types.head;
jlahoda@5502
  2759
                }
jjg@4002
  2760
            } else if (missingTypeVariables.isEmpty() !=
jjg@4002
  2761
                       foundTypeVariables.isEmpty()) {
jjg@4002
  2762
                Name name = missingTypeVariables.head.tsym.name;
jjg@4002
  2763
                throw badClassFile("undecl.type.var", name);
duke@0
  2764
            }
jjg@4454
  2765
jjg@4454
  2766
            if ((c.flags_field & Flags.ANNOTATION) != 0) {
jjg@4454
  2767
                c.setAnnotationTypeMetadata(new AnnotationTypeMetadata(c, new CompleterDeproxy(c, target, repeatable)));
jjg@4454
  2768
            } else {
jjg@4454
  2769
                c.setAnnotationTypeMetadata(AnnotationTypeMetadata.notAnAnnotationType());
jjg@4454
  2770
            }
alanb@5016
  2771
alanb@5016
  2772
            if (c == currentModule.module_info) {
tzezula@5367
  2773
                Assert.check(currentModule.isCompleted());
tzezula@5367
  2774
                currentModule.usesProvidesCompleter =
alanb@5016
  2775
                            new UsesProvidesCompleter(currentModule, interimUses, interimProvides);
alanb@5016
  2776
            }
jjg@5674
  2777
        } catch (IOException | ClosedFileSystemException ex) {
jjg@5674
  2778
            throw badClassFile("unable.to.access.file", ex.toString());
jjg@4002
  2779
        } catch (ArrayIndexOutOfBoundsException ex) {
jjg@4002
  2780
            throw badClassFile("bad.class.file", c.flatname);
jjg@4002
  2781
        } finally {
alanb@5016
  2782
            interimUses = List.nil();
alanb@5016
  2783
            interimProvides = List.nil();
jjg@4002
  2784
            missingTypeVariables = List.nil();
jjg@4002
  2785
            foundTypeVariables = List.nil();
jjg@4002
  2786
            filling = false;
duke@0
  2787
        }
duke@0
  2788
    }
duke@0
  2789
    // where
jlahoda@5
  2790
        static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
duke@0
  2791
            try {
duke@0
  2792
                buf = ensureCapacity(buf, s.available());
duke@0
  2793
                int r = s.read(buf);
duke@0
  2794
                int bp = 0;
duke@0
  2795
                while (r != -1) {
duke@0
  2796
                    bp += r;
duke@0
  2797
                    buf = ensureCapacity(buf, bp);
duke@0
  2798
                    r = s.read(buf, bp, buf.length - bp);
duke@0
  2799
                }
duke@0
  2800
                return buf;
duke@0
  2801
            } finally {
duke@0
  2802
                try {
duke@0
  2803
                    s.close();
duke@0
  2804
                } catch (IOException e) {
duke@0
  2805
                    /* Ignore any errors, as this stream may have already
duke@0
  2806
                     * thrown a related exception which is the one that
duke@0
  2807
                     * should be reported.
duke@0
  2808
                     */
duke@0
  2809
                }
duke@0
  2810
            }
duke@0
  2811
        }
ksrini@1265
  2812
        /*
ksrini@1265
  2813
         * ensureCapacity will increase the buffer as needed, taking note that
ksrini@1265
  2814
         * the new buffer will always be greater than the needed and never
ksrini@1265
  2815
         * exactly equal to the needed size or bp. If equal then the read (above)
ksrini@1265
  2816
         * will infinitely loop as buf.length - bp == 0.
ksrini@1265
  2817
         */
duke@0
  2818
        private static byte[] ensureCapacity(byte[] buf, int needed) {
ksrini@1265
  2819
            if (buf.length <= needed) {
duke@0
  2820
                byte[] old = buf;
duke@0
  2821
                buf = new byte[Integer.highestOneBit(needed) << 1];
duke@0
  2822
                System.arraycopy(old, 0, buf, 0, old.length);
duke@0
  2823
            }
duke@0
  2824
            return buf;
duke@0
  2825
        }
duke@0
  2826
jjg@4002
  2827
    /** We can only read a single class file at a time; this
jjg@4002
  2828
     *  flag keeps track of when we are currently reading a class
jjg@4002
  2829
     *  file.
jjg@4002
  2830
     */
jjg@4002
  2831
    public boolean filling = false;
jfranck@3822
  2832
jjg@4002
  2833
/************************************************************************
jjg@4002
  2834
 * Adjusting flags
jjg@4002
  2835
 ***********************************************************************/
jjg@4002
  2836
jjg@4002
  2837
    long adjustFieldFlags(long flags) {
dbalek@4426
  2838
        return flags | FROMCLASS;
duke@0
  2839
    }
duke@0
  2840
jjg@4002
  2841
    long adjustMethodFlags(long flags) {
jjg@4002
  2842
        if ((flags & ACC_BRIDGE) != 0) {
jjg@4002
  2843
            flags &= ~ACC_BRIDGE;
jjg@4002
  2844
            flags |= BRIDGE;
duke@0
  2845
        }
jjg@4002
  2846
        if ((flags & ACC_VARARGS) != 0) {
jjg@4002
  2847
            flags &= ~ACC_VARARGS;
jjg@4002
  2848
            flags |= VARARGS;
jjg@4002
  2849
        }
dbalek@4426
  2850
        flags |= FROMCLASS;
jjg@4002
  2851
        return flags;
duke@0
  2852
    }
duke@0
  2853
jjg@4002
  2854
    long adjustClassFlags(long flags) {
alanb@5016
  2855
        if ((flags & ACC_MODULE) != 0) {
alanb@5016
  2856
            flags &= ~ACC_MODULE;
alanb@5016
  2857
            flags |= MODULE;
alanb@5016
  2858
        }
dbalek@4426
  2859
        flags |= FROMCLASS;
jjg@4002
  2860
        return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
duke@0
  2861
    }
duke@0
  2862
duke@0
  2863
    /**
duke@0
  2864
     * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
duke@0
  2865
     * The attribute is only the last component of the original filename, so is unlikely
duke@0
  2866
     * to be valid as is, so operations other than those to access the name throw
duke@0
  2867
     * UnsupportedOperationException
duke@0
  2868
     */
jjg@4877
  2869
    private static class SourceFileObject implements JavaFileObject {
duke@0
  2870
duke@0
  2871
        /** The file's name.
duke@0
  2872
         */
jjg@4877
  2873
        private final Name name;
jjg@4877
  2874
        private final Name flatname;
duke@0
  2875
jjg@231
  2876
        public SourceFileObject(Name name, Name flatname) {
duke@0
  2877
            this.name = name;
jjg@231
  2878
            this.flatname = flatname;
duke@0
  2879
        }
duke@0
  2880
jlahoda@4176
  2881
        @Override @DefinedBy(Api.COMPILER)
jjg@705
  2882
        public URI toUri() {
jjg@705
  2883
            try {
jjg@705
  2884
                return new URI(null, name.toString(), null);
jjg@705
  2885
            } catch (URISyntaxException e) {
jjg@4877
  2886
                throw new PathFileObject.CannotCreateUriError(name.toString(), e);
jjg@705
  2887
            }
jjg@705
  2888
        }
jjg@705
  2889
jlahoda@4176
  2890
        @Override @DefinedBy(Api.COMPILER)
jjg@705
  2891
        public String getName() {
jjg@705
  2892
            return name.toString();
jjg@705
  2893
        }
jjg@705
  2894
jlahoda@4176
  2895
        @Override @DefinedBy(Api.COMPILER)
jjg@705
  2896
        public JavaFileObject.Kind getKind() {
jjg@4877
  2897
            return BaseFileManager.getKind(getName());
jjg@705
  2898
        }
jjg@705
  2899
jlahoda@4176
  2900
        @Override @DefinedBy(Api.COMPILER)
duke@0
  2901
        public InputStream openInputStream() {
duke@0
  2902
            throw new UnsupportedOperationException();
duke@0
  2903
        }
duke@0
  2904
jlahoda@4176
  2905
        @Override @DefinedBy(Api.COMPILER)
duke@0
  2906
        public OutputStream openOutputStream() {
duke@0
  2907
            throw new UnsupportedOperationException();
duke@0
  2908
        }
duke@0
  2909
jlahoda@4176
  2910
        @Override @DefinedBy(Api.COMPILER)
jjg@705
  2911
        public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
duke@0
  2912
            throw new UnsupportedOperationException();
duke@0
  2913
        }
duke@0
  2914
jlahoda@4176
  2915
        @Override @DefinedBy(Api.COMPILER)
jjg@705
  2916
        public Reader openReader(boolean ignoreEncodingErrors) {
jjg@705
  2917
            throw new UnsupportedOperationException();
jjg@705
  2918
        }
jjg@705
  2919
jlahoda@4176
  2920
        @Override @DefinedBy(Api.COMPILER)
duke@0
  2921
        public Writer openWriter() {
duke@0
  2922
            throw new UnsupportedOperationException();
duke@0
  2923
        }
duke@0
  2924
jlahoda@4176
  2925
        @Override @DefinedBy(Api.COMPILER)
duke@0
  2926
        public long getLastModified() {
duke@0
  2927
            throw new UnsupportedOperationException();
duke@0
  2928
        }
duke@0
  2929
jlahoda@4176
  2930
        @Override @DefinedBy(Api.COMPILER)
duke@0
  2931
        public boolean delete() {
duke@0
  2932
            throw new UnsupportedOperationException();
duke@0
  2933
        }
duke@0
  2934
jjg@4877
  2935
        @Override @DefinedBy(Api.COMPILER)
jjg@4877
  2936
        public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) {
jjg@4877
  2937
            return true; // fail-safe mode
jjg@705
  2938
        }
jjg@705
  2939
jlahoda@4176
  2940
        @Override @DefinedBy(Api.COMPILER)
jjg@4877
  2941
        public NestingKind getNestingKind() {
jjg@4877
  2942
            return null;
jjg@4877
  2943
        }
jjg@4877
  2944
jjg@4877
  2945
        @Override @DefinedBy(Api.COMPILER)
jjg@4877
  2946
        public Modifier getAccessLevel() {
jjg@4877
  2947
            return null;
duke@0
  2948
        }
duke@0
  2949
jjg@714
  2950
        /**
jjg@714
  2951
         * Check if two file objects are equal.
jjg@714
  2952
         * SourceFileObjects are just placeholder objects for the value of a
jjg@714
  2953
         * SourceFile attribute, and do not directly represent specific files.
jjg@714
  2954
         * Two SourceFileObjects are equal if their names are equal.
jjg@714
  2955
         */
duke@0
  2956
        @Override
duke@0
  2957
        public boolean equals(Object other) {
jjg@714
  2958
            if (this == other)
jjg@714
  2959
                return true;
jjg@714
  2960
duke@0
  2961
            if (!(other instanceof SourceFileObject))
duke@0
  2962
                return false;
jjg@714
  2963
duke@0
  2964
            SourceFileObject o = (SourceFileObject) other;
duke@0
  2965
            return name.equals(o.name);
duke@0
  2966
        }
duke@0
  2967
duke@0
  2968
        @Override
duke@0
  2969
        public int hashCode() {
duke@0
  2970
            return name.hashCode();
duke@0
  2971
        }
duke@0
  2972
    }
jjg@4454
  2973
jjg@4454
  2974
    private class CompleterDeproxy implements AnnotationTypeCompleter {
jjg@4454
  2975
        ClassSymbol proxyOn;
jjg@4454
  2976
        CompoundAnnotationProxy target;
jjg@4454
  2977
        CompoundAnnotationProxy repeatable;
jjg@4454
  2978
jjg@4454
  2979
        public CompleterDeproxy(ClassSymbol c, CompoundAnnotationProxy target,
jjg@4454
  2980
                CompoundAnnotationProxy repeatable)
jjg@4454
  2981
        {
jjg@4454
  2982
            this.proxyOn = c;
jjg@4454
  2983
            this.target = target;
jjg@4454
  2984
            this.repeatable = repeatable;
jjg@4454
  2985
        }
jjg@4454
  2986
jjg@4454
  2987
        @Override
jjg@4454
  2988
        public void complete(ClassSymbol sym) {
jjg@4454
  2989
            Assert.check(proxyOn == sym);
jjg@4454
  2990
            Attribute.Compound theTarget = null, theRepeatable = null;
jjg@4454
  2991
            AnnotationDeproxy deproxy;
jjg@4454
  2992
jjg@4454
  2993
            try {
jjg@4454
  2994
                if (target != null) {
jjg@4454
  2995
                    deproxy = new AnnotationDeproxy(proxyOn);
jjg@4454
  2996
                    theTarget = deproxy.deproxyCompound(target);
jjg@4454
  2997
                }
jjg@4454
  2998
jjg@4454
  2999
                if (repeatable != null) {
jjg@4454
  3000
                    deproxy = new AnnotationDeproxy(proxyOn);
jjg@4454
  3001
                    theRepeatable = deproxy.deproxyCompound(repeatable);
jjg@4454
  3002
                }
jjg@4454
  3003
            } catch (Exception e) {
jjg@4454
  3004
                throw new CompletionFailure(sym, e.getMessage());
jjg@4454
  3005
            }
jjg@4454
  3006
jjg@4454
  3007
            sym.getAnnotationTypeMetadata().setTarget(theTarget);
jjg@4454
  3008
            sym.getAnnotationTypeMetadata().setRepeatable(theRepeatable);
jjg@4454
  3009
        }
jjg@4454
  3010
    }
alanb@5016
  3011
alanb@5550
  3012
    private class ProxyType extends Type {
alanb@5550
  3013
alanb@5550
  3014
        private final byte[] content;
alanb@5550
  3015
alanb@5550
  3016
        public ProxyType(byte[] content) {
alanb@5550
  3017
            super(syms.noSymbol, TypeMetadata.EMPTY);
alanb@5550
  3018
            this.content = content;
alanb@5550
  3019
        }
alanb@5550
  3020
alanb@5550
  3021
        @Override
alanb@5550
  3022
        public TypeTag getTag() {
alanb@5550
  3023
            return TypeTag.NONE;
alanb@5550
  3024
        }
alanb@5550
  3025
alanb@5550
  3026
        @Override
alanb@5550
  3027
        public Type cloneWithMetadata(TypeMetadata metadata) {
alanb@5550
  3028
            throw new UnsupportedOperationException();
alanb@5550
  3029
        }
alanb@5550
  3030
alanb@5550
  3031
        public Type resolve() {
alanb@5550
  3032
            return sigToType(content, 0, content.length);
alanb@5550
  3033
        }
alanb@5550
  3034
alanb@5550
  3035
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
alanb@5550
  3036
        public String toString() {
alanb@5550
  3037
            return "<ProxyType>";
alanb@5550
  3038
        }
alanb@5550
  3039
alanb@5550
  3040
    }
alanb@5550
  3041
alanb@5016
  3042
    private static final class InterimUsesDirective {
alanb@5016
  3043
        public final Name service;
alanb@5016
  3044
alanb@5016
  3045
        public InterimUsesDirective(Name service) {
alanb@5016
  3046
            this.service = service;
alanb@5016
  3047
        }
alanb@5016
  3048
alanb@5016
  3049
    }
alanb@5016
  3050
alanb@5016
  3051
    private static final class InterimProvidesDirective {
alanb@5016
  3052
        public final Name service;
alanb@5550
  3053
        public final List<Name> impls;
alanb@5016
  3054
alanb@5550
  3055
        public InterimProvidesDirective(Name service, List<Name> impls) {
alanb@5016
  3056
            this.service = service;
alanb@5550
  3057
            this.impls = impls;
alanb@5016
  3058
        }
alanb@5016
  3059
alanb@5016
  3060
    }
alanb@5016
  3061
alanb@5016
  3062
    private final class UsesProvidesCompleter implements Completer {
alanb@5016
  3063
        private final ModuleSymbol currentModule;
alanb@5016
  3064
        private final List<InterimUsesDirective> interimUsesCopy;
alanb@5016
  3065
        private final List<InterimProvidesDirective> interimProvidesCopy;
alanb@5016
  3066
alanb@5016
  3067
        public UsesProvidesCompleter(ModuleSymbol currentModule, List<InterimUsesDirective> interimUsesCopy, List<InterimProvidesDirective> interimProvidesCopy) {
alanb@5016
  3068
            this.currentModule = currentModule;
alanb@5016
  3069
            this.interimUsesCopy = interimUsesCopy;
alanb@5016
  3070
            this.interimProvidesCopy = interimProvidesCopy;
alanb@5016
  3071
        }
alanb@5016
  3072
alanb@5016
  3073
        @Override
alanb@5016
  3074
        public void complete(Symbol sym) throws CompletionFailure {
alanb@5016
  3075
            ListBuffer<Directive> directives = new ListBuffer<>();
alanb@5016
  3076
            directives.addAll(currentModule.directives);
alanb@5016
  3077
            ListBuffer<UsesDirective> uses = new ListBuffer<>();
alanb@5016
  3078
            for (InterimUsesDirective interim : interimUsesCopy) {
alanb@5016
  3079
                UsesDirective d = new UsesDirective(syms.enterClass(currentModule, interim.service));
alanb@5016
  3080
                uses.add(d);
alanb@5016
  3081
                directives.add(d);
alanb@5016
  3082
            }
alanb@5016
  3083
            currentModule.uses = uses.toList();
alanb@5016
  3084
            ListBuffer<ProvidesDirective> provides = new ListBuffer<>();
alanb@5016
  3085
            for (InterimProvidesDirective interim : interimProvidesCopy) {
alanb@5550
  3086
                ListBuffer<ClassSymbol> impls = new ListBuffer<>();
alanb@5550
  3087
                for (Name impl : interim.impls) {
alanb@5550
  3088
                    impls.append(syms.enterClass(currentModule, impl));
alanb@5550
  3089
                }
alanb@5016
  3090
                ProvidesDirective d = new ProvidesDirective(syms.enterClass(currentModule, interim.service),
alanb@5550
  3091
                                                            impls.toList());
alanb@5016
  3092
                provides.add(d);
alanb@5016
  3093
                directives.add(d);
alanb@5016
  3094
            }
alanb@5016
  3095
            currentModule.provides = provides.toList();
tzezula@5367
  3096
            for (RequiresDirective rd : currentModule.requires) {
tzezula@5367
  3097
                if (rd.flags.contains(RequiresFlag.EXTRA)) {
tzezula@5367
  3098
                    directives.add(rd);
tzezula@5367
  3099
                }
tzezula@5367
  3100
            }
alanb@5016
  3101
            currentModule.directives = directives.toList();
alanb@5016
  3102
        }
alanb@5016
  3103
    }
duke@0
  3104
}