Skip to content

Commit 6a832f3

Browse files
committed
use id instead of type string to uniquely identify type
1 parent 4ea101e commit 6a832f3

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

compiler/prelude/types.go

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var $ifaceKeyFor = function(x) {
5151
5252
var $identity = function(x) { return x; };
5353
54+
var $typeIDCounter = 0;
55+
5456
var $newType = function(size, kind, string, name, pkg, constructor) {
5557
var typ;
5658
switch(kind) {
@@ -362,6 +364,8 @@ var $newType = function(size, kind, string, name, pkg, constructor) {
362364
$panic(new $String("invalid kind: " + kind));
363365
}
364366
367+
typ.id = $typeIDCounter;
368+
$typeIDCounter++;
365369
typ.size = size;
366370
typ.kind = kind;
367371
typ.string = string;
@@ -496,11 +500,11 @@ var $toNativeArray = function(elemKind, array) {
496500
};
497501
var $arrayTypes = {};
498502
var $arrayType = function(elem, len) {
499-
var string = "[" + len + "]" + elem.string;
500-
var typ = $arrayTypes[string];
503+
var typeKey = elem.id + "$" + len;
504+
var typ = $arrayTypes[typeKey];
501505
if (typ === undefined) {
502-
typ = $newType(12, $kindArray, string, "", "", null);
503-
$arrayTypes[string] = typ;
506+
typ = $newType(12, $kindArray, "[" + len + "]" + elem.string, "", "", null);
507+
$arrayTypes[typeKey] = typ;
504508
typ.init(elem, len);
505509
}
506510
return typ;
@@ -520,37 +524,39 @@ var $chanType = function(elem, sendOnly, recvOnly) {
520524
521525
var $funcTypes = {};
522526
var $funcType = function(params, results, variadic) {
523-
var paramTypes = $mapArray(params, function(p) { return p.string; });
524-
if (variadic) {
525-
paramTypes[paramTypes.length - 1] = "..." + paramTypes[paramTypes.length - 1].substr(2);
526-
}
527-
var string = "func(" + paramTypes.join(", ") + ")";
528-
if (results.length === 1) {
529-
string += " " + results[0].string;
530-
} else if (results.length > 1) {
531-
string += " (" + $mapArray(results, function(r) { return r.string; }).join(", ") + ")";
532-
}
533-
var typ = $funcTypes[string];
527+
var typeKey = $mapArray(params, function(p) { return p.id; }).join(",") + "$" + $mapArray(results, function(r) { return r.id; }).join(",") + "$" + variadic;
528+
var typ = $funcTypes[typeKey];
534529
if (typ === undefined) {
530+
var paramTypes = $mapArray(params, function(p) { return p.string; });
531+
if (variadic) {
532+
paramTypes[paramTypes.length - 1] = "..." + paramTypes[paramTypes.length - 1].substr(2);
533+
}
534+
var string = "func(" + paramTypes.join(", ") + ")";
535+
if (results.length === 1) {
536+
string += " " + results[0].string;
537+
} else if (results.length > 1) {
538+
string += " (" + $mapArray(results, function(r) { return r.string; }).join(", ") + ")";
539+
}
535540
typ = $newType(4, $kindFunc, string, "", "", null);
536-
$funcTypes[string] = typ;
541+
$funcTypes[typeKey] = typ;
537542
typ.init(params, results, variadic);
538543
}
539544
return typ;
540545
};
541546
542547
var $interfaceTypes = {};
543548
var $interfaceType = function(methods) {
544-
var string = "interface {}";
545-
if (methods.length !== 0) {
546-
string = "interface { " + $mapArray(methods, function(m) {
547-
return (m.pkg !== "" ? m.pkg + "." : "") + m.name + m.typ.string.substr(4);
548-
}).join("; ") + " }";
549-
}
550-
var typ = $interfaceTypes[string];
549+
var typeKey = $mapArray(methods, function(m) { return m.pkg + "," + m.name + "," + m.typ.id; }).join("$");
550+
var typ = $interfaceTypes[typeKey];
551551
if (typ === undefined) {
552+
var string = "interface {}";
553+
if (methods.length !== 0) {
554+
string = "interface { " + $mapArray(methods, function(m) {
555+
return (m.pkg !== "" ? m.pkg + "." : "") + m.name + m.typ.string.substr(4);
556+
}).join("; ") + " }";
557+
}
552558
typ = $newType(8, $kindInterface, string, "", "", null);
553-
$interfaceTypes[string] = typ;
559+
$interfaceTypes[typeKey] = typ;
554560
typ.init(methods);
555561
}
556562
return typ;
@@ -569,11 +575,11 @@ var $Map = function() {};
569575
})();
570576
var $mapTypes = {};
571577
var $mapType = function(key, elem) {
572-
var string = "map[" + key.string + "]" + elem.string;
573-
var typ = $mapTypes[string];
578+
var typeKey = key.id + "$" + elem.id;
579+
var typ = $mapTypes[typeKey];
574580
if (typ === undefined) {
575-
typ = $newType(4, $kindMap, string, "", "", null);
576-
$mapTypes[string] = typ;
581+
typ = $newType(4, $kindMap, "map[" + key.string + "]" + elem.string, "", "", null);
582+
$mapTypes[typeKey] = typ;
577583
typ.init(key, elem);
578584
}
579585
return typ;
@@ -602,10 +608,10 @@ var $indexPtr = function(array, index, constructor) {
602608
};
603609
604610
var $sliceType = function(elem) {
605-
var typ = elem.Slice;
611+
var typ = elem.slice;
606612
if (typ === undefined) {
607613
typ = $newType(12, $kindSlice, "[]" + elem.string, "", "", null);
608-
elem.Slice = typ;
614+
elem.slice = typ;
609615
typ.init(elem);
610616
}
611617
return typ;
@@ -625,14 +631,15 @@ var $makeSlice = function(typ, length, capacity) {
625631
626632
var $structTypes = {};
627633
var $structType = function(fields) {
628-
var string = "struct { " + $mapArray(fields, function(f) {
629-
return f.name + " " + f.typ.string + (f.tag !== "" ? (" \"" + f.tag.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"") : "");
630-
}).join("; ") + " }";
631-
if (fields.length === 0) {
632-
string = "struct {}";
633-
}
634-
var typ = $structTypes[string];
634+
var typeKey = $mapArray(fields, function(f) { return f.name + "," + f.typ.id + "," + f.tag; }).join("$");
635+
var typ = $structTypes[typeKey];
635636
if (typ === undefined) {
637+
var string = "struct { " + $mapArray(fields, function(f) {
638+
return f.name + " " + f.typ.string + (f.tag !== "" ? (" \"" + f.tag.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"") : "");
639+
}).join("; ") + " }";
640+
if (fields.length === 0) {
641+
string = "struct {}";
642+
}
636643
typ = $newType(0, $kindStruct, string, "", "", function() {
637644
this.$val = this;
638645
for (var i = 0; i < fields.length; i++) {
@@ -641,7 +648,7 @@ var $structType = function(fields) {
641648
this[f.prop] = arg !== undefined ? arg : f.typ.zero();
642649
}
643650
});
644-
$structTypes[string] = typ;
651+
$structTypes[typeKey] = typ;
645652
typ.init(fields);
646653
}
647654
return typ;

0 commit comments

Comments
 (0)