crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.42k stars 1.62k forks source link

External variable names that are not valid identifiers #14210

Open HertzDevil opened 9 months ago

HertzDevil commented 9 months ago

Funs inside a lib can have a quoted real name if that name cannot be represented as a Crystal identifier:

lib Lib
  fun foo = "llvm.ceil.f32"
end

The same cannot be done for external variables:

lib Lib
  $foo = "llvm.ceil.f32" : Int32 # Error: expecting any of these tokens: IDENT, CONST (not 'DELIMITER_START')
end

Since both are similarly mangled and Crystal doesn't have control over the mangling scheme, I think we should allow quoted strings here for external variables too.

HertzDevil commented 1 week ago

Another concrete example is implementing the throw_info primitive, used for exception handling on Windows, in plain code:

https://github.com/crystal-lang/crystal/blob/401eb47bf373f11b2da6e382c306ead36615e1d6/src/compiler/crystal/codegen/primitives.cr#L1338

lib LibC
  $type_info_vtable = "\u{1}??_7type_info@@6B@" : Void*
end