Skip to content

Commit 0b547dc

Browse files
cushonJavac Team
authored andcommitted
Initial support for sealed classes
PiperOrigin-RevId: 400303125
1 parent 3452789 commit 0b547dc

27 files changed

Lines changed: 229 additions & 8 deletions

java/com/google/turbine/binder/CanonicalTypeBinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static SourceTypeBoundClass bind(
6767
ImmutableList<FieldInfo> fields = fields(base.source(), env, sym, base.fields());
6868
return new SourceTypeBoundClass(
6969
interfaceTypes.build(),
70+
base.permits(),
7071
superClassType,
7172
typParamTypes,
7273
base.access(),

java/com/google/turbine/binder/CompUnitPreprocessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ private static TyDecl packageInfoTree(PkgDecl pkgDecl) {
225225
ImmutableList.of(),
226226
ImmutableList.of(),
227227
ImmutableList.of(),
228+
ImmutableList.of(),
228229
TurbineTyKind.INTERFACE,
229230
/* javadoc= */ null);
230231
}

java/com/google/turbine/binder/ConstBinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public SourceTypeBoundClass bind() {
110110
ImmutableList<MethodInfo> methods = bindMethods(base.methods());
111111
return new SourceTypeBoundClass(
112112
bindTypes(base.interfaceTypes()),
113+
base.permits(),
113114
base.superClassType() != null ? bindType(base.superClassType()) : null,
114115
bindTypeParameters(base.typeParameterTypes()),
115116
base.access(),

java/com/google/turbine/binder/DisambiguateTypeAnnotations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static SourceTypeBoundClass bind(
7171
SourceTypeBoundClass base, Env<ClassSymbol, TypeBoundClass> env) {
7272
return new SourceTypeBoundClass(
7373
base.interfaceTypes(),
74+
base.permits(),
7475
base.superClassType(),
7576
base.typeParameterTypes(),
7677
base.access(),

java/com/google/turbine/binder/TypeBinder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ private SourceTypeBoundClass bind() {
229229
interfaceTypes.add(bindClassTy(bindingScope, i));
230230
}
231231

232+
ImmutableList.Builder<ClassSymbol> permits = ImmutableList.builder();
233+
for (Tree.ClassTy i : base.decl().permits()) {
234+
Type type = bindClassTy(bindingScope, i);
235+
if (!type.tyKind().equals(Type.TyKind.CLASS_TY)) {
236+
throw new AssertionError(type.tyKind());
237+
}
238+
permits.add(((Type.ClassTy) type).sym());
239+
}
240+
232241
CompoundScope scope =
233242
base.scope()
234243
.toScope(Resolve.resolveFunction(env, owner))
@@ -251,6 +260,7 @@ private SourceTypeBoundClass bind() {
251260

252261
return new SourceTypeBoundClass(
253262
interfaceTypes.build(),
263+
permits.build(),
254264
superClassType,
255265
typeParameterTypes,
256266
base.access(),

java/com/google/turbine/binder/bound/SourceTypeBoundClass.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class SourceTypeBoundClass implements TypeBoundClass {
4444
private final ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes;
4545
private final @Nullable Type superClassType;
4646
private final ImmutableList<Type> interfaceTypes;
47+
private final ImmutableList<ClassSymbol> permits;
4748
private final ImmutableList<RecordComponentInfo> components;
4849
private final ImmutableList<MethodInfo> methods;
4950
private final ImmutableList<FieldInfo> fields;
@@ -57,6 +58,7 @@ public class SourceTypeBoundClass implements TypeBoundClass {
5758

5859
public SourceTypeBoundClass(
5960
ImmutableList<Type> interfaceTypes,
61+
ImmutableList<ClassSymbol> permits,
6062
@Nullable Type superClassType,
6163
ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes,
6264
int access,
@@ -75,6 +77,7 @@ public SourceTypeBoundClass(
7577
SourceFile source,
7678
Tree.TyDecl decl) {
7779
this.interfaceTypes = interfaceTypes;
80+
this.permits = permits;
7881
this.superClassType = superClassType;
7982
this.typeParameterTypes = typeParameterTypes;
8083
this.access = access;
@@ -116,6 +119,11 @@ public ImmutableList<ClassSymbol> interfaces() {
116119
return result.build();
117120
}
118121

122+
@Override
123+
public ImmutableList<ClassSymbol> permits() {
124+
return permits;
125+
}
126+
119127
@Override
120128
public int access() {
121129
return access;

java/com/google/turbine/binder/bound/TypeBoundClass.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.turbine.binder.sym.ClassSymbol;
2122
import com.google.turbine.binder.sym.FieldSymbol;
2223
import com.google.turbine.binder.sym.MethodSymbol;
2324
import com.google.turbine.binder.sym.ParamSymbol;
@@ -43,6 +44,9 @@ public interface TypeBoundClass extends HeaderBoundClass {
4344
/** Implemented interface types. */
4445
ImmutableList<Type> interfaceTypes();
4546

47+
/** The permitted direct subclasses. */
48+
ImmutableList<ClassSymbol> permits();
49+
4650
ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes();
4751

4852
/** Declared fields. */

java/com/google/turbine/binder/bytecode/BytecodeBoundClass.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ public ImmutableList<Type> interfaceTypes() {
306306
return interfaceTypes.get();
307307
}
308308

309+
@Override
310+
public ImmutableList<ClassSymbol> permits() {
311+
return ImmutableList.of();
312+
}
313+
309314
private final Supplier<ImmutableMap<TyVarSymbol, TyVarInfo>> typeParameterTypes =
310315
Suppliers.memoize(
311316
new Supplier<ImmutableMap<TyVarSymbol, TyVarInfo>>() {

java/com/google/turbine/bytecode/Attribute.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ enum Kind {
4545
NEST_HOST("NestHost"),
4646
NEST_MEMBERS("NestMembers"),
4747
RECORD("Record"),
48-
TURBINE_TRANSITIVE_JAR("TurbineTransitiveJar");
48+
TURBINE_TRANSITIVE_JAR("TurbineTransitiveJar"),
49+
PERMITTED_SUBCLASSES("PermittedSubclasses");
4950

5051
private final String signature;
5152

@@ -396,6 +397,20 @@ List<Attribute> attributes() {
396397
}
397398
}
398399

400+
/** A JVMS §4.7.31 PermittedSubclasses attribute. */
401+
class PermittedSubclasses implements Attribute {
402+
final List<String> permits;
403+
404+
public PermittedSubclasses(List<String> permits) {
405+
this.permits = permits;
406+
}
407+
408+
@Override
409+
public Kind kind() {
410+
return Kind.PERMITTED_SUBCLASSES;
411+
}
412+
}
413+
399414
/** A custom attribute for recording the original jar of repackaged transitive classes. */
400415
class TurbineTransitiveJar implements Attribute {
401416

java/com/google/turbine/bytecode/AttributeWriter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public void write(ByteArrayDataOutput output, Attribute attribute) {
9595
case RECORD:
9696
writeRecord(output, (Attribute.Record) attribute);
9797
break;
98+
case PERMITTED_SUBCLASSES:
99+
writePermittedSubclasses(output, (Attribute.PermittedSubclasses) attribute);
100+
break;
98101
case TURBINE_TRANSITIVE_JAR:
99102
writeTurbineTransitiveJar(output, (Attribute.TurbineTransitiveJar) attribute);
100103
break;
@@ -318,6 +321,16 @@ private void writeRecord(ByteArrayDataOutput output, Attribute.Record attribute)
318321
output.write(data);
319322
}
320323

324+
private void writePermittedSubclasses(
325+
ByteArrayDataOutput output, Attribute.PermittedSubclasses attribute) {
326+
output.writeShort(pool.utf8(attribute.kind().signature()));
327+
output.writeInt(2 + attribute.permits.size() * 2);
328+
output.writeShort(attribute.permits.size());
329+
for (String permits : attribute.permits) {
330+
output.writeShort(pool.classInfo(permits));
331+
}
332+
}
333+
321334
private void writeTurbineTransitiveJar(
322335
ByteArrayDataOutput output, TurbineTransitiveJar attribute) {
323336
output.writeShort(pool.utf8(attribute.kind().signature()));

0 commit comments

Comments
 (0)