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
|
/*
* 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.lower;
import com.google.common.collect.ImmutableList;
import com.google.turbine.binder.bound.SourceTypeBoundClass;
import com.google.turbine.binder.bound.TypeBoundClass;
import com.google.turbine.binder.bound.TypeBoundClass.TyVarInfo;
import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.binder.sym.TyVarSymbol;
import com.google.turbine.bytecode.sig.Sig;
import com.google.turbine.bytecode.sig.Sig.ClassSig;
import com.google.turbine.bytecode.sig.Sig.ClassTySig;
import com.google.turbine.bytecode.sig.Sig.LowerBoundTySig;
import com.google.turbine.bytecode.sig.Sig.MethodSig;
import com.google.turbine.bytecode.sig.Sig.SimpleClassTySig;
import com.google.turbine.bytecode.sig.Sig.TySig;
import com.google.turbine.bytecode.sig.Sig.UpperBoundTySig;
import com.google.turbine.bytecode.sig.SigWriter;
import com.google.turbine.model.TurbineFlag;
import com.google.turbine.model.TurbineTyKind;
import com.google.turbine.type.Type;
import com.google.turbine.type.Type.ArrayTy;
import com.google.turbine.type.Type.ClassTy;
import com.google.turbine.type.Type.ClassTy.SimpleClassTy;
import com.google.turbine.type.Type.PrimTy;
import com.google.turbine.type.Type.TyKind;
import com.google.turbine.type.Type.TyVar;
import com.google.turbine.type.Type.WildTy;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
/** Translator from {@link Type}s to {@link Sig}natures. */
public class LowerSignature {
final Set<ClassSymbol> classes = new LinkedHashSet<>();
/** Translates types to signatures. */
public Sig.TySig signature(Type ty) {
switch (ty.tyKind()) {
case CLASS_TY:
return classTySig((Type.ClassTy) ty);
case TY_VAR:
return tyVarSig((TyVar) ty);
case ARRAY_TY:
return arrayTySig((ArrayTy) ty);
case PRIM_TY:
return refBaseTy((PrimTy) ty);
case VOID_TY:
return Sig.VOID;
case WILD_TY:
return wildTy((WildTy) ty);
default:
throw new AssertionError(ty.tyKind());
}
}
private Sig.BaseTySig refBaseTy(PrimTy t) {
return new Sig.BaseTySig(t.primkind());
}
private Sig.ArrayTySig arrayTySig(ArrayTy t) {
return new Sig.ArrayTySig(signature(t.elementType()));
}
private Sig.TyVarSig tyVarSig(TyVar t) {
return new Sig.TyVarSig(t.sym().name());
}
private ClassTySig classTySig(ClassTy t) {
classes.add(t.sym());
ImmutableList.Builder<SimpleClassTySig> classes = ImmutableList.builder();
Iterator<SimpleClassTy> it = t.classes().iterator();
SimpleClassTy curr = it.next();
while (curr.targs().isEmpty() && it.hasNext()) {
curr = it.next();
}
String pkg = curr.sym().packageName();
classes.add(new Sig.SimpleClassTySig(curr.sym().simpleName(), tyArgSigs(curr)));
while (it.hasNext()) {
SimpleClassTy outer = curr;
curr = it.next();
String shortname = curr.sym().binaryName().substring(outer.sym().binaryName().length() + 1);
classes.add(new Sig.SimpleClassTySig(shortname, tyArgSigs(curr)));
}
return new ClassTySig(pkg, classes.build());
}
private ImmutableList<TySig> tyArgSigs(SimpleClassTy part) {
ImmutableList.Builder<TySig> tyargs = ImmutableList.builder();
for (Type targ : part.targs()) {
tyargs.add(signature(targ));
}
return tyargs.build();
}
private TySig wildTy(WildTy ty) {
switch (ty.boundKind()) {
case NONE:
return new Sig.WildTyArgSig();
case UPPER:
return new UpperBoundTySig(signature(((Type.WildUpperBoundedTy) ty).bound()));
case LOWER:
return new LowerBoundTySig(signature(((Type.WildLowerBoundedTy) ty).bound()));
}
throw new AssertionError(ty.boundKind());
}
/**
* Produces a method signature attribute for a generic method, or {@code null} if the signature is
* unnecessary.
*/
public @Nullable String methodSignature(
Env<ClassSymbol, TypeBoundClass> env, TypeBoundClass.MethodInfo method, ClassSymbol sym) {
if (!needsMethodSig(sym, env, method)) {
return null;
}
ImmutableList<Sig.TyParamSig> typarams = tyParamSig(method.tyParams(), env);
ImmutableList.Builder<Sig.TySig> fparams = ImmutableList.builder();
for (TypeBoundClass.ParamInfo t : method.parameters()) {
if (t.synthetic()) {
continue;
}
fparams.add(signature(t.type()));
}
Sig.TySig ret = signature(method.returnType());
ImmutableList.Builder<Sig.TySig> excn = ImmutableList.builder();
boolean needsExnSig = false;
for (Type e : method.exceptions()) {
if (needsSig(e)) {
needsExnSig = true;
break;
}
}
if (needsExnSig) {
for (Type e : method.exceptions()) {
excn.add(signature(e));
}
}
MethodSig sig = new MethodSig(typarams, fparams.build(), ret, excn.build());
return SigWriter.method(sig);
}
private boolean needsMethodSig(
ClassSymbol sym, Env<ClassSymbol, TypeBoundClass> env, TypeBoundClass.MethodInfo m) {
if ((env.getNonNull(sym).access() & TurbineFlag.ACC_ENUM) == TurbineFlag.ACC_ENUM
&& m.name().equals("<init>")) {
// JDK-8024694: javac always expects signature attribute for enum constructors
return true;
}
if (!m.tyParams().isEmpty()) {
return true;
}
if (m.returnType() != null && needsSig(m.returnType())) {
return true;
}
for (TypeBoundClass.ParamInfo t : m.parameters()) {
if (t.synthetic()) {
continue;
}
if (needsSig(t.type())) {
return true;
}
}
for (Type t : m.exceptions()) {
if (needsSig(t)) {
return true;
}
}
return false;
}
/**
* Produces a class signature attribute for a generic class, or {@code null} if the signature is
* unnecessary.
*/
public @Nullable String classSignature(
SourceTypeBoundClass info, Env<ClassSymbol, TypeBoundClass> env) {
if (!classNeedsSig(info)) {
return null;
}
ImmutableList<Sig.TyParamSig> typarams = tyParamSig(info.typeParameterTypes(), env);
ClassTySig xtnd = classTySig((ClassTy) info.superClassType());
ImmutableList.Builder<ClassTySig> impl = ImmutableList.builder();
for (Type i : info.interfaceTypes()) {
impl.add(classTySig((ClassTy) i));
}
ClassSig sig = new ClassSig(typarams, xtnd, impl.build());
return SigWriter.classSig(sig);
}
/**
* A field signature, or {@code null} if the descriptor provides all necessary type information.
*/
public @Nullable String fieldSignature(Type type) {
return needsSig(type) ? SigWriter.type(signature(type)) : null;
}
private boolean classNeedsSig(SourceTypeBoundClass ci) {
if (!ci.typeParameters().isEmpty()) {
return true;
}
if (ci.superClassType() != null && needsSig(ci.superClassType())) {
return true;
}
for (Type i : ci.interfaceTypes()) {
if (needsSig(i)) {
return true;
}
}
return false;
}
private boolean needsSig(Type ty) {
switch (ty.tyKind()) {
case PRIM_TY:
case VOID_TY:
return false;
case CLASS_TY:
{
for (SimpleClassTy s : ((ClassTy) ty).classes()) {
if (!s.targs().isEmpty()) {
return true;
}
}
return false;
}
case ARRAY_TY:
return needsSig(((ArrayTy) ty).elementType());
case TY_VAR:
return true;
default:
throw new AssertionError(ty.tyKind());
}
}
private ImmutableList<Sig.TyParamSig> tyParamSig(
Map<TyVarSymbol, TyVarInfo> px, Env<ClassSymbol, TypeBoundClass> env) {
ImmutableList.Builder<Sig.TyParamSig> result = ImmutableList.builder();
for (Map.Entry<TyVarSymbol, TyVarInfo> entry : px.entrySet()) {
result.add(tyParamSig(entry.getKey(), entry.getValue(), env));
}
return result.build();
}
private Sig.TyParamSig tyParamSig(
TyVarSymbol sym, TyVarInfo info, Env<ClassSymbol, TypeBoundClass> env) {
String identifier = sym.name();
Sig.TySig cbound = null;
ImmutableList.Builder<Sig.TySig> ibounds = ImmutableList.builder();
if (info.upperBound().bounds().isEmpty()) {
cbound =
new ClassTySig(
"java/lang", ImmutableList.of(new SimpleClassTySig("Object", ImmutableList.of())));
} else {
boolean first = true;
for (Type bound : info.upperBound().bounds()) {
TySig sig = signature(bound);
if (first) {
if (!isInterface(bound, env)) {
cbound = sig;
continue;
}
}
ibounds.add(sig);
first = false;
}
}
return new Sig.TyParamSig(identifier, cbound, ibounds.build());
}
private boolean isInterface(Type type, Env<ClassSymbol, TypeBoundClass> env) {
return type.tyKind() == TyKind.CLASS_TY
&& env.getNonNull(((ClassTy) type).sym()).kind() == TurbineTyKind.INTERFACE;
}
public String descriptor(ClassSymbol sym) {
classes.add(sym);
return sym.binaryName();
}
String objectType(ClassSymbol sym) {
return "L" + descriptor(sym) + ";";
}
}
|