Skip to content
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

call overloading, and make constructors use it #8712

Merged
merged 53 commits into from
Oct 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1f527b7
initial implementation of call overloading: f(x...) gets turned into …
stevengj Aug 12, 2014
4a58fa4
make sure call passes through keywords
stevengj Aug 12, 2014
5b0aedd
fix call overloading for type constructors, add some tests
stevengj Aug 12, 2014
0df037e
support generic call in apply(f, ...)
stevengj Aug 14, 2014
762a727
(broken) patch to eliminate emit_func_check
stevengj Aug 14, 2014
b430c48
get basic call overloading working with 1 bootstrap stage
JeffBezanson Sep 1, 2014
27e476c
Merge branch 'master' into call_overload
JeffBezanson Sep 1, 2014
bd955f2
update emit_call for jl_fptr_llvmt => jl_pfptr_llvmt change
JeffBezanson Sep 1, 2014
a4dcc69
Merge branch 'master' of github.com:JuliaLang/julia into call_overload
JeffBezanson Oct 8, 2014
aeae50f
some work on call overloading
JeffBezanson Oct 10, 2014
06471db
Merge branch 'master' of github.com:JuliaLang/julia into call_overload
JeffBezanson Oct 10, 2014
2463704
fix codegen for changes to apply() for call overloading
JeffBezanson Oct 10, 2014
5b03766
temporarily restore inference/startup performance
JeffBezanson Oct 10, 2014
51a3069
fully redesign constructors to use `call` overloading
JeffBezanson Oct 11, 2014
2604610
fix keyword args in constructors-via-`call`
JeffBezanson Oct 11, 2014
79e006b
carefully arrange `call` methods in Core not to get specialized on ty…
JeffBezanson Oct 11, 2014
2e46b1b
remove unsupported use of chained type parameters
JeffBezanson Oct 12, 2014
1888e85
fix constructor method sort order by preserving typevar bounds in the…
JeffBezanson Oct 12, 2014
b2bfd52
my last trick for constructor+call+keywordargs didn't quite work; fix it
JeffBezanson Oct 12, 2014
208b2cd
fix a memory bug in codegen of getfield on a 0-size struct
JeffBezanson Oct 13, 2014
7068390
type system improvements needed for constructors-via-call
JeffBezanson Oct 13, 2014
d5aebc7
a couple tweaks to type intersection that were helpful when eliminati…
JeffBezanson Oct 14, 2014
c62c8f0
remove convert_default builtin
JeffBezanson Oct 14, 2014
4e0dd59
code cleanup, fix error displaying types, change Callable
JeffBezanson Oct 14, 2014
0f67c69
remove old ASCIIString constructor code from dump.c
JeffBezanson Oct 14, 2014
bec065f
faster Expr constructor (large impact on sysimg build time)
JeffBezanson Oct 14, 2014
2bfa247
handle apply_type in static_eval
JeffBezanson Oct 16, 2014
6a2032e
add DataType back to Callable for now
JeffBezanson Oct 16, 2014
dbc2d0d
Merge branch 'master' of github.com:JuliaLang/julia into jb/call_cons…
JeffBezanson Oct 16, 2014
ab61bd9
fix a missing gc root in codegen
JeffBezanson Oct 16, 2014
95c0ecf
fix 2 ambiguity warnings
JeffBezanson Oct 16, 2014
0d60213
remove no-longer-needed deserialize method for TypeVar
JeffBezanson Oct 17, 2014
a1a6563
allow passing type parameters to `new` with `new{ ... }`
JeffBezanson Oct 17, 2014
9a6b825
Merge branch 'master' of github.com:JuliaLang/julia into jb/call_cons…
JeffBezanson Oct 17, 2014
4d6160a
Merge branch 'master' of github.com:JuliaLang/julia into jb/call_cons…
JeffBezanson Oct 17, 2014
e5cbd8f
fix some incorrect uses of alloc_tuple_uninit (memory bug)
JeffBezanson Oct 17, 2014
4ee2b41
sharpen the result of tmerge() for Tuple types
JeffBezanson Oct 17, 2014
36c5ad6
make `methods` and `functionloc` work with `call` definitions
JeffBezanson Oct 18, 2014
a3d637f
remove error for failing to import a non-function used in a method de…
JeffBezanson Oct 18, 2014
12ecc25
more surgical approach to importing Core.call
JeffBezanson Oct 18, 2014
1a35753
Merge branch 'master' of github.com:JuliaLang/julia into jb/call_cons…
JeffBezanson Oct 19, 2014
d382a46
fix order of initialization in jl_new_datatype (memory bug)
JeffBezanson Oct 19, 2014
5b9ec88
fix a missing GC root in codegen
JeffBezanson Oct 19, 2014
d2dd9f1
fix crash in sortrows
JeffBezanson Oct 20, 2014
4fdf200
slightly cut down lowered code for kwcall
JeffBezanson Oct 20, 2014
6236a24
Merge branch 'master' of github.com:JuliaLang/julia into jb/call_cons…
JeffBezanson Oct 20, 2014
adb53bb
fix a method specificity problem
JeffBezanson Oct 20, 2014
8569cd5
fix uses of `Callable`
JeffBezanson Oct 20, 2014
bcbb53c
update `precompile` for `call` overloading
JeffBezanson Oct 20, 2014
39dfd92
add the long-awaited call=>convert fallback
JeffBezanson Oct 20, 2014
ff88a00
avoid overflowing environment in type intersection when a typevar mee…
JeffBezanson Oct 20, 2014
f0d0c1c
Merge branch 'master' of https://github.com/JuliaLang/julia into jb/c…
JeffBezanson Oct 21, 2014
b73cf12
update serializer changes for call_constructors branch
JeffBezanson Oct 21, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function getindex(T::NonTupleType, vals...)
return a
end

function getindex(::Type{Any}, vals::ANY...)
a = Array(Any,length(vals))
for i = 1:length(vals)
a[i] = vals[i]
end
return a
end

getindex(T::(Type...)) = Array(T,0)

# T[a:b] and T[a:s:b] also construct typed ranges
Expand Down
46 changes: 40 additions & 6 deletions base/base.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
import Core.Array # to add methods

const NonTupleType = Union(DataType,UnionType,TypeConstructor)

typealias Callable Union(Function,DataType)

const Bottom = Union()

convert(T, x) = convert_default(T, x, convert)
# constructors for Core types in boot.jl
call(T::Type{BoundsError}) = Core.call(T)
call(T::Type{DivideError}) = Core.call(T)
call(T::Type{DomainError}) = Core.call(T)
call(T::Type{OverflowError}) = Core.call(T)
call(T::Type{InexactError}) = Core.call(T)
call(T::Type{MemoryError}) = Core.call(T)
call(T::Type{StackOverflowError}) = Core.call(T)
call(T::Type{UndefRefError}) = Core.call(T)
call(T::Type{UndefVarError}, var::Symbol) = Core.call(T, var)
call(T::Type{InterruptException}) = Core.call(T)
call(T::Type{SymbolNode}, name::Symbol, t::ANY) = Core.call(T, name, t)
call(T::Type{GetfieldNode}, value, name::Symbol, typ) = Core.call(T, value, name, typ)
call(T::Type{ASCIIString}, d::Array{Uint8,1}) = Core.call(T, d)
call(T::Type{UTF8String}, d::Array{Uint8,1}) = Core.call(T, d)
call(T::Type{TypeVar}, args...) = Core.call(T, args...)
call(T::Type{TypeConstructor}, args...) = Core.call(T, args...)
call(T::Type{Expr}, args::ANY...) = _expr(args...)
call(T::Type{LineNumberNode}, n::Int) = Core.call(T, n)
call(T::Type{LabelNode}, n::Int) = Core.call(T, n)
call(T::Type{GotoNode}, n::Int) = Core.call(T, n)
call(T::Type{QuoteNode}, x::ANY) = Core.call(T, x)
call(T::Type{NewvarNode}, s::Symbol) = Core.call(T, s)
call(T::Type{TopNode}, s::Symbol) = Core.call(T, s)
call(T::Type{Module}, args...) = Core.call(T, args...)
call(T::Type{Task}, f::ANY) = Core.call(T, f)

call{T}(::Type{T}, args...) = convert(T, args...)::T

convert{T}(::Type{T}, x::T) = x

convert(::(), ::()) = ()
convert(::Type{Tuple}, x::Tuple) = x

# allow convert to be called as if it were a single-argument constructor
# call(T::Type, x) = convert(T, x)

argtail(x, rest...) = rest
tupletail(x::Tuple) = argtail(x...)

convert(T::(Type, Type...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tupletail(T), tupletail(x))...)
convert(T::(Any, Any...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tupletail(T), tupletail(x))...)

Expand Down Expand Up @@ -180,7 +212,11 @@ map(f::Callable, a::Array{Any,1}) = Any[ f(a[i]) for i=1:length(a) ]
macro thunk(ex); :(()->$(esc(ex))); end
macro L_str(s); s; end

function precompile(f, args::Tuple)
function precompile(f::ANY, args::Tuple)
if isa(f,DataType)
args = tuple(Type{f}, args...)
f = f.name.module.call
end
if isgeneric(f)
ccall(:jl_compile_hint, Void, (Any, Any), f, args)
end
Expand All @@ -207,8 +243,6 @@ macro goto(name::Symbol)
Expr(:symbolicgoto, name)
end

# NOTE: Base shares Array with Core so we can add definitions to it

Array{T,N}(::Type{T}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)

Expand Down
44 changes: 38 additions & 6 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ export
Expr, GotoNode, LabelNode, LineNumberNode, QuoteNode, SymbolNode, TopNode,
GetfieldNode, NewvarNode,
# object model functions
apply, fieldtype, getfield, setfield!, yieldto, throw, tuple, is, ===, isdefined,
# arraylen, arrayref, arrayset, arraysize, tuplelen, tupleref, convert_default,
# kwcall,
fieldtype, getfield, setfield!, yieldto, throw, tuple, is, ===, isdefined,
# arraylen, arrayref, arrayset, arraysize, tuplelen, tupleref,
# _apply, kwcall,
# sizeof # not exported, to avoid conflicting with Base.sizeof
# type reflection
issubtype, typeof, isa,
Expand Down Expand Up @@ -217,9 +217,6 @@ type InterruptException <: Exception end
abstract String
abstract DirectIndexString <: String

# simple convert for use by constructors of types in Core
convert(T, x) = convert_default(T, x, convert)

type SymbolNode
name::Symbol
typ
Expand All @@ -243,3 +240,38 @@ end
typealias ByteString Union(ASCIIString,UTF8String)

include(fname::ByteString) = ccall(:jl_load_, Any, (Any,), fname)

# constructors for built-in types

TypeVar(n::Symbol) =
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), Any)::TypeVar
TypeVar(n::Symbol, ub::ANY) =
(isa(ub,Bool) ?
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), Any, ub)::TypeVar :
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), ub::Type)::TypeVar)
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
(isa(ub,Bool) ?
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), lb::Type, ub)::TypeVar :
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, lb::Type, ub::Type)::TypeVar)
TypeVar(n::Symbol, lb::ANY, ub::ANY, b::Bool) =
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, lb::Type, ub::Type, b)::TypeVar

TypeConstructor(p::ANY, t::ANY) = ccall(:jl_new_type_constructor, Any, (Any, Any), p::Tuple, t::Type)

Expr(args::ANY...) = _expr(args...)

LineNumberNode(n::Int) = ccall(:jl_new_struct, Any, (Any,Any...), LineNumberNode, n)::LineNumberNode
LabelNode(n::Int) = ccall(:jl_new_struct, Any, (Any,Any...), LabelNode, n)::LabelNode
GotoNode(n::Int) = ccall(:jl_new_struct, Any, (Any,Any...), GotoNode, n)::GotoNode
QuoteNode(x::ANY) = ccall(:jl_new_struct, Any, (Any,Any...), QuoteNode, x)::QuoteNode
NewvarNode(s::Symbol) = ccall(:jl_new_struct, Any, (Any,Any...), NewvarNode, s)::NewvarNode
TopNode(s::Symbol) = ccall(:jl_new_struct, Any, (Any,Any...), TopNode, s)::TopNode

Module(name::Symbol) = ccall(:jl_f_new_module, Any, (Any,), name)::Module
Module() = Module(:anonymous)

Task(f::ANY) = ccall(:jl_new_task, Any, (Any, Int), f::Function, 0)::Task

# simple convert for use by constructors of types in Core
convert(::Type{Any}, x::ANY) = x
convert{T}(::Type{T}, x::T) = x
4 changes: 2 additions & 2 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ function find_library{T<:ByteString, S<:ByteString}(libnames::Array{T,1}, extrap
return ""
end

function ccallable(f::Callable, rt::Type, argt::(Type...), name::Union(String,Symbol)=string(f))
function ccallable(f::Function, rt::Type, argt::(Type...), name::Union(String,Symbol)=string(f))
ccall(:jl_extern_c, Void, (Any, Any, Any, Ptr{Uint8}), f, rt, argt, name)
end

function ccallable(f::Callable, argt::(Type...), name::Union(String,Symbol)=string(f))
function ccallable(f::Function, argt::(Type...), name::Union(String,Symbol)=string(f))
ccall(:jl_extern_c, Void, (Any, Ptr{Void}, Any, Ptr{Uint8}), f, C_NULL, argt, name)
end

Expand Down
8 changes: 7 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ end

scale!{T<:Base.LinAlg.BlasReal}(X::Array{T}, s::Complex) = error("scale!: Cannot scale a real array by a complex value in-place. Use scale(X::Array{Real}, s::Complex) instead.")

@deprecate which(f::Callable, args...) @which f(args...)
@deprecate which(f, args...) @which f(args...)
@deprecate rmdir rm

# 0.4 deprecations
Expand Down Expand Up @@ -186,6 +186,12 @@ const Nothing = Void
export None
const None = Union()

export apply
function apply(f, args...)
depwarn("apply(f, x) is deprecated, use `f(x...)` instead", :apply)
return Core._apply(call, f, args...)
end

@deprecate median(v::AbstractArray; checknan::Bool=true) median(v)
@deprecate median(v::AbstractArray, region; checknan::Bool=true) median(v, region)
@deprecate median!(v::AbstractVector; checknan::Bool=true) median!(v)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export
At_mul_Bt!,
At_rdiv_B,
At_rdiv_Bt,
call,

# scalar math
@evalpoly,
Expand Down
2 changes: 1 addition & 1 deletion base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function show(io::IO, tv::TypeVar)
show(io, tv.lb)
print(io, "<:")
end
print(io, tv.name)
write(io, tv.name)
if !is(tv.ub, Any)
print(io, "<:")
show(io, tv.ub)
Expand Down
Loading