Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Call.new constructor overload without obj parameter #15441

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -236,29 +236,29 @@ module Crystal

describe "#doc" do
it "returns an empty string if there are no docs on the node (wants_doc = false)" do
assert_macro "{{ x.doc }}", %(""), {x: Call.new(nil, "some_call")}
assert_macro "{{ x.doc }}", %(""), {x: Call.new("some_call")}
end

it "returns the call's docs if present (wants_doc = true)" do
assert_macro "{{ x.doc }}", %("Some docs"), {x: Call.new(nil, "some_call").tap { |c| c.doc = "Some docs" }}
assert_macro "{{ x.doc }}", %("Some docs"), {x: Call.new("some_call").tap { |c| c.doc = "Some docs" }}
end

it "returns a multiline comment" do
assert_macro "{{ x.doc }}", %("Some\\nmulti\\nline\\ndocs"), {x: Call.new(nil, "some_call").tap { |c| c.doc = "Some\nmulti\nline\ndocs" }}
assert_macro "{{ x.doc }}", %("Some\\nmulti\\nline\\ndocs"), {x: Call.new("some_call").tap { |c| c.doc = "Some\nmulti\nline\ndocs" }}
end
end

describe "#doc_comment" do
it "returns an empty MacroId if there are no docs on the node (wants_doc = false)" do
assert_macro "{{ x.doc_comment }}", %(), {x: Call.new(nil, "some_call")}
assert_macro "{{ x.doc_comment }}", %(), {x: Call.new("some_call")}
end

it "returns the call's docs if present as a MacroId (wants_doc = true)" do
assert_macro "{{ x.doc_comment }}", %(Some docs), {x: Call.new(nil, "some_call").tap { |c| c.doc = "Some docs" }}
assert_macro "{{ x.doc_comment }}", %(Some docs), {x: Call.new("some_call").tap { |c| c.doc = "Some docs" }}
end

it "ensures each newline has a `#` prefix" do
assert_macro "{{ x.doc_comment }}", %(Some\n# multi\n# line\n# docs), {x: Call.new(nil, "some_call").tap { |c| c.doc = "Some\nmulti\nline\ndocs" }}
assert_macro "{{ x.doc_comment }}", %(Some\n# multi\n# line\n# docs), {x: Call.new("some_call").tap { |c| c.doc = "Some\nmulti\nline\ndocs" }}
end
end
end
Expand Down Expand Up @@ -2781,15 +2781,15 @@ module Crystal

describe "macro for methods" do
it "executes vars" do
assert_macro %({{x.vars}}), "[bar]", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new(nil, "puts", [Var.new("bar")] of ASTNode))}
assert_macro %({{x.vars}}), "[bar]", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new("puts", [Var.new("bar")] of ASTNode))}
end

it "executes exp" do
assert_macro %({{x.exp}}), "foo", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new(nil, "puts", [Var.new("bar")] of ASTNode))}
assert_macro %({{x.exp}}), "foo", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new("puts", [Var.new("bar")] of ASTNode))}
end

it "executes body" do
assert_macro %({{x.body}}), "puts(bar)", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new(nil, "puts", [Var.new("bar")] of ASTNode))}
assert_macro %({{x.body}}), "puts(bar)", {x: MacroFor.new([Var.new("bar")], Var.new("foo"), Call.new("puts", [Var.new("bar")] of ASTNode))}
end
end

Expand Down Expand Up @@ -2938,7 +2938,7 @@ module Crystal
end

it "executes args" do
assert_macro %({{x.args}}), "[1, 3]", {x: Call.new(nil, "some_call", [1.int32, 3.int32] of ASTNode)}
assert_macro %({{x.args}}), "[1, 3]", {x: Call.new("some_call", [1.int32, 3.int32] of ASTNode)}
end

it "executes receiver" do
Expand Down Expand Up @@ -2971,7 +2971,7 @@ module Crystal

it "executes global?" do
assert_macro %({{x.global?}}), "false", {x: Call.new(1.int32, "some_call")}
assert_macro %({{x.global?}}), "true", {x: Call.new(nil, "some_call", global: true)}
assert_macro %({{x.global?}}), "true", {x: Call.new("some_call", global: true)}
end
end

Expand Down
390 changes: 195 additions & 195 deletions spec/compiler/parser/parser_spec.cr

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spec/compiler/parser/to_s_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe "ASTNode#to_s" do
expect_to_s %q(%r{#{1}\/\0}), %q(/#{1}\/\0/)
expect_to_s %q(`\n\0`), %q(`\n\u0000`)
expect_to_s %q(`#{1}\n\0`), %q(`#{1}\n\u0000`)
expect_to_s Call.new(nil, "`", Call.new("String".path, "interpolation", "x".var, global: true)), %q(`#{::String.interpolation(x)}`)
expect_to_s Call.new("`", Call.new("String".path, "interpolation", "x".var, global: true)), %q(`#{::String.interpolation(x)}`)
expect_to_s "macro foo\n{% verbatim do %}1{% end %}\nend"
expect_to_s Assign.new("x".var, Expressions.new([1.int32, 2.int32] of ASTNode)), "x = (1\n2\n)"
expect_to_s "foo.*"
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def prepare_macro_call(macro_body, flags = nil, &)
call_args.concat(args.values) if args

a_macro = Parser.parse("macro foo(#{macro_params});#{macro_body};end").as(Macro)
call = Call.new(nil, "", call_args)
call = Call.new("", call_args)

{program, a_macro, call}
end
Expand Down Expand Up @@ -299,7 +299,7 @@ def run(code, filename : String? = nil, inject_primitives = true, debug = Crysta
ast = Parser.parse(code).as(Expressions)
last = ast.expressions.last
assign = Assign.new(Var.new("__tempvar"), last)
call = Call.new(nil, "print", Var.new("__tempvar"))
call = Call.new("print", Var.new("__tempvar"))
exps = Expressions.new([assign, call] of ASTNode)
ast.expressions[-1] = exps
code = ast.to_s
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/interpreter/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Crystal::Repl::Context

# This returns the CompiledDef that corresponds to __crystal_raise_overflow
getter(crystal_raise_overflow_compiled_def : CompiledDef) do
call = Call.new(nil, "__crystal_raise_overflow", global: true)
call = Call.new("__crystal_raise_overflow", global: true)
program.semantic(call)

local_vars = LocalVars.new(self)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/interpreter/repl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Crystal::Repl
end

private def interpret_exit
interpret(Call.new(nil, "exit", global: true))
interpret(Call.new("exit", global: true))
end

private def interpret_crystal_exit(exception : EscapingException)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module Crystal
#
# and in this case the parser has no idea about this, so the only
# solution is to do it now.
if value = interpret_top_level_call?(Call.new(nil, node.name))
if value = interpret_top_level_call?(Call.new(node.name))
@last = value
return false
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/default_arguments.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class Crystal::Def
end
end

call = Call.new(nil, name, new_args).at(self)
call = Call.new(name, new_args).at(self)
call.expansion = true
body << call

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ module Crystal
if node_else = node.else
case_else = node_else.clone
else
case_else = Call.new(nil, "raise", StringLiteral.new("BUG: invalid select index"), global: true).at(node)
case_else = Call.new("raise", StringLiteral.new("BUG: invalid select index"), global: true).at(node)
end

call = Call.new(
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/method_missing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ module Crystal
a_def = Def.new(signature.name, args_nodes_names.map { |ext_name, name| Arg.new(name, external_name: ext_name) })
a_def.splat_index = signature.arg_types.size if signature.named_args

call = Call.new(nil, signature.name,
call = Call.new(signature.name,
args: args_nodes,
named_args: named_args_nodes,
block: block_node.is_a?(Block) ? block_node : nil)
fake_call = Call.new(nil, "method_missing", call)
fake_call = Call.new("method_missing", call)

expanded_macro, macro_expansion_pragmas = program.expand_macro method_missing, fake_call, self, self

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/new.cr
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module Crystal
new_generic = Generic.new(Path.new(instance_type.name), generic_type_args)
alloc = Call.new(new_generic, "allocate").at(self)
else
alloc = Call.new(nil, "allocate").at(self)
alloc = Call.new("allocate").at(self)
end

# This creates:
Expand Down Expand Up @@ -235,7 +235,7 @@ module Crystal
# x
# end
var = Var.new("x").at(loc)
alloc = Call.new(nil, "allocate").at(loc)
alloc = Call.new("allocate").at(loc)
assign = Assign.new(var, alloc).at(loc)

call = Call.new(Path.global("GC").at(loc), "add_finalizer", var.clone).at(loc)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor

if !@method_added_running && has_hooks?(target_type.metaclass)
@method_added_running = true
run_hooks target_type.metaclass, target_type, :method_added, node, Call.new(nil, "method_added", node).at(node)
run_hooks target_type.metaclass, target_type, :method_added, node, Call.new("method_added", node).at(node)
@method_added_running = false
end
end
Expand Down
16 changes: 12 additions & 4 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -656,20 +656,28 @@ module Crystal
property? args_in_brackets = false
property? has_parentheses = false

def initialize(@obj, @name, @args : Array(ASTNode) = [] of ASTNode, @block = nil, @block_arg = nil, @named_args = nil, @global : Bool = false)
def initialize(@obj : ASTNode?, @name : String, @args : Array(ASTNode) = [] of ASTNode, @block = nil, @block_arg = nil, @named_args = nil, @global : Bool = false)
if block = @block
block.call = self
end
end

def self.new(obj, name, *args : ASTNode, global = false)
def self.new(obj : ASTNode?, name : String, *args : ASTNode, block : Block? = nil, block_arg : ASTNode? = nil, named_args : Array(NamedArgument)? = nil, global : Bool = false)
{% if compare_versions(Crystal::VERSION, "1.5.0") > 0 %}
new obj, name, [*args] of ASTNode, global: global
new obj, name, [*args] of ASTNode, block: block, block_arg: block_arg, named_args: named_args, global: global
{% else %}
new obj, name, args.to_a(&.as(ASTNode)), global: global
new obj, name, args.to_a(&.as(ASTNode)), block: block, block_arg: block_arg, named_args: named_args, global: global
{% end %}
end

def self.new(name : String, args : Array(ASTNode) = [] of ASTNode, block : Block? = nil, block_arg : ASTNode? = nil, named_args : Array(NamedArgument)? = nil, global : Bool = false)
new(nil, name, args, block: block, block_arg: block_arg, named_args: named_args, global: global)
end

def self.new(name : String, *args : ASTNode, block : Block? = nil, block_arg : ASTNode? = nil, named_args : Array(NamedArgument)? = nil, global : Bool = false)
new(nil, name, *args, block: block, block_arg: block_arg, named_args: named_args, global: global)
end

def self.global(name, *args : ASTNode)
new nil, name, *args, global: true
end
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ module Crystal

case delimiter_state.kind
when .command?
result = Call.new(nil, "`", result).at(location)
result = Call.new("`", result).at(location)
when .regex?
if result.is_a?(StringLiteral) && (regex_error = Regex.error?(result.value))
raise "invalid regex: #{regex_error}", location
Expand Down Expand Up @@ -4347,7 +4347,7 @@ module Crystal

node =
if block || block_arg || global
call = Call.new(nil, name, (args || [] of ASTNode), block, block_arg, named_args, global)
call = Call.new(name, (args || [] of ASTNode), block, block_arg, named_args, global)
call.name_location = name_location
call.has_parentheses = has_parentheses
call
Expand All @@ -4357,7 +4357,7 @@ module Crystal
if maybe_var
Var.new(name)
else
call = Call.new(nil, name, args, nil, nil, named_args, global)
call = Call.new(name, args, nil, nil, named_args, global)
call.name_location = name_location
call.has_parentheses = has_parentheses
call
Expand Down Expand Up @@ -4385,7 +4385,7 @@ module Crystal
raise "can't use variable name '#{name}' inside assignment to variable '#{name}'", location
end

call = Call.new(nil, name, [] of ASTNode, nil, nil, named_args, global)
call = Call.new(name, [] of ASTNode, nil, nil, named_args, global)
call.name_location = name_location
call.has_parentheses = has_parentheses
call
Expand Down
28 changes: 14 additions & 14 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3132,9 +3132,9 @@ module Crystal
when IsA
if body.obj.is_a?(Var)
if body.nil_check?
call = Call.new(nil, "nil?")
call = Call.new("nil?")
else
call = Call.new(nil, "is_a?", body.const)
call = Call.new("is_a?", body.const)
end
accept call
else
Expand All @@ -3143,39 +3143,39 @@ module Crystal
end
when RespondsTo
if body.obj.is_a?(Var)
call = Call.new(nil, "responds_to?", SymbolLiteral.new(body.name.to_s))
call = Call.new("responds_to?", SymbolLiteral.new(body.name.to_s))
accept call
else
clear_object(body)
accept body
end
when Cast
if body.obj.is_a?(Var)
call = Call.new(nil, "as", body.to)
call = Call.new("as", body.to)
accept call
else
clear_object(body)
accept body
end
when NilableCast
if body.obj.is_a?(Var)
call = Call.new(nil, "as?", body.to)
call = Call.new("as?", body.to)
accept call
else
clear_object(body)
accept body
end
when ReadInstanceVar
if body.obj.is_a?(Var)
call = Call.new(nil, body.name)
call = Call.new(body.name)
accept call
else
clear_object(body)
accept body
end
when Not
if body.exp.is_a?(Var)
call = Call.new(nil, "!")
call = Call.new("!")
accept call
else
clear_object(body)
Expand Down Expand Up @@ -3964,31 +3964,31 @@ module Crystal
end

def visit(node : TypeOf)
visit Call.new(nil, "typeof", node.expressions)
visit Call.new("typeof", node.expressions)
end

def visit(node : SizeOf)
visit Call.new(nil, "sizeof", node.exp)
visit Call.new("sizeof", node.exp)
end

def visit(node : InstanceSizeOf)
visit Call.new(nil, "instance_sizeof", node.exp)
visit Call.new("instance_sizeof", node.exp)
end

def visit(node : AlignOf)
visit Call.new(nil, "alignof", node.exp)
visit Call.new("alignof", node.exp)
end

def visit(node : InstanceAlignOf)
visit Call.new(nil, "instance_alignof", node.exp)
visit Call.new("instance_alignof", node.exp)
end

def visit(node : OffsetOf)
visit Call.new(nil, "offsetof", [node.offsetof_type, node.offset])
visit Call.new("offsetof", [node.offsetof_type, node.offset])
end

def visit(node : PointerOf)
visit Call.new(nil, "pointerof", node.exp)
visit Call.new("pointerof", node.exp)
end

def visit(node : Underscore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module Crystal
if node.is_a?(TupleLiteral)
args << ArrayLiteral.new(node.elements.map { |e| StringLiteral.new(e.to_s).as(ASTNode) })
end
call = Call.new(Call.new(nil, "_p"), "i", args, Block.new([] of Var, node.as(ASTNode)))
call = Call.new(Call.new("_p"), "i", args, Block.new([] of Var, node.as(ASTNode)))
call = Cast.new(call, TypeOf.new([node.clone] of ASTNode)) if add_as_typeof
call = Splat.new(call) if splat
call
Expand Down
Loading