This adds a few helpers to Type which all return Options representing what is known, from a Ruby perspective, about the type.
This includes:
known_class_of: If known, the class represented by this type
known_value_type: If known, the T_ value type
known_exact_value: If known, the exact VALUE represented by this type (currently this is only available for true/false/nil)
known_truthy: If known, whether or not this value evaluates as true (not false or nil)
The goal of this is to abstract away the specifics of the mappings between types wherever possible from the codegen. For example previously by introducing Type::CString as a more specific version of Type::TString, uses of Type::TString in codegen needed to be updated to check either case. Now by using known_value_type, at least in theory we can introduce new types with minimal (if any) codegen changes.
I think rust's Option type allows us to represent this uncertainty fairly well, and should help avoid mistakes, and the matching using this turned out pretty cleanly.
YJIT: Add known_* helpers for Type (#6208)
This adds a few helpers to Type which all return Options representing
what is known, from a Ruby perspective, about the type.
This includes:
(currently this is only available for true/false/nil)
(not false or nil)
The goal of this is to abstract away the specifics of the mappings
between types wherever possible from the codegen. For example previously
by introducing Type::CString as a more specific version of
Type::TString, uses of Type::TString in codegen needed to be updated to
check either case. Now by using known_value_type, at least in theory we
can introduce new types with minimal (if any) codegen changes.
I think rust's Option type allows us to represent this uncertainty
fairly well, and should help avoid mistakes, and the matching using this
turned out pretty cleanly.
YJIT: Use known_value_type for checktype
YJIT: Use known_value_type for T_STRING check
YJIT: Use known_class_of in guard_known_klass
YJIT: Use known truthyness in jit_rb_obj_not
YJIT: Rename known_class_of => known_class