Modularize
This commit is contained in:
parent
c61d74c22c
commit
eacf0381c9
13 changed files with 1492 additions and 1399 deletions
101
src/Class/FieldInfo.zig
Normal file
101
src/Class/FieldInfo.zig
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
const std = @import("std");
|
||||
|
||||
const EnumFlags = @import("../EnumFlags.zig").EnumFlags;
|
||||
|
||||
const Class = @import("../Class.zig");
|
||||
const ConstantPoolInfo = Class.ConstantPoolInfo;
|
||||
const AttributeInfo = Class.AttributeInfo;
|
||||
const ResolveError = Class.ResolveError;
|
||||
|
||||
access_flags: FieldAccessFlags,
|
||||
name_index: u16,
|
||||
descriptor_index: u16,
|
||||
attributes: []AttributeInfo,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub const FieldFlags = enum(u16) {
|
||||
public = 0x0001,
|
||||
private = 0x0002,
|
||||
protected = 0x0004,
|
||||
static = 0x0008,
|
||||
final = 0x0010,
|
||||
@"volatile" = 0x0040,
|
||||
transient = 0x0080,
|
||||
synthetic = 0x1000,
|
||||
@"enum" = 0x4000,
|
||||
};
|
||||
|
||||
pub const FieldAccessFlags = EnumFlags(FieldFlags);
|
||||
|
||||
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) !Self {
|
||||
const access_flags: FieldAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
|
||||
const name_index = try input.takeInt(u16, .big);
|
||||
const descriptor_index = try input.takeInt(u16, .big);
|
||||
const attributes = try Class.parseAttributes(input, allocator);
|
||||
return .{
|
||||
.access_flags = access_flags,
|
||||
.name_index = name_index,
|
||||
.descriptor_index = descriptor_index,
|
||||
.attributes = attributes,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
for (self.attributes) |*attr_info| {
|
||||
attr_info.deinit(allocator);
|
||||
}
|
||||
allocator.free(self.attributes);
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: *const Self,
|
||||
w: *std.Io.Writer,
|
||||
depth: usize,
|
||||
indent: u8,
|
||||
allocator: std.mem.Allocator,
|
||||
constant_pool: []const ?ConstantPoolInfo,
|
||||
) std.Io.Writer.Error!void {
|
||||
try w.splatByteAll(' ', depth * indent);
|
||||
try w.print("flags: {f}\n", .{ self.access_flags });
|
||||
|
||||
try w.splatByteAll(' ', depth * indent);
|
||||
_ = try w.write("name:\n");
|
||||
const field_name = self.name(constant_pool) catch return error.WriteFailed;
|
||||
try field_name.format(w, depth, indent + 1, constant_pool);
|
||||
|
||||
try w.splatByteAll(' ', depth * indent);
|
||||
_ = try w.write("descriptor:\n");
|
||||
const field_descriptor = self.descriptor(constant_pool) catch return error.WriteFailed;
|
||||
try field_descriptor.format(w, depth, indent + 1, constant_pool);
|
||||
|
||||
try w.splatByteAll(' ', depth * indent);
|
||||
_ = try w.write("attributes:\n");
|
||||
var attr_indent = indent;
|
||||
for (self.attributes, 0..) |*attribute, j| {
|
||||
attr_indent += 1;
|
||||
defer attr_indent -= 1;
|
||||
|
||||
try w.splatByteAll(' ', depth * attr_indent);
|
||||
try w.print("{d}:\n", .{ j });
|
||||
|
||||
try attribute.format(w, depth, attr_indent + 1, allocator, constant_pool);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(self: *const Self, constant_pool: []const ?ConstantPoolInfo) ResolveError!ConstantPoolInfo {
|
||||
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(ConstantPoolInfo.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
|
||||
return cp_info.?;
|
||||
}
|
||||
|
||||
pub fn descriptor(self: *const Self, constant_pool: []const ?ConstantPoolInfo) ResolveError!ConstantPoolInfo {
|
||||
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(ConstantPoolInfo.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
|
||||
return cp_info.?;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue