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

Expand macro expressions arguments before macro calls. Fixes #2388 #2392

Merged
merged 1 commit into from
Apr 1, 2016
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: 22 additions & 0 deletions spec/compiler/codegen/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1269,4 +1269,26 @@ describe "Code gen: macro" do
id(A)
)).to_i.should eq(1)
end

it "solves macro expression arguments before macro expansion (type)" do
run(%(
macro name(x)
{{x.name.stringify}}
end

name({{String}})
)).to_string.should eq("String")
end

it "solves macro expression arguments before macro expansion (constant)" do
run(%(
CONST = 1

macro id(x)
{{x}}
end

id({{CONST}})
)).to_i.should eq(1)
end
end
41 changes: 38 additions & 3 deletions src/compiler/crystal/semantic/base_type_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,18 @@ module Crystal
node.raise "macro '#{node.name}' must be defined before this point but is defined later"
end

@exp_nest -= 1
expansion_scope = (macro_scope || @scope || current_type)

args = expand_macro_arguments(node, expansion_scope)

@exp_nest -= 1
generated_nodes = expand_macro(the_macro, node) do
@mod.expand_macro the_macro, node, (macro_scope || @scope || current_type)
old_args = node.args
node.args = args
expanded = @mod.expand_macro the_macro, node, expansion_scope
node.args = old_args
expanded
end

@exp_nest += 1

node.expanded = generated_nodes
Expand Down Expand Up @@ -531,6 +537,35 @@ module Crystal
generated_nodes
end

def expand_macro_arguments(node, expansion_scope)
# If any argument is a MacroExpression, solve it first and
# replace Path with Const/TypeNode if it denotes such thing
args = node.args
if args.any? &.is_a?(MacroExpression)
@exp_nest -= 1
args = args.map do |arg|
if arg.is_a?(MacroExpression)
arg.accept self
expanded = arg.expanded.not_nil!
if expanded.is_a?(Path)
expanded_type = expansion_scope.lookup_type(expanded)
case expanded_type
when Const
expanded = expanded_type.value
when Type
expanded = TypeNode.new(expanded_type)
end
end
expanded
else
arg
end
end
@exp_nest += 1
end
args
end

def visit(node : MacroExpression)
expand_inline_macro node
end
Expand Down