Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 16, 2017
1 parent 7dcfc2c commit 7ef48b7
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 141 deletions.
2 changes: 1 addition & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using Core: CodeInfo

typealias Callable Union{Function,DataType}
typealias Callable Union{Function,Type}

const Bottom = Union{}

Expand Down
72 changes: 44 additions & 28 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,14 @@ add_tfunc(arraysize, 2, 2, (a::ANY, d::ANY)->Int)
add_tfunc(pointerref, 3, 3,
function (a::ANY, i::ANY, align::ANY)
a = widenconst(a)
if isa(a,DataType) && a<:Ptr
if isa(a.parameters[1],Type)
if a <: Ptr
if isa(a,DataType) && isa(a.parameters[1],Type)
return a.parameters[1]
elseif isa(a.parameters[1],TypeVar)
return a.parameters[1].ub
elseif isa(a,UnionAll) && !has_free_typevars(a)
unw = unwrap_unionall(a)
if isa(unw,DataType)
return rewrap_unionall(unw.parameters[1], a)
end
end
end
return Any
Expand Down Expand Up @@ -581,11 +584,8 @@ function getfield_tfunc(s00::ANY, name)
if isa(sv, Module) && isa(nv, Symbol)
return abstract_eval_global(sv, nv)
end
if isa(sv,DataType) || isimmutable(sv)
try
return abstract_eval_constant(getfield(sv, nv))
catch
end
if (isa(sv,DataType) || isimmutable(sv)) && isdefined(sv, nv)
return abstract_eval_constant(getfield(sv, nv))
end
end
s = typeof(sv)
Expand All @@ -612,6 +612,7 @@ function getfield_tfunc(s00::ANY, name)
if length(s.types) == 1
return rewrap_unionall(unwrapva(s.types[1]), s00)
end
# union together types of all fields
R = reduce(tmerge, Bottom, map(t->rewrap_unionall(unwrapva(t),s00), s.types))
# do the same limiting as the known-symbol case to preserve type-monotonicity
if isempty(s.parameters)
Expand Down Expand Up @@ -723,6 +724,7 @@ function apply_type_tfunc(args...)
elseif isa(ai, Const) && valid_tparam(ai.val)
push!(tparams, ai.val)
else
# TODO: return `Bottom` for trying to apply a non-UnionAll
#if !istuple && i-1 > length(headtype.parameters)
# # too many parameters for type
# return Bottom
Expand All @@ -731,6 +733,7 @@ function apply_type_tfunc(args...)
if istuple
push!(tparams, Any)
else
# TODO: use rewrap_unionall to skip only the unknown parameters
#push!(tparams, headtype.parameters[i-1])
break
end
Expand Down Expand Up @@ -818,12 +821,17 @@ function builtin_tfunction(f::ANY, argtypes::Array{Any,1}, sv::InferenceState)
return Bottom
end
a = widenconst(argtypes[1])
if isa(a,UnionAll)
a = unwrap_unionall(a)
end
if isa(a,DataType) && a<:Array && isa(a.parameters[1],Union{Type,TypeVar})
a = a.parameters[1]
return isa(a,TypeVar) ? a.ub : a
if a <: Array
if isa(a,DataType) && (isa(a.parameters[1],Type) || isa(a.parameters[1],TypeVar))
# TODO: the TypeVar case should not be needed here
a = a.parameters[1]
return isa(a,TypeVar) ? a.ub : a
elseif isa(a,UnionAll) && !has_free_typevars(a)
unw = unwrap_unionall(a)
if isa(unw,DataType)
return rewrap_unionall(unw.parameters[1], a)
end
end
end
return Any
elseif f === Expr
Expand Down Expand Up @@ -873,10 +881,18 @@ function limit_tuple_depth_(params::InferenceParams, t::ANY, d::Int)
if isa(t,Union)
# also limit within Union types.
# may have to recur into other stuff in the future too.
return Union{map(x->limit_tuple_depth_(params,x,d+1), (t.a,t.b))...}
return Union{limit_tuple_depth_(params, t.a, d+1),
limit_tuple_depth_(params, t.b, d+1)}
elseif isa(t,UnionAll)
var = TypeVar(t.var.name, t.var.lb, limit_tuple_depth_(params, t.var.ub, d))
body = limit_tuple_depth_(params, t{var}, d)
ub = limit_tuple_depth_(params, t.var.ub, d)
if ub !== t.var.ub
var = TypeVar(t.var.name, t.var.lb, ub)
body = t{var}
else
var = t.var
body = t.body
end
body = limit_tuple_depth_(params, body, d)
return UnionAll(var, body)
elseif !(isa(t,DataType) && t.name === Tuple.name)
return t
Expand Down Expand Up @@ -1249,16 +1265,16 @@ function abstract_call(f::ANY, fargs, argtypes::Vector{Any}, vtypes::VarTable, s
return Any
elseif f === UnionAll
if length(fargs) == 3 && isa(argtypes[2],Const)
if isType(argtypes[3])
body = argtypes[3].parameters[1]
elseif isa(argtypes[3],Const)
body = argtypes[3].val
else
return Any
end
try
return abstract_eval_constant(UnionAll(argtypes[2].val, body))
catch
tv = argtypes[2].val
if isa(tv,TypeVar)
if isType(argtypes[3])
body = argtypes[3].parameters[1]
elseif isa(argtypes[3],Const)
body = argtypes[3].val
else
return Any
end
return abstract_eval_constant(UnionAll(tv, body))
end
end
return Any
Expand Down
3 changes: 2 additions & 1 deletion base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function argtype_decl(env, n, sig::DataType, i::Int, nargs, isva::Bool) # -> (ar
t = t.body
end
end
tt, tn = t.parameters[1], t.parameters[2]
ut = unwrap_unionall(t)
tt, tn = ut.parameters[1], ut.parameters[2]
if isa(tn, TypeVar) && (tn === v1 || tn === v2)
if tt === Any || (isa(tt, TypeVar) && (tt === v1 || tt === v2))
return string(s, "..."), ""
Expand Down
7 changes: 3 additions & 4 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extern "C" {
JL_DLLEXPORT jl_value_t *jl_true;
JL_DLLEXPORT jl_value_t *jl_false;

jl_tvar_t *jl_typetype_tvar;
jl_unionall_t *jl_typetype_type;
jl_value_t *jl_ANY_flag;

Expand Down Expand Up @@ -675,9 +674,11 @@ jl_method_t *jl_new_method(jl_code_info_t *definition,
}
jl_value_t *root = (jl_value_t*)sparam_syms;
jl_method_t *m = NULL;
JL_GC_PUSH2(&root, &m);
JL_GC_PUSH1(&root);

m = jl_new_method_uninit();
m->sparam_syms = sparam_syms;
root = (jl_value_t*)m;
m->min_world = ++jl_world_counter;
m->isstaged = isstaged;
m->name = name;
Expand All @@ -688,8 +689,6 @@ jl_method_t *jl_new_method(jl_code_info_t *definition,
m->tvars = (jl_svec_t*)jl_svecref(tvars, 0);
else
m->tvars = tvars;
m->sparam_syms = sparam_syms;
root = (jl_value_t*)m;
jl_method_set_source(m, definition);
if (isstaged) {
// create and store generator for generated functions
Expand Down
36 changes: 26 additions & 10 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,29 @@ static int type_in_worklist(jl_datatype_t *dt)
return 0;
}

static int type_recursively_external(jl_datatype_t *dt);

static int type_parameter_recursively_external(jl_value_t *p0)
{
jl_datatype_t *p = (jl_datatype_t*)p0;
while (jl_is_unionall(p)) {
if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->lb))
return 0;
if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->ub))
return 0;
p = (jl_datatype_t*)((jl_unionall_t*)p)->body;
}
if (!jl_is_datatype(p) || p->uid == 0)
return 0;
if (module_in_worklist(p->name->module))
return 0;
if (p->name->wrapper != (jl_value_t*)p0) {
if (!type_recursively_external(p))
return 0;
}
return 1;
}

// returns true if all of the parameters are tag 6 or 7
static int type_recursively_external(jl_datatype_t *dt)
{
Expand All @@ -499,16 +522,8 @@ static int type_recursively_external(jl_datatype_t *dt)

int i, l = jl_svec_len(dt->parameters);
for (i = 0; i < l; i++) {
jl_value_t *p0 = jl_tparam(dt, i);
jl_datatype_t *p = (jl_datatype_t*)jl_unwrap_unionall(p0);
if (!jl_is_datatype(p))
return 0;
if (module_in_worklist(p->name->module))
if (!type_parameter_recursively_external(jl_tparam(dt, i)))
return 0;
if (p->name->wrapper != p0) {
if (!type_recursively_external(p))
return 0;
}
}
return 1;
}
Expand Down Expand Up @@ -2863,6 +2878,7 @@ jl_method_t *jl_recache_method(jl_method_t *m, size_t start)
jl_method_instance_t *jl_recache_method_instance(jl_method_instance_t *li, size_t start)
{
jl_datatype_t *sig = (jl_datatype_t*)li->def;
assert(jl_is_datatype(sig) || jl_is_unionall(sig));
jl_datatype_t *ftype = jl_first_argument_datatype((jl_value_t*)sig);
jl_methtable_t *mt = ftype->name->mt;
jl_method_t *m = (jl_method_t*)jl_methtable_lookup(mt, sig, /*TODO*/jl_world_counter);
Expand Down Expand Up @@ -3070,7 +3086,7 @@ void jl_init_serializer(void)
jl_pointer_type, jl_vararg_type, jl_abstractarray_type, jl_void_type,
jl_densearray_type, jl_function_type, jl_unionall_type, jl_typename_type,
jl_builtin_type, jl_task_type, jl_uniontype_type, jl_typetype_type,
jl_typetype_tvar, jl_ANY_flag, jl_array_any_type, jl_intrinsic_type,
jl_ANY_flag, jl_array_any_type, jl_intrinsic_type,
jl_abstractslot_type, jl_methtable_type, jl_typemap_level_type,
jl_voidpointer_type, jl_newvarnode_type, jl_abstractstring_type,
jl_array_symbol_type, jl_anytuple_type, jl_tparam0(jl_anytuple_type),
Expand Down
Loading

0 comments on commit 7ef48b7

Please sign in to comment.