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

Side by Side Diff: runtime/lib/mirrors_impl.dart

Issue 62273003: Use the runtime for generic subsitution of variable types, return types, parameter types. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // VM-specific implementation of the dart:mirrors library. 5 // VM-specific implementation of the dart:mirrors library.
6 6
7 import "dart:collection"; 7 import "dart:collection";
8 8
9 final emptyList = new UnmodifiableListView([]); 9 final emptyList = new UnmodifiableListView([]);
10 final emptyMap = new _UnmodifiableMapView({}); 10 final emptyMap = new _UnmodifiableMapView({});
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 this._reflectedType = reflectedType, 412 this._reflectedType = reflectedType,
413 this._instantiator = reflectedType, 413 this._instantiator = reflectedType,
414 super(reflectee); 414 super(reflectee);
415 415
416 final Type _reflectedType; 416 final Type _reflectedType;
417 final bool _isGeneric; 417 final bool _isGeneric;
418 final bool _isMixinTypedef; 418 final bool _isMixinTypedef;
419 final bool _isGenericDeclaration; 419 final bool _isGenericDeclaration;
420 Type _instantiator; 420 Type _instantiator;
421 421
422 TypeMirror _instantiateInContextOf(declaration) => this;
423
424 bool get hasReflectedType => !_isGenericDeclaration; 422 bool get hasReflectedType => !_isGenericDeclaration;
425 Type get reflectedType { 423 Type get reflectedType {
426 if (!hasReflectedType) { 424 if (!hasReflectedType) {
427 throw new UnsupportedError( 425 throw new UnsupportedError(
428 "Declarations of generics have no reflected type"); 426 "Declarations of generics have no reflected type");
429 } 427 }
430 return _reflectedType; 428 return _reflectedType;
431 } 429 }
432 430
433 Symbol _simpleName; 431 Symbol _simpleName;
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 MethodMirror get callMethod { 774 MethodMirror get callMethod {
777 if (_callMethod == null) { 775 if (_callMethod == null) {
778 _callMethod = this._FunctionTypeMirror_call_method(_reflectee); 776 _callMethod = this._FunctionTypeMirror_call_method(_reflectee);
779 } 777 }
780 return _callMethod; 778 return _callMethod;
781 } 779 }
782 780
783 TypeMirror _returnType = null; 781 TypeMirror _returnType = null;
784 TypeMirror get returnType { 782 TypeMirror get returnType {
785 if (_returnType == null) { 783 if (_returnType == null) {
786 _returnType = reflectType(_FunctionTypeMirror_return_type(_reflectee)); 784 _returnType = reflectType(
787 _returnType = _returnType._instantiateInContextOf(reflectType(_instantiato r)); 785 _FunctionTypeMirror_return_type(_reflectee, _instantiator));
788 } 786 }
789 return _returnType; 787 return _returnType;
790 } 788 }
791 789
792 List<ParameterMirror> _parameters = null; 790 List<ParameterMirror> _parameters = null;
793 List<ParameterMirror> get parameters { 791 List<ParameterMirror> get parameters {
794 if (_parameters == null) { 792 if (_parameters == null) {
795 _parameters = _FunctionTypeMirror_parameters(_reflectee); 793 _parameters = _FunctionTypeMirror_parameters(_reflectee);
796 _parameters.forEach((p) {
797 p._type = p.type._instantiateInContextOf(reflectType(_instantiator));
798 });
799 _parameters = new UnmodifiableListView(_parameters); 794 _parameters = new UnmodifiableListView(_parameters);
800 } 795 }
801 return _parameters; 796 return _parameters;
802 } 797 }
803 798
804 bool get isOriginalDeclaration => true; 799 bool get isOriginalDeclaration => true;
805 get originalDeclaration => this; 800 get originalDeclaration => this;
806 get typeVariables => emptyList; 801 get typeVariables => emptyList;
807 get typeArguments => emptyList; 802 get typeArguments => emptyList;
808 get metadata => emptyList; 803 get metadata => emptyList;
809 Map<Symbol, Mirror> get members => emptyMap; 804 Map<Symbol, Mirror> get members => emptyMap;
810 Map<Symbol, MethodMirror> get constructors => emptyMap; 805 Map<Symbol, MethodMirror> get constructors => emptyMap;
811 806
812 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'"; 807 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'";
813 808
814 MethodMirror _FunctionTypeMirror_call_method(reflectee) 809 MethodMirror _FunctionTypeMirror_call_method(reflectee)
815 native "FunctionTypeMirror_call_method"; 810 native "FunctionTypeMirror_call_method";
816 811
817 static Type _FunctionTypeMirror_return_type(reflectee) 812 static Type _FunctionTypeMirror_return_type(reflectee, instantiator)
818 native "FunctionTypeMirror_return_type"; 813 native "FunctionTypeMirror_return_type";
819 814
820 List<ParameterMirror> _FunctionTypeMirror_parameters(reflectee) 815 List<ParameterMirror> _FunctionTypeMirror_parameters(reflectee)
821 native "FunctionTypeMirror_parameters"; 816 native "FunctionTypeMirror_parameters";
822 } 817 }
823 818
824 abstract class _LocalDeclarationMirrorImpl extends _LocalMirrorImpl 819 abstract class _LocalDeclarationMirrorImpl extends _LocalMirrorImpl
825 implements DeclarationMirror { 820 implements DeclarationMirror {
826 _LocalDeclarationMirrorImpl(this._reflectee, this._simpleName); 821 _LocalDeclarationMirrorImpl(this._reflectee, this._simpleName);
827 822
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 && simpleName == other.simpleName 896 && simpleName == other.simpleName
902 && owner == other.owner; 897 && owner == other.owner;
903 } 898 }
904 int get hashCode => simpleName.hashCode; 899 int get hashCode => simpleName.hashCode;
905 900
906 static DeclarationMirror _TypeVariableMirror_owner(reflectee) 901 static DeclarationMirror _TypeVariableMirror_owner(reflectee)
907 native "TypeVariableMirror_owner"; 902 native "TypeVariableMirror_owner";
908 903
909 static Type _TypeVariableMirror_upper_bound(reflectee) 904 static Type _TypeVariableMirror_upper_bound(reflectee)
910 native "TypeVariableMirror_upper_bound"; 905 native "TypeVariableMirror_upper_bound";
911
912 static Type _TypeVariableMirror_instantiate_from(reflectee, instantiator)
913 native "TypeVariableMirror_instantiate_from";
914
915 TypeMirror _instantiateInContextOf(declaration) {
916 var instantiator = declaration;
917 while (instantiator is MethodMirror) instantiator = instantiator.owner;
918 if (instantiator is LibraryMirror) return this;
919 if (!(instantiator is ClassMirror || instantiator is TypedefMirror))
920 throw "UNREACHABLE";
921 if (instantiator.isOriginalDeclaration) return this;
922
923 return reflectType(
924 _TypeVariableMirror_instantiate_from(_reflectee,
925 instantiator._reflectedType));
926 }
927 } 906 }
928 907
929 908
930 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl 909 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl
931 implements TypedefMirror { 910 implements TypedefMirror {
932 _LocalTypedefMirrorImpl(reflectee, 911 _LocalTypedefMirrorImpl(reflectee,
933 this._reflectedType, 912 this._reflectedType,
934 String simpleName, 913 String simpleName,
935 this._isGeneric, 914 this._isGeneric,
936 this._isGenericDeclaration, 915 this._isGenericDeclaration,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 if(_isGenericDeclaration) { 976 if(_isGenericDeclaration) {
998 _typeArguments = emptyList; 977 _typeArguments = emptyList;
999 } else { 978 } else {
1000 _typeArguments = new UnmodifiableListView( 979 _typeArguments = new UnmodifiableListView(
1001 _LocalClassMirrorImpl._computeTypeArguments(_reflectedType)); 980 _LocalClassMirrorImpl._computeTypeArguments(_reflectedType));
1002 } 981 }
1003 } 982 }
1004 return _typeArguments; 983 return _typeArguments;
1005 } 984 }
1006 985
1007 TypeMirror _instantiateInContextOf(declaration) {
1008 var instantiator = declaration;
1009 while (instantiator is MethodMirror) instantiator = instantiator.owner;
1010 if (instantiator is LibraryMirror) return this;
1011 if (!(instantiator is ClassMirror || instantiator is TypedefMirror))
1012 throw "UNREACHABLE";
1013
1014 return reflectType(
1015 _nativeInstatniateFrom(_reflectedType, instantiator._reflectedType));
1016 }
1017
1018 String toString() => "TypedefMirror on '${_n(simpleName)}'"; 986 String toString() => "TypedefMirror on '${_n(simpleName)}'";
1019 987
1020 static _nativeReferent(reflectedType) 988 static _nativeReferent(reflectedType)
1021 native "TypedefMirror_referent"; 989 native "TypedefMirror_referent";
1022 990
1023 static _nativeInstatniateFrom(reflectedType, instantiator)
1024 native "TypedefMirror_instantiate_from";
1025
1026 static _nativeDeclaration(reflectedType) 991 static _nativeDeclaration(reflectedType)
1027 native "TypedefMirror_declaration"; 992 native "TypedefMirror_declaration";
1028 } 993 }
1029 994
1030 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl 995 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl
1031 implements LibraryMirror { 996 implements LibraryMirror {
1032 _LocalLibraryMirrorImpl(reflectee, 997 _LocalLibraryMirrorImpl(reflectee,
1033 String simpleName, 998 String simpleName,
1034 String url) 999 String url)
1035 : this.simpleName = _s(simpleName), 1000 : this.simpleName = _s(simpleName),
1036 this.uri = Uri.parse(url), 1001 this.uri = Uri.parse(url),
1037 super(reflectee); 1002 super(reflectee);
1038 1003
1039 final Symbol simpleName; 1004 final Symbol simpleName;
1040 1005
1041 // The simple name and the qualified name are the same for a library. 1006 // The simple name and the qualified name are the same for a library.
1042 Symbol get qualifiedName => simpleName; 1007 Symbol get qualifiedName => simpleName;
1043 1008
1044 // Always null for libraries. 1009 // Always null for libraries.
1045 final DeclarationMirror owner = null; 1010 final DeclarationMirror owner = null;
1046 1011
1047 // Always false for libraries. 1012 // Always false for libraries.
1048 final bool isPrivate = false; 1013 final bool isPrivate = false;
1049 1014
1050 // Always false for libraries. 1015 // Always false for libraries.
1051 final bool isTopLevel = false; 1016 final bool isTopLevel = false;
1052 1017
1018 Type get _instantiator => null;
1019
1053 SourceLocation get location { 1020 SourceLocation get location {
1054 throw new UnimplementedError('LibraryMirror.location is not implemented'); 1021 throw new UnimplementedError('LibraryMirror.location is not implemented');
1055 } 1022 }
1056 1023
1057 final Uri uri; 1024 final Uri uri;
1058 1025
1059 Map<Symbol, DeclarationMirror> _declarations; 1026 Map<Symbol, DeclarationMirror> _declarations;
1060 Map<Symbol, DeclarationMirror> get declarations { 1027 Map<Symbol, DeclarationMirror> get declarations {
1061 if (_declarations != null) return _declarations; 1028 if (_declarations != null) return _declarations;
1062 return _declarations = 1029 return _declarations =
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 1168
1202 bool get isPrivate => _n(simpleName).startsWith('_') || 1169 bool get isPrivate => _n(simpleName).startsWith('_') ||
1203 _n(constructorName).startsWith('_'); 1170 _n(constructorName).startsWith('_');
1204 1171
1205 bool get isTopLevel => owner is LibraryMirror; 1172 bool get isTopLevel => owner is LibraryMirror;
1206 1173
1207 SourceLocation get location { 1174 SourceLocation get location {
1208 throw new UnimplementedError('MethodMirror.location is not implemented'); 1175 throw new UnimplementedError('MethodMirror.location is not implemented');
1209 } 1176 }
1210 1177
1178 Type get _instantiator {
1179 var o = owner;
1180 while (o is MethodMirror) o = o.owner;
1181 return o._instantiator;
1182 }
1183
1211 TypeMirror _returnType = null; 1184 TypeMirror _returnType = null;
1212 TypeMirror get returnType { 1185 TypeMirror get returnType {
1213 if (_returnType == null) { 1186 if (_returnType == null) {
1214 if (isConstructor) { 1187 if (isConstructor) {
1215 _returnType = owner; 1188 _returnType = owner;
1216 } else { 1189 } else {
1217 _returnType = reflectType(_MethodMirror_return_type(_reflectee)); 1190 _returnType = reflectType(
1191 _MethodMirror_return_type(_reflectee, _instantiator));
1218 } 1192 }
1219 _returnType = _returnType._instantiateInContextOf(owner);
1220 } 1193 }
1221 return _returnType; 1194 return _returnType;
1222 } 1195 }
1223 1196
1224 List<ParameterMirror> _parameters = null; 1197 List<ParameterMirror> _parameters = null;
1225 List<ParameterMirror> get parameters { 1198 List<ParameterMirror> get parameters {
1226 if (_parameters == null) { 1199 if (_parameters == null) {
1227 _parameters = _MethodMirror_parameters(_reflectee); 1200 _parameters = _MethodMirror_parameters(_reflectee);
1228 _parameters = new UnmodifiableListView(_parameters); 1201 _parameters = new UnmodifiableListView(_parameters);
1229 } 1202 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1269 } else { 1242 } else {
1270 _simpleName = _s(ownerName + "." + cn); 1243 _simpleName = _s(ownerName + "." + cn);
1271 } 1244 }
1272 } 1245 }
1273 1246
1274 String toString() => "MethodMirror on '${MirrorSystem.getName(simpleName)}'"; 1247 String toString() => "MethodMirror on '${MirrorSystem.getName(simpleName)}'";
1275 1248
1276 static dynamic _MethodMirror_owner(reflectee) 1249 static dynamic _MethodMirror_owner(reflectee)
1277 native "MethodMirror_owner"; 1250 native "MethodMirror_owner";
1278 1251
1279 static dynamic _MethodMirror_return_type(reflectee) 1252 static dynamic _MethodMirror_return_type(reflectee, instantiator)
1280 native "MethodMirror_return_type"; 1253 native "MethodMirror_return_type";
1281 1254
1282 List<ParameterMirror> _MethodMirror_parameters(reflectee) 1255 List<ParameterMirror> _MethodMirror_parameters(reflectee)
1283 native "MethodMirror_parameters"; 1256 native "MethodMirror_parameters";
1284 1257
1285 static String _MethodMirror_source(reflectee) 1258 static String _MethodMirror_source(reflectee)
1286 native "MethodMirror_source"; 1259 native "MethodMirror_source";
1287 } 1260 }
1288 1261
1289 class _LocalVariableMirrorImpl extends _LocalDeclarationMirrorImpl 1262 class _LocalVariableMirrorImpl extends _LocalDeclarationMirrorImpl
(...skipping 11 matching lines...) Expand all
1301 final bool isFinal; 1274 final bool isFinal;
1302 1275
1303 bool get isPrivate => _n(simpleName).startsWith('_'); 1276 bool get isPrivate => _n(simpleName).startsWith('_');
1304 1277
1305 bool get isTopLevel => owner is LibraryMirror; 1278 bool get isTopLevel => owner is LibraryMirror;
1306 1279
1307 SourceLocation get location { 1280 SourceLocation get location {
1308 throw new UnimplementedError('VariableMirror.location is not implemented'); 1281 throw new UnimplementedError('VariableMirror.location is not implemented');
1309 } 1282 }
1310 1283
1284 Type get _instantiator {
1285 return owner._instantiator;
1286 }
1287
1311 TypeMirror _type; 1288 TypeMirror _type;
1312 TypeMirror get type { 1289 TypeMirror get type {
1313 if (_type == null) { 1290 if (_type == null) {
1314 _type = reflectType(_VariableMirror_type(_reflectee)); 1291 _type = reflectType(_VariableMirror_type(_reflectee, _instantiator));
1315 _type = _type._instantiateInContextOf(owner);
1316 } 1292 }
1317 return _type; 1293 return _type;
1318 } 1294 }
1319 1295
1320 String toString() => "VariableMirror on '${MirrorSystem.getName(simpleName)}'" ; 1296 String toString() => "VariableMirror on '${MirrorSystem.getName(simpleName)}'" ;
1321 1297
1322 static _VariableMirror_type(reflectee) 1298 static _VariableMirror_type(reflectee, instantiator)
1323 native "VariableMirror_type"; 1299 native "VariableMirror_type";
1324 } 1300 }
1325 1301
1326 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl 1302 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl
1327 implements ParameterMirror { 1303 implements ParameterMirror {
1328 _LocalParameterMirrorImpl(reflectee, 1304 _LocalParameterMirrorImpl(reflectee,
1329 String simpleName, 1305 String simpleName,
1330 DeclarationMirror owner, 1306 DeclarationMirror owner,
1331 this._position, 1307 this._position,
1332 this.isOptional, 1308 this.isOptional,
(...skipping 21 matching lines...) Expand all
1354 } 1330 }
1355 if (_defaultValue == null) { 1331 if (_defaultValue == null) {
1356 _defaultValue = reflect(_defaultValueReflectee); 1332 _defaultValue = reflect(_defaultValueReflectee);
1357 } 1333 }
1358 return _defaultValue; 1334 return _defaultValue;
1359 } 1335 }
1360 1336
1361 bool get hasDefaultValue => _defaultValueReflectee != null; 1337 bool get hasDefaultValue => _defaultValueReflectee != null;
1362 1338
1363 List<InstanceMirror> get metadata { 1339 List<InstanceMirror> get metadata {
1364 if ( _unmirroredMetadata == null) return emptyList; 1340 if (_unmirroredMetadata == null) return emptyList;
1365 return new UnmodifiableListView(_unmirroredMetadata.map(reflect)); 1341 return new UnmodifiableListView(_unmirroredMetadata.map(reflect));
1366 } 1342 }
1367 1343
1344 Type get _instantiator {
1345 var o = owner;
1346 while (o is MethodMirror) o = o.owner;
1347 return o._instantiator;
1348 }
1349
1368 TypeMirror _type = null; 1350 TypeMirror _type = null;
1369 TypeMirror get type { 1351 TypeMirror get type {
1370 if (_type == null) { 1352 if (_type == null) {
1371 _type = reflectType(_ParameterMirror_type(_reflectee, _position)); 1353 _type = reflectType(
1372 _type = _type._instantiateInContextOf(owner); 1354 _ParameterMirror_type(_reflectee, _position, _instantiator));
1373 } 1355 }
1374 return _type; 1356 return _type;
1375 } 1357 }
1376 1358
1377 String toString() => "ParameterMirror on '${_n(simpleName)}'"; 1359 String toString() => "ParameterMirror on '${_n(simpleName)}'";
1378 1360
1379 static Type _ParameterMirror_type(_reflectee, _position) 1361 static Type _ParameterMirror_type(_reflectee, _position, instantiator)
1380 native "ParameterMirror_type"; 1362 native "ParameterMirror_type";
1381 } 1363 }
1382 1364
1383 class _SpecialTypeMirrorImpl extends _LocalMirrorImpl 1365 class _SpecialTypeMirrorImpl extends _LocalMirrorImpl
1384 implements TypeMirror, DeclarationMirror { 1366 implements TypeMirror, DeclarationMirror {
1385 _SpecialTypeMirrorImpl(String name) : simpleName = _s(name); 1367 _SpecialTypeMirrorImpl(String name) : simpleName = _s(name);
1386 1368
1387 final bool isPrivate = false; 1369 final bool isPrivate = false;
1388 final DeclarationMirror owner = null; 1370 final DeclarationMirror owner = null;
1389 final Symbol simpleName; 1371 final Symbol simpleName;
(...skipping 17 matching lines...) Expand all
1407 bool operator ==(other) { 1389 bool operator ==(other) {
1408 if (other is! _SpecialTypeMirrorImpl) { 1390 if (other is! _SpecialTypeMirrorImpl) {
1409 return false; 1391 return false;
1410 } 1392 }
1411 return this.simpleName == other.simpleName; 1393 return this.simpleName == other.simpleName;
1412 } 1394 }
1413 1395
1414 int get hashCode => simpleName.hashCode; 1396 int get hashCode => simpleName.hashCode;
1415 1397
1416 String toString() => "TypeMirror on '${_n(simpleName)}'"; 1398 String toString() => "TypeMirror on '${_n(simpleName)}'";
1417
1418 TypeMirror _instantiateInContextOf(declaration) => this;
1419 } 1399 }
1420 1400
1421 class _Mirrors { 1401 class _Mirrors {
1422 // Does a port refer to our local isolate? 1402 // Does a port refer to our local isolate?
1423 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; 1403 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort';
1424 1404
1425 static MirrorSystem _currentMirrorSystem = null; 1405 static MirrorSystem _currentMirrorSystem = null;
1426 1406
1427 // Creates a new local MirrorSystem. 1407 // Creates a new local MirrorSystem.
1428 static MirrorSystem makeLocalMirrorSystem() 1408 static MirrorSystem makeLocalMirrorSystem()
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1468 if (typeMirror == null) { 1448 if (typeMirror == null) {
1469 typeMirror = makeLocalTypeMirror(key); 1449 typeMirror = makeLocalTypeMirror(key);
1470 _instanitationCache[key] = typeMirror; 1450 _instanitationCache[key] = typeMirror;
1471 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { 1451 if (typeMirror is ClassMirror && !typeMirror._isGeneric) {
1472 _declarationCache[key] = typeMirror; 1452 _declarationCache[key] = typeMirror;
1473 } 1453 }
1474 } 1454 }
1475 return typeMirror; 1455 return typeMirror;
1476 } 1456 }
1477 } 1457 }
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698