Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed Sep 10, 2021
1 parent 9a8c427 commit e3e240c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export StaticInt, StaticFloat64, StaticSymbol, True, False, StaticBool, NDIndex
export dynamic, is_static, known, static

@static if VERSION >= v"1.7.0-DEV.421"
using Base: @aggressive_constprop
using Base: @constprop
else
macro aggressive_constprop(ex)
macro constprop(_, ex)
ex
end
end
Expand All @@ -30,7 +30,7 @@ Returns the known value corresponding to a static type `T`. If `T` is not a stat
See also: [`static`](@ref), [`is_static`](@ref)
"""
known
@aggressive_constprop known(x) = known(typeof(x))
@constprop :aggressive known(x) = known(typeof(x))
known(::Type{T}) where {T} = nothing
known(::Type{StaticInt{N}}) where {N} = N::Int
known(::Type{StaticFloat64{N}}) where {N} = N::Float64
Expand Down Expand Up @@ -67,19 +67,19 @@ static(:x)
```
"""
static
@aggressive_constprop static(x::X) where {X} = ifelse(is_static(X), identity, _no_static_type)(x)
@aggressive_constprop static(x::Int) = StaticInt(x)
@aggressive_constprop static(x::Union{Int8,UInt8,Int16,UInt16}) = StaticInt(x % Int)
@constprop :aggressive static(x::X) where {X} = ifelse(is_static(X), identity, _no_static_type)(x)
@constprop :aggressive static(x::Int) = StaticInt(x)
@constprop :aggressive static(x::Union{Int8,UInt8,Int16,UInt16}) = StaticInt(x % Int)
@static if sizeof(Int) == 8
@aggressive_constprop static(x::Union{Int32,UInt32}) = StaticInt(x % Int)
@aggressive_constprop static(x::UInt64) = StaticInt(Int(x))
@constprop :aggressive static(x::Union{Int32,UInt32}) = StaticInt(x % Int)
@constprop :aggressive static(x::UInt64) = StaticInt(Int(x))
else
@aggressive_constprop static(x::UInt32) = StaticInt(Int(x))
@constprop :aggressive static(x::UInt32) = StaticInt(Int(x))
end
@aggressive_constprop static(x::Float64) = StaticFloat64(x)
@aggressive_constprop static(x::Bool) = StaticBool(x)
@aggressive_constprop static(x::Symbol) = StaticSymbol(x)
@aggressive_constprop static(x::Tuple{Vararg{Any}}) = map(static, x)
@constprop :aggressive static(x::Float64) = StaticFloat64(x)
@constprop :aggressive static(x::Bool) = StaticBool(x)
@constprop :aggressive static(x::Symbol) = StaticSymbol(x)
@constprop :aggressive static(x::Tuple{Vararg{Any}}) = map(static, x)
@generated static(::Val{V}) where {V} = static(V)
function _no_static_type(@nospecialize(x))
error("There is no static alternative for type $(typeof(x)).")
Expand All @@ -102,7 +102,7 @@ is_static(@nospecialize(x::Type{<:Val})) = True()
is_static(@nospecialize(x::Type{<:StaticFloat64})) = True()
is_static(x::Type{T}) where {T} = False()

@aggressive_constprop _tuple_static(::Type{T}, i) where {T} = is_static(_get_tuple(T, i))
@constprop :aggressive _tuple_static(::Type{T}, i) where {T} = is_static(_get_tuple(T, i))
function is_static(::Type{T}) where {N,T<:Tuple{Vararg{Any,N}}}
if all(eachop(_tuple_static, nstatic(Val(N)), T))
return True()
Expand All @@ -119,7 +119,7 @@ Returns the "dynamic" or non-static form of `x`.
dynamic(x::X) where {X} = _dynamic(is_static(X), x)
_dynamic(::True, x::X) where {X} = known(X)
_dynamic(::False, x::X) where {X} = x
@aggressive_constprop dynamic(x::Tuple) = map(dynamic, x)
@constprop :aggressive dynamic(x::Tuple) = map(dynamic, x)
dynamic(x::NDIndex) = CartesianIndex(dynamic(Tuple(x)))

end

0 comments on commit e3e240c

Please sign in to comment.