Change the syntax of Primitive.attr! to Symbol (#7501)

This commit is contained in:
Takashi Kokubun 2023-03-10 23:40:57 -08:00 committed by GitHub
parent ac47b8df8f
commit 3a02c7818c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2023-03-11 07:41:16 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>
3 changed files with 38 additions and 20 deletions

View File

@ -16,7 +16,7 @@ module Kernel
#++ #++
# #
def class def class
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_obj_class(self)' Primitive.cexpr! 'rb_obj_class(self)'
end end
@ -65,7 +65,7 @@ module Kernel
#++ #++
# #
def frozen? def frozen?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_obj_frozen_p(self)' Primitive.cexpr! 'rb_obj_frozen_p(self)'
end end

View File

@ -86,7 +86,7 @@ class Integer
# #
# Returns +int+, negated. # Returns +int+, negated.
def -@ def -@
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_uminus(self)' Primitive.cexpr! 'rb_int_uminus(self)'
end end
@ -102,7 +102,7 @@ class Integer
# #
# sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA" # sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
def ~ def ~
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_comp(self)' Primitive.cexpr! 'rb_int_comp(self)'
end end
@ -117,7 +117,7 @@ class Integer
# 12345.abs #=> 12345 # 12345.abs #=> 12345
# #
def abs def abs
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_abs(self)' Primitive.cexpr! 'rb_int_abs(self)'
end end
@ -163,7 +163,7 @@ class Integer
# raise "overflow" # raise "overflow"
# end # end
def bit_length def bit_length
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_bit_length(self)' Primitive.cexpr! 'rb_int_bit_length(self)'
end end
@ -172,7 +172,7 @@ class Integer
# #
# Returns +true+ if +int+ is an even number. # Returns +true+ if +int+ is an even number.
def even? def even?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_even_p(self)' Primitive.cexpr! 'rb_int_even_p(self)'
end end
@ -191,7 +191,7 @@ class Integer
# #
# Returns +true+ if +int+ is an odd number. # Returns +true+ if +int+ is an odd number.
def odd? def odd?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_odd_p(self)' Primitive.cexpr! 'rb_int_odd_p(self)'
end end
@ -226,7 +226,7 @@ class Integer
# (256**40 - 1).size #=> 40 # (256**40 - 1).size #=> 40
# #
def size def size
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_size(self)' Primitive.cexpr! 'rb_int_size(self)'
end end
@ -251,7 +251,7 @@ class Integer
# #
# Returns +true+ if +int+ has a zero value. # Returns +true+ if +int+ has a zero value.
def zero? def zero?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_int_zero_p(self)' Primitive.cexpr! 'rb_int_zero_p(self)'
end end
@ -316,12 +316,12 @@ class Float
# 34.56.abs #=> 34.56 # 34.56.abs #=> 34.56
# #
def abs def abs
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_float_abs(self)' Primitive.cexpr! 'rb_float_abs(self)'
end end
def magnitude def magnitude
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_float_abs(self)' Primitive.cexpr! 'rb_float_abs(self)'
end end
@ -332,7 +332,7 @@ class Float
# Returns +float+, negated. # Returns +float+, negated.
# #
def -@ def -@
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'rb_float_uminus(self)' Primitive.cexpr! 'rb_float_uminus(self)'
end end
@ -343,7 +343,7 @@ class Float
# Returns +true+ if +float+ is 0.0. # Returns +true+ if +float+ is 0.0.
# #
def zero? def zero?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))' Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
end end
@ -354,7 +354,7 @@ class Float
# Returns +true+ if +float+ is greater than 0. # Returns +true+ if +float+ is greater than 0.
# #
def positive? def positive?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)' Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
end end
@ -365,7 +365,7 @@ class Float
# Returns +true+ if +float+ is less than 0. # Returns +true+ if +float+ is less than 0.
# #
def negative? def negative?
Primitive.attr! 'inline' Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)' Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
end end

View File

@ -6,6 +6,7 @@ require_relative 'ruby_vm/helpers/c_escape'
SUBLIBS = {} SUBLIBS = {}
REQUIRED = {} REQUIRED = {}
BUILTIN_ATTRS = %w[inline]
def string_literal(lit, str = []) def string_literal(lit, str = [])
while lit while lit
@ -25,6 +26,17 @@ def string_literal(lit, str = [])
end end
end end
# e.g. [:symbol_literal, [:symbol, [:@ident, "inline", [19, 21]]]]
def symbol_literal(lit)
symbol_literal, symbol_lit = lit
raise "#{lit.inspect} was not :symbol_literal" if symbol_literal != :symbol_literal
symbol, ident_lit = symbol_lit
raise "#{symbol_lit.inspect} was not :symbol" if symbol != :symbol
ident, symbol_name, = ident_lit
raise "#{ident.inspect} was not :@ident" if ident != :@ident
symbol_name
end
def inline_text argc, arg1 def inline_text argc, arg1
raise "argc (#{argc}) of inline! should be 1" unless argc == 1 raise "argc (#{argc}) of inline! should be 1" unless argc == 1
arg1 = string_literal(arg1) arg1 = string_literal(arg1)
@ -32,6 +44,15 @@ def inline_text argc, arg1
arg1.join("").rstrip arg1.join("").rstrip
end end
def inline_attr(argc, arg1)
raise "argc (#{argc}) of attr! should be 1" unless argc == 1
attr = symbol_literal(arg1)
unless BUILTIN_ATTRS.include?(attr)
raise "attr (#{attr}) was not in: #{BUILTIN_ATTRS.join(', ')}"
end
attr
end
def make_cfunc_name inlines, name, lineno def make_cfunc_name inlines, name, lineno
case name case name
when /\[\]/ when /\[\]/
@ -138,10 +159,7 @@ def collect_builtin base, tree, name, bs, inlines, locals = nil
if /(.+)[\!\?]\z/ =~ func_name if /(.+)[\!\?]\z/ =~ func_name
case $1 case $1
when 'attr' when 'attr'
text = inline_text(argc, args.first) text = inline_attr(argc, args.first)
if text != 'inline'
raise "Only 'inline' is allowed to be annotated (but got: '#{text}')"
end
break break
when 'cstmt' when 'cstmt'
text = inline_text argc, args.first text = inline_text argc, args.first