compiler: propagate AST node location info

This commit is contained in:
Sebastien Bourdeauducq 2014-06-21 14:53:09 +02:00
parent c71eb702bb
commit 5a8074a12f
3 changed files with 15 additions and 8 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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, "<ast>", "eval")
return eval(code, symdict)