diff --git a/src/ast.c b/src/ast.c index b6d88ab62dcfe..cf675efe95de3 100644 --- a/src/ast.c +++ b/src/ast.c @@ -745,7 +745,7 @@ static value_t julia_to_scm_(fl_context_t *fl_ctx, jl_value_t *v, int check_vali // GC Note: jl_fieldref(v, 0) allocates for GotoNode // but we don't need a GC root here because julia_to_list2_noalloc // shouldn't allocate in this case. - if (jl_typetagis(v, jl_linenumbernode_type)) { + if (jl_is_linenode(v)) { jl_value_t *file = jl_fieldref_noalloc(v,1); jl_value_t *line = jl_fieldref(v,0); value_t args = julia_to_list2_noalloc(fl_ctx, line, file, check_valid); @@ -834,7 +834,7 @@ JL_DLLEXPORT jl_value_t *jl_fl_parse(const char *text, size_t text_len, } // returns either an expression or a thunk -jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule) +static jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule) { jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule); fl_context_t *fl_ctx = &ctx->fl; @@ -847,8 +847,8 @@ jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module return result; } -static jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr, - jl_module_t *inmodule, const char *file, int line) +jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr, + jl_module_t *inmodule, const char *file, int line) { jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule); fl_context_t *fl_ctx = &ctx->fl; @@ -1011,7 +1011,7 @@ static jl_value_t *jl_invoke_julia_macro(jl_array_t *args, jl_module_t *inmodule // __source__ argument jl_value_t *lno = jl_array_ptr_ref(args, 1); margs[1] = lno; - if (!jl_typetagis(lno, jl_linenumbernode_type)) { + if (!jl_is_linenode(lno)) { margs[1] = jl_new_struct(jl_linenumbernode_type, jl_box_long(0), jl_nothing); } margs[2] = (jl_value_t*)inmodule; diff --git a/src/jlfrontend.scm b/src/jlfrontend.scm index f72c79f281480..aefac6d102aea 100644 --- a/src/jlfrontend.scm +++ b/src/jlfrontend.scm @@ -179,14 +179,9 @@ ;; construct default definitions of `eval` for non-bare modules ;; called by jl_eval_module_expr -(define (module-default-defs e) +(define (module-default-defs name file line) (jl-expand-to-thunk - (let* ((name (caddr e)) - (body (cadddr e)) - (loc (if (null? (cdr body)) () (cadr body))) - (loc (if (and (pair? loc) (eq? (car loc) 'line)) - (list loc) - '())) + (let* ((loc (if (and (eq? file 'none) (eq? line 0)) '() `((line ,line ,file)))) (x (if (eq? name 'x) 'y 'x)) (mex (if (eq? name 'mapexpr) 'map_expr 'mapexpr))) `(block @@ -202,7 +197,7 @@ (block ,@loc (call (core _call_latest) (top include) ,mex ,name ,x))))) - 'none 0)) + file line)) ; run whole frontend on a string. useful for testing. (define (fe str) diff --git a/src/julia_internal.h b/src/julia_internal.h index 49f0b19ec4209..3b9da93fcf802 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -742,7 +742,8 @@ jl_value_t *jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_t *e, jl_code_info_t *src, jl_svec_t *sparam_vals); JL_DLLEXPORT int jl_is_toplevel_only_expr(jl_value_t *e) JL_NOTSAFEPOINT; -jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule); +jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr, + jl_module_t *inmodule, const char *file, int line); jl_method_instance_t *jl_method_lookup(jl_value_t **args, size_t nargs, size_t world); diff --git a/src/toplevel.c b/src/toplevel.c index 8f148727e0249..5daf27043e938 100644 --- a/src/toplevel.c +++ b/src/toplevel.c @@ -185,17 +185,28 @@ static jl_value_t *jl_eval_module_expr(jl_module_t *parent_module, jl_expr_t *ex size_t last_age = ct->world_age; // add standard imports unless baremodule + jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 2))->args; + int lineno = 0; + const char *filename = "none"; + if (jl_array_len(exprs) > 0) { + jl_value_t *lineex = jl_array_ptr_ref(exprs, 0); + if (jl_is_linenode(lineex)) { + lineno = jl_linenode_line(lineex); + jl_value_t *file = jl_linenode_file(lineex); + if (jl_is_symbol(file)) + filename = jl_symbol_name((jl_sym_t*)file); + } + } if (std_imports) { if (jl_base_module != NULL) { jl_add_standard_imports(newm); } // add `eval` function - form = jl_call_scm_on_ast("module-default-defs", (jl_value_t*)ex, newm); + form = jl_call_scm_on_ast_and_loc("module-default-defs", (jl_value_t*)name, newm, filename, lineno); jl_toplevel_eval_flex(newm, form, 0, 1); form = NULL; } - jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 2))->args; for (int i = 0; i < jl_array_len(exprs); i++) { // process toplevel form ct->world_age = jl_atomic_load_acquire(&jl_world_counter);