Skip to content

Commit

Permalink
intr SameLayoutRepr eliminate all ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasIsensee committed Oct 16, 2024
1 parent f8b8190 commit a698e3f
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 100 deletions.
8 changes: 4 additions & 4 deletions src/backwards_compatibility.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# The following block allows reading Union types written prior to v0.2
const LEGACY_H5TYPE_UNION = VariableLengthDatatype(H5TYPE_DATATYPE)

function jlconvert(::ReadRepresentation{Union, Vlen{DataTypeODR()}}, f::JLDFile,
function jlconvert(::ReadRepresentation{Union, Vlen{DataTypeODR}}, f::JLDFile,
ptr::Ptr, header_offset::RelOffset)
v = Union{jlconvert(ReadRepresentation{DataType,Vlen{DataTypeODR()}}(), f, ptr, NULL_REFERENCE)...}
v = Union{jlconvert(ReadRepresentation{DataType,Vlen{DataTypeODR}}(), f, ptr, NULL_REFERENCE)...}
track_weakref!(f, header_offset, v)
v
end

constructrr(::JLDFile, ::Type{T}, dt::VariableLengthDatatype, ::Vector{ReadAttribute}) where {T<:Union} =
dt == LEGACY_H5TYPE_UNION ? (ReadRepresentation{Union,Vlen{DataTypeODR()}}(), true) :
dt == LEGACY_H5TYPE_UNION ? (ReadRepresentation{Union,Vlen{DataTypeODR}}(), true) :
throw(UnsupportedFeatureException())

# The following definition is needed to correctly load Strings written
Expand All @@ -18,7 +18,7 @@ function read_array(f::JLDFile, dataspace::ReadDataspace,
rr::FixedLengthString{String}, layout::DataLayout,
filters::FilterPipeline, header_offset::RelOffset,
attributes::Union{Vector{ReadAttribute},Nothing})
rrv = ReadRepresentation{UInt8,odr(UInt8)}()
rrv = readrepr(UInt8, UInt8)()

Check warning on line 21 in src/backwards_compatibility.jl

View check run for this annotation

Codecov / codecov/patch

src/backwards_compatibility.jl#L21

Added line #L21 was not covered by tests
v = read_array(f, dataspace, rrv, layout, filters, NULL_REFERENCE, attributes)
String(v)
end
Expand Down
8 changes: 4 additions & 4 deletions src/committed_datatype_introspection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function stringify_committed_datatype(f, cdt; showfields=false)
else
# These are normal julia types
dtrr = jltype(f, dt.members[i])
fieldtype = string(typeof(dtrr).parameters[1])
fieldtype = string(eltype(dtrr))

Check warning on line 44 in src/committed_datatype_introspection.jl

View check run for this annotation

Codecov / codecov/patch

src/committed_datatype_introspection.jl#L44

Added line #L44 was not covered by tests
end
push!(field_strs, "$(dt.names[i])::$(fieldtype)")
end
Expand Down Expand Up @@ -127,7 +127,7 @@ function jlconvert_string_wrap(rr, f, offset)
end
end

function jlconvert_string(rr::ReadRepresentation{T,DataTypeODR()},
function jlconvert_string(::ReadRepresentation{T,DataTypeODR},

Check warning on line 130 in src/committed_datatype_introspection.jl

View check run for this annotation

Codecov / codecov/patch

src/committed_datatype_introspection.jl#L130

Added line #L130 was not covered by tests
f::JLDFile,
ptr::Ptr) where T
mypath = String(jlconvert(ReadRepresentation{UInt8,Vlen{UInt8}}(), f, ptr, NULL_REFERENCE))
Expand All @@ -143,7 +143,7 @@ function jlconvert_string(rr::ReadRepresentation{T,DataTypeODR()},
end
end

function jlconvert_string(::ReadRepresentation{Union, UnionTypeODR()}, f::JLDFile,
function jlconvert_string(::ReadRepresentation{Union, UnionTypeODR}, f::JLDFile,

Check warning on line 146 in src/committed_datatype_introspection.jl

View check run for this annotation

Codecov / codecov/patch

src/committed_datatype_introspection.jl#L146

Added line #L146 was not covered by tests
ptr::Ptr)#, header_offset::RelOffset)
# Skip union type description in the beginning
ptr += odr_sizeof(Vlen{String})
Expand All @@ -154,7 +154,7 @@ function jlconvert_string(::ReadRepresentation{Union, UnionTypeODR()}, f::JLDFil
"Union{"*join(vcat(datatypes, unionalls), ",")*"}"
end

function jlconvert_string(rr::ReadRepresentation,
function jlconvert_string(rr::AbstractReadRepr,

Check warning on line 157 in src/committed_datatype_introspection.jl

View check run for this annotation

Codecov / codecov/patch

src/committed_datatype_introspection.jl#L157

Added line #L157 was not covered by tests
f::JLDFile,
ptr::Ptr)
# so apparently this is a custom struct with plain fields that wants to be loaded
Expand Down
2 changes: 1 addition & 1 deletion src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function decompress!(io::IO, data_length, element_size, n, decompressor)
end

function read_compressed_array!(v::Array{T}, f::JLDFile,
rr::ReadRepresentation{T,RR},
rr::AbstractReadRepr{T,RR},
data_length::Integer,
filters,
) where {T,RR}
Expand Down
4 changes: 2 additions & 2 deletions src/data/custom_serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}},
track_weakref!(f, header_offset, obj)

# actually load the data
v = rconvert(T, jlconvert(ReadRepresentation{S,ODR}(), f, ptr, header_offset))::T
v = rconvert(T, jlconvert(readrepr(S,ODR)(), f, ptr, header_offset))::T
# copy fields to initial struct
for i in 0:nfields(obj)-1
fieldval = ccall(:jl_get_nth_field, Any, (Any, Csize_t), v, i)
ccall(:jl_set_nth_field, Nothing, (Any, Csize_t, Any), obj, i, fieldval)
end
return obj
else
v = rconvert(T, jlconvert(ReadRepresentation{S,ODR}(), f, ptr, header_offset))::T
v = rconvert(T, jlconvert(readrepr(S,ODR)(), f, ptr, header_offset))::T
track_weakref!(f, header_offset, v)
return v
end
Expand Down
20 changes: 10 additions & 10 deletions src/data/number_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ function jltype(f::JLDFile, dt::FixedPointDatatype)
throw(UnsupportedFeatureException())
if endianness == 0
if dt.size == 8
return signed ? ReadRepresentation{Int64,Int64}() : ReadRepresentation{UInt64,UInt64}()
return signed ? SameLayoutRepr{Int64}() : SameLayoutRepr{UInt64}()
elseif dt.size == 1
return signed ? ReadRepresentation{Int8,Int8}() : ReadRepresentation{UInt8,UInt8}()
return signed ? SameLayoutRepr{Int8}() : SameLayoutRepr{UInt8}()
elseif dt.size == 4
return signed ? ReadRepresentation{Int32,Int32}() : ReadRepresentation{UInt32,UInt32}()
return signed ? SameLayoutRepr{Int32}() : SameLayoutRepr{Int32}()
elseif dt.size == 2
return signed ? ReadRepresentation{Int16,Int16}() : ReadRepresentation{UInt16,UInt16}()
return signed ? SameLayoutRepr{Int16}() : SameLayoutRepr{UInt16}()
elseif dt.size == 16
return signed ? ReadRepresentation{Int128,Int128}() : ReadRepresentation{UInt128,UInt128}()
return signed ? SameLayoutRepr{Int128}() : SameLayoutRepr{UInt128}()
else
throw(UnsupportedFeatureException())
end
Expand All @@ -64,7 +64,7 @@ end

# Special handling for booleans as they are not considered <: Integer in HDF5
h5fieldtype(::JLDFile, ::Type{Bool}, ::Type{Bool}, ::Initialized) =BitFieldDatatype(1)
jltype(::JLDFile, ::BitFieldDatatype) = ReadRepresentation{Bool, Bool}()
jltype(::JLDFile, ::BitFieldDatatype) = readrepr(Bool, Bool)()

h5fieldtype(::JLDFile, ::Type{Float16}, ::Type{Float16}, ::Initialized) =
FloatingPointDatatype(UInt8(DT_FLOATING_POINT) + 0x3<<4, 0x20, 0x0f, 0x00, 2, 0, 16, 10, 5, 0, 10, 0x0000000f)
Expand All @@ -82,11 +82,11 @@ h5fieldtype(::JLDFile, ::Type{BENumber{Float64}}, ::Type{Float64}, ::Initialized

function jltype(f::JLDFile, dt::FloatingPointDatatype)
if dt == h5fieldtype(f, Float64, Float64, Val{true})
return ReadRepresentation{Float64,Float64}()
return SameLayoutRepr{Float64}()
elseif dt == h5fieldtype(f, Float32, Float32, Val{true})
return ReadRepresentation{Float32,Float32}()
return SameLayoutRepr{Float32}()
elseif dt == h5fieldtype(f, Float16, Float16, Val{true})
return ReadRepresentation{Float16,Float16}()
return SameLayoutRepr{Float16}()
elseif dt == h5fieldtype(f, BENumber{Float64}, Float64, Val{true})
return ReadRepresentation{Float32,BENumber{Float32}}()
elseif dt == h5fieldtype(f, BENumber{Float32}, Float32, Val{true})
Expand All @@ -108,5 +108,5 @@ h5type(f::JLDFile, writeas::PrimitiveTypeTypes, x) =
# Used only for custom serialization
constructrr(f::JLDFile, T::PrimitiveTypeTypes, dt::Union{FixedPointDatatype,FloatingPointDatatype},
::Vector{ReadAttribute}) =
dt == h5fieldtype(f, T, T, Val{true}) ? (ReadRepresentation{T,T}(), true) :
dt == h5fieldtype(f, T, T, Val{true}) ? (SameLayoutRepr{T}(), true) :
throw(UnsupportedFeatureException())
56 changes: 29 additions & 27 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ end
# H5Datatype. We handle committed datatypes here, and other datatypes below.
function jltype(f::JLDFile, sdt::Union{SharedDatatype,CommittedDatatype})
cdt = get(f.datatype_locations, sdt.header_offset, sdt)
haskey(f.h5jltype, cdt) && return f.h5jltype[cdt]::ReadRepresentation
haskey(f.h5jltype, cdt) && return f.h5jltype[cdt]::AbstractReadRepr

dt, attrs = read_shared_datatype(f, cdt)

Expand All @@ -93,7 +93,7 @@ function jltype(f::JLDFile, sdt::Union{SharedDatatype,CommittedDatatype})
end
f.jlh5type[DataType] = cdt
f.datatypes[cdt.index] = dt
return (f.h5jltype[cdt] = ReadRepresentation{DataType, DataTypeODR()}())
return (f.h5jltype[cdt] = ReadRepresentation{DataType, DataTypeODR}())
end

f.plain && return f.h5jltype[cdt] = jltype(f, dt)
Expand All @@ -111,7 +111,7 @@ function jltype(f::JLDFile, sdt::Union{SharedDatatype,CommittedDatatype})
canonical = false
else
rr, canonical = constructrr(f, custom_datatype, dt, attrs)
rr = ReadRepresentation{read_as, CustomSerialization{typeof(rr).parameters...}}()
rr = ReadRepresentation{read_as, CustomSerialization{eltype(rr), readodr(rr)}}()
canonical &= writeas(read_as) === custom_datatype
end
else
Expand All @@ -129,7 +129,7 @@ end
function constructrr(::JLDFile, T::DataType, dt::BasicDatatype, attrs::Vector{ReadAttribute})
dt.class == DT_OPAQUE || throw(UnsupportedFeatureException())
if sizeof(T) == dt.size && isempty(T.types)
return (ReadRepresentation{T,T}(), true)
return (SameLayoutRepr{T}(), true)
end
empty = any(a->a.name==:empty, attrs)
if empty
Expand All @@ -146,7 +146,7 @@ end
struct TypeMappingException <: Exception end


unpack_odr(::OnDiskRepresentation{Offsets,JLTypes,H5Types}) where {Offsets,JLTypes,H5Types} =
unpack_odr(::Type{<:OnDiskRepresentation{Offsets,JLTypes,H5Types}}) where {Offsets,JLTypes,H5Types} =
(Offsets, JLTypes.parameters, H5Types.parameters)


Expand Down Expand Up @@ -201,7 +201,8 @@ will have a matching memory layout without first inspecting the memory layout.
dtrr = jltype(f, dt.members[dtindex])
end

readtype, odrtype = typeof(dtrr).parameters
readtype = eltype(dtrr)
odrtype = readodr(dtrr)

if typeintersect(readtype, wstype) === Union{} &&
!hasmethod(convert, Tuple{Type{wstype}, readtype})
Expand All @@ -226,7 +227,7 @@ will have a matching memory layout without first inspecting the memory layout.
types[i] === wstype &&
# An OnDiskRepresentation as odr means that something "fixable" went wrong
# for this field
!(odrs[i] isa OnDiskRepresentation) &&
!(odrs[i] <: OnDiskRepresentation) &&
!(odrs[i] <: CustomSerialization)
mapped[dtindex] = true
end
Expand All @@ -239,9 +240,9 @@ will have a matching memory layout without first inspecting the memory layout.
"\n\nData in these fields will not be accessible")
end

samelayout && return (ReadRepresentation{T,T}(), true)
samelayout && return (SameLayoutRepr{T}(), true)
offsets = (offsets...,)
if (wodr = odr(T)) isa OnDiskRepresentation
if (wodr = odr(T)) <: OnDiskRepresentation
odr_offsets, odr_types, odr_h5types = unpack_odr(wodr)
tequal = length(odr_types) == length(types)
for i = 1:length(types)
Expand All @@ -252,7 +253,7 @@ will have a matching memory layout without first inspecting the memory layout.
tequal &= odr_offsets == offsets
tequal && return (ReadRepresentation{T,wodr}(), true)
end
return (ReadRepresentation{T,OnDiskRepresentation{offsets, Tuple{types...}, Tuple{odrs...}, Int(offsets[end]+odr_sizeof(odrs[end]))}()}(), false)
return (ReadRepresentation{T,OnDiskRepresentation{offsets, Tuple{types...}, Tuple{odrs...}, Int(offsets[end]+odr_sizeof(odrs[end]))}}(), false)
end

function constructrr(f::JLDFile, u::Upgrade, dt::CompoundDatatype,
Expand All @@ -261,7 +262,7 @@ function constructrr(f::JLDFile, u::Upgrade, dt::CompoundDatatype,
field_datatypes = read_field_datatypes(f, dt, attrs)
rodr = reconstruct_odr(f, dt, field_datatypes)
fnames = tuple((Symbol(k) for k in keys(field_datatypes))...)
T2 = NamedTuple{fnames, typeof(rodr).parameters[2]}
T2 = NamedTuple{fnames, rodr.parameters[2]}
return (ReadRepresentation{u.target, CustomSerialization{T2, rodr}}(), false)
end

Expand All @@ -280,7 +281,7 @@ end

# Find types in modules
# returns the result of searching for the type in the specified module m
function _resolve_type_singlemodule(::ReadRepresentation{T,DataTypeODR()},
function _resolve_type_singlemodule(::ReadRepresentation{T,DataTypeODR},
m,
parts,
mypath,
Expand All @@ -299,7 +300,7 @@ isunknowntype(x) = false
isunknowntype(::Type{Union{}}) = false
isunknowntype(x::Type) = x <: UnknownType ? true : false

function _resolve_type(rr::ReadRepresentation{T,DataTypeODR()},
function _resolve_type(rr::ReadRepresentation{T,DataTypeODR},
f::JLDFile,
ptr::Ptr,
header_offset::RelOffset,
Expand Down Expand Up @@ -344,10 +345,10 @@ end

# Read a type. Returns an instance of UnknownType if the type or parameters
# could not be resolved.
function jlconvert(rr::ReadRepresentation{T,DataTypeODR()},
function jlconvert(rr::ReadRepresentation{<:Type,DataTypeODR},
f::JLDFile,
ptr::Ptr,
header_offset::RelOffset) where T
header_offset::RelOffset)

params, unknown_params = types_from_refs(f, ptr+odr_sizeof(Vlen{UInt8}))
# For cross-platform compatibility convert integer type parameters to system precision
Expand Down Expand Up @@ -390,7 +391,7 @@ function jlconvert(rr::ReadRepresentation{T,DataTypeODR()},
end

constructrr(::JLDFile, ::Type{T}, dt::CompoundDatatype, ::Vector{ReadAttribute}) where {T<:DataType} =
dt == H5TYPE_DATATYPE ? (ReadRepresentation{DataType,DataTypeODR()}(), true) :
dt == H5TYPE_DATATYPE ? (ReadRepresentation{DataType,DataTypeODR}(), true) :
throw(UnsupportedFeatureException())


Expand Down Expand Up @@ -451,7 +452,7 @@ function reconstruct_bitstype(name::Union{Symbol,String}, size::Integer, empty::
return (ReadRepresentation{ReconstructedSingleton{Symbol(name)}, nothing}(), false)
else
T = ReconstructedPrimitive{Symbol(name), uintofsize(size)}
return (ReadRepresentation{T, T}(), false)
return (SameLayoutRepr{T}(), false)

Check warning on line 455 in src/data/reconstructing_datatypes.jl

View check run for this annotation

Codecov / codecov/patch

src/data/reconstructing_datatypes.jl#L455

Added line #L455 was not covered by tests
end
end

Expand Down Expand Up @@ -601,12 +602,12 @@ function reconstruct_odr(f::JLDFile, dt::CompoundDatatype,
else
throw(InternalError("Field $k not found in datatype"))
end
push!(types, typeof(dtrr).parameters[1])
push!(h5types, typeof(dtrr).parameters[2])
push!(types, eltype(dtrr))
push!(h5types, readodr(dtrr))
push!(offsets, offset)
offset += odr_sizeof(dtrr)
end
OnDiskRepresentation{(offsets...,), Tuple{types...}, Tuple{h5types...},Int(dt.size)}()
OnDiskRepresentation{(offsets...,), Tuple{types...}, Tuple{h5types...},Int(dt.size)}
end

# Reconstruct type that is a "lost cause": either we were not able to resolve
Expand All @@ -619,7 +620,7 @@ function reconstruct_compound(f::JLDFile, T::String, dt::H5Datatype,
fnames = tuple((Symbol(k) for k in keys(field_datatypes))...,)
if !any(jlconvert_canbeuninitialized(ReadRepresentation{types[i], odrs[i]}()) for i = 1:length(types))
rt = ReconstructedStatic{Symbol(T), fnames, Tuple{types...}}
odr = OnDiskRepresentation{(0,), Tuple{NamedTuple{fnames,Tuple{types...}}}, Tuple{rodr}, Int(dt.size)}()
odr = OnDiskRepresentation{(0,), Tuple{NamedTuple{fnames,Tuple{types...}}}, Tuple{rodr}, Int(dt.size)}
return (ReadRepresentation{rt, odr}(), false)
end
T = ReconstructedMutable{Symbol(T), fnames, Tuple{types...}}
Expand All @@ -632,7 +633,7 @@ end
jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
header_offset::RelOffset) = Union{}

function jlconvert(::ReadRepresentation{T, S}, f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T<:ReconstructedMutable, S}
function jlconvert(::ReadRepresentation{T,S}, f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T<:ReconstructedMutable, S<:OnDiskRepresentation}
offsets, types, odrs = unpack_odr(S)
res = Vector{Any}(undef, length(types))
for i = 1:length(types)
Expand All @@ -644,11 +645,12 @@ function jlconvert(::ReadRepresentation{T, S}, f::JLDFile, ptr::Ptr, header_offs
return T(res)
end

jlconvert(::ReadRepresentation{T, S}, f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S} =
rconvert(T, jlunsafe_load(pconvert(Ptr{S}, ptr)))

# This jlconvert method handles compound types with padding or references
@generated function jlconvert(::ReadRepresentation{T,S}, f::JLDFile, ptr::Ptr,
header_offset::RelOffset) where {T,S}
isa(S, DataType) && return :(convert(T, jlunsafe_load(pconvert(Ptr{S}, ptr))))
@assert isa(S, OnDiskRepresentation)
header_offset::RelOffset) where {T,S<:OnDiskRepresentation}
offsets, types, odrs = unpack_odr(S)
fn = T === Tuple ? [Symbol(i) for i = 1:length(types)] : fieldnames(T)

Expand All @@ -659,7 +661,7 @@ end
end
for i = 1:length(types)
rtype = types[i]
rr = ReadRepresentation{rtype,odrs[i]}()
rr = readrepr(rtype,odrs[i])()
ttype = T.types[i]
if isnothing(odrs[i])
# Type is not stored or single instance
Expand All @@ -685,7 +687,7 @@ end
rtype = types[i]
fsym = Symbol("field_", fn[i])
push!(fsyms, fsym)
rr = ReadRepresentation{rtype,odrs[i]}()
rr = readrepr(rtype,odrs[i])()

if isnothing(odrs[i])
push!(blk.args, :($fsym = $(Expr(:new, rtype))))
Expand Down
4 changes: 2 additions & 2 deletions src/data/specialcased_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ function jltype(f::JLDFile, dt::VariableLengthDatatype)
return ReadRepresentation{String,Vlen{String}}()
else#if dt.bitfield1 & 0x1 == 0x0 # it's a sequence
rr = jltype(f, dt.basetype)
T = typeof(rr).parameters[1]
odr = typeof(rr).parameters[2]
T = eltype(rr)
odr = readodr(rr)
return ReadRepresentation{Vector{T}, Vlen{odr}}()
end
end
Expand Down
Loading

0 comments on commit a698e3f

Please sign in to comment.