Commit 6e1c13a7
6e1c13a7f0e2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add built-in class reopening support (open_class.rb passes)
- SelfNode in a built-in class method now returns the correct scalar type (TY_STRING for class String, TY_INT for Integer, etc.)
- infer_call: implicit-self calls to built-in type methods inside a reopened built-in class resolve to correct return types
- infer_call: recv>=0 calls on scalar built-in types dispatch to user-defined reopening methods
- emit_method_signature: built-in class self params emit as scalar C types (const char *, mrb_int, double, sp_PolyArray *) not as sp_XX *
- emit_call_expr: implicit-self calls to built-in string/int/float/symbol methods inside a reopened class emit sp_str_xxx(self) etc.
- emit_call_expr: receiver calls on built-in scalar types dispatch to user-defined reopening methods (sp_String_shout(x) etc.)
- is_builtin_reopen(): guard typedef/struct/new/scan emission for built-in class names to avoid conflicts with runtime definitions
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 6e1c13a7f0e2
- Parent
- 20269ccf9c71
src/analyze.cmodifiedView file
@@ -600,6 +600,23 @@
600600
}601601
}602602
603+
/* built-in class reopening: look up user-defined methods on scalar built-in types */604+
if (recv >= 0) {605+
const char *oc_cn = NULL;606+
if (rt == TY_STRING) oc_cn = "String";607+
else if (rt == TY_INT) oc_cn = "Integer";608+
else if (rt == TY_FLOAT) oc_cn = "Float";609+
else if (rt == TY_SYMBOL) oc_cn = "Symbol";610+
else if (rt == TY_BOOL) oc_cn = "TrueClass";611+
if (oc_cn) {612+
int oc_ci = comp_class_index(c, oc_cn);613+
if (oc_ci >= 0) {614+
int oc_mi = comp_method_in_chain(c, oc_ci, name, NULL);615+
if (oc_mi >= 0) return method_call_ret(c, oc_mi, id);616+
}617+
}618+
}619+
603620
/* obj.method(...) -> the method's return type (walks the superclass chain) */604621
if (recv >= 0 && ty_is_object(rt)) {605622
int cid = ty_object_class(rt);@@ -673,6 +690,84 @@
673690
}674691
return r;675692
}693+
/* Built-in class reopening: implicit self → delegate to built-in type lookup */694+
if (mi < 0 && !self->is_cmethod) {695+
const char *bcn = c->classes[self->class_id].name;696+
TyKind brt = TY_UNKNOWN;697+
if (!strcmp(bcn, "String")) brt = TY_STRING;698+
else if (!strcmp(bcn, "Integer")) brt = TY_INT;699+
else if (!strcmp(bcn, "Float")) brt = TY_FLOAT;700+
else if (!strcmp(bcn, "Symbol")) brt = TY_SYMBOL;701+
if (brt != TY_UNKNOWN) {702+
/* Temporarily set rt to the built-in type and recursively call infer_call703+
is not safe. Instead inline key return types for common method names. */704+
if (brt == TY_STRING) {705+
if (!strcmp(name, "upcase") || !strcmp(name, "downcase") ||706+
!strcmp(name, "capitalize") || !strcmp(name, "reverse") || !strcmp(name, "strip") ||707+
!strcmp(name, "lstrip") || !strcmp(name, "rstrip") || !strcmp(name, "chomp") ||708+
!strcmp(name, "chop") || !strcmp(name, "dup") || !strcmp(name, "clone") ||709+
!strcmp(name, "to_s") || !strcmp(name, "inspect") || !strcmp(name, "succ") ||710+
!strcmp(name, "next") || !strcmp(name, "chr") || !strcmp(name, "encode") ||711+
!strcmp(name, "b") || !strcmp(name, "force_encoding") || !strcmp(name, "scrub") ||712+
!strcmp(name, "squeeze") || !strcmp(name, "tr") || !strcmp(name, "delete"))713+
return TY_STRING;714+
if ((!strcmp(name, "+") || !strcmp(name, "*")) && argc >= 1) return TY_STRING;715+
if (!strcmp(name, "gsub") || !strcmp(name, "sub")) return TY_STRING;716+
if (!strcmp(name, "[]") || !strcmp(name, "slice")) return TY_STRING;717+
if (!strcmp(name, "length") || !strcmp(name, "size") || !strcmp(name, "bytesize") ||718+
!strcmp(name, "to_i") || !strcmp(name, "count") || !strcmp(name, "ord") ||719+
!strcmp(name, "hex") || !strcmp(name, "oct") || !strcmp(name, "rindex") ||720+
!strcmp(name, "index"))721+
return TY_INT;722+
if (!strcmp(name, "to_f")) return TY_FLOAT;723+
if (!strcmp(name, "to_sym")) return TY_SYMBOL;724+
if (!strcmp(name, "empty?") || !strcmp(name, "include?") ||725+
!strcmp(name, "start_with?") || !strcmp(name, "end_with?") ||726+
!strcmp(name, "==") || !strcmp(name, "!="))727+
return TY_BOOL;728+
if (!strcmp(name, "split") || !strcmp(name, "chars") || !strcmp(name, "lines") ||729+
!strcmp(name, "bytes"))730+
return TY_STR_ARRAY;731+
}732+
else if (brt == TY_INT) {733+
if (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") ||734+
!strcmp(name, "/") || !strcmp(name, "%") || !strcmp(name, "**") ||735+
!strcmp(name, "abs") || !strcmp(name, "succ") || !strcmp(name, "next") ||736+
!strcmp(name, "pred") || !strcmp(name, "gcd") || !strcmp(name, "lcm") ||737+
!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^") ||738+
!strcmp(name, "<<") || !strcmp(name, ">>"))739+
return TY_INT;740+
if (!strcmp(name, "to_f")) return TY_FLOAT;741+
if (!strcmp(name, "to_s")) return TY_STRING;742+
if (!strcmp(name, "to_r")) return TY_POLY;743+
if (!strcmp(name, "odd?") || !strcmp(name, "even?") || !strcmp(name, "zero?") ||744+
!strcmp(name, "==") || !strcmp(name, "!=") || !strcmp(name, "<") ||745+
!strcmp(name, "<=") || !strcmp(name, ">") || !strcmp(name, ">="))746+
return TY_BOOL;747+
}748+
else if (brt == TY_FLOAT) {749+
if (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") ||750+
!strcmp(name, "/") || !strcmp(name, "abs") || !strcmp(name, "floor") ||751+
!strcmp(name, "ceil") || !strcmp(name, "round") || !strcmp(name, "truncate"))752+
return TY_FLOAT;753+
if (!strcmp(name, "to_i")) return TY_INT;754+
if (!strcmp(name, "to_s")) return TY_STRING;755+
if (!strcmp(name, "zero?") || !strcmp(name, "nan?") || !strcmp(name, "infinite?") ||756+
!strcmp(name, "finite?") || !strcmp(name, "==") || !strcmp(name, "!=") ||757+
!strcmp(name, "<") || !strcmp(name, "<=") || !strcmp(name, ">") ||758+
!strcmp(name, ">="))759+
return TY_BOOL;760+
}761+
else if (brt == TY_SYMBOL) {762+
if (!strcmp(name, "to_s") || !strcmp(name, "id2name") || !strcmp(name, "inspect"))763+
return TY_STRING;764+
if (!strcmp(name, "to_sym") || !strcmp(name, "itself")) return TY_SYMBOL;765+
if (!strcmp(name, "length") || !strcmp(name, "size")) return TY_INT;766+
if (!strcmp(name, "empty?") || !strcmp(name, "==") || !strcmp(name, "!="))767+
return TY_BOOL;768+
}769+
}770+
}676771
/* Method defined only in descendants (not in base chain):677772
unify return types of all descendant implementations. */678773
if (self->is_cmethod) {@@ -1560,7 +1655,15 @@
15601655
}15611656
if (!strcmp(ty, "SelfNode")) {15621657
Scope *s = comp_scope_of(c, id);1563-
return s->class_id >= 0 ? ty_object(s->class_id) : TY_UNKNOWN;1658+
if (s->class_id < 0) return TY_UNKNOWN;1659+
const char *cn = c->classes[s->class_id].name;1660+
if (!strcmp(cn, "String")) return TY_STRING;1661+
if (!strcmp(cn, "Integer")) return TY_INT;1662+
if (!strcmp(cn, "Float")) return TY_FLOAT;1663+
if (!strcmp(cn, "Symbol")) return TY_SYMBOL;1664+
if (!strcmp(cn, "TrueClass") || !strcmp(cn, "FalseClass") || !strcmp(cn, "NilClass")) return TY_BOOL;1665+
if (!strcmp(cn, "Array")) return TY_POLY_ARRAY;1666+
return ty_object(s->class_id);15641667
}15651668
if (!strcmp(ty, "InstanceVariableReadNode")) {15661669
const char *nm = nt_str(nt, id, "name");src/codegen.cmodifiedView file
Loading diff…