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 TypeNode#has_method? #4474

Merged
merged 2 commits into from Jun 8, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/compiler/codegen/macro_spec.cr
Expand Up @@ -1601,6 +1601,30 @@ describe "Code gen: macro" do
)).to_b.should be_false
end

it "determines if method exists (true)" do
run(%(
class Foo
def foo
42
end
end

{{ Foo.has_method?(:foo) }}
)).to_b.should be_true
end

it "determines if method exists (false)" do
run(%(
class Foo
def foo
42
end
end

{{ Foo.has_method?(:bar) }}
)).to_b.should be_false
end

it "forwards file location" do
run(%(
macro foo
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/crystal/macros.cr
Expand Up @@ -1543,6 +1543,12 @@ module Crystal::Macros
def methods : ArrayLiteral(Def)
end

# Returns `true` if this type has a method. For example `default_options`
# (the name you pass to this method is "default_options" or :default_options
# in this cases).
def has_method?(name : StringLiteral | SymbolLiteral) : BoolLiteral
end

# Returns true if this type has an attribute. For example `@[Flags]`
# or `@[Packed]` (the name you pass to this method is "Flags" or "Packed"
# in these cases).
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Expand Up @@ -1346,6 +1346,11 @@ module Crystal
end
when "methods"
interpret_argless_method(method, args) { TypeNode.methods(type) }
when "has_method?"
interpret_one_arg_method(method, args) do |arg|
value = arg.to_string("argument to 'TypeNode#has_method?'")
TypeNode.has_method?(type, value)
end
when "has_attribute?"
interpret_one_arg_method(method, args) do |arg|
value = arg.to_string("argument to 'TypeNode#has_attribute?'")
Expand Down Expand Up @@ -1533,6 +1538,10 @@ module Crystal
ArrayLiteral.new(defs)
end

def self.has_method?(type, name)
BoolLiteral.new(!!type.has_def?(name))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that BoolLiteral.new(type.has_def?(name)) is enough, as Type#has_def? is already returning a boolean

Copy link
Contributor Author

@Sija Sija Jun 7, 2017

Choose a reason for hiding this comment

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

@bew nope, returned type is Bool | Nil. I'd say it should indeed return just Bool, yet it might be out of the scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

right, sorry about that (found why: some_hash.try &.has_key?(some_key) can be Nil with the try..)

end

def self.overrides?(type, target, method)
overrides = type.lookup_defs(method).any? do |a_def|
a_def.owner != target && a_def.macro_owner != target && !target.implements?(a_def.owner)
Expand Down