forked from M-Labs/artiq
1
0
Fork 0

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)) result = value_to_ast(op(operand))
except: except:
return node return node
return result return ast.copy_location(result, node)
def visit_BinOp(self, node): def visit_BinOp(self, node):
self.generic_visit(node) self.generic_visit(node)
@ -77,6 +77,6 @@ class _ConstantFolder(ast.NodeTransformer):
result = value_to_ast(op(left, right)) result = value_to_ast(op(left, right))
except: except:
return node return node
return result return ast.copy_location(result, node)
fold_constants = make_stmt_transformer(_ConstantFolder) fold_constants = make_stmt_transformer(_ConstantFolder)

View File

@ -136,7 +136,9 @@ class _ReferenceReplacer(ast.NodeTransformer):
self.module = inspect.getmodule(self.obj) self.module = inspect.getmodule(self.obj)
def visit_ref(self, node): 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_Name = visit_ref
visit_Attribute = visit_ref visit_Attribute = visit_ref
@ -151,8 +153,10 @@ class _ReferenceReplacer(ast.NodeTransformer):
if func in _embeddable_calls: if func in _embeddable_calls:
new_func = ast.Name(func.__name__, ast.Load()) new_func = ast.Name(func.__name__, ast.Load())
return ast.Call(func=new_func, args=new_args, return ast.copy_location(
keywords=[], starargs=None, kwargs=None) 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: elif hasattr(func, "k_function_info") and getattr(func.__self__, func.k_function_info.core_name) is self.core:
args = [func.__self__] + new_args args = [func.__self__] + new_args
inlined, _ = inline(self.core, func.k_function_info.k_function, args, dict(), self.rm) inlined, _ = inline(self.core, func.k_function_info.k_function, args, dict(), self.rm)
@ -160,8 +164,10 @@ class _ReferenceReplacer(ast.NodeTransformer):
else: else:
args = [ast.Str("rpc"), ast.Num(self.rm.rpc_map[func])] args = [ast.Str("rpc"), ast.Num(self.rm.rpc_map[func])]
args += new_args args += new_args
return ast.Call(func=ast.Name("syscall", ast.Load()), return ast.copy_location(
args=args, keywords=[], starargs=None, kwargs=None) ast.Call(func=ast.Name("syscall", ast.Load()),
args=args, keywords=[], starargs=None, kwargs=None),
node)
def visit_Expr(self, node): def visit_Expr(self, node):
if isinstance(node.value, ast.Call): if isinstance(node.value, ast.Call):

View File

@ -4,7 +4,8 @@ from artiq.language import experiment, units
def eval_ast(expr, symdict=dict()): def eval_ast(expr, symdict=dict()):
if not isinstance(expr, ast.Expression): 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") code = compile(expr, "<ast>", "eval")
return eval(code, symdict) return eval(code, symdict)