-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: some type reflection improvements #22350
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -946,6 +946,7 @@ export | |
fieldoffset, | ||
fieldname, | ||
fieldnames, | ||
fieldcount, | ||
isleaftype, | ||
oftype, | ||
promote, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,7 +597,7 @@ function isdefined_tfunc(args...) | |
end | ||
if 1 <= idx <= a1.ninitialized | ||
return Const(true) | ||
elseif idx <= 0 || (idx > nfields(a1) && !isvatuple(a1)) | ||
elseif idx <= 0 || (!isvatuple(a1) && idx > fieldcount(a1)) | ||
return Const(false) | ||
end | ||
end | ||
|
@@ -617,16 +617,18 @@ add_tfunc(Core.sizeof, 1, 1, | |
function (x::ANY) | ||
isa(x, Const) && return _const_sizeof(x.val) | ||
isa(x, Conditional) && return _const_sizeof(Bool) | ||
isType(x) && return _const_sizeof(x.parameters[1]) | ||
isconstType(x) && return _const_sizeof(x.parameters[1]) | ||
x !== DataType && isleaftype(x) && return _const_sizeof(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This case isn't valid unless we define that |
||
return Int | ||
end, 0) | ||
old_nfields(x::ANY) = length((isa(x,DataType) ? x : typeof(x)).types) | ||
add_tfunc(nfields, 1, 1, | ||
function (x::ANY) | ||
isa(x,Const) && return Const(nfields(x.val)) | ||
isa(x,Conditional) && return Const(nfields(Bool)) | ||
isa(x,Const) && return Const(old_nfields(x.val)) | ||
isa(x,Conditional) && return Const(old_nfields(Bool)) | ||
if isType(x) | ||
isleaftype(x.parameters[1]) && return Const(nfields(x.parameters[1])) | ||
# TODO: remove with deprecation in builtins.c for nfields(::Type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a note in deprecated.jl or this will get missed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
isleaftype(x.parameters[1]) && return Const(old_nfields(x.parameters[1])) | ||
elseif isa(x,DataType) && !x.abstract && !(x.name === Tuple.name && isvatuple(x)) && x !== DataType | ||
return Const(length(x.types)) | ||
end | ||
|
@@ -5593,8 +5595,8 @@ function is_allocation(e::ANY, sv::InferenceState) | |
if isa(typ, DataType) && isleaftype(typ) | ||
nf = length(e.args) - 1 | ||
names = fieldnames(typ) | ||
@assert(nf <= nfields(typ)) | ||
if nf < nfields(typ) | ||
@assert(nf <= length(names)) | ||
if nf < length(names) | ||
# some fields were left undef | ||
# we could potentially propagate Bottom | ||
# for pointer fields | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,7 +124,8 @@ julia> fieldname(SparseMatrixCSC, 5) | |
""" | ||
fieldname(t::DataType, i::Integer) = t.name.names[i]::Symbol | ||
fieldname(t::UnionAll, i::Integer) = fieldname(unwrap_unionall(t), i) | ||
fieldname(t::Type{<:Tuple}, i::Integer) = i < 1 || i > nfields(t) ? throw(BoundsError(t, i)) : Int(i) | ||
fieldname(t::Type{<:Tuple}, i::Integer) = | ||
i < 1 || i > fieldcount(t) ? throw(BoundsError(t, i)) : Int(i) | ||
|
||
""" | ||
fieldnames(x::DataType) | ||
|
@@ -139,16 +140,9 @@ julia> fieldnames(Hermitian) | |
:uplo | ||
``` | ||
""" | ||
function fieldnames(v) | ||
t = typeof(v) | ||
if !isa(t,DataType) | ||
throw(ArgumentError("cannot call fieldnames() on a non-composite type")) | ||
end | ||
return fieldnames(t) | ||
end | ||
fieldnames(t::DataType) = Symbol[fieldname(t, n) for n in 1:nfields(t)] | ||
fieldnames(t::DataType) = Symbol[fieldname(t, n) for n in 1:fieldcount(t)] | ||
fieldnames(t::UnionAll) = fieldnames(unwrap_unionall(t)) | ||
fieldnames(t::Type{<:Tuple}) = Int[n for n in 1:nfields(t)] | ||
fieldnames(t::Type{<:Tuple}) = Int[n for n in 1:fieldcount(t)] | ||
|
||
""" | ||
Base.datatype_name(t) -> Symbol | ||
|
@@ -265,7 +259,7 @@ false | |
``` | ||
""" | ||
isimmutable(x::ANY) = (@_pure_meta; (isa(x,Tuple) || !typeof(x).mutable)) | ||
isstructtype(t::DataType) = (@_pure_meta; nfields(t) != 0 || (t.size==0 && !t.abstract)) | ||
isstructtype(t::DataType) = (@_pure_meta; length(t.types) != 0 || (t.size==0 && !t.abstract)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the C definition has this same bug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better definition is probably There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that would certainly help with reviewing how those definitions intersect :) |
||
isstructtype(x) = (@_pure_meta; false) | ||
|
||
""" | ||
|
@@ -371,7 +365,7 @@ The byte offset of field `i` of a type relative to the data start. For example, | |
use it in the following manner to summarize information about a struct: | ||
|
||
```jldoctest | ||
julia> structinfo(T) = [(fieldoffset(T,i), fieldname(T,i), fieldtype(T,i)) for i = 1:nfields(T)]; | ||
julia> structinfo(T) = [(fieldoffset(T,i), fieldname(T,i), fieldtype(T,i)) for i = 1:fieldcount(T)]; | ||
|
||
julia> structinfo(Base.Filesystem.StatStruct) | ||
12-element Array{Tuple{UInt64,Symbol,DataType},1}: | ||
|
@@ -440,6 +434,31 @@ end | |
|
||
type_alignment(x::DataType) = (@_pure_meta; ccall(:jl_get_alignment, Csize_t, (Any,), x)) | ||
|
||
""" | ||
fieldcount(t::Type) | ||
|
||
Get the number of fields that an instance of the given type would have. | ||
An error is thrown if the type is too abstract to determine this. | ||
""" | ||
function fieldcount(t::ANY) | ||
if t isa UnionAll || t isa Union | ||
t = ccall(:jl_argument_datatype, Any, (Any,), t) | ||
if t === nothing | ||
error("type does not have a definite number of fields") | ||
end | ||
t = t::DataType | ||
elseif t == Union{} | ||
return 0 | ||
end | ||
if !(t isa DataType) | ||
throw(TypeError(:fieldcount, "", Type, t)) | ||
end | ||
if t.abstract || (t.name === Tuple.name && isvatuple(t)) | ||
error("type does not have a definite number of fields") | ||
end | ||
return length(t.types) | ||
end | ||
|
||
# return all instances, for types that can be enumerated | ||
|
||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,7 +153,7 @@ end | |
|
||
function show(io::IO, ::MIME"text/plain", opt::JLOptions) | ||
println(io, "JLOptions(") | ||
fields = fieldnames(opt) | ||
fields = fieldnames(JLOptions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reminding me that we have two nearly identical versions of this method... |
||
nfields = length(fields) | ||
for (i, f) in enumerate(fields) | ||
v = getfield(opt, i) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ struct SummarySize | |
chargeall::Any | ||
end | ||
|
||
_nfields(x::ANY) = length(typeof(x).types) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also remove along with deprecation? |
||
|
||
""" | ||
Base.summarysize(obj; exclude=Union{...}, chargeall=Union{...}) -> Int | ||
|
@@ -41,7 +42,7 @@ function summarysize(obj::ANY; | |
val = x[i] | ||
end | ||
else | ||
nf = nfields(x) | ||
nf = _nfields(x) | ||
ft = typeof(x).types | ||
if !isbits(ft[i]) && isdefined(x, i) | ||
val = getfield(x, i) | ||
|
@@ -65,11 +66,11 @@ end | |
@noinline function _summarysize(ss::SummarySize, obj::ANY) | ||
key = pointer_from_objref(obj) | ||
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true) | ||
if nfields(obj) > 0 | ||
if _nfields(obj) > 0 | ||
push!(ss.frontier_x, obj) | ||
push!(ss.frontier_i, 1) | ||
end | ||
if isa(obj, UnionAll) | ||
if isa(obj, UnionAll) || isa(obj, Union) | ||
# black-list of items that don't have a Core.sizeof | ||
return 2 * sizeof(Int) | ||
end | ||
|
@@ -84,7 +85,7 @@ function (ss::SummarySize)(obj::DataType) | |
key = pointer_from_objref(obj) | ||
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true) | ||
size::Int = 7 * Core.sizeof(Int) + 6 * Core.sizeof(Int32) | ||
size += 4 * nfields(obj) + ifelse(Sys.WORD_SIZE == 64, 4, 0) | ||
size += 4 * _nfields(obj) + ifelse(Sys.WORD_SIZE == 64, 4, 0) | ||
size += ss(obj.parameters)::Int | ||
size += ss(obj.types)::Int | ||
return size | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be added to stdlib doc index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.