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

Side by Side Diff: packages/analyzer/lib/src/task/strong/rules.dart

Issue 1521693002: Roll Observatory deps (charted -> ^0.3.0) (Closed) Base URL: https://chromium.googlesource.com/external/github.com/dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 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 unified diff | Download patch
« no previous file with comments | « packages/analyzer/lib/src/task/strong/info.dart ('k') | packages/analyzer/lib/task/model.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be
6 // refactored to fit into analyzer.
7 library analyzer.src.task.strong.rules;
8
9 import 'package:analyzer/src/generated/ast.dart';
10 import 'package:analyzer/src/generated/element.dart';
11 import 'package:analyzer/src/generated/resolver.dart';
12
13 import 'info.dart';
14
15 // TODO(jmesserly): this entire file needs to be removed in favor of TypeSystem.
16
17 final _objectMap = new Expando('providerToObjectMap');
18 Map<String, DartType> getObjectMemberMap(TypeProvider typeProvider) {
19 var map = _objectMap[typeProvider] as Map<String, DartType>;
20 if (map == null) {
21 map = <String, DartType>{};
22 _objectMap[typeProvider] = map;
23 var objectType = typeProvider.objectType;
24 var element = objectType.element;
25 // Only record methods (including getters) with no parameters. As parameter s are contravariant wrt
26 // type, using Object's version may be too strict.
27 // Add instance methods.
28 element.methods.where((method) => !method.isStatic).forEach((method) {
29 map[method.name] = method.type;
30 });
31 // Add getters.
32 element.accessors
33 .where((member) => !member.isStatic && member.isGetter)
34 .forEach((member) {
35 map[member.name] = member.type.returnType;
36 });
37 }
38 return map;
39 }
40
41 class TypeRules {
42 final TypeProvider provider;
43
44 /// Map of fields / properties / methods on Object.
45 final Map<String, DartType> objectMembers;
46
47 DownwardsInference inferrer;
48
49 TypeRules(TypeProvider provider)
50 : provider = provider,
51 objectMembers = getObjectMemberMap(provider) {
52 inferrer = new DownwardsInference(this);
53 }
54
55 /// Given a type t, if t is an interface type with a call method
56 /// defined, return the function type for the call method, otherwise
57 /// return null.
58 FunctionType getCallMethodType(DartType t) {
59 if (t is InterfaceType) {
60 ClassElement element = t.element;
61 InheritanceManager manager = new InheritanceManager(element.library);
62 FunctionType callType = manager.lookupMemberType(t, "call");
63 return callType;
64 }
65 return null;
66 }
67
68 /// Given an expression, return its type assuming it is
69 /// in the caller position of a call (that is, accounting
70 /// for the possibility of a call method). Returns null
71 /// if expression is not statically callable.
72 FunctionType getTypeAsCaller(Expression applicand) {
73 var t = getStaticType(applicand);
74 if (t is InterfaceType) {
75 return getCallMethodType(t);
76 }
77 if (t is FunctionType) return t;
78 return null;
79 }
80
81 /// Gets the expected return type of the given function [body], either from
82 /// a normal return/yield, or from a yield*.
83 DartType getExpectedReturnType(FunctionBody body, {bool yieldStar: false}) {
84 FunctionType functionType;
85 var parent = body.parent;
86 if (parent is Declaration) {
87 functionType = elementType(parent.element);
88 } else {
89 assert(parent is FunctionExpression);
90 functionType = getStaticType(parent);
91 }
92
93 var type = functionType.returnType;
94
95 InterfaceType expectedType = null;
96 if (body.isAsynchronous) {
97 if (body.isGenerator) {
98 // Stream<T> -> T
99 expectedType = provider.streamType;
100 } else {
101 // Future<T> -> T
102 // TODO(vsm): Revisit with issue #228.
103 expectedType = provider.futureType;
104 }
105 } else {
106 if (body.isGenerator) {
107 // Iterable<T> -> T
108 expectedType = provider.iterableType;
109 } else {
110 // T -> T
111 return type;
112 }
113 }
114 if (yieldStar) {
115 if (type.isDynamic) {
116 // Ensure it's at least a Stream / Iterable.
117 return expectedType.substitute4([provider.dynamicType]);
118 } else {
119 // Analyzer will provide a separate error if expected type
120 // is not compatible with type.
121 return type;
122 }
123 }
124 if (type.isDynamic) {
125 return type;
126 } else if (type is InterfaceType && type.element == expectedType.element) {
127 return type.typeArguments[0];
128 } else {
129 // Malformed type - fallback on analyzer error.
130 return null;
131 }
132 }
133
134 DartType getStaticType(Expression expr) {
135 return expr.staticType ?? provider.dynamicType;
136 }
137
138 bool _isBottom(DartType t, {bool dynamicIsBottom: false}) {
139 if (t.isDynamic && dynamicIsBottom) return true;
140 // TODO(vsm): We need direct support for non-nullability in DartType.
141 // This should check on "true/nonnullable" Bottom
142 if (t.isBottom) return true;
143 return false;
144 }
145
146 bool _isTop(DartType t, {bool dynamicIsBottom: false}) {
147 if (t.isDynamic && !dynamicIsBottom) return true;
148 if (t.isObject) return true;
149 return false;
150 }
151
152 bool _anyParameterType(FunctionType ft, bool predicate(DartType t)) {
153 return ft.normalParameterTypes.any(predicate) ||
154 ft.optionalParameterTypes.any(predicate) ||
155 ft.namedParameterTypes.values.any(predicate);
156 }
157
158 // TODO(leafp): Revisit this.
159 bool isGroundType(DartType t) {
160 if (t is TypeParameterType) return false;
161 if (_isTop(t)) return true;
162
163 if (t is FunctionType) {
164 if (!_isTop(t.returnType) ||
165 _anyParameterType(t, (pt) => !_isBottom(pt, dynamicIsBottom: true))) {
166 return false;
167 } else {
168 return true;
169 }
170 }
171
172 if (t is InterfaceType) {
173 var typeArguments = t.typeArguments;
174 for (var typeArgument in typeArguments) {
175 if (!_isTop(typeArgument)) return false;
176 }
177 return true;
178 }
179
180 // We should not see any other type aside from malformed code.
181 return false;
182 }
183
184 /// Check that f1 is a subtype of f2. [ignoreReturn] is used in the DDC
185 /// checker to determine whether f1 would be a subtype of f2 if the return
186 /// type of f1 is set to match f2's return type.
187 // [fuzzyArrows] indicates whether or not the f1 and f2 should be
188 // treated as fuzzy arrow types (and hence dynamic parameters to f2 treated as
189 // bottom).
190 bool isFunctionSubTypeOf(FunctionType f1, FunctionType f2,
191 {bool fuzzyArrows: true, bool ignoreReturn: false}) {
192 final r1s = f1.normalParameterTypes;
193 final o1s = f1.optionalParameterTypes;
194 final n1s = f1.namedParameterTypes;
195 final r2s = f2.normalParameterTypes;
196 final o2s = f2.optionalParameterTypes;
197 final n2s = f2.namedParameterTypes;
198 final ret1 = ignoreReturn ? f2.returnType : f1.returnType;
199 final ret2 = f2.returnType;
200
201 // A -> B <: C -> D if C <: A and
202 // either D is void or B <: D
203 if (!ret2.isVoid && !isSubTypeOf(ret1, ret2)) return false;
204
205 // Reject if one has named and the other has optional
206 if (n1s.length > 0 && o2s.length > 0) return false;
207 if (n2s.length > 0 && o1s.length > 0) return false;
208
209 // f2 has named parameters
210 if (n2s.length > 0) {
211 // Check that every named parameter in f2 has a match in f1
212 for (String k2 in n2s.keys) {
213 if (!n1s.containsKey(k2)) return false;
214 if (!isSubTypeOf(n2s[k2], n1s[k2],
215 dynamicIsBottom: fuzzyArrows)) return false;
216 }
217 }
218 // If we get here, we either have no named parameters,
219 // or else the named parameters match and we have no optional
220 // parameters
221
222 // If f1 has more required parameters, reject
223 if (r1s.length > r2s.length) return false;
224
225 // If f2 has more required + optional parameters, reject
226 if (r2s.length + o2s.length > r1s.length + o1s.length) return false;
227
228 // The parameter lists must look like the following at this point
229 // where rrr is a region of required, and ooo is a region of optionals.
230 // f1: rrr ooo ooo ooo
231 // f2: rrr rrr ooo
232 int rr = r1s.length; // required in both
233 int or = r2s.length - r1s.length; // optional in f1, required in f2
234 int oo = o2s.length; // optional in both
235
236 for (int i = 0; i < rr; ++i) {
237 if (!isSubTypeOf(r2s[i], r1s[i],
238 dynamicIsBottom: fuzzyArrows)) return false;
239 }
240 for (int i = 0, j = rr; i < or; ++i, ++j) {
241 if (!isSubTypeOf(r2s[j], o1s[i],
242 dynamicIsBottom: fuzzyArrows)) return false;
243 }
244 for (int i = or, j = 0; i < oo; ++i, ++j) {
245 if (!isSubTypeOf(o2s[j], o1s[i],
246 dynamicIsBottom: fuzzyArrows)) return false;
247 }
248 return true;
249 }
250
251 bool _isInterfaceSubTypeOf(InterfaceType i1, InterfaceType i2) {
252 if (i1 == i2) return true;
253
254 if (i1.element == i2.element) {
255 List<DartType> tArgs1 = i1.typeArguments;
256 List<DartType> tArgs2 = i2.typeArguments;
257
258 // TODO(leafp): Verify that this is always true
259 // Do raw types get filled in?
260 assert(tArgs1.length == tArgs2.length);
261
262 for (int i = 0; i < tArgs1.length; i++) {
263 DartType t1 = tArgs1[i];
264 DartType t2 = tArgs2[i];
265 if (!isSubTypeOf(t1, t2)) return false;
266 }
267 return true;
268 }
269
270 if (i2.isDartCoreFunction) {
271 if (i1.element.getMethod("call") != null) return true;
272 }
273
274 if (i1 == provider.objectType) return false;
275
276 if (_isInterfaceSubTypeOf(i1.superclass, i2)) return true;
277
278 for (final parent in i1.interfaces) {
279 if (_isInterfaceSubTypeOf(parent, i2)) return true;
280 }
281
282 for (final parent in i1.mixins) {
283 if (_isInterfaceSubTypeOf(parent, i2)) return true;
284 }
285
286 return false;
287 }
288
289 bool isSubTypeOf(DartType t1, DartType t2, {bool dynamicIsBottom: false}) {
290 if (t1 == t2) return true;
291
292 // Trivially true.
293 if (_isTop(t2, dynamicIsBottom: dynamicIsBottom) ||
294 _isBottom(t1, dynamicIsBottom: dynamicIsBottom)) {
295 return true;
296 }
297
298 // Trivially false.
299 if (_isTop(t1, dynamicIsBottom: dynamicIsBottom) ||
300 _isBottom(t2, dynamicIsBottom: dynamicIsBottom)) {
301 return false;
302 }
303
304 // The null type is a subtype of any nullable type, which is all Dart types.
305 // TODO(vsm): Note, t1.isBottom still allows for null confusingly.
306 // _isBottom(t1) does not necessarily imply t1.isBottom if there are
307 // nonnullable types in the system.
308 if (t1.isBottom) {
309 return true;
310 }
311
312 // S <: T where S is a type variable
313 // T is not dynamic or object (handled above)
314 // S != T (handled above)
315 // So only true if bound of S is S' and
316 // S' <: T
317 if (t1 is TypeParameterType) {
318 DartType bound = t1.element.bound;
319 if (bound == null) return false;
320 return isSubTypeOf(bound, t2);
321 }
322
323 if (t2 is TypeParameterType) {
324 return false;
325 }
326
327 if (t2.isDartCoreFunction) {
328 if (t1 is FunctionType) return true;
329 if (t1.element is ClassElement) {
330 if ((t1.element as ClassElement).getMethod("call") != null) return true;
331 }
332 }
333
334 // "Traditional" name-based subtype check.
335 if (t1 is InterfaceType && t2 is InterfaceType) {
336 return _isInterfaceSubTypeOf(t1, t2);
337 }
338
339 if (t1 is! FunctionType && t2 is! FunctionType) return false;
340
341 if (t1 is InterfaceType && t2 is FunctionType) {
342 var callType = getCallMethodType(t1);
343 if (callType == null) return false;
344 return isFunctionSubTypeOf(callType, t2);
345 }
346
347 if (t1 is FunctionType && t2 is InterfaceType) {
348 return false;
349 }
350
351 // Functions
352 // Note: it appears under the hood all Dart functions map to a class /
353 // hidden type that:
354 // (a) subtypes Object (an internal _FunctionImpl in the VM)
355 // (b) implements Function
356 // (c) provides standard Object members (hashCode, toString)
357 // (d) contains private members (corresponding to _FunctionImpl?)
358 // (e) provides a call method to handle the actual function invocation
359 //
360 // The standard Dart subtyping rules are structural in nature. I.e.,
361 // bivariant on arguments and return type.
362 //
363 // The below tries for a more traditional subtyping rule:
364 // - covariant on return type
365 // - contravariant on parameters
366 // - 'sensible' (?) rules on optional and/or named params
367 // but doesn't properly mix with class subtyping. I suspect Java 8 lambdas
368 // essentially map to dynamic (and rely on invokedynamic) due to similar
369 // issues.
370 return isFunctionSubTypeOf(t1 as FunctionType, t2 as FunctionType);
371 }
372
373 bool isAssignable(DartType t1, DartType t2) {
374 return isSubTypeOf(t1, t2);
375 }
376
377 // Produce a coercion which coerces something of type fromT
378 // to something of type toT.
379 // If wrap is true and both are function types, a closure
380 // wrapper coercion is produced using _wrapTo (see above)
381 // Returns the error coercion if the types cannot be coerced
382 // according to our current criteria.
383 Coercion _coerceTo(DartType fromT, DartType toT) {
384 // We can use anything as void
385 if (toT.isVoid) return Coercion.identity(toT);
386
387 // fromT <: toT, no coercion needed
388 if (isSubTypeOf(fromT, toT)) return Coercion.identity(toT);
389
390 // For now, reject conversions between function types and
391 // call method objects. We could choose to allow casts here.
392 // Wrapping a function type to assign it to a call method
393 // object will never succeed. Wrapping the other way could
394 // be allowed.
395 if ((fromT is FunctionType && getCallMethodType(toT) != null) ||
396 (toT is FunctionType && getCallMethodType(fromT) != null)) {
397 return Coercion.error();
398 }
399
400 // Downcast if toT <: fromT
401 if (isSubTypeOf(toT, fromT)) return Coercion.cast(fromT, toT);
402
403 // Downcast if toT <===> fromT
404 // The intention here is to allow casts that are sideways in the restricted
405 // type system, but allowed in the regular dart type system, since these
406 // are likely to succeed. The canonical example is List<dynamic> and
407 // Iterable<T> for some concrete T (e.g. Object). These are unrelated
408 // in the restricted system, but List<dynamic> <: Iterable<T> in dart.
409 if (fromT.isAssignableTo(toT)) {
410 return Coercion.cast(fromT, toT);
411 }
412 return Coercion.error();
413 }
414
415 StaticInfo checkAssignment(Expression expr, DartType toT) {
416 final fromT = getStaticType(expr);
417 final Coercion c = _coerceTo(fromT, toT);
418 if (c is Identity) return null;
419 if (c is CoercionError) return new StaticTypeError(this, expr, toT);
420 var reason = null;
421
422 var errors = <String>[];
423 var ok = inferrer.inferExpression(expr, toT, errors);
424 if (ok) return InferredType.create(this, expr, toT);
425 reason = (errors.isNotEmpty) ? errors.first : null;
426
427 if (c is Cast) return DownCast.create(this, expr, c, reason: reason);
428 assert(false);
429 return null;
430 }
431
432 DartType elementType(Element e) {
433 if (e == null) {
434 // Malformed code - just return dynamic.
435 return provider.dynamicType;
436 }
437 return (e as dynamic).type;
438 }
439
440 bool _isLibraryPrefix(Expression node) =>
441 node is SimpleIdentifier && node.staticElement is PrefixElement;
442
443 /// Returns `true` if the target expression is dynamic.
444 bool isDynamicTarget(Expression node) {
445 if (node == null) return false;
446
447 if (_isLibraryPrefix(node)) return false;
448
449 // Null type happens when we have unknown identifiers, like a dart: import
450 // that doesn't resolve.
451 var type = node.staticType;
452 return type == null || type.isDynamic;
453 }
454
455 /// Returns `true` if the expression is a dynamic function call or method
456 /// invocation.
457 bool isDynamicCall(Expression call) {
458 var ft = getTypeAsCaller(call);
459 // TODO(leafp): This will currently return true if t is Function
460 // This is probably the most correct thing to do for now, since
461 // this code is also used by the back end. Maybe revisit at some
462 // point?
463 if (ft == null) return true;
464 // Dynamic as the parameter type is treated as bottom. A function with
465 // a dynamic parameter type requires a dynamic call in general.
466 // However, as an optimization, if we have an original definition, we know
467 // dynamic is reified as Object - in this case a regular call is fine.
468 if (call is SimpleIdentifier) {
469 var element = call.staticElement;
470 if (element is FunctionElement || element is MethodElement) {
471 // An original declaration.
472 return false;
473 }
474 }
475
476 return _anyParameterType(ft, (pt) => pt.isDynamic);
477 }
478 }
479
480 class DownwardsInference {
481 final TypeRules rules;
482
483 DownwardsInference(this.rules);
484
485 /// Called for each list literal which gets inferred
486 void annotateListLiteral(ListLiteral e, List<DartType> targs) {}
487
488 /// Called for each map literal which gets inferred
489 void annotateMapLiteral(MapLiteral e, List<DartType> targs) {}
490
491 /// Called for each new/const which gets inferred
492 void annotateInstanceCreationExpression(
493 InstanceCreationExpression e, List<DartType> targs) {}
494
495 /// Called for cast from dynamic required for inference to succeed
496 void annotateCastFromDynamic(Expression e, DartType t) {}
497
498 /// Called for each function expression return type inferred
499 void annotateFunctionExpression(FunctionExpression e, DartType returnType) {}
500
501 /// Downward inference
502 bool inferExpression(Expression e, DartType t, List<String> errors) {
503 // Don't cast top level expressions, only sub-expressions
504 return _inferExpression(e, t, errors, cast: false);
505 }
506
507 /// Downward inference
508 bool _inferExpression(Expression e, DartType t, List<String> errors,
509 {cast: true}) {
510 if (e is ConditionalExpression) {
511 return _inferConditionalExpression(e, t, errors);
512 }
513 if (e is ParenthesizedExpression) {
514 return _inferParenthesizedExpression(e, t, errors);
515 }
516 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true;
517 if (cast && rules.getStaticType(e).isDynamic) {
518 annotateCastFromDynamic(e, t);
519 return true;
520 }
521 if (e is FunctionExpression) return _inferFunctionExpression(e, t, errors);
522 if (e is ListLiteral) return _inferListLiteral(e, t, errors);
523 if (e is MapLiteral) return _inferMapLiteral(e, t, errors);
524 if (e is NamedExpression) return _inferNamedExpression(e, t, errors);
525 if (e is InstanceCreationExpression) {
526 return _inferInstanceCreationExpression(e, t, errors);
527 }
528 errors.add("$e cannot be typed as $t");
529 return false;
530 }
531
532 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype
533 /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'>
534 /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm'
535 /// to derive a concrete instantation for I of the form <T0, ..., Tn>,
536 /// such that I<T0, .., Tn> <: t2
537 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) {
538 if (t1 == t2) return t2.typeArguments;
539 var tArgs1 = t1.typeArguments;
540 var tArgs2 = t2.typeArguments;
541 // If t1 isn't a raw type, bail out
542 if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) return null;
543
544 // This is our inferred type argument list. We start at all dynamic,
545 // and fill in with inferred types when we reach a match.
546 var actuals =
547 new List<DartType>.filled(tArgs1.length, rules.provider.dynamicType);
548
549 // When we find the supertype of t1 with the same
550 // classname as t2 (see below), we have the following:
551 // If t1 is an instantiation of a class T1<X0, ..., Xn>
552 // and t2 is an instantiation of a class T2<Y0, ...., Ym>
553 // of the form t2 = T2<S0, ..., Sm>
554 // then we want to choose instantiations for the Xi
555 // T0, ..., Tn such that T1<T0, ..., Tn> <: t2 .
556 // To find this, we simply instantate T1 with
557 // X0, ..., Xn, and then find its superclass
558 // T2<T0', ..., Tn'>. We then solve the constraint
559 // set T0' <: S0, ..., Tn' <: Sn for the Xi.
560 // Currently, we only handle constraints where
561 // the Ti' is one of the Xi'. If there are multiple
562 // constraints on some Xi, we choose the lower of the
563 // two (if it exists).
564 bool permute(List<DartType> permutedArgs) {
565 if (permutedArgs == null) return false;
566 var ps = t1.typeParameters;
567 var ts = ps.map((p) => p.type).toList();
568 for (int i = 0; i < permutedArgs.length; i++) {
569 var tVar = permutedArgs[i];
570 var tActual = tArgs2[i];
571 var index = ts.indexOf(tVar);
572 if (index >= 0 && rules.isSubTypeOf(tActual, actuals[index])) {
573 actuals[index] = tActual;
574 }
575 }
576 return actuals.any((x) => !x.isDynamic);
577 }
578
579 // Look for the first supertype of t1 with the same class name as t2.
580 bool match(InterfaceType t1) {
581 if (t1.element == t2.element) {
582 return permute(t1.typeArguments);
583 }
584
585 if (t1 == rules.provider.objectType) return false;
586
587 if (match(t1.superclass)) return true;
588
589 for (final parent in t1.interfaces) {
590 if (match(parent)) return true;
591 }
592
593 for (final parent in t1.mixins) {
594 if (match(parent)) return true;
595 }
596 return false;
597 }
598
599 // We have that t1 = T1<dynamic, ..., dynamic>.
600 // To match t1 against t2, we use the uninstantiated version
601 // of t1, essentially treating it as an instantiation with
602 // fresh variables, and solve for the variables.
603 // t1.element.type will be of the form T1<X0, ..., Xn>
604 if (!match(t1.element.type)) return null;
605 var newT1 = t1.element.type.substitute4(actuals);
606 // If we found a solution, return it.
607 if (rules.isSubTypeOf(newT1, t2)) return actuals;
608 return null;
609 }
610
611 /// These assume that e is not already a subtype of t
612
613 bool _inferConditionalExpression(
614 ConditionalExpression e, DartType t, errors) {
615 return _inferExpression(e.thenExpression, t, errors) &&
616 _inferExpression(e.elseExpression, t, errors);
617 }
618
619 bool _inferParenthesizedExpression(
620 ParenthesizedExpression e, DartType t, errors) {
621 return _inferExpression(e.expression, t, errors);
622 }
623
624 bool _inferInstanceCreationExpression(
625 InstanceCreationExpression e, DartType t, errors) {
626 var arguments = e.argumentList.arguments;
627 var rawType = rules.getStaticType(e);
628 // rawType is the instantiated type of the instance
629 if (rawType is! InterfaceType) return false;
630 var type = (rawType as InterfaceType);
631 if (type.typeParameters == null ||
632 type.typeParameters.length == 0) return false;
633 if (e.constructorName.type == null) return false;
634 // classTypeName is the type name of the class being instantiated
635 var classTypeName = e.constructorName.type;
636 // Check that we were not passed any type arguments
637 if (classTypeName.typeArguments != null) return false;
638 // Infer type arguments
639 if (t is! InterfaceType) return false;
640 var targs = _matchTypes(type, t);
641 if (targs == null) return false;
642 if (e.staticElement == null) return false;
643 var constructorElement = e.staticElement;
644 // From the constructor element get:
645 // the instantiated type of the constructor, then
646 // the uninstantiated element for the constructor, then
647 // the uninstantiated type for the constructor
648 var rawConstructorElement =
649 constructorElement.type.element as ConstructorElement;
650 var baseType = rawConstructorElement.type;
651 if (baseType == null) return false;
652 // From the interface type (instantiated), get:
653 // the uninstantiated element, then
654 // the uninstantiated type, then
655 // the type arguments (aka the type parameters)
656 var tparams = type.element.type.typeArguments;
657 // Take the uninstantiated constructor type, and replace the type
658 // parameters with the inferred arguments.
659 var fType = baseType.substitute2(targs, tparams);
660 {
661 var rTypes = fType.normalParameterTypes;
662 var oTypes = fType.optionalParameterTypes;
663 var pTypes = new List.from(rTypes)..addAll(oTypes);
664 var pArgs = arguments.where((x) => x is! NamedExpression);
665 var pi = 0;
666 for (var arg in pArgs) {
667 if (pi >= pTypes.length) return false;
668 var argType = pTypes[pi];
669 if (!_inferExpression(arg, argType, errors)) return false;
670 pi++;
671 }
672 var nTypes = fType.namedParameterTypes;
673 for (var arg0 in arguments) {
674 if (arg0 is! NamedExpression) continue;
675 var arg = arg0 as NamedExpression;
676 SimpleIdentifier nameNode = arg.name.label;
677 String name = nameNode.name;
678 var argType = nTypes[name];
679 if (argType == null) return false;
680 if (!_inferExpression(arg, argType, errors)) return false;
681 }
682 }
683 annotateInstanceCreationExpression(e, targs);
684 return true;
685 }
686
687 bool _inferNamedExpression(NamedExpression e, DartType t, errors) {
688 return _inferExpression(e.expression, t, errors);
689 }
690
691 bool _inferFunctionExpression(FunctionExpression e, DartType t, errors) {
692 if (t is! FunctionType) return false;
693 var fType = t as FunctionType;
694 var eType = e.staticType as FunctionType;
695 if (eType is! FunctionType) return false;
696
697 // We have a function literal, so we can treat the arrow type
698 // as non-fuzzy. Since we're not improving on parameter types
699 // currently, if this check fails then we cannot succeed, so
700 // bail out. Otherwise, we never need to check the parameter types
701 // again.
702 if (!rules.isFunctionSubTypeOf(eType, fType,
703 fuzzyArrows: false, ignoreReturn: true)) return false;
704
705 // This only entered inference because of fuzzy typing.
706 // The function type is already specific enough, we can just
707 // succeed and treat it as a successful inference
708 if (rules.isSubTypeOf(eType.returnType, fType.returnType)) return true;
709
710 // Fuzzy typing again, handle the void case (not caught by the previous)
711 if (fType.returnType.isVoid) return true;
712
713 if (e.body is! ExpressionFunctionBody) return false;
714 var body = (e.body as ExpressionFunctionBody).expression;
715 if (!_inferExpression(body, fType.returnType, errors)) return false;
716
717 // TODO(leafp): Try narrowing the argument types if possible
718 // to get better code in the function body. This requires checking
719 // that the body is well-typed at the more specific type.
720
721 // At this point, we know that the parameter types are in the appropriate su btype
722 // relation, and we have checked that we can type the body at the appropriat e return
723 // type, so we can are done.
724 annotateFunctionExpression(e, fType.returnType);
725 return true;
726 }
727
728 bool _inferListLiteral(ListLiteral e, DartType t, errors) {
729 var dyn = rules.provider.dynamicType;
730 var listT = rules.provider.listType.substitute4([dyn]);
731 // List <: t (using dart rules) must be true
732 if (!listT.isSubtypeOf(t)) return false;
733 // The list literal must have no type arguments
734 if (e.typeArguments != null) return false;
735 if (t is! InterfaceType) return false;
736 var targs = _matchTypes(listT, t);
737 if (targs == null) return false;
738 assert(targs.length == 1);
739 var etype = targs[0];
740 assert(!etype.isDynamic);
741 var elements = e.elements;
742 var b = elements.every((e) => _inferExpression(e, etype, errors));
743 if (b) annotateListLiteral(e, targs);
744 return b;
745 }
746
747 bool _inferMapLiteral(MapLiteral e, DartType t, errors) {
748 var dyn = rules.provider.dynamicType;
749 var mapT = rules.provider.mapType.substitute4([dyn, dyn]);
750 // Map <: t (using dart rules) must be true
751 if (!mapT.isSubtypeOf(t)) return false;
752 // The map literal must have no type arguments
753 if (e.typeArguments != null) return false;
754 if (t is! InterfaceType) return false;
755 var targs = _matchTypes(mapT, t);
756 if (targs == null) return false;
757 assert(targs.length == 2);
758 var kType = targs[0];
759 var vType = targs[1];
760 assert(!(kType.isDynamic && vType.isDynamic));
761 var entries = e.entries;
762 bool inferEntry(MapLiteralEntry entry) {
763 return _inferExpression(entry.key, kType, errors) &&
764 _inferExpression(entry.value, vType, errors);
765 }
766 var b = entries.every(inferEntry);
767 if (b) annotateMapLiteral(e, targs);
768 return b;
769 }
770 }
OLDNEW
« no previous file with comments | « packages/analyzer/lib/src/task/strong/info.dart ('k') | packages/analyzer/lib/task/model.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698