Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Unified Diff: compiler/lib/implementation/rtt.js

Issue 8845002: Function type checking: Part Deux (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiler/lib/implementation/rtt.js
diff --git a/compiler/lib/implementation/rtt.js b/compiler/lib/implementation/rtt.js
index daea1603a37a7ad39eb8f5847f152b6ffefd17c6..3b6a6a93f3e6d87432285b09e85c388ccad41577 100644
--- a/compiler/lib/implementation/rtt.js
+++ b/compiler/lib/implementation/rtt.js
@@ -10,16 +10,20 @@
* @param {string} classkey
* @param {string=} typekey
* @param {Array.<RTT>=} typeargs
+ * @param {RTT} returnType
+ * @param {bool} functionType
*/
-function RTT(classkey, typekey, typeargs) {
+function RTT(classkey, typekey, typeargs, returnType, functionType) {
this.classKey = classkey;
this.typeKey = typekey ? typekey : classkey;
this.typeArgs = typeargs;
+ this.returnType = returnType; // key for the return type
this.implementedTypes = {};
+ this.functionType = functionType;
// Add self
this.implementedTypes[classkey] = this;
// Add Object
- if (classkey != $cls('Object')) {
+ if (!functionType && classkey != $cls('Object')) {
this.implementedTypes[$cls('Object')] = RTT.objectType;
}
}
@@ -39,6 +43,7 @@ RTT.prototype.toString = function() { return this.typeKey; }
*/
RTT.prototype.implementedBy = function(value){
return (value == null) ? RTT.nullInstanceOf(this) :
+ this.functionType ? this.implementedByTypeFunc(value) :
this.implementedByType(RTT.getTypeInfo(value));
};
@@ -53,6 +58,11 @@ function $mapLookup(map, key) {
return map.hasOwnProperty(key) ? map[key] : null;
}
+RTT.prototype.implementedByTypeSwitch = function(value){
+ return this.functionType ? this.implementedByTypeFunc(value) :
+ this.implementedByType(value);
+};
+
/**
* @param {!RTT} other
* @return {boolean} Whether this type is implement by other
@@ -67,7 +77,32 @@ RTT.prototype.implementedByType = function(otherType) {
}
if (targetTypeInfo.typeArgs && this.typeArgs) {
for(var i = this.typeArgs.length - 1; i >= 0; i--) {
- if (!this.typeArgs[i].implementedByType(targetTypeInfo.typeArgs[i])) {
+ if (!this.typeArgs[i].implementedByTypeSwitch(targetTypeInfo.typeArgs[i])) {
+ return false;
+ }
+ }
+ }
+ return true;
+};
+
+/**
+ * @param {!RTT} other
+ * @return {boolean} Whether this type is assignable by other
+ */
+RTT.prototype.assignableByType = function(otherType) {
+ if (otherType === this || otherType === RTT.dynamicType || this === RTT.dynamicType) {
+ return true;
+ }
+ var targetTypeInfo = $mapLookup(otherType.implementedTypes, this.classKey);
+ if (targetTypeInfo == null) {
+ targetTypeInfo = $mapLookup(this.implementedTypes, otherType.classKey);
+ if (targetTypeInfo == null) {
+ return false;
+ }
+ }
+ if (targetTypeInfo.typeArgs && this.typeArgs) {
+ for(var i = this.typeArgs.length - 1; i >= 0; i--) {
+ if (!this.typeArgs[i].assignableByType(targetTypeInfo.typeArgs[i])) {
return false;
}
}
@@ -75,6 +110,44 @@ RTT.prototype.implementedByType = function(otherType) {
return true;
};
+
+/**
+ * @param {!RTT} other
+ * @return {boolean} Whether this type is implemented by other
+ */
+RTT.prototype.implementedByTypeFunc = function(otherType) {
+ if (otherType.$lookupRTT) {
+ otherType = otherType.$lookupRTT();
+ } else if (!(otherType instanceof RTT)) {
+ return false;
+ }
+ if (otherType === this || otherType === RTT.dynamicType) {
+ return true;
+ }
+ var props = Object.getOwnPropertyNames(otherType.implementedTypes);
+ NEXT_PROPERTY: for (var i = 0 ; i < props.length; i++) {
+ var mapped = otherType.implementedTypes[props[i]];
+ if (mapped.returnType && this.returnType &&
+ !this.returnType.assignableByType(mapped.returnType)) {
+ continue;
+ }
+ if (mapped.typeArgs && this.typeArgs) {
+ if (this.typeArgs.length != mapped.typeArgs.length) {
+ continue;
+ }
+ for (var x = this.typeArgs.length - 1; x >= 0; x--) {
+ if (!this.typeArgs[x].assignableByType(mapped.typeArgs[x])) {
+ continue NEXT_PROPERTY;
+ }
+ }
+ } else if (mapped.typeArgs || this.typeArgs) {
+ return false;
+ }
+ return true;
+ }
+ return false;
+};
+
/**
* @return {string} the class name associated with this type
*/
@@ -134,15 +207,37 @@ RTT.create = function(name, implementsSupplier, typeArgs) {
};
/**
+ * @param {Array.<RTT>=} typeArgs
+ * @param {<RTT>=} returnType (if defined)
+ * @return {RTT} The RTT information object
+ */
+RTT.createFunction = function(typeArgs, returnType) {
+ var name = $cls("Function");
+ var typekey = RTT.getTypeKey(name, typeArgs, returnType);
+ var rtt = $mapLookup(RTT.types, typekey);
+ if (rtt) {
+ return rtt;
+ }
+ var classkey = RTT.getTypeKey(name);
+ rtt = new RTT(classkey, typekey, typeArgs, returnType, true);
+ RTT.types[typekey] = rtt;
+ return rtt;
+};
+
+/**
* @param {string} classkey
* @param {Array.<(RTT|string)>=} typeargs
+ * @param {string} returntype
* @return {string}
*/
-RTT.getTypeKey = function(classkey, typeargs) {
+RTT.getTypeKey = function(classkey, typeargs, returntype) {
var key = classkey;
if (typeargs) {
key += "<" + typeargs.join(",") + ">";
}
+ if (returntype) {
+ key += "-><" + returntype + ">";
+ }
return key;
};
@@ -219,6 +314,10 @@ RTT.objectType = new ImplementsAll($cls('Object'));
RTT.dynamicType = new ImplementsAll($cls('Dynamic'));
RTT.placeholderType = new ImplementsAll($cls('::'));
+function getDynamic() {
mmendez 2011/12/07 20:03:00 Do you still need this function to pass optimized
codefu 2011/12/07 21:44:54 Not anymore. John Lenz solved this by pointing out
+ return RTT.dynamicType;
+}
+
/**
* Checks that a value is assignable to an expected type, and either returns that
* value if it is, or else throws a TypeMismatchException.

Powered by Google App Engine
This is Rietveld 408576698