Skip to content

Commit 5e7336e

Browse files
committed
rustc: Add a shallow type equality function, not used yet
1 parent 735435b commit 5e7336e

File tree

1 file changed

+321
-0
lines changed

1 file changed

+321
-0
lines changed

src/comp/middle/ty.rs

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import std._str;
22
import std._uint;
33
import std._vec;
4+
import std.Box;
45
import std.UFind;
56
import std.map;
67
import std.map.hashmap;
@@ -862,6 +863,326 @@ fn hash_type_structure(&sty st) -> uint {
862863
fn hash_ty(&@t typ) -> uint { ret typ.hash; }
863864

864865

866+
// Type equality. This function is private to this module (and slow); external
867+
// users should use `eq_ty()` instead.
868+
fn equal_type_structures(&sty a, &sty b) -> bool {
869+
fn equal_ty(@t a, @t b) -> bool { ret Box.ptr_eq[t](a, b); }
870+
871+
fn equal_proto(ast.proto a, ast.proto b) -> bool {
872+
alt (a) {
873+
case (ast.proto_iter) {
874+
alt (b) {
875+
case (ast.proto_iter) { ret true; }
876+
case (_) { ret false; }
877+
}
878+
}
879+
case (ast.proto_fn) {
880+
alt (b) {
881+
case (ast.proto_fn) { ret true; }
882+
case (_) { ret false; }
883+
}
884+
}
885+
}
886+
}
887+
888+
fn equal_abi(ast.native_abi a, ast.native_abi b) -> bool {
889+
alt (a) {
890+
case (ast.native_abi_rust) {
891+
alt (b) {
892+
case (ast.native_abi_rust) { ret true; }
893+
case (_) { ret false; }
894+
}
895+
}
896+
case (ast.native_abi_cdecl) {
897+
alt (b) {
898+
case (ast.native_abi_cdecl) { ret true; }
899+
case (_) { ret false; }
900+
}
901+
}
902+
case (ast.native_abi_llvm) {
903+
alt (b) {
904+
case (ast.native_abi_llvm) { ret true; }
905+
case (_) { ret false; }
906+
}
907+
}
908+
}
909+
}
910+
911+
fn equal_mut(ast.mutability a, ast.mutability b) -> bool {
912+
alt (a) {
913+
case (ast.mut) {
914+
alt (b) {
915+
case (ast.mut) { ret true; }
916+
case (_) { ret false; }
917+
}
918+
}
919+
case (ast.imm) {
920+
alt (b) {
921+
case (ast.imm) { ret true; }
922+
case (_) { ret false; }
923+
}
924+
}
925+
case (ast.maybe_mut) {
926+
alt (b) {
927+
case (ast.maybe_mut) { ret true; }
928+
case (_) { ret false; }
929+
}
930+
}
931+
}
932+
}
933+
934+
fn equal_mode(ast.mode a, ast.mode b) -> bool {
935+
alt (a) {
936+
case (ast.val) {
937+
alt (b) {
938+
case (ast.val) { ret true; }
939+
case (_) { ret false; }
940+
}
941+
}
942+
case (ast.alias) {
943+
alt (b) {
944+
case (ast.alias) { ret true; }
945+
case (_) { ret false; }
946+
}
947+
}
948+
}
949+
}
950+
951+
fn equal_mt(&mt a, &mt b) -> bool {
952+
ret equal_mut(a.mut, b.mut) && equal_ty(a.ty, b.ty);
953+
}
954+
955+
fn equal_fn(vec[arg] args_a, @t rty_a,
956+
vec[arg] args_b, @t rty_b) -> bool {
957+
if (!equal_ty(rty_a, rty_b)) { ret false; }
958+
959+
auto len = _vec.len[arg](args_a);
960+
if (len != _vec.len[arg](args_b)) { ret false; }
961+
auto i = 0u;
962+
while (i < len) {
963+
auto arg_a = args_a.(i); auto arg_b = args_b.(i);
964+
if (!equal_mode(arg_a.mode, arg_b.mode) ||
965+
!equal_ty(arg_a.ty, arg_b.ty)) {
966+
ret false;
967+
}
968+
i += 1u;
969+
}
970+
ret true;
971+
}
972+
973+
fn equal_def(ast.def_id did_a, ast.def_id did_b) -> bool {
974+
ret did_a._0 == did_b._0 && did_a._1 == did_b._1;
975+
}
976+
977+
alt (a) {
978+
case (ty_nil) {
979+
alt (b) {
980+
case (ty_nil) { ret true; }
981+
case (_) { ret false; }
982+
}
983+
}
984+
case (ty_bool) {
985+
alt (b) {
986+
case (ty_bool) { ret true; }
987+
case (_) { ret false; }
988+
}
989+
}
990+
case (ty_int) {
991+
alt (b) {
992+
case (ty_int) { ret true; }
993+
case (_) { ret false; }
994+
}
995+
}
996+
case (ty_float) {
997+
alt (b) {
998+
case (ty_float) { ret true; }
999+
case (_) { ret false; }
1000+
}
1001+
}
1002+
case (ty_uint) {
1003+
alt (b) {
1004+
case (ty_uint) { ret true; }
1005+
case (_) { ret false; }
1006+
}
1007+
}
1008+
case (ty_machine(?tm_a)) {
1009+
alt (b) {
1010+
case (ty_machine(?tm_b)) {
1011+
ret hash_type_structure(a) == hash_type_structure(b);
1012+
}
1013+
case (_) { ret false; }
1014+
}
1015+
}
1016+
case (ty_char) {
1017+
alt (b) {
1018+
case (ty_char) { ret true; }
1019+
case (_) { ret false; }
1020+
}
1021+
}
1022+
case (ty_str) {
1023+
alt (b) {
1024+
case (ty_str) { ret true; }
1025+
case (_) { ret false; }
1026+
}
1027+
}
1028+
case (ty_tag(?id_a, ?tys_a)) {
1029+
alt (b) {
1030+
case (ty_tag(?id_b, ?tys_b)) {
1031+
if (id_a != id_b) { ret false; }
1032+
1033+
auto len = _vec.len[@ty.t](tys_a);
1034+
if (len != _vec.len[@ty.t](tys_b)) { ret false; }
1035+
auto i = 0u;
1036+
while (i < len) {
1037+
if (!equal_ty(tys_a.(i), tys_b.(i))) { ret false; }
1038+
i += 1u;
1039+
}
1040+
ret true;
1041+
}
1042+
case (_) { ret false; }
1043+
}
1044+
}
1045+
case (ty_box(?mt_a)) {
1046+
alt (b) {
1047+
case (ty_box(?mt_b)) { ret equal_mt(mt_a, mt_b); }
1048+
case (_) { ret false; }
1049+
}
1050+
}
1051+
case (ty_vec(?mt_a)) {
1052+
alt (b) {
1053+
case (ty_vec(?mt_b)) { ret equal_mt(mt_a, mt_b); }
1054+
case (_) { ret false; }
1055+
}
1056+
}
1057+
case (ty_port(?t_a)) {
1058+
alt (b) {
1059+
case (ty_port(?t_b)) { ret equal_ty(t_a, t_b); }
1060+
case (_) { ret false; }
1061+
}
1062+
}
1063+
case (ty_chan(?t_a)) {
1064+
alt (b) {
1065+
case (ty_chan(?t_b)) { ret equal_ty(t_a, t_b); }
1066+
case (_) { ret false; }
1067+
}
1068+
}
1069+
case (ty_task) {
1070+
alt (b) {
1071+
case (ty_task) { ret true; }
1072+
case (_) { ret false; }
1073+
}
1074+
}
1075+
case (ty_tup(?mts_a)) {
1076+
alt (b) {
1077+
case (ty_tup(?mts_b)) {
1078+
auto len = _vec.len[mt](mts_a);
1079+
if (len != _vec.len[mt](mts_b)) { ret false; }
1080+
auto i = 0u;
1081+
while (i < len) {
1082+
if (!equal_mt(mts_a.(i), mts_b.(i))) { ret false; }
1083+
i += 1u;
1084+
}
1085+
ret true;
1086+
}
1087+
case (_) { ret false; }
1088+
}
1089+
}
1090+
case (ty_rec(?flds_a)) {
1091+
alt (b) {
1092+
case (ty_rec(?flds_b)) {
1093+
auto len = _vec.len[field](flds_a);
1094+
if (len != _vec.len[field](flds_b)) { ret false; }
1095+
auto i = 0u;
1096+
while (i < len) {
1097+
auto fld_a = flds_a.(i); auto fld_b = flds_b.(i);
1098+
if (!_str.eq(fld_a.ident, fld_b.ident) ||
1099+
!equal_mt(fld_a.mt, fld_b.mt)) {
1100+
ret false;
1101+
}
1102+
i += 1u;
1103+
}
1104+
ret true;
1105+
}
1106+
case (_) { ret false; }
1107+
}
1108+
}
1109+
case (ty_fn(?p_a, ?args_a, ?rty_a)) {
1110+
alt (b) {
1111+
case (ty_fn(?p_b, ?args_b, ?rty_b)) {
1112+
ret equal_proto(p_a, p_b) &&
1113+
equal_fn(args_a, rty_a, args_b, rty_b);
1114+
}
1115+
case (_) { ret false; }
1116+
}
1117+
}
1118+
case (ty_native_fn(?abi_a, ?args_a, ?rty_a)) {
1119+
alt (b) {
1120+
case (ty_native_fn(?abi_b, ?args_b, ?rty_b)) {
1121+
ret equal_abi(abi_a, abi_b) &&
1122+
equal_fn(args_a, rty_a, args_b, rty_b);
1123+
}
1124+
case (_) { ret false; }
1125+
}
1126+
}
1127+
case (ty_obj(?methods_a)) {
1128+
alt (b) {
1129+
case (ty_obj(?methods_b)) {
1130+
auto len = _vec.len[method](methods_a);
1131+
if (len != _vec.len[method](methods_b)) { ret false; }
1132+
auto i = 0u;
1133+
while (i < len) {
1134+
auto m_a = methods_a.(i); auto m_b = methods_b.(i);
1135+
if (!equal_proto(m_a.proto, m_b.proto) ||
1136+
!_str.eq(m_a.ident, m_b.ident) ||
1137+
equal_fn(m_a.inputs, m_a.output,
1138+
m_b.inputs, m_b.output)) {
1139+
ret false;
1140+
}
1141+
}
1142+
ret true;
1143+
}
1144+
case (_) { ret false; }
1145+
}
1146+
}
1147+
case (ty_var(?v_a)) {
1148+
alt (b) {
1149+
case (ty_var(?v_b)) { ret v_a == v_b; }
1150+
case (_) { ret false; }
1151+
}
1152+
}
1153+
case (ty_local(?did_a)) {
1154+
alt (b) {
1155+
case (ty_local(?did_b)) { ret equal_def(did_a, did_b); }
1156+
case (_) { ret false; }
1157+
}
1158+
}
1159+
case (ty_param(?pid_a)) {
1160+
alt (b) {
1161+
case (ty_param(?pid_b)) { ret pid_a == pid_b; }
1162+
case (_) { ret false; }
1163+
}
1164+
}
1165+
case (ty_bound_param(?pid_a)) {
1166+
alt (b) {
1167+
case (ty_bound_param(?pid_b)) { ret pid_a == pid_b; }
1168+
case (_) { ret false; }
1169+
}
1170+
}
1171+
case (ty_type) {
1172+
alt (b) {
1173+
case (ty_type) { ret true; }
1174+
case (_) { ret false; }
1175+
}
1176+
}
1177+
case (ty_native) {
1178+
alt (b) {
1179+
case (ty_native) { ret true; }
1180+
case (_) { ret false; }
1181+
}
1182+
}
1183+
}
1184+
}
1185+
8651186

8661187
fn ty_is_simple(&@t a) -> bool {
8671188
// a "simple" type is one in which the hash

0 commit comments

Comments
 (0)