Refactor constant pool

This commit is contained in:
ktkk 2026-07-05 23:29:37 +02:00
parent 9135e95dbd
commit 146d90ccec
6 changed files with 257 additions and 500 deletions

View file

@ -44,12 +44,10 @@ pub const AttributeInfo = union(enum) {
const Self = @This();
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPool.Info, allocator: std.mem.Allocator) (ParseError || ResolveError)!AttributeInfo {
const attribute_name_index = try input.takeInt(u16, .big) - 1;
if (attribute_name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[attribute_name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
const attribute_name = cp_info.?.utf8;
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) (ParseError || ResolveError)!AttributeInfo {
const attribute_name_index = try input.takeInt(u16, .big);
const attribute_name = try constant_pool.getTag(attribute_name_index, .utf8);
const name = attribute_name.utf8.bytes;
const attribute_length = try input.takeInt(u32, .big);
@ -57,169 +55,169 @@ pub const AttributeInfo = union(enum) {
var limited_reader: std.Io.Reader.Limited = .init(input, .limited(attribute_length), &limited_buf);
const limited = &limited_reader.interface;
if (std.mem.eql(u8, attribute_name.bytes, "ConstantValue")) {
if (std.mem.eql(u8, name, "ConstantValue")) {
return .{
.predefined = .{
.constant_value = try ConstantValue.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Code")) {
} else if (std.mem.eql(u8, name, "Code")) {
return .{
.predefined = .{
.code = try Code.parse(limited, constant_pool, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "StackMapTable")) {
} else if (std.mem.eql(u8, name, "StackMapTable")) {
return .{
.predefined = .{
.stack_map_table = try StackMapTable.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Exceptions")) {
} else if (std.mem.eql(u8, name, "Exceptions")) {
return .{
.predefined = .{
.exceptions = try Exceptions.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "InnerClasses")) {
} else if (std.mem.eql(u8, name, "InnerClasses")) {
return .{
.predefined = .{
.inner_classes = try InnerClasses.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "EnclosingMethod")) {
} else if (std.mem.eql(u8, name, "EnclosingMethod")) {
return .{
.predefined = .{
.enclosing_method = try EnclosingMethod.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Synthetic")) {
} else if (std.mem.eql(u8, name, "Synthetic")) {
return .{
.predefined = .{
.synthetic = .{},
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Signature")) {
} else if (std.mem.eql(u8, name, "Signature")) {
return .{
.predefined = .{
.signature = try Signature.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "SourceFile")) {
} else if (std.mem.eql(u8, name, "SourceFile")) {
return .{
.predefined = .{
.source_file = try SourceFile.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "SourceDebugExtension")) {
} else if (std.mem.eql(u8, name, "SourceDebugExtension")) {
return .{
.predefined = .{
.source_debug_extension = try SourceDebugExtension.parse(limited, attribute_length, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "LineNumberTable")) {
} else if (std.mem.eql(u8, name, "LineNumberTable")) {
return .{
.predefined = .{
.line_number_table = try LineNumberTable.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "LocalVariableTable")) {
} else if (std.mem.eql(u8, name, "LocalVariableTable")) {
return .{
.predefined = .{
.local_variable_table = try LocalVariableTable.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "LocalVariableTypeTable")) {
} else if (std.mem.eql(u8, name, "LocalVariableTypeTable")) {
return .{
.predefined = .{
.local_variable_type_table = try LocalVariableTypeTable.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Deprecated")) {
} else if (std.mem.eql(u8, name, "Deprecated")) {
return .{
.predefined = .{
.deprecated = .{},
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "RuntimeVisibleAnnotations")) {
} else if (std.mem.eql(u8, name, "RuntimeVisibleAnnotations")) {
return .{
.predefined = .{
.runtime_visible_annotations = try RuntimeVisibleAnnotations.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "RuntimeInvisibleAnnotations")) {
} else if (std.mem.eql(u8, name, "RuntimeInvisibleAnnotations")) {
return .{
.predefined = .{
.runtime_invisible_annotations = try RuntimeInvisibleAnnotations.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "RuntimeVisibleParameterAnnotations")) {
} else if (std.mem.eql(u8, name, "RuntimeVisibleParameterAnnotations")) {
return .{
.predefined = .{
.runtime_visible_parameter_annotations = try RuntimeVisibleParameterAnnotations.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "RuntimeInvisibleParameterAnnotations")) {
} else if (std.mem.eql(u8, name, "RuntimeInvisibleParameterAnnotations")) {
return .{
.predefined = .{
.runtime_invisible_parameter_annotations = try RuntimeInvisibleParameterAnnotations.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "AnnotationDefault")) {
} else if (std.mem.eql(u8, name, "AnnotationDefault")) {
return .{
.predefined = .{
.annotation_default = try AnnotationDefault.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "BootstrapMethods")) {
} else if (std.mem.eql(u8, name, "BootstrapMethods")) {
return .{
.predefined = .{
.bootstrap_methods = try BootstrapMethods.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "MethodParameters")) {
} else if (std.mem.eql(u8, name, "MethodParameters")) {
return .{
.predefined = .{
.method_parameters = try MethodParameters.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Module")) {
} else if (std.mem.eql(u8, name, "Module")) {
return .{
.predefined = .{
.module = try Module.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "ModulePackages")) {
} else if (std.mem.eql(u8, name, "ModulePackages")) {
return .{
.predefined = .{
.module_packages = try ModulePackages.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "ModuleMainClass")) {
} else if (std.mem.eql(u8, name, "ModuleMainClass")) {
return .{
.predefined = .{
.module_main_class = try ModuleMainClass.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "NestHost")) {
} else if (std.mem.eql(u8, name, "NestHost")) {
return .{
.predefined = .{
.nest_host = try NestHost.parse(limited),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "NestMembers")) {
} else if (std.mem.eql(u8, name, "NestMembers")) {
return .{
.predefined = .{
.nest_members = try NestMembers.parse(limited, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "Record")) {
} else if (std.mem.eql(u8, name, "Record")) {
return .{
.predefined = .{
.record = try Record.parse(limited, constant_pool, allocator),
},
};
} else if (std.mem.eql(u8, attribute_name.bytes, "PermittedSubclasses")) {
} else if (std.mem.eql(u8, name, "PermittedSubclasses")) {
return .{
.predefined = .{
.permitted_subclasses = try PermittedSubclasses.parse(limited, allocator),
@ -266,7 +264,7 @@ pub const AttributeInfo = union(enum) {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
switch (self.*) {
.new => |new| {
@ -343,15 +341,11 @@ pub const AttributeInfo = union(enum) {
exception_indent += 1;
defer exception_indent -= 1;
const index = exception_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return error.WriteFailed;
const info = constant_pool.getTag(exception_index, .class) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * exception_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, exception_indent + 1, constant_pool);
try info.indentedFormat(w, depth, exception_indent + 1, constant_pool);
}
},
.inner_classes => |attr| {
@ -482,15 +476,11 @@ pub const AttributeInfo = union(enum) {
package_indent += 1;
defer package_indent -= 1;
const index = package_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .package) return error.WriteFailed;
const info = constant_pool.getTag(package_index, .package) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * package_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, package_indent + 1, constant_pool);
try info.indentedFormat(w, depth, package_indent + 1, constant_pool);
}
},
.module_main_class => |attr| {
@ -513,15 +503,11 @@ pub const AttributeInfo = union(enum) {
class_indent += 1;
defer class_indent -= 1;
const index = class_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return error.WriteFailed;
const info = constant_pool.getTag(class_index, .class) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * class_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, class_indent + 1, constant_pool);
try info.indentedFormat(w, depth, class_indent + 1, constant_pool);
}
},
.record => |attr| {
@ -545,15 +531,11 @@ pub const AttributeInfo = union(enum) {
class_indent += 1;
defer class_indent -= 1;
const index = class_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return error.WriteFailed;
const info = constant_pool.getTag(class_index, .class) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * class_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, class_indent + 1, constant_pool);
try info.indentedFormat(w, depth, class_indent + 1, constant_pool);
}
},
else => {},
@ -580,13 +562,8 @@ pub const NewAttribute = struct {
allocator.free(self.info);
}
pub fn attributeName(self: *const Self, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const attribute_name_index = self.attribute_name_index - 1;
if (attribute_name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[attribute_name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn attributeName(self: *const Self, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.attribute_name_index, .utf8);
}
};
@ -601,13 +578,8 @@ pub const ConstantValue = struct {
};
}
pub fn constantValue(self: *const Self, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const constant_value_index = self.constant_value_index - 1;
if (constant_value_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[constant_value_index];
if (cp_info == null) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn constantValue(self: *const Self, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.get(self.constant_value_index);
}
};
@ -631,7 +603,7 @@ pub const Code = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("start_pc: {d}\n", .{ self.start_pc });
@ -650,17 +622,12 @@ pub const Code = struct {
}
}
pub fn catchType(self: *const ExceptionHandler, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.catch_type == 0) return null;
const catch_type = self.catch_type - 1;
if (catch_type >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[catch_type];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn catchType(self: *const ExceptionHandler, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.catch_type, .class);
}
};
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPool.Info, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {
const max_stack = try input.takeInt(u16, .big);
const max_locals = try input.takeInt(u16, .big);
@ -947,7 +914,7 @@ pub const InnerClasses = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("flags: {f}\n", .{ self.inner_class_access_flags });
@ -972,32 +939,16 @@ pub const InnerClasses = struct {
}
}
pub fn innerClass(self: *const InnerClass, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const inner_class_info_index = self.inner_class_info_index - 1;
if (inner_class_info_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[inner_class_info_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn innerClass(self: *const InnerClass, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.inner_class_info_index, .class);
}
pub fn outerClass(self: *const InnerClass, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.outer_class_info_index == 0) return null;
const outer_class_info_index = self.outer_class_info_index - 1;
if (outer_class_info_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[outer_class_info_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn outerClass(self: *const InnerClass, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.outer_class_info_index, .class);
}
pub fn innerName(self: *const InnerClass, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.inner_name_index == 0) return null;
const inner_name_index = self.inner_name_index - 1;
if (inner_name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[inner_name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn innerName(self: *const InnerClass, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.inner_name_index, .utf8);
}
};
@ -1037,21 +988,12 @@ pub const EnclosingMethod = struct {
};
}
pub fn class(self: *const EnclosingMethod, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const class_index = self.class_index - 1;
if (class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[class_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn class(self: *const EnclosingMethod, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.class_index, .class);
}
pub fn method(self: *const EnclosingMethod, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.method_index == 0) return null;
const method_index = self.method_index - 1;
if (method_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[method_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn method(self: *const EnclosingMethod, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.method_index, .name_and_type);
}
};
@ -1068,12 +1010,8 @@ pub const Signature = struct {
};
}
pub fn signature(self: *const Signature, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const signature_index = self.signature_index - 1;
if (signature_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[signature_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn signature(self: *const Signature, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.signature_index, .utf8);
}
};
@ -1088,12 +1026,8 @@ pub const SourceFile = struct {
};
}
pub fn sourceFile(self: *const SourceFile, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const source_file_index = self.source_file_index - 1;
if (source_file_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[source_file_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn sourceFile(self: *const SourceFile, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.source_file_index, .utf8);
}
};
@ -1174,7 +1108,7 @@ pub const LocalVariableTable = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("start_pc: {d}\n", .{ self.start_pc });
@ -1193,20 +1127,12 @@ pub const LocalVariableTable = struct {
try descriptor_.indentedFormat(w, depth, indent + 1, constant_pool);
}
pub fn name(self: *const LocalVariable, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn name(self: *const LocalVariable, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.name_index, .utf8);
}
pub fn descriptor(self: *const LocalVariable, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const descriptor_index = self.descriptor_index - 1;
if (descriptor_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[descriptor_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn descriptor(self: *const LocalVariable, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.descriptor_index, .utf8);
}
};
@ -1251,7 +1177,7 @@ pub const LocalVariableTypeTable = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("start_pc: {d}\n", .{ self.start_pc });
@ -1270,20 +1196,12 @@ pub const LocalVariableTypeTable = struct {
try signature_.indentedFormat(w, depth, indent + 1, constant_pool);
}
pub fn name(self: *const LocalVariableType, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn name(self: *const LocalVariableType, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.name_index, .utf8);
}
pub fn signature(self: *const LocalVariableType, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const signature_index = self.signature_index - 1;
if (signature_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[signature_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn signature(self: *const LocalVariableType, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.signature_index, .utf8);
}
};
@ -1623,7 +1541,7 @@ pub const BootstrapMethods = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const bootstrap_method = self.bootstrapMethod(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -1637,24 +1555,16 @@ pub const BootstrapMethods = struct {
bootstrap_argument_indent += 1;
defer bootstrap_argument_indent -= 1;
const index = bootstrap_argument - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or !@as(ConstantPool.Info.Tag, cp_info.?).loadable()) return error.WriteFailed;
const info = constant_pool.getLoadable(bootstrap_argument) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * bootstrap_argument_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, bootstrap_argument_indent + 1, constant_pool);
try info.indentedFormat(w, depth, bootstrap_argument_indent + 1, constant_pool);
}
}
pub fn bootstrapMethod(self: *const BootstrapMethod, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const bootstrap_method_ref = self.bootstrap_method_ref - 1;
if (bootstrap_method_ref >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[bootstrap_method_ref];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .method_handle) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn bootstrapMethod(self: *const BootstrapMethod, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.bootstrap_method_ref, .method_handle);
}
};
@ -1711,7 +1621,7 @@ pub const MethodParameters = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("flags: {f}\n", .{ self.access_flags });
@ -1722,12 +1632,8 @@ pub const MethodParameters = struct {
try name_.indentedFormat(w, depth, indent + 1, constant_pool);
}
pub fn name(self: *const Parameter, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn name(self: *const Parameter, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.name_index, .utf8);
}
};
@ -1787,7 +1693,7 @@ pub const Module = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const requires_ = self.requires(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -1805,21 +1711,12 @@ pub const Module = struct {
}
}
pub fn requires(self: *const Requires, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const requires_index = self.requires_index - 1;
if (requires_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[requires_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .module) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn requires(self: *const Requires, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.requires_index, .module);
}
pub fn requiresVersion(self: *const Requires, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.requires_version_index == 0) return null;
const requires_version_index = self.requires_version_index - 1;
if (requires_version_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[requires_version_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn requiresVersion(self: *const Requires, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.requires_version_index, .utf8);
}
};
@ -1838,7 +1735,7 @@ pub const Module = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const exports_ = self.exports(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -1855,24 +1752,16 @@ pub const Module = struct {
exports_to_indent += 1;
defer exports_to_indent -= 1;
const index = exports_to_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .module) return error.WriteFailed;
const info = constant_pool.getTag(exports_to_index, .module) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * exports_to_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, exports_to_indent + 1, constant_pool);
try info.indentedFormat(w, depth, exports_to_indent + 1, constant_pool);
}
}
pub fn exports(self: *const Exports, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const exports_index = self.exports_index - 1;
if (exports_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[exports_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .package) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn exports(self: *const Exports, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.exports_index, .package);
}
};
@ -1891,7 +1780,7 @@ pub const Module = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const opens_ = self.opens(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -1908,24 +1797,16 @@ pub const Module = struct {
opens_to_indent += 1;
defer opens_to_indent -= 1;
const index = opens_to_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .module) return error.WriteFailed;
const info = constant_pool.getTag(opens_to_index, .module) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * opens_to_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, opens_to_indent + 1, constant_pool);
try info.indentedFormat(w, depth, opens_to_indent + 1, constant_pool);
}
}
pub fn opens(self: *const Opens, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const opens_index = self.opens_index - 1;
if (opens_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[opens_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .package) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn opens(self: *const Opens, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.opens_index, .package);
}
};
@ -1938,7 +1819,7 @@ pub const Module = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const provides_ = self.provides(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -1952,24 +1833,16 @@ pub const Module = struct {
provides_with_indent += 1;
defer provides_with_indent -= 1;
const index = provides_with_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return error.WriteFailed;
const info = constant_pool.getTag(provides_with_index, .class) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * provides_with_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, provides_with_indent + 1, constant_pool);
try info.indentedFormat(w, depth, provides_with_indent + 1, constant_pool);
}
}
pub fn provides(self: *const Provides, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const provides_index = self.provides_index - 1;
if (provides_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[provides_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn provides(self: *const Provides, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.provides_index, .class);
}
};
@ -2088,7 +1961,7 @@ pub const Module = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const module_name = self.moduleName(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -2148,15 +2021,11 @@ pub const Module = struct {
uses_indent += 1;
defer uses_indent -= 1;
const index = uses_index - 1;
if (index >= constant_pool.len) return error.WriteFailed;
const cp_info = constant_pool[index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return error.WriteFailed;
const info = constant_pool.getTag(uses_index, .class) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * uses_indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.indentedFormat(w, depth, uses_indent + 1, constant_pool);
try info.indentedFormat(w, depth, uses_indent + 1, constant_pool);
}
try w.splatByteAll(' ', depth * indent);
@ -2172,21 +2041,12 @@ pub const Module = struct {
}
}
pub fn moduleName(self: *const Module, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const module_name_index = self.module_name_index - 1;
if (module_name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[module_name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn moduleName(self: *const Module, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.module_name_index, .utf8);
}
pub fn moduleVersion(self: *const Module, constant_pool: []const ?ConstantPool.Info) ResolveError!?ConstantPool.Info {
if (self.module_version_index == 0) return null;
const module_version_index = self.module_version_index - 1;
if (module_version_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[module_version_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn moduleVersion(self: *const Module, constant_pool: ConstantPool) ResolveError!?ConstantPool.Info {
return constant_pool.getTagOptional(self.module_version_index, .utf8);
}
};
@ -2224,12 +2084,8 @@ pub const ModuleMainClass = struct {
};
}
pub fn mainClass(self: *const ModuleMainClass, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const main_class_index = self.main_class_index - 1;
if (main_class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[main_class_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn mainClass(self: *const ModuleMainClass, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.main_class_index, .class);
}
};
@ -2244,12 +2100,8 @@ pub const NestHost = struct {
};
}
pub fn hostClass(self: *const NestHost, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const host_class_index = self.host_class_index - 1;
if (host_class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[host_class_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn hostClass(self: *const NestHost, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.host_class_index, .class);
}
};
@ -2285,7 +2137,7 @@ pub const Record = struct {
descriptor_index: u16,
attributes: []AttributeInfo,
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPool.Info, allocator: std.mem.Allocator) (ParseError || ResolveError)!RecordComponentInfo {
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) (ParseError || ResolveError)!RecordComponentInfo {
const name_index = try input.takeInt(u16, .big);
const descriptor_index = try input.takeInt(u16, .big);
@ -2315,7 +2167,7 @@ pub const Record = struct {
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?ConstantPool.Info,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const name_ = self.name(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
@ -2336,29 +2188,20 @@ pub const Record = struct {
try w.splatByteAll(' ', depth * attr_indent);
try w.print("{d}:\n", .{ i });
try attr.indentedFormat(w, depth, attr_indent + 1, constant_pool);
}
}
pub fn name(self: *const RecordComponentInfo, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn name(self: *const RecordComponentInfo, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.name_index, .utf8);
}
pub fn descriptor(self: *const RecordComponentInfo, constant_pool: []const ?ConstantPool.Info) ResolveError!ConstantPool.Info {
const descriptor_index = self.descriptor_index - 1;
if (descriptor_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[descriptor_index];
if (cp_info == null or @as(ConstantPool.Info.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
pub fn descriptor(self: *const RecordComponentInfo, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.descriptor_index, .utf8);
}
};
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPool.Info, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {
const components_count = try input.takeInt(u16, .big);
const components = try allocator.alloc(RecordComponentInfo, components_count);
for (components) |*component| {