1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.turbine.bytecode.sig;
import com.google.common.collect.ImmutableList;
import com.google.turbine.model.TurbineConstantTypeKind;
import org.checkerframework.checker.nullness.qual.Nullable;
/** JVMS 4.7.9.1 signatures. */
public final class Sig {
/** A JVMS 4.7.9.1 ClassSignature. */
public static class ClassSig {
private final ImmutableList<TyParamSig> tyParams;
private final ClassTySig superClass;
private final ImmutableList<ClassTySig> interfaces;
public ClassSig(
ImmutableList<TyParamSig> tyParams,
ClassTySig superClass,
ImmutableList<ClassTySig> interfaces) {
this.tyParams = tyParams;
this.superClass = superClass;
this.interfaces = interfaces;
}
/** Formal type parameters. */
public ImmutableList<TyParamSig> tyParams() {
return tyParams;
}
/** The super class. */
public ClassTySig superClass() {
return superClass;
}
/** The interface list. */
public ImmutableList<ClassTySig> interfaces() {
return interfaces;
}
}
/** A JVMS 4.7.9.1 FormalTypeParameter. */
public static class TyParamSig {
private final String name;
private final @Nullable TySig classBound;
private final ImmutableList<TySig> interfaceBounds;
public TyParamSig(
String name, @Nullable TySig classBound, ImmutableList<TySig> interfaceBounds) {
this.name = name;
this.classBound = classBound;
this.interfaceBounds = interfaceBounds;
}
/** A single class upper-bound, or {@code null}. */
public @Nullable TySig classBound() {
return classBound;
}
/** Interface upper-bounds. */
public ImmutableList<TySig> interfaceBounds() {
return interfaceBounds;
}
/** The name of the type parameter. */
public String name() {
return name;
}
}
/** A JVMS 4.7.9.1 ClassTypeSignature. */
public static class ClassTySig extends TySig {
private final String pkg;
private final ImmutableList<SimpleClassTySig> classes;
public ClassTySig(String pkg, ImmutableList<SimpleClassTySig> classes) {
this.pkg = pkg;
this.classes = classes;
}
/** The package name of the class. */
public String pkg() {
return pkg;
}
/**
* A list of a simple names, containing at least one top-level type and possible repeated member
* class names. Each element may include type arguments.
*
* <p>It's possible for the top-level type to be a desugared nested class with no type
* arguments, in this case the first element is the simple name of the lowered type, e.g. in
* {@code Foo$Bar<X>.<Y>} the first element may be an nested class {@code Bar} with an enclosing
* type {@code Foo}, but it may also be a top-level class that was named {@code Foo$Bar} in
* source. The signature is the same either way.
*/
public ImmutableList<SimpleClassTySig> classes() {
return classes;
}
@Override
public TySigKind kind() {
return TySigKind.CLASS_TY_SIG;
}
}
/** A JVMS 4.7.9.1 SimpleClassTypeSignature. */
public static class SimpleClassTySig {
private final String simpleName;
private final ImmutableList<TySig> tyArgs;
public SimpleClassTySig(String simpleName, ImmutableList<TySig> tyArgs) {
this.tyArgs = tyArgs;
this.simpleName = simpleName;
}
/** Type arguments. */
public ImmutableList<TySig> tyArgs() {
return tyArgs;
}
/** The simple name of the class. */
public String simpleName() {
return simpleName;
}
}
/**
* A wildcard type.
*
* <p>Wildcard are represented as first class types, instead only allowing them as top-level type
* arguments. This diverges from the buggy grammar in JVMS 4.7.9.1, see:
* http://mail.openjdk.java.net/pipermail/compiler-dev/2016-October/010450.html
*/
public abstract static class WildTySig extends TySig {
/** A wildcard bound kind. */
public enum BoundKind {
/** An unbounded wildcard. */
NONE,
/** A lower-bounded wildcard. */
LOWER,
/** An upper-bounded wildcard. */
UPPER
}
/** Returns the wildcard bound kind. */
public abstract BoundKind boundKind();
@Override
public TySigKind kind() {
return TySigKind.WILD_TY_SIG;
}
}
/** An upper-bounded wildcard. */
public static class UpperBoundTySig extends WildTySig {
private final TySig bound;
public UpperBoundTySig(TySig bound) {
this.bound = bound;
}
/** The upper bound. */
public TySig bound() {
return bound;
}
@Override
public BoundKind boundKind() {
return BoundKind.UPPER;
}
}
/** An lower-bounded wildcard. */
public static class LowerBoundTySig extends WildTySig {
private final TySig bound;
public LowerBoundTySig(TySig bound) {
this.bound = bound;
}
/** The lower bound. */
public TySig bound() {
return bound;
}
@Override
public BoundKind boundKind() {
return BoundKind.LOWER;
}
}
/** An unbounded wildcard. */
public static class WildTyArgSig extends WildTySig {
@Override
public BoundKind boundKind() {
return BoundKind.NONE;
}
}
/** A JVMS 4.7.9.1 ArrayTypeSignature. */
public static class ArrayTySig extends TySig {
private final TySig elementType;
public ArrayTySig(TySig elementType) {
this.elementType = elementType;
}
/** The element type. */
public TySig elementType() {
return elementType;
}
@Override
public TySigKind kind() {
return TySigKind.ARRAY_TY_SIG;
}
}
/** A JVMS 4.7.9.1 TypeVariableSignature. */
public static class TyVarSig extends TySig {
public final String name;
public TyVarSig(String name) {
this.name = name;
}
/** The name of the type variable. */
public String name() {
return name;
}
@Override
public TySigKind kind() {
return TySigKind.TY_VAR_SIG;
}
}
/** An abstract class for all JVMS 4.7.9.1 JavaTypeSignatures. */
public abstract static class TySig {
/** The type kind. */
public enum TySigKind {
VOID_TY_SIG,
BASE_TY_SIG,
CLASS_TY_SIG,
ARRAY_TY_SIG,
TY_VAR_SIG,
WILD_TY_SIG
}
/** The type kind. */
public abstract TySigKind kind();
}
/** A JVMS 4.3.3 VoidDescriptor. */
public static final TySig VOID =
new TySig() {
@Override
public TySigKind kind() {
return TySigKind.VOID_TY_SIG;
}
};
/** A JVMS 4.3.2 BaseType. */
public static class BaseTySig extends TySig {
@Override
public TySigKind kind() {
return TySigKind.BASE_TY_SIG;
}
private final TurbineConstantTypeKind type;
public BaseTySig(TurbineConstantTypeKind type) {
this.type = type;
}
/** The base type kind. */
public TurbineConstantTypeKind type() {
return type;
}
}
/** A JVMS 4.7.9.1 MethodTypeSignature. */
public static class MethodSig {
private final ImmutableList<TyParamSig> tyParams;
private final ImmutableList<TySig> params;
private final TySig returnType;
private final ImmutableList<TySig> exceptions;
public MethodSig(
ImmutableList<TyParamSig> tyParams,
ImmutableList<TySig> params,
TySig returnType,
ImmutableList<TySig> exceptions) {
this.tyParams = tyParams;
this.params = params;
this.returnType = returnType;
this.exceptions = exceptions;
}
/** The formal type parameters. */
public ImmutableList<TyParamSig> tyParams() {
return tyParams;
}
/** The return type. Non-null, possibly {@link #VOID}. */
public TySig returnType() {
return returnType;
}
/** The formal parameters. */
public ImmutableList<TySig> params() {
return params;
}
/** The thrown exceptions. */
public ImmutableList<TySig> exceptions() {
return exceptions;
}
}
private Sig() {}
}
|