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

Side by Side Diff: lib/src/checker/rules.dart

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

Powered by Google App Engine
This is Rietveld 408576698