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

Compiler: simplify some calls #12417

Merged
merged 2 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/compiler/crystal/interpreter/repl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Crystal::Repl

private def interpret_crystal_exit(exception : EscapingException)
decl = UninitializedVar.new(Var.new("ex"), TypeNode.new(@context.program.exception.virtual_type))
call = Call.new(Path.global("Crystal"), "exit", [NumberLiteral.new(1), Var.new("ex")] of ASTNode)
call = Call.new(Path.global("Crystal"), "exit", NumberLiteral.new(1), Var.new("ex"))
exps = Expressions.new([decl, call] of ASTNode)

begin
Expand Down
19 changes: 10 additions & 9 deletions src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module Crystal
if node.elements.any?(Splat)
ary_var = new_temp_var.at(node)

ary_instance = Call.new(generic, "new", args: [NumberLiteral.new(capacity).at(node)] of ASTNode).at(node)
ary_instance = Call.new(generic, "new", NumberLiteral.new(capacity).at(node)).at(node)

exps = Array(ASTNode).new(node.elements.size + 2)
exps << Assign.new(ary_var.clone, ary_instance).at(node)
Expand All @@ -74,7 +74,7 @@ module Crystal
else
ary_var = new_temp_var.at(node)

ary_instance = Call.new(generic, "unsafe_build", args: [NumberLiteral.new(capacity).at(node)] of ASTNode).at(node)
ary_instance = Call.new(generic, "unsafe_build", NumberLiteral.new(capacity).at(node)).at(node)

buffer = Call.new(ary_var, "to_unsafe").at(node)
buffer_var = new_temp_var.at(node)
Expand Down Expand Up @@ -242,7 +242,7 @@ module Crystal
exps = Array(ASTNode).new(node.entries.size + 2)
exps << Assign.new(temp_var.clone, constructor).at(node)
node.entries.each do |entry|
exps << Call.new(temp_var.clone, "[]=", [entry.key.clone, entry.value.clone] of ASTNode).at(node)
exps << Call.new(temp_var.clone, "[]=", entry.key.clone, entry.value.clone).at(node)
end
exps << temp_var.clone

Expand Down Expand Up @@ -377,7 +377,7 @@ module Crystal
def expand(node : RangeLiteral)
path = Path.global("Range").at(node)
bool = BoolLiteral.new(node.exclusive?).at(node)
Call.new(path, "new", [node.from, node.to, bool]).at(node)
Call.new(path, "new", node.from, node.to, bool).at(node)
end

# Convert an interpolation to a call to `String.interpolation`
Expand Down Expand Up @@ -616,13 +616,14 @@ module Crystal
if node_else = node.else
case_else = node_else.clone
else
case_else = Call.new(nil, "raise", args: [StringLiteral.new("BUG: invalid select index")] of ASTNode, global: true).at(node)
case_else = Call.new(nil, "raise", StringLiteral.new("BUG: invalid select index"), global: true).at(node)
end

call_name = node.else ? "non_blocking_select" : "select"
call_args = [TupleLiteral.new(tuple_values).at(node)] of ASTNode

call = Call.new(channel, call_name, call_args).at(node)
call = Call.new(
channel,
node.else ? "non_blocking_select" : "select",
TupleLiteral.new(tuple_values).at(node),
).at(node)
multi = MultiAssign.new(targets, [call] of ASTNode)
case_cond = Var.new(index_name).at(node)
a_case = Case.new(case_cond, case_whens, case_else, exhaustive: false).at(node)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ module Crystal
extra = Call.new(
nil,
"raise",
args: [StringLiteral.new("Can't instantiate abstract #{base_type.type_desc} #{base_type}")] of ASTNode,
StringLiteral.new("Can't instantiate abstract #{base_type.type_desc} #{base_type}"),
global: true)
extra.accept self

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/method_missing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Crystal
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] of ASTNode)
fake_call = Call.new(nil, "method_missing", call)

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

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 @@ -445,7 +445,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor

unless @method_added_running
@method_added_running = true
run_hooks target_type.metaclass, target_type, :method_added, node, Call.new(nil, "method_added", [node] of ASTNode).at(node.location)
run_hooks target_type.metaclass, target_type, :method_added, node, Call.new(nil, "method_added", node).at(node.location)
@method_added_running = false
end
end
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,18 @@ module Crystal
end
end

def self.new(obj, name, arg : ASTNode)
new obj, name, [arg] of ASTNode
def self.new(obj, name, arg : ASTNode, global = false)
new obj, name, [arg] of ASTNode, global: global
end

def self.new(obj, name, arg1 : ASTNode, arg2 : ASTNode)
new obj, name, [arg1, arg2] of ASTNode
end

def self.new(obj, name, arg1 : ASTNode, arg2 : ASTNode, arg3 : ASTNode)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know at this point we could maybe use a splat, but I'd like to avoid creating intermediate tuples. 0..3 is enough.

new obj, name, [arg1, arg2, arg3] of ASTNode
end

def self.global(name, arg : ASTNode)
new nil, name, [arg] of ASTNode, global: true
end
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3015,7 +3015,7 @@ module Crystal
if body.nil_check?
call = Call.new(nil, "nil?")
else
call = Call.new(nil, "is_a?", args: [body.const] of ASTNode)
call = Call.new(nil, "is_a?", body.const)
end
accept call
else
Expand All @@ -3024,23 +3024,23 @@ module Crystal
end
when RespondsTo
if body.obj.is_a?(Var)
call = Call.new(nil, "responds_to?", args: [SymbolLiteral.new(body.name.to_s)] of ASTNode)
call = Call.new(nil, "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", args: [body.to] of ASTNode)
call = Call.new(nil, "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?", args: [body.to] of ASTNode)
call = Call.new(nil, "as?", body.to)
accept call
else
clear_object(body)
Expand Down