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

Use enum (flags) instead of hardcoded string values for emit kinds #6515

Merged
merged 3 commits into from
Aug 12, 2018

Conversation

bew
Copy link
Contributor

@bew bew commented Aug 10, 2018

I think that using enum flags is cleaner in this case.
It's better to remove handcoded emit values from obscure places in the compiler code 😃

I tried to keep the emit values the same (e.g: llvm-ir), but it would be simpler (?) to use llvm_ir as it's how the enum flag can be easily parsed. This could be changed though, but a breaking change.

This is the beginning to work on #5820, and maybe #5821.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Could be a bit smarter about using the enum API.

@@ -98,13 +98,21 @@ module Crystal
# and can later be used to generate API docs.
property? wants_doc = false

# Can be set to an array of strings to emit other files other
@[Flags]
enum EmitKinds
Copy link
Member

Choose a reason for hiding this comment

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

Better have the name in singular, that's used throughout the code base.
Maybe EmitTarget would even be a better name.

Copy link
Member

Choose a reason for hiding this comment

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

And I'd suggest to have the values either all uppercase or all lower case. I prefer the former.

@@ -309,7 +307,10 @@ class Crystal::Command
end

unless no_codegen
opts.on("--emit [#{VALID_EMIT_VALUES.join('|')}]", "Comma separated list of types of output for the compiler to emit") do |emit_values|
valid_emit_values = {{ Compiler::EmitKinds.constants.map(&.stringify).reject { |m| %w(none all).includes?(m.downcase) } }}
Copy link
Member

Choose a reason for hiding this comment

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

Why use a macro instead of Enum.names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow how did I miss that (PR-ing at 3am may be why 😄 )
thanks!

when "obj"
FileUtils.cp(object_name, "#{output_filename}.o")
def emit(emit_kinds : EmitKinds, output_filename)
emit_kinds.each do |kind|
Copy link
Member

Choose a reason for hiding this comment

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

Why use a loop? You can just have consecutive conditionals:

if emit_kinds.asm?
  # do asm
end
if emit_kinds.llvm_bc?
  # do llvm-bc
end
# etc...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just matching the previous behavior, but that's right, it makes no sense to have a loop now

@asterite
Copy link
Member

Thank you, but I disagree. The code became much harder to write, read and understand.

@asterite
Copy link
Member

31 additions and 23 deletions. Now we have more code. Also more macro code, which is always harder to understand. A gsub/tr call to replace dash with underscore.

The old code was just fine.

@bew
Copy link
Contributor Author

bew commented Aug 10, 2018

@asterite How is it much harder to write/read/understand?

Now we have more code. Also more macro code, which is always harder to understand.

The macro code has been removed as I didn't noticed Enum.names

A gsub/tr call to replace dash with underscore.

I don't like it either, that's why I asked what I should do in the OP.
I'm also thinking about a simple case .. when .. block to match all possible emit values to their respective flags, to remove the dependence between the value for CLI, and the name of the flag in the code.

The old code was just fine.

For the current use-case I agree, but I'd like to change when some emit targets are emitted (for #5820 & #5821), and I really don't want to have hardcoded strings related to CLI input lying around when an enum flag can do the trick (and be typesafe!).

@@ -309,7 +307,10 @@ class Crystal::Command
end

unless no_codegen
opts.on("--emit [#{VALID_EMIT_VALUES.join('|')}]", "Comma separated list of types of output for the compiler to emit") do |emit_values|
valid_emit_values = Compiler::EmitTarget.names
valid_emit_values.map! { |v| v.tr("_", "-").downcase }
Copy link
Contributor

@Sija Sija Aug 10, 2018

Choose a reason for hiding this comment

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

You should use Chars instead of Strings here.
ditto below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's what I did originally, but there is only tr(String, String) not tr(Char, Char)

Copy link
Member

Choose a reason for hiding this comment

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

There's gsub(Char, Char)

@straight-shoota
Copy link
Member

straight-shoota commented Aug 10, 2018

The macro code has been removed as I didn't noticed Enum.names

It has just been moved into Enum.names. It's still executing a macro an thus more complexity at compile time.

Under the current circumstances, @asterite is probably right that this isn't really useful as an improvement. But in the light of changes to the locations where these flags are used, it's a different story.
Maybe it would help to see where you want to go with this. A look at the next step.

@straight-shoota
Copy link
Member

The CLI options could just hard coded into the description string to avoid the macro. It's not that they change all the time to be automatically generated.

@RX14 RX14 added this to the Next milestone Aug 12, 2018
@RX14 RX14 merged commit ba55954 into crystal-lang:master Aug 12, 2018
@bew bew deleted the emit-kinds-to-enum-flags branch August 13, 2018 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants