From 5a8074a12feb9df8484246eeb5149b1ff6e11e4c Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 21 Jun 2014 14:53:09 +0200 Subject: [PATCH] compiler: propagate AST node location info --- artiq/compiler/fold_constants.py | 4 ++-- artiq/compiler/inline.py | 16 +++++++++++----- artiq/compiler/tools.py | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/artiq/compiler/fold_constants.py b/artiq/compiler/fold_constants.py index 3ab9d31c0..8d80e0824 100644 --- a/artiq/compiler/fold_constants.py +++ b/artiq/compiler/fold_constants.py @@ -61,7 +61,7 @@ class _ConstantFolder(ast.NodeTransformer): result = value_to_ast(op(operand)) except: return node - return result + return ast.copy_location(result, node) def visit_BinOp(self, node): self.generic_visit(node) @@ -77,6 +77,6 @@ class _ConstantFolder(ast.NodeTransformer): result = value_to_ast(op(left, right)) except: return node - return result + return ast.copy_location(result, node) fold_constants = make_stmt_transformer(_ConstantFolder) diff --git a/artiq/compiler/inline.py b/artiq/compiler/inline.py index 60523cc24..6f007738a 100644 --- a/artiq/compiler/inline.py +++ b/artiq/compiler/inline.py @@ -136,7 +136,9 @@ class _ReferenceReplacer(ast.NodeTransformer): self.module = inspect.getmodule(self.obj) def visit_ref(self, node): - return self.rm.get(self.obj, self.funcname, node) + return ast.copy_location( + self.rm.get(self.obj, self.funcname, node), + node) visit_Name = visit_ref visit_Attribute = visit_ref @@ -151,8 +153,10 @@ class _ReferenceReplacer(ast.NodeTransformer): if func in _embeddable_calls: new_func = ast.Name(func.__name__, ast.Load()) - return ast.Call(func=new_func, args=new_args, - keywords=[], starargs=None, kwargs=None) + return ast.copy_location( + ast.Call(func=new_func, args=new_args, + keywords=[], starargs=None, kwargs=None), + node) elif hasattr(func, "k_function_info") and getattr(func.__self__, func.k_function_info.core_name) is self.core: args = [func.__self__] + new_args inlined, _ = inline(self.core, func.k_function_info.k_function, args, dict(), self.rm) @@ -160,8 +164,10 @@ class _ReferenceReplacer(ast.NodeTransformer): else: args = [ast.Str("rpc"), ast.Num(self.rm.rpc_map[func])] args += new_args - return ast.Call(func=ast.Name("syscall", ast.Load()), - args=args, keywords=[], starargs=None, kwargs=None) + return ast.copy_location( + ast.Call(func=ast.Name("syscall", ast.Load()), + args=args, keywords=[], starargs=None, kwargs=None), + node) def visit_Expr(self, node): if isinstance(node.value, ast.Call): diff --git a/artiq/compiler/tools.py b/artiq/compiler/tools.py index 867affcbb..8eca52e74 100644 --- a/artiq/compiler/tools.py +++ b/artiq/compiler/tools.py @@ -4,7 +4,8 @@ from artiq.language import experiment, units def eval_ast(expr, symdict=dict()): if not isinstance(expr, ast.Expression): - expr = ast.Expression(expr) + expr = ast.copy_location(ast.Expression(expr), expr) + ast.fix_missing_locations(expr) code = compile(expr, "", "eval") return eval(code, symdict)