rt/emul/compact/src/main/java/java/lang/invoke/BoundMethodHandle.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Aug 2014 07:02:12 +0200
branchjdk8
changeset 1653 bd151459ee4f
parent 1646 c880a8a8803b
permissions -rw-r--r--
Capable to compile the java.lang.invoke stuff
jaroslav@1646
     1
/*
jaroslav@1646
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646
     4
 *
jaroslav@1646
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1646
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1646
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1646
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646
    10
 *
jaroslav@1646
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1646
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646
    15
 * accompanied this code).
jaroslav@1646
    16
 *
jaroslav@1646
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1646
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646
    20
 *
jaroslav@1646
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1646
    23
 * questions.
jaroslav@1646
    24
 */
jaroslav@1646
    25
jaroslav@1646
    26
package java.lang.invoke;
jaroslav@1646
    27
jaroslav@1646
    28
import static java.lang.invoke.LambdaForm.basicTypes;
jaroslav@1646
    29
import static java.lang.invoke.MethodHandleStatics.*;
jaroslav@1646
    30
jaroslav@1646
    31
import java.lang.invoke.LambdaForm.Name;
jaroslav@1646
    32
import java.lang.invoke.LambdaForm.NamedFunction;
jaroslav@1646
    33
import java.lang.invoke.MethodHandles.Lookup;
jaroslav@1646
    34
import java.util.Arrays;
jaroslav@1646
    35
import java.util.HashMap;
jaroslav@1646
    36
jaroslav@1646
    37
import sun.invoke.util.ValueConversions;
jaroslav@1646
    38
jaroslav@1646
    39
/**
jaroslav@1646
    40
 * The flavor of method handle which emulates an invoke instruction
jaroslav@1646
    41
 * on a predetermined argument.  The JVM dispatches to the correct method
jaroslav@1646
    42
 * when the handle is created, not when it is invoked.
jaroslav@1646
    43
 *
jaroslav@1646
    44
 * All bound arguments are encapsulated in dedicated species.
jaroslav@1646
    45
 */
jaroslav@1646
    46
/* non-public */ abstract class BoundMethodHandle extends MethodHandle {
jaroslav@1646
    47
jaroslav@1646
    48
    /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) {
jaroslav@1646
    49
        super(type, form);
jaroslav@1646
    50
    }
jaroslav@1646
    51
jaroslav@1646
    52
    //
jaroslav@1646
    53
    // BMH API and internals
jaroslav@1646
    54
    //
jaroslav@1646
    55
jaroslav@1646
    56
    static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) {
jaroslav@1646
    57
        // for some type signatures, there exist pre-defined concrete BMH classes
jaroslav@1646
    58
        try {
jaroslav@1646
    59
            switch (xtype) {
jaroslav@1646
    60
            case 'L':
jaroslav@1646
    61
                if (true)  return bindSingle(type, form, x);  // Use known fast path.
jaroslav@1646
    62
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x);
jaroslav@1646
    63
            case 'I':
jaroslav@1646
    64
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
jaroslav@1646
    65
            case 'J':
jaroslav@1646
    66
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x);
jaroslav@1646
    67
            case 'F':
jaroslav@1646
    68
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x);
jaroslav@1646
    69
            case 'D':
jaroslav@1646
    70
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x);
jaroslav@1646
    71
            default : throw new InternalError("unexpected xtype: " + xtype);
jaroslav@1646
    72
            }
jaroslav@1646
    73
        } catch (Throwable t) {
jaroslav@1646
    74
            throw newInternalError(t);
jaroslav@1646
    75
        }
jaroslav@1646
    76
    }
jaroslav@1646
    77
jaroslav@1646
    78
    static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) {
jaroslav@1646
    79
            return new Species_L(type, form, x);
jaroslav@1646
    80
    }
jaroslav@1646
    81
jaroslav@1646
    82
    MethodHandle cloneExtend(MethodType type, LambdaForm form, char xtype, Object x) {
jaroslav@1646
    83
        try {
jaroslav@1646
    84
            switch (xtype) {
jaroslav@1646
    85
            case 'L': return cloneExtendL(type, form, x);
jaroslav@1646
    86
            case 'I': return cloneExtendI(type, form, ValueConversions.widenSubword(x));
jaroslav@1646
    87
            case 'J': return cloneExtendJ(type, form, (long) x);
jaroslav@1646
    88
            case 'F': return cloneExtendF(type, form, (float) x);
jaroslav@1646
    89
            case 'D': return cloneExtendD(type, form, (double) x);
jaroslav@1646
    90
            }
jaroslav@1646
    91
        } catch (Throwable t) {
jaroslav@1646
    92
            throw newInternalError(t);
jaroslav@1646
    93
        }
jaroslav@1646
    94
        throw new InternalError("unexpected type: " + xtype);
jaroslav@1646
    95
    }
jaroslav@1646
    96
jaroslav@1646
    97
    @Override
jaroslav@1646
    98
    MethodHandle bindArgument(int pos, char basicType, Object value) {
jaroslav@1646
    99
        MethodType type = type().dropParameterTypes(pos, pos+1);
jaroslav@1646
   100
        LambdaForm form = internalForm().bind(1+pos, speciesData());
jaroslav@1646
   101
        return cloneExtend(type, form, basicType, value);
jaroslav@1646
   102
    }
jaroslav@1646
   103
jaroslav@1646
   104
    @Override
jaroslav@1646
   105
    MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
jaroslav@1646
   106
        LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos+drops));
jaroslav@1646
   107
        try {
jaroslav@1646
   108
             return clone(srcType, form);
jaroslav@1646
   109
         } catch (Throwable t) {
jaroslav@1646
   110
             throw newInternalError(t);
jaroslav@1646
   111
         }
jaroslav@1646
   112
    }
jaroslav@1646
   113
jaroslav@1646
   114
    @Override
jaroslav@1646
   115
    MethodHandle permuteArguments(MethodType newType, int[] reorder) {
jaroslav@1646
   116
        try {
jaroslav@1646
   117
             return clone(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList())));
jaroslav@1646
   118
         } catch (Throwable t) {
jaroslav@1646
   119
             throw newInternalError(t);
jaroslav@1646
   120
         }
jaroslav@1646
   121
    }
jaroslav@1646
   122
jaroslav@1646
   123
    static final String EXTENSION_TYPES = "LIJFD";
jaroslav@1646
   124
    static final byte INDEX_L = 0, INDEX_I = 1, INDEX_J = 2, INDEX_F = 3, INDEX_D = 4;
jaroslav@1646
   125
    static byte extensionIndex(char type) {
jaroslav@1646
   126
        int i = EXTENSION_TYPES.indexOf(type);
jaroslav@1646
   127
        if (i < 0)  throw new InternalError();
jaroslav@1646
   128
        return (byte) i;
jaroslav@1646
   129
    }
jaroslav@1646
   130
jaroslav@1646
   131
    /**
jaroslav@1646
   132
     * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a
jaroslav@1646
   133
     * static field containing this value, and they must accordingly implement this method.
jaroslav@1646
   134
     */
jaroslav@1646
   135
    protected abstract SpeciesData speciesData();
jaroslav@1646
   136
jaroslav@1646
   137
    @Override
jaroslav@1646
   138
    final Object internalProperties() {
jaroslav@1646
   139
        return "/BMH="+internalValues();
jaroslav@1646
   140
    }
jaroslav@1646
   141
jaroslav@1646
   142
    @Override
jaroslav@1646
   143
    final Object internalValues() {
jaroslav@1646
   144
        Object[] boundValues = new Object[speciesData().fieldCount()];
jaroslav@1646
   145
        for (int i = 0; i < boundValues.length; ++i) {
jaroslav@1646
   146
            boundValues[i] = arg(i);
jaroslav@1646
   147
        }
jaroslav@1646
   148
        return Arrays.asList(boundValues);
jaroslav@1646
   149
    }
jaroslav@1646
   150
jaroslav@1646
   151
    public final Object arg(int i) {
jaroslav@1646
   152
        try {
jaroslav@1646
   153
            switch (speciesData().fieldType(i)) {
jaroslav@1646
   154
            case 'L': return argL(i);
jaroslav@1646
   155
            case 'I': return argI(i);
jaroslav@1646
   156
            case 'F': return argF(i);
jaroslav@1646
   157
            case 'D': return argD(i);
jaroslav@1646
   158
            case 'J': return argJ(i);
jaroslav@1646
   159
            }
jaroslav@1646
   160
        } catch (Throwable ex) {
jaroslav@1646
   161
            throw newInternalError(ex);
jaroslav@1646
   162
        }
jaroslav@1646
   163
        throw new InternalError("unexpected type: " + speciesData().types+"."+i);
jaroslav@1646
   164
    }
jaroslav@1646
   165
    public final Object argL(int i) throws Throwable { return          speciesData().getters[i].invokeBasic(this); }
jaroslav@1646
   166
    public final int    argI(int i) throws Throwable { return (int)    speciesData().getters[i].invokeBasic(this); }
jaroslav@1646
   167
    public final float  argF(int i) throws Throwable { return (float)  speciesData().getters[i].invokeBasic(this); }
jaroslav@1646
   168
    public final double argD(int i) throws Throwable { return (double) speciesData().getters[i].invokeBasic(this); }
jaroslav@1646
   169
    public final long   argJ(int i) throws Throwable { return (long)   speciesData().getters[i].invokeBasic(this); }
jaroslav@1646
   170
jaroslav@1646
   171
    //
jaroslav@1646
   172
    // cloning API
jaroslav@1646
   173
    //
jaroslav@1646
   174
jaroslav@1646
   175
    public abstract BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable;
jaroslav@1646
   176
    public abstract BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable;
jaroslav@1646
   177
    public abstract BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int    narg) throws Throwable;
jaroslav@1646
   178
    public abstract BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long   narg) throws Throwable;
jaroslav@1646
   179
    public abstract BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float  narg) throws Throwable;
jaroslav@1646
   180
    public abstract BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable;
jaroslav@1646
   181
jaroslav@1646
   182
    // The following is a grossly irregular hack:
jaroslav@1646
   183
    @Override MethodHandle reinvokerTarget() {
jaroslav@1646
   184
        try {
jaroslav@1646
   185
            return (MethodHandle) argL(0);
jaroslav@1646
   186
        } catch (Throwable ex) {
jaroslav@1646
   187
            throw newInternalError(ex);
jaroslav@1646
   188
        }
jaroslav@1646
   189
    }
jaroslav@1646
   190
jaroslav@1646
   191
    //
jaroslav@1646
   192
    // concrete BMH classes required to close bootstrap loops
jaroslav@1646
   193
    //
jaroslav@1646
   194
jaroslav@1646
   195
    private  // make it private to force users to access the enclosing class first
jaroslav@1646
   196
    static final class Species_L extends BoundMethodHandle {
jaroslav@1646
   197
        final Object argL0;
jaroslav@1646
   198
        public Species_L(MethodType mt, LambdaForm lf, Object argL0) {
jaroslav@1646
   199
            super(mt, lf);
jaroslav@1646
   200
            this.argL0 = argL0;
jaroslav@1646
   201
        }
jaroslav@1646
   202
        // The following is a grossly irregular hack:
jaroslav@1646
   203
        @Override MethodHandle reinvokerTarget() { return (MethodHandle) argL0; }
jaroslav@1646
   204
        @Override
jaroslav@1646
   205
        public SpeciesData speciesData() {
jaroslav@1646
   206
            return SPECIES_DATA;
jaroslav@1646
   207
        }
jaroslav@1646
   208
        public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class);
jaroslav@1646
   209
        @Override
jaroslav@1646
   210
        public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
jaroslav@1646
   211
            return new Species_L(mt, lf, argL0);
jaroslav@1646
   212
        }
jaroslav@1646
   213
        @Override
jaroslav@1646
   214
        public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
jaroslav@1646
   215
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, narg);
jaroslav@1646
   216
        }
jaroslav@1646
   217
        @Override
jaroslav@1646
   218
        public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
jaroslav@1646
   219
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, narg);
jaroslav@1646
   220
        }
jaroslav@1646
   221
        @Override
jaroslav@1646
   222
        public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
jaroslav@1646
   223
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, narg);
jaroslav@1646
   224
        }
jaroslav@1646
   225
        @Override
jaroslav@1646
   226
        public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
jaroslav@1646
   227
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, narg);
jaroslav@1646
   228
        }
jaroslav@1646
   229
        @Override
jaroslav@1646
   230
        public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
jaroslav@1646
   231
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, narg);
jaroslav@1646
   232
        }
jaroslav@1646
   233
    }
jaroslav@1646
   234
jaroslav@1646
   235
/*
jaroslav@1646
   236
    static final class Species_LL extends BoundMethodHandle {
jaroslav@1646
   237
        final Object argL0;
jaroslav@1646
   238
        final Object argL1;
jaroslav@1646
   239
        public Species_LL(MethodType mt, LambdaForm lf, Object argL0, Object argL1) {
jaroslav@1646
   240
            super(mt, lf);
jaroslav@1646
   241
            this.argL0 = argL0;
jaroslav@1646
   242
            this.argL1 = argL1;
jaroslav@1646
   243
        }
jaroslav@1646
   244
        @Override
jaroslav@1646
   245
        public SpeciesData speciesData() {
jaroslav@1646
   246
            return SPECIES_DATA;
jaroslav@1646
   247
        }
jaroslav@1646
   248
        public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LL", Species_LL.class);
jaroslav@1646
   249
        @Override
jaroslav@1646
   250
        public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
jaroslav@1646
   251
            return new Species_LL(mt, lf, argL0, argL1);
jaroslav@1646
   252
        }
jaroslav@1646
   253
        @Override
jaroslav@1646
   254
        public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
jaroslav@1646
   255
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
jaroslav@1646
   256
        }
jaroslav@1646
   257
        @Override
jaroslav@1646
   258
        public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
jaroslav@1646
   259
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
jaroslav@1646
   260
        }
jaroslav@1646
   261
        @Override
jaroslav@1646
   262
        public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
jaroslav@1646
   263
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
jaroslav@1646
   264
        }
jaroslav@1646
   265
        @Override
jaroslav@1646
   266
        public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
jaroslav@1646
   267
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
jaroslav@1646
   268
        }
jaroslav@1646
   269
        @Override
jaroslav@1646
   270
        public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
jaroslav@1646
   271
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
jaroslav@1646
   272
        }
jaroslav@1646
   273
    }
jaroslav@1646
   274
jaroslav@1646
   275
    static final class Species_JL extends BoundMethodHandle {
jaroslav@1646
   276
        final long argJ0;
jaroslav@1646
   277
        final Object argL1;
jaroslav@1646
   278
        public Species_JL(MethodType mt, LambdaForm lf, long argJ0, Object argL1) {
jaroslav@1646
   279
            super(mt, lf);
jaroslav@1646
   280
            this.argJ0 = argJ0;
jaroslav@1646
   281
            this.argL1 = argL1;
jaroslav@1646
   282
        }
jaroslav@1646
   283
        @Override
jaroslav@1646
   284
        public SpeciesData speciesData() {
jaroslav@1646
   285
            return SPECIES_DATA;
jaroslav@1646
   286
        }
jaroslav@1646
   287
        public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("JL", Species_JL.class);
jaroslav@1646
   288
        @Override public final long   argJ0() { return argJ0; }
jaroslav@1646
   289
        @Override public final Object argL1() { return argL1; }
jaroslav@1646
   290
        @Override
jaroslav@1646
   291
        public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
jaroslav@1646
   292
            return new Species_JL(mt, lf, argJ0, argL1);
jaroslav@1646
   293
        }
jaroslav@1646
   294
        @Override
jaroslav@1646
   295
        public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
jaroslav@1646
   296
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
jaroslav@1646
   297
        }
jaroslav@1646
   298
        @Override
jaroslav@1646
   299
        public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
jaroslav@1646
   300
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
jaroslav@1646
   301
        }
jaroslav@1646
   302
        @Override
jaroslav@1646
   303
        public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
jaroslav@1646
   304
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
jaroslav@1646
   305
        }
jaroslav@1646
   306
        @Override
jaroslav@1646
   307
        public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
jaroslav@1646
   308
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
jaroslav@1646
   309
        }
jaroslav@1646
   310
        @Override
jaroslav@1646
   311
        public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
jaroslav@1646
   312
            return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
jaroslav@1646
   313
        }
jaroslav@1646
   314
    }
jaroslav@1646
   315
*/
jaroslav@1646
   316
jaroslav@1646
   317
    //
jaroslav@1646
   318
    // BMH species meta-data
jaroslav@1646
   319
    //
jaroslav@1646
   320
jaroslav@1646
   321
    /**
jaroslav@1646
   322
     * Meta-data wrapper for concrete BMH classes.
jaroslav@1646
   323
     */
jaroslav@1646
   324
    static class SpeciesData {
jaroslav@1646
   325
        final String                             types;
jaroslav@1646
   326
        final Class<? extends BoundMethodHandle> clazz;
jaroslav@1646
   327
        // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH
jaroslav@1646
   328
        // Therefore, we need a non-final link in the chain.  Use array elements.
jaroslav@1646
   329
        final MethodHandle[]                     constructor;
jaroslav@1646
   330
        final MethodHandle[]                     getters;
jaroslav@1646
   331
        final SpeciesData[]                      extensions;
jaroslav@1646
   332
jaroslav@1646
   333
        public int fieldCount() {
jaroslav@1646
   334
            return types.length();
jaroslav@1646
   335
        }
jaroslav@1646
   336
        public char fieldType(int i) {
jaroslav@1646
   337
            return types.charAt(i);
jaroslav@1646
   338
        }
jaroslav@1646
   339
jaroslav@1646
   340
        public String toString() {
jaroslav@1646
   341
            return "SpeciesData["+(isPlaceholder() ? "<placeholder>" : clazz.getSimpleName())+":"+types+"]";
jaroslav@1646
   342
        }
jaroslav@1646
   343
jaroslav@1646
   344
        /**
jaroslav@1646
   345
         * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that
jaroslav@1646
   346
         * represents a MH bound to a generic invoker, which in turn forwards to the corresponding
jaroslav@1646
   347
         * getter.
jaroslav@1646
   348
         */
jaroslav@1646
   349
        Name getterName(Name mhName, int i) {
jaroslav@1646
   350
            MethodHandle mh = getters[i];
jaroslav@1646
   351
            assert(mh != null) : this+"."+i;
jaroslav@1646
   352
            return new Name(mh, mhName);
jaroslav@1646
   353
        }
jaroslav@1646
   354
jaroslav@1646
   355
        NamedFunction getterFunction(int i) {
jaroslav@1646
   356
            return new NamedFunction(getters[i]);
jaroslav@1646
   357
        }
jaroslav@1646
   358
jaroslav@1646
   359
        static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class);
jaroslav@1646
   360
jaroslav@1646
   361
        private SpeciesData(String types, Class<? extends BoundMethodHandle> clazz) {
jaroslav@1646
   362
            this.types = types;
jaroslav@1646
   363
            this.clazz = clazz;
jaroslav@1646
   364
            if (!INIT_DONE) {
jaroslav@1646
   365
                this.constructor = new MethodHandle[1];
jaroslav@1646
   366
                this.getters = new MethodHandle[types.length()];
jaroslav@1646
   367
            } else {
jaroslav@1653
   368
                throw new IllegalStateException("bound method handle");
jaroslav@1653
   369
//                this.constructor = Factory.makeCtors(clazz, types, null);
jaroslav@1653
   370
//                this.getters = Factory.makeGetters(clazz, types, null);
jaroslav@1646
   371
            }
jaroslav@1646
   372
            this.extensions = new SpeciesData[EXTENSION_TYPES.length()];
jaroslav@1646
   373
        }
jaroslav@1646
   374
jaroslav@1646
   375
        private void initForBootstrap() {
jaroslav@1646
   376
            assert(!INIT_DONE);
jaroslav@1646
   377
            if (constructor[0] == null) {
jaroslav@1653
   378
//                Factory.makeCtors(clazz, types, this.constructor);
jaroslav@1653
   379
//                Factory.makeGetters(clazz, types, this.getters);
jaroslav@1646
   380
            }
jaroslav@1646
   381
        }
jaroslav@1646
   382
jaroslav@1646
   383
        private SpeciesData(String types) {
jaroslav@1646
   384
            // Placeholder only.
jaroslav@1646
   385
            this.types = types;
jaroslav@1646
   386
            this.clazz = null;
jaroslav@1646
   387
            this.constructor = null;
jaroslav@1646
   388
            this.getters = null;
jaroslav@1646
   389
            this.extensions = null;
jaroslav@1646
   390
        }
jaroslav@1646
   391
        private boolean isPlaceholder() { return clazz == null; }
jaroslav@1646
   392
jaroslav@1646
   393
        private static final HashMap<String, SpeciesData> CACHE = new HashMap<>();
jaroslav@1646
   394
        static { CACHE.put("", EMPTY); }  // make bootstrap predictable
jaroslav@1646
   395
        private static final boolean INIT_DONE;  // set after <clinit> finishes...
jaroslav@1646
   396
jaroslav@1646
   397
        SpeciesData extendWithType(char type) {
jaroslav@1646
   398
            int i = extensionIndex(type);
jaroslav@1646
   399
            SpeciesData d = extensions[i];
jaroslav@1646
   400
            if (d != null)  return d;
jaroslav@1646
   401
            extensions[i] = d = get(types+type);
jaroslav@1646
   402
            return d;
jaroslav@1646
   403
        }
jaroslav@1646
   404
jaroslav@1646
   405
        SpeciesData extendWithIndex(byte index) {
jaroslav@1646
   406
            SpeciesData d = extensions[index];
jaroslav@1646
   407
            if (d != null)  return d;
jaroslav@1646
   408
            extensions[index] = d = get(types+EXTENSION_TYPES.charAt(index));
jaroslav@1646
   409
            return d;
jaroslav@1646
   410
        }
jaroslav@1646
   411
jaroslav@1646
   412
        private static SpeciesData get(String types) {
jaroslav@1646
   413
            // Acquire cache lock for query.
jaroslav@1646
   414
            SpeciesData d = lookupCache(types);
jaroslav@1646
   415
            if (!d.isPlaceholder())
jaroslav@1646
   416
                return d;
jaroslav@1646
   417
            synchronized (d) {
jaroslav@1646
   418
                // Use synch. on the placeholder to prevent multiple instantiation of one species.
jaroslav@1646
   419
                // Creating this class forces a recursive call to getForClass.
jaroslav@1646
   420
                if (lookupCache(types).isPlaceholder())
jaroslav@1653
   421
                    throw new IllegalStateException("Cannot generate anything");
jaroslav@1646
   422
            }
jaroslav@1646
   423
            // Reacquire cache lock.
jaroslav@1646
   424
            d = lookupCache(types);
jaroslav@1646
   425
            // Class loading must have upgraded the cache.
jaroslav@1646
   426
            assert(d != null && !d.isPlaceholder());
jaroslav@1646
   427
            return d;
jaroslav@1646
   428
        }
jaroslav@1646
   429
        static SpeciesData getForClass(String types, Class<? extends BoundMethodHandle> clazz) {
jaroslav@1646
   430
            // clazz is a new class which is initializing its SPECIES_DATA field
jaroslav@1646
   431
            return updateCache(types, new SpeciesData(types, clazz));
jaroslav@1646
   432
        }
jaroslav@1646
   433
        private static synchronized SpeciesData lookupCache(String types) {
jaroslav@1646
   434
            SpeciesData d = CACHE.get(types);
jaroslav@1646
   435
            if (d != null)  return d;
jaroslav@1646
   436
            d = new SpeciesData(types);
jaroslav@1646
   437
            assert(d.isPlaceholder());
jaroslav@1646
   438
            CACHE.put(types, d);
jaroslav@1646
   439
            return d;
jaroslav@1646
   440
        }
jaroslav@1646
   441
        private static synchronized SpeciesData updateCache(String types, SpeciesData d) {
jaroslav@1646
   442
            SpeciesData d2;
jaroslav@1646
   443
            assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder());
jaroslav@1646
   444
            assert(!d.isPlaceholder());
jaroslav@1646
   445
            CACHE.put(types, d);
jaroslav@1646
   446
            return d;
jaroslav@1646
   447
        }
jaroslav@1646
   448
jaroslav@1646
   449
        static {
jaroslav@1646
   450
            // pre-fill the BMH speciesdata cache with BMH's inner classes
jaroslav@1646
   451
            final Class<BoundMethodHandle> rootCls = BoundMethodHandle.class;
jaroslav@1646
   452
            SpeciesData d0 = BoundMethodHandle.SPECIES_DATA;  // trigger class init
jaroslav@1646
   453
            assert(d0 == null || d0 == lookupCache("")) : d0;
jaroslav@1646
   454
            try {
jaroslav@1653
   455
                /*
jaroslav@1646
   456
                for (Class<?> c : rootCls.getDeclaredClasses()) {
jaroslav@1646
   457
                    if (rootCls.isAssignableFrom(c)) {
jaroslav@1646
   458
                        final Class<? extends BoundMethodHandle> cbmh = c.asSubclass(BoundMethodHandle.class);
jaroslav@1646
   459
                        SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh);
jaroslav@1646
   460
                        assert(d != null) : cbmh.getName();
jaroslav@1646
   461
                        assert(d.clazz == cbmh);
jaroslav@1646
   462
                        assert(d == lookupCache(d.types));
jaroslav@1646
   463
                    }
jaroslav@1646
   464
                }
jaroslav@1653
   465
                */
jaroslav@1646
   466
            } catch (Throwable e) {
jaroslav@1646
   467
                throw newInternalError(e);
jaroslav@1646
   468
            }
jaroslav@1646
   469
jaroslav@1646
   470
            for (SpeciesData d : CACHE.values()) {
jaroslav@1646
   471
                d.initForBootstrap();
jaroslav@1646
   472
            }
jaroslav@1646
   473
            // Note:  Do not simplify this, because INIT_DONE must not be
jaroslav@1646
   474
            // a compile-time constant during bootstrapping.
jaroslav@1646
   475
            INIT_DONE = Boolean.TRUE;
jaroslav@1646
   476
        }
jaroslav@1646
   477
    }
jaroslav@1646
   478
jaroslav@1646
   479
    static SpeciesData getSpeciesData(String types) {
jaroslav@1646
   480
        return SpeciesData.get(types);
jaroslav@1646
   481
    }
jaroslav@1646
   482
jaroslav@1646
   483
jaroslav@1646
   484
jaroslav@1646
   485
    private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP;
jaroslav@1646
   486
jaroslav@1646
   487
    /**
jaroslav@1646
   488
     * All subclasses must provide such a value describing their type signature.
jaroslav@1646
   489
     */
jaroslav@1646
   490
    static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY;
jaroslav@1646
   491
}