langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 5857 84d4886d48aa
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
     2
 * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4872
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.jvm;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.tools.FileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.code.Type.*;
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3300
diff changeset
    39
import com.sun.tools.javac.file.BaseFileObject;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import static com.sun.tools.javac.code.BoundKind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import static com.sun.tools.javac.jvm.UninitializedType.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import static javax.tools.StandardLocation.CLASS_OUTPUT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
/** This class provides operations to map an internal symbol table graph
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  rooted in a ClassSymbol into a classfile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    52
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    53
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
public class ClassWriter extends ClassFile {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    protected static final Context.Key<ClassWriter> classWriterKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        new Context.Key<ClassWriter>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private final Options options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
    65
    /** Switch: debugging output for JSR 308-related operations.
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
    66
     */
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
    67
    private boolean debugJSR308;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
    68
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** Switch: verbose output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private boolean verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /** Switch: scrable private names.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private boolean scramble;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** Switch: scrable private names.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private boolean scrambleAll;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /** Switch: retrofit mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private boolean retrofit;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** Switch: emit source file attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    private boolean emitSourceFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /** Switch: generate CharacterRangeTable attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    private boolean genCrt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /** Switch: describe the generated stackmap
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    boolean debugstackmap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Target class version.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    private Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * Source language version.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    private Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /** Type utilities. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /** The initial sizes of the data and constant pool buffers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *  sizes are increased when buffers get full.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    static final int DATA_BUF_SIZE = 0x0fff0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    static final int POOL_BUF_SIZE = 0x1fff0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /** An output buffer for member info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    ByteBuffer databuf = new ByteBuffer(DATA_BUF_SIZE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    /** An output buffer for the constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    ByteBuffer poolbuf = new ByteBuffer(POOL_BUF_SIZE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /** An output buffer for type signatures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    ByteBuffer sigbuf = new ByteBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    /** The constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    Pool pool;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    /** The inner classes to be written, as a set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    Set<ClassSymbol> innerClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /** The inner classes to be written, as a queue where
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *  enclosing classes come first.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    ListBuffer<ClassSymbol> innerClassesQueue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    /** The log to use for verbose output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /** The name table. */
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   146
    private final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Access to files. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    private final JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    /** The tags and constants used in compressed stackmap. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    static final int SAME_FRAME_SIZE = 64;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    static final int SAME_FRAME_EXTENDED = 251;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    static final int FULL_FRAME = 255;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    static final int MAX_LOCAL_LENGTH_DIFF = 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /** Get the ClassWriter instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public static ClassWriter instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        ClassWriter instance = context.get(classWriterKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            instance = new ClassWriter(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /** Construct a class writer, given an options table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    private ClassWriter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        context.put(classWriterKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        log = Log.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   172
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        target = Target.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   180
        debugJSR308    = options.get("TA:writer") != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        verbose        = options.get("-verbose")     != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        scramble       = options.get("-scramble")    != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        scrambleAll    = options.get("-scrambleAll") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        retrofit       = options.get("-retrofit") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        genCrt         = options.get("-Xjcov") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        debugstackmap  = options.get("debugstackmap") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        emitSourceFile = options.get("-g:")==null || options.get("-g:source")!=null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        String dumpModFlags = options.get("dumpmodifiers");
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        dumpClassModifiers =
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            (dumpModFlags != null && dumpModFlags.indexOf('c') != -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        dumpFieldModifiers =
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            (dumpModFlags != null && dumpModFlags.indexOf('f') != -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        dumpInnerClassModifiers =
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            (dumpModFlags != null && dumpModFlags.indexOf('i') != -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        dumpMethodModifiers =
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            (dumpModFlags != null && dumpModFlags.indexOf('m') != -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
/******************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
 * Diagnostics: dump generated class names and modifiers
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
 ******************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    /** Value of option 'dumpmodifiers' is a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     *  indicating which modifiers should be dumped for debugging:
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     *    'c' -- classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     *    'f' -- fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     *    'i' -- innerclass attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     *    'm' -- methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     *  For example, to dump everything:
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     *    javac -XDdumpmodifiers=cifm MyProg.java
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    private final boolean dumpClassModifiers; // -XDdumpmodifiers=c
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    private final boolean dumpFieldModifiers; // -XDdumpmodifiers=f
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    private final boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    private final boolean dumpMethodModifiers; // -XDdumpmodifiers=m
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    /** Return flags as a string, separated by " ".
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    public static String flagNames(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        StringBuffer sbuf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        long f = flags & StandardFlags;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        while (f != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            if ((f & 1) != 0) sbuf.append(" " + flagName[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            f = f >> 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        return sbuf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        private final static String[] flagName = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            "SUPER", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE",
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            "ABSTRACT", "STRICTFP"};
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
/******************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
 * Output routines
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
 ******************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    /** Write a character into given byte buffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     *  byte buffer will not be grown.
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    void putChar(ByteBuffer buf, int op, int x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        buf.elems[op  ] = (byte)((x >>  8) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        buf.elems[op+1] = (byte)((x      ) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    /** Write an integer into given byte buffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     *  byte buffer will not be grown.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    void putInt(ByteBuffer buf, int adr, int x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        buf.elems[adr  ] = (byte)((x >> 24) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        buf.elems[adr+1] = (byte)((x >> 16) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        buf.elems[adr+2] = (byte)((x >>  8) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        buf.elems[adr+3] = (byte)((x      ) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
/******************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
 * Signature Generation
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
 ******************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
    /** Assemble signature of given type in string buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    void assembleSig(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        switch (type.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            sigbuf.appendByte('B');
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            sigbuf.appendByte('S');
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            sigbuf.appendByte('C');
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            sigbuf.appendByte('I');
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            sigbuf.appendByte('J');
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            sigbuf.appendByte('F');
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            sigbuf.appendByte('D');
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            sigbuf.appendByte('Z');
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            sigbuf.appendByte('V');
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            sigbuf.appendByte('L');
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            assembleClassSig(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            sigbuf.appendByte(';');
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            ArrayType at = (ArrayType)type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            sigbuf.appendByte('[');
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            assembleSig(at.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            MethodType mt = (MethodType)type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            sigbuf.appendByte('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            assembleSig(mt.argtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            sigbuf.appendByte(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            assembleSig(mt.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            if (hasTypeVar(mt.thrown)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    sigbuf.appendByte('^');
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    assembleSig(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        case WILDCARD: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            WildcardType ta = (WildcardType) type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            switch (ta.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                sigbuf.appendByte('-');
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                assembleSig(ta.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            case EXTENDS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                sigbuf.appendByte('+');
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                assembleSig(ta.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            case UNBOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                sigbuf.appendByte('*');
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                throw new AssertionError(ta.kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            sigbuf.appendByte('T');
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            sigbuf.appendName(type.tsym.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            sigbuf.appendByte(';');
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        case FORALL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            ForAll ft = (ForAll)type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            assembleParamsSig(ft.tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            assembleSig(ft.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        case UNINITIALIZED_THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        case UNINITIALIZED_OBJECT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            // we don't yet have a spec for uninitialized types in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            // local variable table
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            assembleSig(types.erasure(((UninitializedType)type).qtype));
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            throw new AssertionError("typeSig " + type.tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    boolean hasTypeVar(List<Type> l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        while (l.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            if (l.head.tag == TypeTags.TYPEVAR) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
    void assembleClassSig(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        ClassType ct = (ClassType)type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        ClassSymbol c = (ClassSymbol)ct.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        enterInner(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        Type outer = ct.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        if (outer.allparams().nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            boolean rawOuter =
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                c.owner.kind == MTH || // either a local class
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                c.name == names.empty; // or anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            assembleClassSig(rawOuter
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                             ? types.erasure(outer)
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                             : outer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            sigbuf.appendByte('.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            assert c.flatname.startsWith(c.owner.enclClass().flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            sigbuf.appendName(rawOuter
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   382
                              ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength()+1,c.flatname.getByteLength())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                              : c.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            sigbuf.appendBytes(externalize(c.flatname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        if (ct.getTypeArguments().nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            sigbuf.appendByte('<');
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            assembleSig(ct.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            sigbuf.appendByte('>');
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    void assembleSig(List<Type> types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            assembleSig(ts.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    void assembleParamsSig(List<Type> typarams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        sigbuf.appendByte('<');
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            TypeVar tvar = (TypeVar)ts.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            sigbuf.appendName(tvar.tsym.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            List<Type> bounds = types.getBounds(tvar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                sigbuf.appendByte(':');
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                sigbuf.appendByte(':');
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                assembleSig(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        sigbuf.appendByte('>');
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    /** Return signature of given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
    Name typeSig(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        assert sigbuf.length == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        //- System.out.println(" ? " + type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        assembleSig(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        Name n = sigbuf.toName(names);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        sigbuf.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        //- System.out.println("   " + n);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        return n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    /** Given a type t, return the extended class name of its erasure in
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     *  external representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    public Name xClassName(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        if (t.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            return names.fromUtf(externalize(t.tsym.flatName()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        } else if (t.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            return typeSig(types.erasure(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            throw new AssertionError("xClassName");
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
/******************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
 * Writing the Constant Pool
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
 ******************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
    /** Thrown when the constant pool is over full.
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    public static class PoolOverflow extends Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        public PoolOverflow() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    public static class StringOverflow extends Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        public final String value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        public StringOverflow(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            value = s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    /** Write constant pool to pool buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
     *  Note: during writing, constant pool
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     *  might grow since some parts of constants still need to be entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
    void writePool(Pool pool) throws PoolOverflow, StringOverflow {
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        int poolCountIdx = poolbuf.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        poolbuf.appendChar(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        while (i < pool.pp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            Object value = pool.pool[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            assert value != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
            if (value instanceof Pool.Method)
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                value = ((Pool.Method)value).m;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            else if (value instanceof Pool.Variable)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                value = ((Pool.Variable)value).v;
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            if (value instanceof MethodSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                MethodSymbol m = (MethodSymbol)value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                poolbuf.appendByte((m.owner.flags() & INTERFACE) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                          ? CONSTANT_InterfaceMethodref
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                          : CONSTANT_Methodref);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                poolbuf.appendChar(pool.put(m.owner));
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                poolbuf.appendChar(pool.put(nameType(m)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            } else if (value instanceof VarSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                VarSymbol v = (VarSymbol)value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                poolbuf.appendByte(CONSTANT_Fieldref);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                poolbuf.appendChar(pool.put(v.owner));
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                poolbuf.appendChar(pool.put(nameType(v)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            } else if (value instanceof Name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                poolbuf.appendByte(CONSTANT_Utf8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                byte[] bs = ((Name)value).toUtf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                poolbuf.appendChar(bs.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                poolbuf.appendBytes(bs, 0, bs.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                if (bs.length > Pool.MAX_STRING_LENGTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    throw new StringOverflow(value.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            } else if (value instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                ClassSymbol c = (ClassSymbol)value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                if (c.owner.kind == TYP) pool.put(c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                poolbuf.appendByte(CONSTANT_Class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                if (c.type.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                    poolbuf.appendChar(pool.put(typeSig(c.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                    poolbuf.appendChar(pool.put(names.fromUtf(externalize(c.flatname))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                    enterInner(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            } else if (value instanceof NameAndType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                NameAndType nt = (NameAndType)value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
                poolbuf.appendByte(CONSTANT_NameandType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
                poolbuf.appendChar(pool.put(nt.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                poolbuf.appendChar(pool.put(typeSig(nt.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            } else if (value instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                poolbuf.appendByte(CONSTANT_Integer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                poolbuf.appendInt(((Integer)value).intValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            } else if (value instanceof Long) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                poolbuf.appendByte(CONSTANT_Long);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                poolbuf.appendLong(((Long)value).longValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
            } else if (value instanceof Float) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                poolbuf.appendByte(CONSTANT_Float);
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                poolbuf.appendFloat(((Float)value).floatValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            } else if (value instanceof Double) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                poolbuf.appendByte(CONSTANT_Double);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                poolbuf.appendDouble(((Double)value).doubleValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            } else if (value instanceof String) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                poolbuf.appendByte(CONSTANT_String);
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                poolbuf.appendChar(pool.put(names.fromString((String)value)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            } else if (value instanceof Type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                Type type = (Type)value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                if (type.tag == CLASS) enterInner((ClassSymbol)type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                poolbuf.appendByte(CONSTANT_Class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                poolbuf.appendChar(pool.put(xClassName(type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                assert false : "writePool " + value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        if (pool.pp > Pool.MAX_ENTRIES)
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            throw new PoolOverflow();
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        putChar(poolbuf, poolCountIdx, pool.pp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    /** Given a field, return its name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    Name fieldName(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        if (scramble && (sym.flags() & PRIVATE) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            scrambleAll && (sym.flags() & (PROTECTED | PUBLIC)) == 0)
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   547
            return names.fromString("_$" + sym.name.getIndex());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
            return sym.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    /** Given a symbol, return its name-and-type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
    NameAndType nameType(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        return new NameAndType(fieldName(sym),
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                               retrofit
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                               ? sym.erasure(types)
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                               : sym.externalType(types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        // if we retrofit, then the NameAndType has been read in as is
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        // and no change is necessary. If we compile normally, the
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        // NameAndType is generated from a symbol reference, and the
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        // adjustment of adding an additional this$n parameter needs to be made.
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
/******************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
 * Writing Attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
 ******************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    /** Write header for an attribute to data buffer and return
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
     *  position past attribute length index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
    int writeAttr(Name attrName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        databuf.appendChar(pool.put(attrName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        databuf.appendInt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        return databuf.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    /** Fill in attribute length.
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    void endAttr(int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        putInt(databuf, index - 4, databuf.length - index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    /** Leave space for attribute count and return index for
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     *  number of attributes field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
    int beginAttrs() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
        databuf.appendChar(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        return databuf.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
    /** Fill in number of attributes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    void endAttrs(int index, int count) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        putChar(databuf, index - 2, count);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
    /** Write the EnclosingMethod attribute if needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
     *  Returns the number of attributes written (0 or 1).
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    int writeEnclosingMethodAttribute(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        if (!target.hasEnclosingMethodAttribute() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            c.owner.kind != MTH && // neither a local class
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            c.name != names.empty) // nor anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        int alenIdx = writeAttr(names.EnclosingMethod);
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        ClassSymbol enclClass = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        MethodSymbol enclMethod =
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            (c.owner.type == null // local to init block
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
             || c.owner.kind != MTH) // or member init
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
            ? null
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            : (MethodSymbol)c.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        databuf.appendChar(pool.put(enclClass));
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        databuf.appendChar(enclMethod == null ? 0 : pool.put(nameType(c.owner)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        return 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
    /** Write flag attributes; return number of attributes written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
    int writeFlagAttrs(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        int acount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
        if ((flags & DEPRECATED) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            int alenIdx = writeAttr(names.Deprecated);
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        if ((flags & ENUM) != 0 && !target.useEnumFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            int alenIdx = writeAttr(names.Enum);
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
        if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            int alenIdx = writeAttr(names.Synthetic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        if ((flags & BRIDGE) != 0 && !target.useBridgeFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
            int alenIdx = writeAttr(names.Bridge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        if ((flags & VARARGS) != 0 && !target.useVarargsFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            int alenIdx = writeAttr(names.Varargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
        if ((flags & ANNOTATION) != 0 && !target.useAnnotationFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            int alenIdx = writeAttr(names.Annotation);
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
        return acount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    /** Write member (field or method) attributes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
     *  return number of attributes written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    int writeMemberAttrs(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        int acount = writeFlagAttrs(sym.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
        long flags = sym.flags();
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
        if (source.allowGenerics() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
            (flags & (SYNTHETIC|BRIDGE)) != SYNTHETIC &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            (flags & ANONCONSTR) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            (!types.isSameType(sym.type, sym.erasure(types)) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
             hasTypeVar(sym.type.getThrownTypes()))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
            // note that a local class with captured variables
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
            // will get a signature attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
            int alenIdx = writeAttr(names.Signature);
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            databuf.appendChar(pool.put(typeSig(sym.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        acount += writeJavaAnnotations(sym.getAnnotationMirrors());
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   676
        acount += writeTypeAnnotations(sym.typeAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        return acount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    /** Write method parameter annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
     *  return number of attributes written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
    int writeParameterAttrs(MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        boolean hasVisible = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        boolean hasInvisible = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        if (m.params != null) for (VarSymbol s : m.params) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            for (Attribute.Compound a : s.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
                switch (getRetention(a.type.tsym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                case SOURCE: break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                case CLASS: hasInvisible = true; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                case RUNTIME: hasVisible = true; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                default: ;// /* fail soft */ throw new AssertionError(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
        int attrCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
        if (hasVisible) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            int attrIndex = writeAttr(names.RuntimeVisibleParameterAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            databuf.appendByte(m.params.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            for (VarSymbol s : m.params) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
                ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
                for (Attribute.Compound a : s.getAnnotationMirrors())
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                    if (getRetention(a.type.tsym) == RetentionPolicy.RUNTIME)
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                        buf.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
                databuf.appendChar(buf.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                for (Attribute.Compound a : buf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                    writeCompoundAttribute(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            endAttr(attrIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
            attrCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
        if (hasInvisible) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            int attrIndex = writeAttr(names.RuntimeInvisibleParameterAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            databuf.appendByte(m.params.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            for (VarSymbol s : m.params) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                for (Attribute.Compound a : s.getAnnotationMirrors())
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                    if (getRetention(a.type.tsym) == RetentionPolicy.CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                        buf.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                databuf.appendChar(buf.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                for (Attribute.Compound a : buf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                    writeCompoundAttribute(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
            endAttr(attrIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
            attrCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        return attrCount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
/**********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
 * Writing Java-language annotations (aka metadata, attributes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
 **********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    /** Write Java-language annotations; return number of JVM
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     *  attributes written (zero or one).
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
    int writeJavaAnnotations(List<Attribute.Compound> attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        if (attrs.isEmpty()) return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        ListBuffer<Attribute.Compound> visibles = new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
        ListBuffer<Attribute.Compound> invisibles = new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        for (Attribute.Compound a : attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
            switch (getRetention(a.type.tsym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            case SOURCE: break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            case CLASS: invisibles.append(a); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            case RUNTIME: visibles.append(a); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            default: ;// /* fail soft */ throw new AssertionError(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
        int attrCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
        if (visibles.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
            int attrIndex = writeAttr(names.RuntimeVisibleAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            databuf.appendChar(visibles.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
            for (Attribute.Compound a : visibles)
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
                writeCompoundAttribute(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
            endAttr(attrIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
            attrCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
        if (invisibles.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            int attrIndex = writeAttr(names.RuntimeInvisibleAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            databuf.appendChar(invisibles.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            for (Attribute.Compound a : invisibles)
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                writeCompoundAttribute(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            endAttr(attrIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            attrCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        return attrCount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   771
    int writeTypeAnnotations(List<Attribute.TypeCompound> typeAnnos) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   772
        if (typeAnnos.isEmpty()) return 0;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   773
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   774
        ListBuffer<Attribute.TypeCompound> visibles = ListBuffer.lb();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   775
        ListBuffer<Attribute.TypeCompound> invisibles = ListBuffer.lb();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   776
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   777
        for (Attribute.TypeCompound tc : typeAnnos) {
3151
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   778
            if (tc.position.type == TargetType.UNKNOWN
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   779
                || !tc.position.emitToClassfile())
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   780
                continue;
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   781
            switch (getRetention(tc.type.tsym)) {
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   782
            case SOURCE: break;
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   783
            case CLASS: invisibles.append(tc); break;
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   784
            case RUNTIME: visibles.append(tc); break;
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   785
            default: ;// /* fail soft */ throw new AssertionError(vis);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   786
            }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   787
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   788
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   789
        int attrCount = 0;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   790
        if (visibles.length() != 0) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   791
            int attrIndex = writeAttr(names.RuntimeVisibleTypeAnnotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   792
            databuf.appendChar(visibles.length());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   793
            for (Attribute.TypeCompound p : visibles)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   794
                writeTypeAnnotation(p);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   795
            endAttr(attrIndex);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   796
            attrCount++;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   797
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   798
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   799
        if (invisibles.length() != 0) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   800
            int attrIndex = writeAttr(names.RuntimeInvisibleTypeAnnotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   801
            databuf.appendChar(invisibles.length());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   802
            for (Attribute.TypeCompound p : invisibles)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   803
                writeTypeAnnotation(p);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   804
            endAttr(attrIndex);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   805
            attrCount++;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   806
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   807
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   808
        return attrCount;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   809
    }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   810
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
    /** A mirror of java.lang.annotation.RetentionPolicy. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
    enum RetentionPolicy {
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        SOURCE,
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        CLASS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        RUNTIME
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
    RetentionPolicy getRetention(TypeSymbol annotationType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        RetentionPolicy vis = RetentionPolicy.CLASS; // the default
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        Attribute.Compound c = annotationType.attribute(syms.retentionType.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
        if (c != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
            Attribute value = c.member(names.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            if (value != null && value instanceof Attribute.Enum) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
                Name levelName = ((Attribute.Enum)value).value.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
                if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                else ;// /* fail soft */ throw new AssertionError(levelName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        return vis;
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
    /** A visitor to write an attribute including its leading
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
     *  single-character marker.
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    class AttributeWriter implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
        public void visitConstant(Attribute.Constant _value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
            Object value = _value.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
            switch (_value.type.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
            case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                databuf.appendByte('B');
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
            case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
                databuf.appendByte('C');
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
                databuf.appendByte('S');
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
            case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                databuf.appendByte('I');
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
                databuf.appendByte('J');
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
                databuf.appendByte('F');
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                databuf.appendByte('D');
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
            case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                databuf.appendByte('Z');
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                assert value instanceof String;
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                databuf.appendByte('s');
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                value = names.fromString(value.toString()); // CONSTANT_Utf8
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
                throw new AssertionError(_value.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            databuf.appendChar(pool.put(value));
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            databuf.appendByte('e');
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
            databuf.appendChar(pool.put(typeSig(e.value.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            databuf.appendChar(pool.put(e.value.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            databuf.appendByte('c');
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
            databuf.appendChar(pool.put(typeSig(clazz.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        public void visitCompound(Attribute.Compound compound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            databuf.appendByte('@');
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            writeCompoundAttribute(compound);
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        public void visitError(Attribute.Error x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            throw new AssertionError(x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
        public void visitArray(Attribute.Array array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            databuf.appendByte('[');
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            databuf.appendChar(array.values.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
            for (Attribute a : array.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                a.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
    AttributeWriter awriter = new AttributeWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
    /** Write a compound attribute excluding the '@' marker. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
    void writeCompoundAttribute(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
        databuf.appendChar(pool.put(typeSig(c.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
        databuf.appendChar(c.values.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
        for (Pair<Symbol.MethodSymbol,Attribute> p : c.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
            databuf.appendChar(pool.put(p.fst.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            p.snd.accept(awriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   911
    void writeTypeAnnotation(Attribute.TypeCompound c) {
3151
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   912
        if (debugJSR308)
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   913
            System.out.println("TA: writing " + c + " at " + c.position
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   914
                    + " in " + log.currentSourceFile());
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   915
        writeCompoundAttribute(c);
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
   916
        writePosition(c.position);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   917
    }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   918
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   919
    void writePosition(TypeAnnotationPosition p) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   920
        databuf.appendByte(p.type.targetTypeValue());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   921
        switch (p.type) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   922
        // type case
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   923
        case TYPECAST:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   924
        case TYPECAST_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   925
        // object creation
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   926
        case INSTANCEOF:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   927
        case INSTANCEOF_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   928
        // new expression
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   929
        case NEW:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   930
        case NEW_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   931
            databuf.appendChar(p.offset);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   932
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   933
         // local variable
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   934
        case LOCAL_VARIABLE:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   935
        case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   936
            databuf.appendChar(p.lvarOffset.length);  // for table length
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   937
            for (int i = 0; i < p.lvarOffset.length; ++i) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   938
                databuf.appendChar(p.lvarOffset[i]);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   939
                databuf.appendChar(p.lvarLength[i]);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   940
                databuf.appendChar(p.lvarIndex[i]);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   941
            }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   942
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   943
         // method receiver
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   944
        case METHOD_RECEIVER:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   945
            // Do nothing
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   946
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   947
        // type parameters
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   948
        case CLASS_TYPE_PARAMETER:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   949
        case METHOD_TYPE_PARAMETER:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   950
            databuf.appendByte(p.parameter_index);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   951
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   952
        // type parameters bounds
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   953
        case CLASS_TYPE_PARAMETER_BOUND:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   954
        case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   955
        case METHOD_TYPE_PARAMETER_BOUND:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   956
        case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   957
            databuf.appendByte(p.parameter_index);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   958
            databuf.appendByte(p.bound_index);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   959
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   960
         // wildcards
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   961
        case WILDCARD_BOUND:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   962
        case WILDCARD_BOUND_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   963
            writePosition(p.wildcard_position);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   964
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   965
         // Class extends and implements clauses
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   966
        case CLASS_EXTENDS:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   967
        case CLASS_EXTENDS_GENERIC_OR_ARRAY:
4872
6c49b0ac56a6 6919986: [308] change size of type_index (of CLASS_EXTENDS and THROWS) from byte to short
jjg
parents: 3995
diff changeset
   968
            databuf.appendChar(p.type_index);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   969
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   970
        // throws
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   971
        case THROWS:
4872
6c49b0ac56a6 6919986: [308] change size of type_index (of CLASS_EXTENDS and THROWS) from byte to short
jjg
parents: 3995
diff changeset
   972
            databuf.appendChar(p.type_index);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   973
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   974
        case CLASS_LITERAL:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   975
        case CLASS_LITERAL_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   976
            databuf.appendChar(p.offset);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   977
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   978
        // method parameter: not specified
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   979
        case METHOD_PARAMETER_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   980
            databuf.appendByte(p.parameter_index);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   981
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   982
        // method type argument: wasn't specified
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   983
        case NEW_TYPE_ARGUMENT:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   984
        case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   985
        case METHOD_TYPE_ARGUMENT:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   986
        case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   987
            databuf.appendChar(p.offset);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   988
            databuf.appendByte(p.type_index);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   989
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   990
        // We don't need to worry abut these
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   991
        case METHOD_RETURN_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   992
        case FIELD_GENERIC_OR_ARRAY:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   993
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   994
        case UNKNOWN:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   995
            break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   996
        default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   997
            throw new AssertionError("unknown position: " + p);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   998
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
   999
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1000
        // Append location data for generics/arrays.
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1001
        if (p.type.hasLocation()) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1002
            databuf.appendChar(p.location.size());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1003
            for (int i : p.location)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1004
                databuf.appendByte((byte)i);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1005
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1006
    }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1007
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
/**********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
 * Writing Objects
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
 **********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
    /** Enter an inner class into the `innerClasses' set/queue.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
    void enterInner(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
        assert !c.type.isCompound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
            c.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            System.err.println("error: " + c + ": " + ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            throw ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
        if (c.type.tag != CLASS) return; // arrays
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
        if (pool != null && // pool might be null if called from xClassName
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
            c.owner.kind != PCK &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
            (innerClasses == null || !innerClasses.contains(c))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
//          log.errWriter.println("enter inner " + c);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
            if (c.owner.kind == TYP) enterInner((ClassSymbol)c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
            pool.put(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            pool.put(c.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
            if (innerClasses == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                innerClasses = new HashSet<ClassSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                innerClassesQueue = new ListBuffer<ClassSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                pool.put(names.InnerClasses);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
            innerClasses.add(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
            innerClassesQueue.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
    /** Write "inner classes" attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
    void writeInnerClasses() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
        int alenIdx = writeAttr(names.InnerClasses);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
        databuf.appendChar(innerClassesQueue.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
        for (List<ClassSymbol> l = innerClassesQueue.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
             l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
            ClassSymbol inner = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
            char flags = (char) adjustFlags(inner.flags_field);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
            if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
            if (inner.name.isEmpty()) flags &= ~FINAL; // Anonymous class: unset FINAL flag
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
            if (dumpInnerClassModifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                log.errWriter.println("INNERCLASS  " + inner.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                log.errWriter.println("---" + flagNames(flags));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
            databuf.appendChar(pool.get(inner));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
            databuf.appendChar(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                inner.owner.kind == TYP ? pool.get(inner.owner) : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            databuf.appendChar(
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
  1060
                !inner.name.isEmpty() ? pool.get(inner.name) : 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            databuf.appendChar(flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
    /** Write field symbol, entering all references into constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
    void writeField(VarSymbol v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
        int flags = adjustFlags(v.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
        databuf.appendChar(flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
        if (dumpFieldModifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
            log.errWriter.println("FIELD  " + fieldName(v));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            log.errWriter.println("---" + flagNames(v.flags()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
        databuf.appendChar(pool.put(fieldName(v)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
        databuf.appendChar(pool.put(typeSig(v.erasure(types))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        int acountIdx = beginAttrs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
        int acount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
        if (v.getConstValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
            int alenIdx = writeAttr(names.ConstantValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            databuf.appendChar(pool.put(v.getConstValue()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        acount += writeMemberAttrs(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
        endAttrs(acountIdx, acount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
    /** Write method symbol, entering all references into constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
    void writeMethod(MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
        int flags = adjustFlags(m.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
        databuf.appendChar(flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
        if (dumpMethodModifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
            log.errWriter.println("METHOD  " + fieldName(m));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            log.errWriter.println("---" + flagNames(m.flags()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
        databuf.appendChar(pool.put(fieldName(m)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
        databuf.appendChar(pool.put(typeSig(m.externalType(types))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
        int acountIdx = beginAttrs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
        int acount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
        if (m.code != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
            int alenIdx = writeAttr(names.Code);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
            writeCode(m.code);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            m.code = null; // to conserve space
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
        List<Type> thrown = m.erasure(types).getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
        if (thrown.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
            int alenIdx = writeAttr(names.Exceptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
            databuf.appendChar(thrown.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
            for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                databuf.appendChar(pool.put(l.head.tsym));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
        if (m.defaultValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            int alenIdx = writeAttr(names.AnnotationDefault);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
            m.defaultValue.accept(awriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
        acount += writeMemberAttrs(m);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        acount += writeParameterAttrs(m);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
        endAttrs(acountIdx, acount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
    /** Write code attribute of method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
    void writeCode(Code code) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
        databuf.appendChar(code.max_stack);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
        databuf.appendChar(code.max_locals);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
        databuf.appendInt(code.cp);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
        databuf.appendBytes(code.code, 0, code.cp);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
        databuf.appendChar(code.catchInfo.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
        for (List<char[]> l = code.catchInfo.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
             l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
            for (int i = 0; i < l.head.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
                databuf.appendChar(l.head[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
        int acountIdx = beginAttrs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
        int acount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
        if (code.lineInfo.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
            int alenIdx = writeAttr(names.LineNumberTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
            databuf.appendChar(code.lineInfo.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
            for (List<char[]> l = code.lineInfo.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                 l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                 l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                for (int i = 0; i < l.head.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                    databuf.appendChar(l.head[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
        if (genCrt && (code.crt != null)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
            CRTable crt = code.crt;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            int alenIdx = writeAttr(names.CharacterRangeTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            int crtIdx = beginAttrs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
            int crtEntries = crt.writeCRT(databuf, code.lineMap, log);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            endAttrs(crtIdx, crtEntries);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
        // counter for number of generic local variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
        int nGenericVars = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        if (code.varBufferSize > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
            int alenIdx = writeAttr(names.LocalVariableTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            databuf.appendChar(code.varBufferSize);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
            for (int i=0; i<code.varBufferSize; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
                Code.LocalVar var = code.varBuffer[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
                // write variable info
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
                assert var.start_pc >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
                assert var.start_pc <= code.cp;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
                databuf.appendChar(var.start_pc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
                assert var.length >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                assert (var.start_pc + var.length) <= code.cp;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                databuf.appendChar(var.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
                VarSymbol sym = var.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                databuf.appendChar(pool.put(sym.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
                Type vartype = sym.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
                if (!types.isSameType(sym.type, vartype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
                    nGenericVars++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                databuf.appendChar(pool.put(typeSig(vartype)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                databuf.appendChar(var.reg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
        if (nGenericVars > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
            int alenIdx = writeAttr(names.LocalVariableTypeTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
            databuf.appendChar(nGenericVars);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
            int count = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
            for (int i=0; i<code.varBufferSize; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
                Code.LocalVar var = code.varBuffer[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
                VarSymbol sym = var.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
                if (types.isSameType(sym.type, sym.erasure(types)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
                    continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
                count++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
                // write variable info
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                databuf.appendChar(var.start_pc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                databuf.appendChar(var.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
                databuf.appendChar(pool.put(sym.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
                databuf.appendChar(pool.put(typeSig(sym.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
                databuf.appendChar(var.reg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
            assert count == nGenericVars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
        if (code.stackMapBufferSize > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
            if (debugstackmap) System.out.println("Stack map for " + code.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
            int alenIdx = writeAttr(code.stackMap.getAttributeName(names));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
            writeStackMap(code);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
        endAttrs(acountIdx, acount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
    void writeStackMap(Code code) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
        int nframes = code.stackMapBufferSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
        if (debugstackmap) System.out.println(" nframes = " + nframes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
        databuf.appendChar(nframes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
        switch (code.stackMap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
        case CLDC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
            for (int i=0; i<nframes; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
                if (debugstackmap) System.out.print("  " + i + ":");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
                Code.StackMapFrame frame = code.stackMapBuffer[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
                // output PC
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
                if (debugstackmap) System.out.print(" pc=" + frame.pc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
                databuf.appendChar(frame.pc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
                // output locals
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
                int localCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
                for (int j=0; j<frame.locals.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
                     j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.locals[j]))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
                    localCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
                if (debugstackmap) System.out.print(" nlocals=" +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
                                                    localCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
                databuf.appendChar(localCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
                for (int j=0; j<frame.locals.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
                     j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.locals[j]))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
                    if (debugstackmap) System.out.print(" local[" + j + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
                    writeStackMapType(frame.locals[j]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
                // output stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
                int stackCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
                for (int j=0; j<frame.stack.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
                     j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.stack[j]))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
                    stackCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
                if (debugstackmap) System.out.print(" nstack=" +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
                                                    stackCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
                databuf.appendChar(stackCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
                for (int j=0; j<frame.stack.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
                     j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.stack[j]))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
                    if (debugstackmap) System.out.print(" stack[" + j + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
                    writeStackMapType(frame.stack[j]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
                if (debugstackmap) System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
        case JSR202: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
            assert code.stackMapBuffer == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
            for (int i=0; i<nframes; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
                if (debugstackmap) System.out.print("  " + i + ":");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
                StackMapTableFrame frame = code.stackMapTableBuffer[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
                frame.write(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
                if (debugstackmap) System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
            throw new AssertionError("Unexpected stackmap format value");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
        //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
        void writeStackMapType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
            if (t == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
                if (debugstackmap) System.out.print("empty");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                databuf.appendByte(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
            else switch(t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
            case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
            case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
            case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
            case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
                if (debugstackmap) System.out.print("int");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
                databuf.appendByte(1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
            case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
                if (debugstackmap) System.out.print("float");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
                databuf.appendByte(2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
            case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
                if (debugstackmap) System.out.print("double");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
                databuf.appendByte(3);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
            case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
                if (debugstackmap) System.out.print("long");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                databuf.appendByte(4);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
            case BOT: // null
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
                if (debugstackmap) System.out.print("null");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
                databuf.appendByte(5);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
                if (debugstackmap) System.out.print("object(" + t + ")");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
                databuf.appendByte(7);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
                databuf.appendChar(pool.put(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
            case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
                if (debugstackmap) System.out.print("object(" + types.erasure(t).tsym + ")");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
                databuf.appendByte(7);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
                databuf.appendChar(pool.put(types.erasure(t).tsym));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
            case UNINITIALIZED_THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
                if (debugstackmap) System.out.print("uninit_this");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
                databuf.appendByte(6);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            case UNINITIALIZED_OBJECT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
                { UninitializedType uninitType = (UninitializedType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
                databuf.appendByte(8);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
                if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                databuf.appendChar(uninitType.offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
                throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
    /** An entry in the JSR202 StackMapTable */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
    abstract static class StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
        abstract int getFrameType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
        void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
            int frameType = getFrameType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
            writer.databuf.appendByte(frameType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
            if (writer.debugstackmap) System.out.print(" frame_type=" + frameType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        static class SameFrame extends StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
            final int offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            SameFrame(int offsetDelta) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
                this.offsetDelta = offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
            int getFrameType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
                return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
            void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                super.write(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                if (getFrameType() == SAME_FRAME_EXTENDED) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                    writer.databuf.appendChar(offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                    if (writer.debugstackmap){
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
                        System.out.print(" offset_delta=" + offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
        static class SameLocals1StackItemFrame extends StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
            final int offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            final Type stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            SameLocals1StackItemFrame(int offsetDelta, Type stack) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                this.offsetDelta = offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                this.stack = stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
            int getFrameType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                return (offsetDelta < SAME_FRAME_SIZE) ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                       (SAME_FRAME_SIZE + offsetDelta) :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                       SAME_LOCALS_1_STACK_ITEM_EXTENDED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
            void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                super.write(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                if (getFrameType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
                    writer.databuf.appendChar(offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
                    if (writer.debugstackmap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
                        System.out.print(" offset_delta=" + offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
                if (writer.debugstackmap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
                    System.out.print(" stack[" + 0 + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
                writer.writeStackMapType(stack);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
        static class ChopFrame extends StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
            final int frameType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
            final int offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
            ChopFrame(int frameType, int offsetDelta) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
                this.frameType = frameType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                this.offsetDelta = offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
            int getFrameType() { return frameType; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
            void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
                super.write(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
                writer.databuf.appendChar(offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
                if (writer.debugstackmap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
                    System.out.print(" offset_delta=" + offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
        static class AppendFrame extends StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
            final int frameType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
            final int offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
            final Type[] locals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
            AppendFrame(int frameType, int offsetDelta, Type[] locals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                this.frameType = frameType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                this.offsetDelta = offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                this.locals = locals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
            int getFrameType() { return frameType; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
            void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
                super.write(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
                writer.databuf.appendChar(offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
                if (writer.debugstackmap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
                    System.out.print(" offset_delta=" + offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
                for (int i=0; i<locals.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
                     if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                     writer.writeStackMapType(locals[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
        static class FullFrame extends StackMapTableFrame {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            final int offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
            final Type[] locals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
            final Type[] stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
            FullFrame(int offsetDelta, Type[] locals, Type[] stack) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
                this.offsetDelta = offsetDelta;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
                this.locals = locals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
                this.stack = stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
            int getFrameType() { return FULL_FRAME; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
            void write(ClassWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
                super.write(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
                writer.databuf.appendChar(offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
                writer.databuf.appendChar(locals.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
                if (writer.debugstackmap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
                    System.out.print(" offset_delta=" + offsetDelta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
                    System.out.print(" nlocals=" + locals.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
                for (int i=0; i<locals.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
                    if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
                    writer.writeStackMapType(locals[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
                writer.databuf.appendChar(stack.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
                for (int i=0; i<stack.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                    if (writer.debugstackmap) System.out.print(" stack[" + i + "]=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
                    writer.writeStackMapType(stack[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
       /** Compare this frame with the previous frame and produce
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
        *  an entry of compressed stack map frame. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
        static StackMapTableFrame getInstance(Code.StackMapFrame this_frame,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                                              int prev_pc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                                              Type[] prev_locals,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                                              Types types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
            Type[] locals = this_frame.locals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
            Type[] stack = this_frame.stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            int offset_delta = this_frame.pc - prev_pc - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
            if (stack.length == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                if (locals.length == prev_locals.length
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                    && compare(prev_locals, locals, types) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                    return new SameLocals1StackItemFrame(offset_delta, stack[0]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
            } else if (stack.length == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                int diff_length = compare(prev_locals, locals, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
                if (diff_length == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                    return new SameFrame(offset_delta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                    // APPEND
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                    Type[] local_diff = new Type[-diff_length];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                    for (int i=prev_locals.length, j=0; i<locals.length; i++,j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
                        local_diff[j] = locals[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                    return new AppendFrame(SAME_FRAME_EXTENDED - diff_length,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                                           offset_delta,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
                                           local_diff);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
                    // CHOP
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                    return new ChopFrame(SAME_FRAME_EXTENDED - diff_length,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
                                         offset_delta);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
            // FULL_FRAME
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            return new FullFrame(offset_delta, locals, stack);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
        static boolean isInt(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
            return (t.tag < TypeTags.INT || t.tag == TypeTags.BOOLEAN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
        static boolean isSameType(Type t1, Type t2, Types types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
            if (t1 == null) { return t2 == null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
            if (t2 == null) { return false; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
            if (isInt(t1) && isInt(t2)) { return true; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
            if (t1.tag == UNINITIALIZED_THIS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
                return t2.tag == UNINITIALIZED_THIS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
            } else if (t1.tag == UNINITIALIZED_OBJECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
                if (t2.tag == UNINITIALIZED_OBJECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
                    return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
            } else if (t2.tag == UNINITIALIZED_THIS || t2.tag == UNINITIALIZED_OBJECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
            return types.isSameType(t1, t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
        static int compare(Type[] arr1, Type[] arr2, Types types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
            int diff_length = arr1.length - arr2.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
            if (diff_length > MAX_LOCAL_LENGTH_DIFF || diff_length < -MAX_LOCAL_LENGTH_DIFF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
                return Integer.MAX_VALUE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
            int len = (diff_length > 0) ? arr2.length : arr1.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
            for (int i=0; i<len; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
                if (!isSameType(arr1[i], arr2[i], types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
                    return Integer.MAX_VALUE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
            return diff_length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
    void writeFields(Scope.Entry e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
        // process them in reverse sibling order;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
        // i.e., process them in declaration order.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
        List<VarSymbol> vars = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
        for (Scope.Entry i = e; i != null; i = i.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
            if (i.sym.kind == VAR) vars = vars.prepend((VarSymbol)i.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
        while (vars.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
            writeField(vars.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            vars = vars.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
    void writeMethods(Scope.Entry e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
        List<MethodSymbol> methods = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
        for (Scope.Entry i = e; i != null; i = i.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
            if (i.sym.kind == MTH && (i.sym.flags() & HYPOTHETICAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
                methods = methods.prepend((MethodSymbol)i.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
        while (methods.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
            writeMethod(methods.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
            methods = methods.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
    /** Emit a class file for a given class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
     *  @param c      The class from which a class file is generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
    public JavaFileObject writeClass(ClassSymbol c)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        throws IOException, PoolOverflow, StringOverflow
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
        JavaFileObject outFile
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
                                               c.flatname.toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
                                               JavaFileObject.Kind.CLASS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
                                               c.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
        OutputStream out = outFile.openOutputStream();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
            writeClassFile(out, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
            if (verbose)
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
  1600
                log.errWriter.println(Log.getLocalizedString("verbose.wrote.file", outFile));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
            out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
            out = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
            if (out != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
                // if we are propogating an exception, delete the file
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
                outFile.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
                outFile = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
        return outFile; // may be null if write failed
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
    /** Write class `c' to outstream `out'.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
    public void writeClassFile(OutputStream out, ClassSymbol c)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
        throws IOException, PoolOverflow, StringOverflow {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
        assert (c.flags() & COMPOUND) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
        databuf.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
        poolbuf.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
        sigbuf.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
        pool = c.pool;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
        innerClasses = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
        innerClassesQueue = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
        Type supertype = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
        List<Type> interfaces = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
        List<Type> typarams = c.type.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
        int flags = adjustFlags(c.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
        if ((flags & PROTECTED) != 0) flags |= PUBLIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
        flags = flags & ClassFlags & ~STRICTFP;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
        if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
        if (c.isInner() && c.name.isEmpty()) flags &= ~FINAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
        if (dumpClassModifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
            log.errWriter.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
            log.errWriter.println("CLASSFILE  " + c.getQualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
            log.errWriter.println("---" + flagNames(flags));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
        databuf.appendChar(flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
        databuf.appendChar(pool.put(c));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
        databuf.appendChar(supertype.tag == CLASS ? pool.put(supertype.tsym) : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
        databuf.appendChar(interfaces.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
        for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            databuf.appendChar(pool.put(l.head.tsym));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
        int fieldsCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
        int methodsCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
        for (Scope.Entry e = c.members().elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
            switch (e.sym.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
            case VAR: fieldsCount++; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
            case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
                      break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
            case TYP: enterInner((ClassSymbol)e.sym); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
            default : assert false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
        databuf.appendChar(fieldsCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
        writeFields(c.members().elems);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
        databuf.appendChar(methodsCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
        writeMethods(c.members().elems);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
        int acountIdx = beginAttrs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
        int acount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
        boolean sigReq =
3138
c9fe0e7472ac 6835430: javac does not generate signature attributes for classes extending parameterized inner classes
mcimadamore
parents: 1264
diff changeset
  1667
            typarams.length() != 0 || supertype.allparams().length() != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
        for (List<Type> l = interfaces; !sigReq && l.nonEmpty(); l = l.tail)
3138
c9fe0e7472ac 6835430: javac does not generate signature attributes for classes extending parameterized inner classes
mcimadamore
parents: 1264
diff changeset
  1669
            sigReq = l.head.allparams().length() != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
        if (sigReq) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
            assert source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
            int alenIdx = writeAttr(names.Signature);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
            if (typarams.length() != 0) assembleParamsSig(typarams);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
            assembleSig(supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
            for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
                assembleSig(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
            databuf.appendChar(pool.put(sigbuf.toName(names)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
            sigbuf.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
        if (c.sourcefile != null && emitSourceFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
            int alenIdx = writeAttr(names.SourceFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
            // WHM 6/29/1999: Strip file path prefix.  We do it here at
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
            // the last possible moment because the sourcefile may be used
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
            // elsewhere in error diagnostics. Fixes 4241573.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
            //databuf.appendChar(c.pool.put(c.sourcefile));
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3300
diff changeset
  1689
            String simpleName = BaseFileObject.getSimpleName(c.sourcefile);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3300
diff changeset
  1690
            databuf.appendChar(c.pool.put(names.fromString(simpleName)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
        if (genCrt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
            // Append SourceID attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
            int alenIdx = writeAttr(names.SourceID);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
            databuf.appendChar(c.pool.put(names.fromString(Long.toString(getLastModified(c.sourcefile)))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
            // Append CompilationID attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
            alenIdx = writeAttr(names.CompilationID);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
            databuf.appendChar(c.pool.put(names.fromString(Long.toString(System.currentTimeMillis()))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
            endAttr(alenIdx);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
        acount += writeFlagAttrs(c.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        acount += writeJavaAnnotations(c.getAnnotationMirrors());
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3138
diff changeset
  1710
        acount += writeTypeAnnotations(c.typeAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
        acount += writeEnclosingMethodAttribute(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
        poolbuf.appendInt(JAVA_MAGIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
        poolbuf.appendChar(target.minorVersion);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
        poolbuf.appendChar(target.majorVersion);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        writePool(c.pool);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
        if (innerClasses != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
            writeInnerClasses();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
            acount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
        endAttrs(acountIdx, acount);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
        poolbuf.appendBytes(databuf.elems, 0, databuf.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
        out.write(poolbuf.elems, 0, poolbuf.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
        pool = c.pool = null; // to conserve space
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
     }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
    int adjustFlags(final long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
        int result = (int)flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
        if ((flags & SYNTHETIC) != 0  && !target.useSyntheticFlag())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
            result &= ~SYNTHETIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
        if ((flags & ENUM) != 0  && !target.useEnumFlag())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
            result &= ~ENUM;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
        if ((flags & ANNOTATION) != 0  && !target.useAnnotationFlag())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
            result &= ~ANNOTATION;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
        if ((flags & BRIDGE) != 0  && target.useBridgeFlag())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
            result |= ACC_BRIDGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
        if ((flags & VARARGS) != 0  && target.useVarargsFlag())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
            result |= ACC_VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
    long getLastModified(FileObject filename) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
        long mod = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
            mod = filename.getLastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
        } catch (SecurityException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
            throw new AssertionError("CRT: couldn't get source file modification date: " + e.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
        return mod;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
}