Skip to content

Commit

Permalink
some more fieldname refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed Feb 26, 2015
1 parent c1ba6a5 commit aa8ee40
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
7 changes: 4 additions & 3 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ function deepcopy_internal(x, stackdict::ObjectIdDict)
end

function _deepcopy_t(x, T::DataType, stackdict::ObjectIdDict)
isbits(T) | isempty(T.names) && return x
nf = nfields(T)
(isbits(T) || nf == 0) && return x
if T.mutable
y = ccall(:jl_new_struct_uninit, Any, (Any,), T)
stackdict[x] = y
for i in 1:length(T.names)
for i in 1:nf
if isdefined(x,i)
y.(i) = deepcopy_internal(x.(i), stackdict)
end
end
else
fields = Any[deepcopy_internal(x.(i), stackdict) for i in 1:length(T.names)]
fields = Any[deepcopy_internal(x.(i), stackdict) for i in 1:nf]
y = ccall(:jl_new_structv, Any, (Any, Ptr{Void}, UInt32),
T, pointer(fields), length(fields))
end
Expand Down
4 changes: 2 additions & 2 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const getfield_tfunc = function (A, s0, name)
end
end
end
for i=1:length(s.names)
for i=1:nfields(s)
if is(s.names[i],fld)
R = s.types[i]
if s.parameters === ()
Expand All @@ -360,7 +360,7 @@ const getfield_tfunc = function (A, s0, name)
return Bottom
end
i::Int = A[2]
if i < 1 || i > length(s.names)
if i < 1 || i > nfields(s)
return Bottom
end
return s.types[i]
Expand Down
17 changes: 14 additions & 3 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ end
fieldnames(m::Module, all::Bool, imported::Bool) = ccall(:jl_module_fieldnames, Array{Symbol,1}, (Any,Int32,Int32), m, all, imported)
fieldnames(m::Module, all::Bool) = fieldnames(m, all, false)
fieldnames(m::Module) = fieldnames(m, false, false)
fieldnames(t::DataType) = collect(t.names)

fieldnames(t::DataType) = collect(t.names)
function fieldnames(v)
t = typeof(v)
if !isa(t,DataType)
Expand All @@ -37,6 +37,17 @@ function fieldnames(v)
return fieldnames(t)
end

fieldname(t::DataType, i::Integer) = t.names[i]

nfields(t::DataType) = length(t.names)
function nfields(v)
t = typeof(v)
if !isa(DataType)
throw(ArgumentError("cannot call nfields() on a non-composite type"))
end
return nfields(t)
end

isconst(s::Symbol) =
ccall(:jl_is_const, Int32, (Ptr{Void}, Any), C_NULL, s) != 0

Expand All @@ -48,7 +59,7 @@ object_id(x::ANY) = ccall(:jl_object_id, UInt, (Any,), x)

# type predicates
isimmutable(x::ANY) = (isa(x,Tuple) || !typeof(x).mutable)
isstructtype(t::DataType) = t.names!=() || (t.size==0 && !t.abstract)
isstructtype(t::DataType) = nfields(t) != 0 || (t.size==0 && !t.abstract)
isstructtype(x) = false
isbits(t::DataType) = !t.mutable & t.pointerfree & isleaftype(t)
isbits(t::Type) = false
Expand All @@ -59,7 +70,7 @@ typeintersect(a::ANY,b::ANY) = ccall(:jl_type_intersection, Any, (Any,Any), a, b
typeseq(a::ANY,b::ANY) = a<:b && b<:a

function fieldoffsets(x::DataType)
offsets = Array(Int, length(x.names))
offsets = Array(Int, nfields(x))
ccall(:jl_field_offsets, Void, (Any, Ptr{Int}), x, offsets)
offsets
end
Expand Down
11 changes: 6 additions & 5 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ function serialize(s, x)
return write_as_tag(s, x)
end
t = typeof(x)
nf = nfields(t)
serialize_type(s, t)
if length(t.names)==0 && t.size>0
if nf == 0 && t.size > 0
write(s, x)
else
for i in 1:length(t.names)
for i in 1:nf
if isdefined(x, i)
serialize(s, getfield(x, i))
else
Expand Down Expand Up @@ -527,11 +528,11 @@ end

# default DataType deserializer
function deserialize(s, t::DataType)
if length(t.names)==0 && t.size>0
nf = nfields(t)
if nf == 0 && t.size > 0
# bits type
return read(s, t)
end
nf = length(t.names)
if nf == 0
return ccall(:jl_new_struct, Any, (Any,Any...), t)
elseif isbits(t)
Expand All @@ -552,7 +553,7 @@ function deserialize(s, t::DataType)
end
else
x = ccall(:jl_new_struct_uninit, Any, (Any,), t)
for i in 1:length(t.names)
for i in 1:nf
tag = int32(read(s, UInt8))
if tag==0 || !is(deser_tag[tag], UndefRefTag)
ccall(:jl_set_nth_field, Void, (Any, Csize_t, Any), x, i-1, handle_deserialize(s, tag))
Expand Down
13 changes: 7 additions & 6 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function show(io::IO, x::ANY)
t = typeof(x)::DataType
show(io, t)
print(io, '(')
if t.names !== () || t.size==0
nf = nfields(t)
if nf != 0 || t.size==0
recorded = false
oid = object_id(x)
shown_set = get(task_local_storage(), :SHOWNSET, nothing)
Expand All @@ -22,15 +23,15 @@ function show(io::IO, x::ANY)
push!(shown_set, oid)
recorded = true

n = length(t.names)
for i=1:n
f = t.names[i]
nf = nfields(t)
for i=1:nf
f = fieldname(t, i)
if !isdefined(x, f)
print(io, undef_ref_str)
else
show(io, x.(f))
end
if i < n
if i < nf
print(io, ',')
end
end
Expand Down Expand Up @@ -735,7 +736,7 @@ end
function xdump(fn::Function, io::IO, x, n::Int, indent)
T = typeof(x)
print(io, T, " ")
if isa(T, DataType) && length(T.names) > 0
if isa(T, DataType) && nfields(T) > 0
println(io)
if n > 0
for field in fieldnames(T)
Expand Down

0 comments on commit aa8ee40

Please sign in to comment.