Commit 2578cf78

2578cf78555cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #325 from ryanseys/fix/class-threeq-primitives

fix(codegen): Class#=== for primitive type names (case-when)

Details

Commit
2578cf78555c
Parent
497d69700e72
Merge parent
bcc60b692718

spinel_codegen.rbmodifiedView file

@@ -3070,6 +3070,9 @@
30703070 if mname == "cover?"
30713071 return "bool"
30723072 end
3073+ if mname == "==="
3074+ return "bool"
3075+ end
30733076 if mname == "match?"
30743077 return "bool"
30753078 end
@@ -20757,6 +20760,93 @@
2075720760 end
2075820761 end
2075920762
20763+ # `Class === expr` — case-when membership for primitive type names.
20764+ # Resolve at compile time based on the arg's inferred type. Has to
20765+ # run before compile_operator_expr / compile_eq (both of which would
20766+ # mishandle the constant receiver as a regular C identifier and emit
20767+ # `<undeclared identifier> == arg`).
20768+ if mname == "===" && recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")
20769+ rcname_threeq = resolve_const_ref_name(recv)
20770+ args_id_threeq = @nd_arguments[nid]
20771+ if args_id_threeq >= 0
20772+ a_threeq = get_args(args_id_threeq)
20773+ if a_threeq.length > 0
20774+ at_threeq = infer_type(a_threeq[0])
20775+ if rcname_threeq == "Integer"
20776+ return at_threeq == "int" ? "TRUE" : "FALSE"
20777+ end
20778+ if rcname_threeq == "Float"
20779+ return at_threeq == "float" ? "TRUE" : "FALSE"
20780+ end
20781+ if rcname_threeq == "Numeric"
20782+ return (at_threeq == "int" || at_threeq == "float") ? "TRUE" : "FALSE"
20783+ end
20784+ if rcname_threeq == "Comparable"
20785+ # Comparable is included by Integer, Float, String, Symbol.
20786+ return (at_threeq == "int" || at_threeq == "float" ||
20787+ at_threeq == "string" || at_threeq == "mutable_str" ||
20788+ at_threeq == "symbol") ? "TRUE" : "FALSE"
20789+ end
20790+ if rcname_threeq == "String"
20791+ return at_threeq == "string" || at_threeq == "mutable_str" ? "TRUE" : "FALSE"
20792+ end
20793+ if rcname_threeq == "Symbol"
20794+ return at_threeq == "symbol" ? "TRUE" : "FALSE"
20795+ end
20796+ if rcname_threeq == "Array"
20797+ return is_array_type(at_threeq) == 1 ? "TRUE" : "FALSE"
20798+ end
20799+ if rcname_threeq == "Hash"
20800+ # Hash type names all end in `_hash` (sym_int_hash, str_str_hash,
20801+ # sym_poly_hash, etc.); poly_hash also matches the runtime form.
20802+ is_hash = 0
20803+ if at_threeq == "poly_hash"
20804+ is_hash = 1
20805+ elsif at_threeq.length > 5 && at_threeq[at_threeq.length - 5, 5] == "_hash"
20806+ is_hash = 1
20807+ end
20808+ return is_hash == 1 ? "TRUE" : "FALSE"
20809+ end
20810+ if rcname_threeq == "Range"
20811+ return at_threeq == "range" ? "TRUE" : "FALSE"
20812+ end
20813+ if rcname_threeq == "TrueClass"
20814+ # TrueClass === arg matches only the literal `true` value.
20815+ # Compile-time decide for TrueNode/FalseNode literals; emit a
20816+ # runtime check for other bool-typed values; FALSE for non-bool.
20817+ if @nd_type[a_threeq[0]] == "TrueNode"
20818+ return "TRUE"
20819+ end
20820+ if @nd_type[a_threeq[0]] == "FalseNode"
20821+ return "FALSE"
20822+ end
20823+ if at_threeq == "bool"
20824+ return "((" + compile_expr(a_threeq[0]) + ") == TRUE)"
20825+ end
20826+ return "FALSE"
20827+ end
20828+ if rcname_threeq == "FalseClass"
20829+ if @nd_type[a_threeq[0]] == "FalseNode"
20830+ return "TRUE"
20831+ end
20832+ if @nd_type[a_threeq[0]] == "TrueNode"
20833+ return "FALSE"
20834+ end
20835+ if at_threeq == "bool"
20836+ return "((" + compile_expr(a_threeq[0]) + ") == FALSE)"
20837+ end
20838+ return "FALSE"
20839+ end
20840+ if rcname_threeq == "NilClass"
20841+ return at_threeq == "nil" ? "TRUE" : "FALSE"
20842+ end
20843+ if rcname_threeq == "Object" || rcname_threeq == "Kernel" || rcname_threeq == "BasicObject"
20844+ return "TRUE"
20845+ end
20846+ end
20847+ end
20848+ end
20849+
2076020850 # Operator on object with custom method
2076120851 if is_operator_name(mname) == 1
2076220852 r = compile_obj_operator_expr(nid, mname, recv)