Skip to content

Commit 2502f27

Browse files
committedJul 21, 2015
replace getfield calls with GlobalRef in more cases. ref #10403
1 parent 0c9d74b commit 2502f27

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed
 

‎base/inference.jl

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function _iisconst(s::Symbol)
8383
end
8484
_iisconst(s::SymbolNode) = _iisconst(s.name)
8585
_iisconst(s::TopNode) = isconst(_topmod(), s.name)
86+
_iisconst(s::GlobalRef) = isconst(s.mod, s.name)
8687
_iisconst(x::Expr) = false
8788
_iisconst(x::ANY) = true
8889

‎src/ast.c

+27-6
Original file line numberDiff line numberDiff line change
@@ -985,25 +985,31 @@ int jl_in_vinfo_array(jl_array_t *a, jl_sym_t *v)
985985
return 0;
986986
}
987987

988-
static int in_sym_array(jl_array_t *a, jl_value_t *v)
988+
int jl_in_sym_array(jl_array_t *a, jl_sym_t *v)
989989
{
990990
size_t i, l=jl_array_len(a);
991991
for(i=0; i<l; i++) {
992-
if (jl_cellref(a,i) == v)
992+
if (jl_cellref(a,i) == (jl_value_t*)v)
993993
return 1;
994994
}
995995
return 0;
996996
}
997997

998+
int jl_local_in_ast(jl_expr_t *ast, jl_sym_t *sym)
999+
{
1000+
return jl_in_vinfo_array(jl_lam_vinfo(ast), sym) ||
1001+
jl_in_vinfo_array(jl_lam_capt(ast), sym) ||
1002+
jl_in_sym_array(jl_lam_staticparams(ast), sym);
1003+
}
1004+
1005+
JL_CALLABLE(jl_f_get_field);
1006+
9981007
static jl_value_t *resolve_globals(jl_value_t *expr, jl_lambda_info_t *lam)
9991008
{
10001009
if (jl_is_symbol(expr)) {
10011010
if (lam->module == NULL)
10021011
return expr;
1003-
int is_local = jl_in_vinfo_array(jl_lam_vinfo((jl_expr_t*)lam->ast), (jl_sym_t*)expr) ||
1004-
jl_in_vinfo_array(jl_lam_capt((jl_expr_t*)lam->ast), (jl_sym_t*)expr) ||
1005-
in_sym_array(jl_lam_staticparams((jl_expr_t*)lam->ast), expr);
1006-
if (!is_local)
1012+
if (!jl_local_in_ast((jl_expr_t*)lam->ast, (jl_sym_t*)expr))
10071013
return jl_module_globalref(lam->module, (jl_sym_t*)expr);
10081014
}
10091015
else if (jl_is_lambda_info(expr)) {
@@ -1020,6 +1026,21 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_lambda_info_t *lam)
10201026
e->head == line_sym || e->head == meta_sym) {
10211027
}
10221028
else {
1029+
if (e->head == call_sym && jl_expr_nargs(e) == 3 && jl_is_quotenode(jl_exprarg(e,2)) &&
1030+
lam->module != NULL) {
1031+
// replace getfield(module_expr, :sym) with GlobalRef
1032+
jl_value_t *s = jl_fieldref(jl_exprarg(e,2),0);
1033+
if (jl_is_symbol(s)) {
1034+
jl_value_t *f = jl_static_eval(jl_exprarg(e,0), NULL, lam->module,
1035+
NULL, (jl_expr_t*)lam->ast, 0, 0);
1036+
if (f && jl_is_func(f) && ((jl_function_t*)f)->fptr == &jl_f_get_field) {
1037+
jl_value_t *m = jl_static_eval(jl_exprarg(e,1), NULL, lam->module,
1038+
NULL, (jl_expr_t*)lam->ast, 0, 0);
1039+
if (m && jl_is_module(m))
1040+
return jl_module_globalref((jl_module_t*)m, (jl_sym_t*)s);
1041+
}
1042+
}
1043+
}
10231044
size_t i = 0;
10241045
if (e->head == method_sym || e->head == abstracttype_sym || e->head == compositetype_sym ||
10251046
e->head == bitstype_sym || e->head == macro_sym || e->head == module_sym)

‎src/codegen.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -1256,16 +1256,6 @@ extern "C" void jl_write_malloc_log(void)
12561256

12571257
// --- constant determination ---
12581258

1259-
static bool in_vinfo(jl_sym_t *s, jl_array_t *vi)
1260-
{
1261-
size_t i, l = jl_array_len(vi);
1262-
for(i=0; i < l; i++) {
1263-
if (s == (jl_sym_t*)jl_cellref(jl_cellref(vi, i), 0))
1264-
return true;
1265-
}
1266-
return false;
1267-
}
1268-
12691259
// try to statically evaluate, NULL if not possible
12701260
extern "C"
12711261
jl_value_t *jl_static_eval(jl_value_t *ex, void *ctx_, jl_module_t *mod,
@@ -1281,7 +1271,7 @@ jl_value_t *jl_static_eval(jl_value_t *ex, void *ctx_, jl_module_t *mod,
12811271
isglob = is_global(sym, ctx);
12821272
}
12831273
else if (ast) {
1284-
isglob = !in_vinfo(sym, jl_lam_vinfo(ast)) && !in_vinfo(sym, jl_lam_capt(ast));
1274+
isglob = !jl_local_in_ast(ast, sym);
12851275
}
12861276
if (isglob) {
12871277
size_t i;

‎src/gf.c

-2
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,6 @@ jl_function_t *jl_get_specialization(jl_function_t *f, jl_tupletype_t *types)
14541454

14551455
void jl_trampoline_compile_function(jl_function_t *f, int always_infer, jl_tupletype_t *sig);
14561456

1457-
int jl_in_vinfo_array(jl_array_t *a, jl_sym_t *v);
1458-
14591457
static void parameters_to_closureenv(jl_value_t *ast, jl_svec_t *tvars)
14601458
{
14611459
jl_array_t *closed = jl_lam_capt((jl_expr_t*)ast);

‎src/julia.h

+2
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,8 @@ jl_array_t *jl_lam_staticparams(jl_expr_t *l);
12251225
jl_sym_t *jl_lam_argname(jl_lambda_info_t *li, int i);
12261226
int jl_lam_vars_captured(jl_expr_t *ast);
12271227
jl_expr_t *jl_lam_body(jl_expr_t *l);
1228+
int jl_in_vinfo_array(jl_array_t *a, jl_sym_t *v);
1229+
int jl_local_in_ast(jl_expr_t *ast, jl_sym_t *sym);
12281230
DLLEXPORT jl_value_t *jl_ast_rettype(jl_lambda_info_t *li, jl_value_t *ast);
12291231
jl_sym_t *jl_decl_var(jl_value_t *ex);
12301232
DLLEXPORT int jl_is_rest_arg(jl_value_t *ex);

5 commit comments

Comments
 (5)

staticfloat commented on Jul 23, 2015

@staticfloat
Member

Is it intended that this change disables the ability to have functions that shadow Base functions in a module? E.g. if Homebrew.jl were to define an info() function that shadows Base.info()? See JuliaPackaging/Homebrew.jl#90 for more.

JeffBezanson commented on Jul 23, 2015

@JeffBezanson
MemberAuthor

No, that was caused by an earlier change (#4345). However I'm considering allowing shadowing using Base (see #12183).

kmsquire commented on Jul 23, 2015

@kmsquire
Member

git bisect showed this commit to be the cause.

JeffBezanson commented on Jul 23, 2015

@JeffBezanson
MemberAuthor

Oh dear. That was not the intention.

JeffBezanson commented on Jul 23, 2015

@JeffBezanson
MemberAuthor

Should be fixed now.

Please sign in to comment.