Skip to content

Commit

Permalink
Compiler: add more locations (#5597)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and RX14 committed Jan 17, 2018
1 parent bba4985 commit 84288b7
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 69 deletions.
22 changes: 22 additions & 0 deletions spec/compiler/codegen/debug_spec.cr
Expand Up @@ -128,4 +128,26 @@ describe "Code gen: debug" do
A
), debug: Crystal::Debug::All)
end

it "has debug info in closure inside if (#5593)" do
codegen(%(
require "prelude"
def foo
if true && true
yield 1
end
end
def bar(&block)
block
end
foo do |i|
bar do
i
end
end
), debug: Crystal::Debug::All)
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/asm.cr
Expand Up @@ -19,7 +19,7 @@ class Crystal::CodeGenVisitor
constraints << "," unless constraints.empty?

inputs.each_with_index do |input, i|
input.exp.accept self
accept input.exp
input_types << llvm_type(input.exp.type)
input_values << @last
constraints << "," if i > 0
Expand All @@ -45,7 +45,7 @@ class Crystal::CodeGenVisitor
asm_value = call value, input_values

if ptrof
ptrof.accept self
accept ptrof
store asm_value, @last
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/call.cr
Expand Up @@ -293,7 +293,7 @@ class Crystal::CodeGenVisitor
end

def codegen_call_with_block_as_fun_literal(node, fun_literal, self_type, call_args)
fun_literal.accept self
accept fun_literal
call_args.push @last

target_def = node.target_def
Expand Down
40 changes: 24 additions & 16 deletions src/compiler/crystal/codegen/codegen.cr
Expand Up @@ -62,7 +62,7 @@ module Crystal

def codegen(node, single_module = false, debug = Debug::Default)
visitor = CodeGenVisitor.new self, node, single_module: single_module, debug: debug
node.accept visitor
visitor.accept node
visitor.process_finished_hooks
visitor.finish

Expand Down Expand Up @@ -381,7 +381,7 @@ module Crystal

emit_vars_debug_info(vars) if @debug.variables?
end
node.node.accept self
accept node.node
@last = llvm_nil
end

Expand Down Expand Up @@ -450,7 +450,7 @@ module Crystal
type = node.type.as(TupleInstanceType)
@last = allocate_tuple(type) do |tuple_type, i|
exp = node.elements[i]
exp.accept self
accept exp
{exp.type, @last}
end
end
Expand All @@ -462,7 +462,7 @@ module Crystal
type = node.type.as(NamedTupleInstanceType)
struct_type = alloca llvm_type(type)
node.entries.each do |entry|
entry.value.accept self
accept entry.value
index = type.name_index(entry.key).not_nil!
assign aggregate_index(struct_type, index), type.entries[index].type, entry.value.type, @last
end
Expand Down Expand Up @@ -490,7 +490,7 @@ module Crystal
const = node_exp.target_const.not_nil!
read_const_pointer(const)
when ReadInstanceVar
node_exp.obj.accept self
accept node_exp.obj
instance_var_ptr (node_exp.obj.type), node_exp.name, @last
when Call
# lib external var
Expand Down Expand Up @@ -637,7 +637,9 @@ module Crystal
end

def visit(node : ClassDef)
node.hook_expansions.try &.each &.accept self
node.hook_expansions.try &.each do |hook|
accept hook
end
accept node.body
@last = llvm_nil
false
Expand All @@ -651,7 +653,7 @@ module Crystal

def visit(node : LibDef)
@in_lib = true
node.body.accept self
accept node.body
@in_lib = false
@last = llvm_nil
false
Expand All @@ -665,7 +667,7 @@ module Crystal
def visit(node : EnumDef)
node.members.each do |member|
if member.is_a?(Assign)
member.accept self
accept member
end
end

Expand Down Expand Up @@ -704,32 +706,36 @@ module Crystal
end

def visit(node : Include)
node.hook_expansions.try &.each &.accept self
node.hook_expansions.try &.each do |hook|
accept hook
end

@last = llvm_nil
false
end

def visit(node : Extend)
node.hook_expansions.try &.each &.accept self
node.hook_expansions.try &.each do |hook|
accept hook
end

@last = llvm_nil
false
end

def visit(node : If)
if node.truthy?
node.cond.accept self
node.then.accept self
accept node.cond
accept node.then
if @needs_value && (node_type = node.type?) && (then_type = node.then.type?)
@last = upcast(@last, node_type, then_type)
end
return false
end

if node.falsey?
node.cond.accept self
node.else.accept self
accept node.cond
accept node.else
if @needs_value && (node_type = node.type?) && (else_type = node.else.type?)
@last = upcast(@last, node_type, else_type)
end
Expand Down Expand Up @@ -1306,7 +1312,9 @@ module Crystal
end

def visit(node : Def)
node.hook_expansions.try &.each &.accept self
node.hook_expansions.try &.each do |hook|
accept hook
end

@last = llvm_nil
false
Expand Down Expand Up @@ -1826,7 +1834,7 @@ module Crystal
context.vars["self"] = LLVMVar.new(type_ptr, real_type)
alloca_vars init.meta_vars

init.value.accept self
accept init.value

ivar_ptr = instance_var_ptr real_type, init.name, type_ptr
assign ivar_ptr, ivar.type, init.value.type, @last
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/exception.cr
Expand Up @@ -205,7 +205,7 @@ class Crystal::CodeGenVisitor
next unless target_ensure

with_context(exception_handler.context) do
target_ensure.accept self
accept target_ensure
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/primitives.cr
Expand Up @@ -504,7 +504,7 @@ class Crystal::CodeGenVisitor
if (extra = node.extra)
existing_value = context.vars["value"]?
context.vars["value"] = LLVMVar.new(call_arg, node.type, true)
request_value { extra.accept self }
request_value { accept extra }
call_arg = @last
context.vars["value"] = existing_value if existing_value
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Expand Up @@ -149,7 +149,7 @@ module Crystal
@last.to_s(str)
end
end
end)
end).at(node)
false
end

Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/semantic/ast.cr
Expand Up @@ -195,6 +195,7 @@ module Crystal
a_def.always_inline = always_inline?
a_def.returns_twice = returns_twice?
a_def.naked = naked?
a_def.new = new?
a_def
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/call.cr
Expand Up @@ -799,7 +799,7 @@ class Crystal::Call
wrong_number_of "block arguments", block.args.size, fun_args.size
end

a_def = Def.new("->", fun_args, block.body)
a_def = Def.new("->", fun_args, block.body).at(block)
a_def.captured_block = true

fun_literal = ProcLiteral.new(a_def).at(self)
Expand Down
20 changes: 10 additions & 10 deletions src/compiler/crystal/semantic/default_arguments.cr
Expand Up @@ -87,14 +87,13 @@ class Crystal::Def
new_name = name
end

expansion = Def.new(new_name, new_args, nil, receiver.clone, block_arg.clone, return_type.clone, macro_def?, yields)
expansion = Def.new(new_name, new_args, nil, receiver.clone, block_arg.clone, return_type.clone, macro_def?, yields).at(self)
expansion.args.each { |arg| arg.default_value = nil }
expansion.calls_super = calls_super?
expansion.calls_initialize = calls_initialize?
expansion.calls_previous_def = calls_previous_def?
expansion.uses_block_arg = uses_block_arg?
expansion.yields = yields
expansion.location = location
expansion.raises = raises?
expansion.free_vars = free_vars
if owner = self.owner?
Expand Down Expand Up @@ -122,10 +121,10 @@ class Crystal::Def
if default_value.is_a?(MagicConstant)
expansion.args.push arg.clone
else
new_body << Assign.new(Var.new(arg.name), default_value)
new_body << Assign.new(Var.new(arg.name).at(arg), default_value).at(arg)

if restriction = arg.restriction
new_body << TypeRestriction.new(Var.new(arg.name), restriction).at(arg)
new_body << TypeRestriction.new(Var.new(arg.name).at(arg), restriction).at(arg)
end
end
end
Expand All @@ -135,10 +134,11 @@ class Crystal::Def
if splat_names && splat_index
tuple_args = [] of ASTNode
splat_size.times do |i|
tuple_args << Var.new(splat_names[i])
tuple_args << Var.new(splat_names[i]).at(self)
end
tuple = TupleLiteral.new(tuple_args).at(args[splat_index])
new_body << Assign.new(Var.new(args[splat_index].name), tuple)
splat_arg = args[splat_index]
tuple = TupleLiteral.new(tuple_args).at(splat_arg)
new_body << Assign.new(Var.new(splat_arg.name).at(splat_arg), tuple).at(splat_arg)
end

# Double splat argument
Expand All @@ -151,7 +151,7 @@ class Crystal::Def
named_tuple_entries << NamedTupleLiteral::Entry.new(named_arg, Var.new(named_arg))
end
named_tuple = NamedTupleLiteral.new(named_tuple_entries).at(double_splat)
new_body << Assign.new(Var.new(double_splat.name), named_tuple)
new_body << Assign.new(Var.new(double_splat.name).at(double_splat), named_tuple).at(double_splat)
end

new_body.push body
Expand Down Expand Up @@ -182,8 +182,8 @@ class Crystal::Def
new_args.push Var.new(arg.name)
expansion.args.push arg.clone
else
body << Assign.new(Var.new(arg.name), default_value.clone)
new_args.push Var.new(arg.name)
body << Assign.new(Var.new(arg.name).at(arg), default_value.clone).at(arg)
new_args.push Var.new(arg.name).at(arg)
end
end
end
Expand Down

0 comments on commit 84288b7

Please sign in to comment.