Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 /// Types needed to implement "strong" checking in the Dart analyzer. | |
| 6 /// This is intended to be used by analyzer_cli and analysis_server packages. | |
| 7 library dev_compiler.strong_mode; | |
| 8 | |
| 9 import 'package:analyzer/src/generated/engine.dart' | |
| 10 show AnalysisContextImpl, AnalysisErrorInfo, AnalysisErrorInfoImpl; | |
| 11 import 'package:analyzer/src/generated/error.dart' | |
| 12 show | |
| 13 AnalysisError, | |
| 14 ErrorCode, | |
| 15 CompileTimeErrorCode, | |
| 16 StaticTypeWarningCode, | |
| 17 HintCode; | |
| 18 import 'package:analyzer/src/generated/source.dart' show Source; | |
| 19 import 'package:args/args.dart'; | |
| 20 import 'package:logging/logging.dart' show Level; | |
| 21 | |
| 22 import 'src/checker/checker.dart' show CodeChecker; | |
| 23 import 'src/checker/resolver.dart' show LibraryResolverWithInference; | |
| 24 import 'src/checker/rules.dart' show RestrictedRules; | |
| 25 import 'src/report.dart' show CheckerReporter, Message; | |
| 26 | |
| 27 /// A type checker for Dart code that operates under stronger rules, and has | |
| 28 /// the ability to do local type inference in some situations. | |
| 29 class StrongChecker { | |
| 30 final AnalysisContextImpl _context; | |
| 31 final CodeChecker _checker; | |
| 32 final _ErrorReporter _reporter; | |
| 33 final StrongModeOptions _options; | |
| 34 | |
| 35 StrongChecker._(this._context, this._options, this._checker, this._reporter); | |
| 36 | |
| 37 factory StrongChecker( | |
| 38 AnalysisContextImpl context, StrongModeOptions options) { | |
| 39 // TODO(jmesserly): is there a cleaner way to plug this in? | |
| 40 if (context.libraryResolverFactory != null) { | |
| 41 throw new ArgumentError.value(context, 'context', | |
| 42 'Analysis context must not have libraryResolverFactory already set.'); | |
| 43 } | |
| 44 context.libraryResolverFactory = | |
| 45 (c) => new LibraryResolverWithInference(c, options); | |
| 46 | |
| 47 var rules = new RestrictedRules(context.typeProvider, options: options); | |
| 48 var reporter = new _ErrorReporter(); | |
| 49 var checker = new CodeChecker(rules, reporter, options); | |
| 50 return new StrongChecker._(context, options, checker, reporter); | |
| 51 } | |
| 52 | |
| 53 /// Computes and returns DDC errors for the [source]. | |
| 54 AnalysisErrorInfo computeErrors(Source source) { | |
| 55 var errors = new List<AnalysisError>(); | |
| 56 | |
| 57 // TODO(jmesserly): change DDC to emit ErrorCodes directly. | |
| 58 _reporter._log = (Message msg) { | |
| 59 // Skip hints unless requested. | |
| 60 if (msg.level < Level.WARNING && !_options.hints) return; | |
| 61 | |
| 62 var errorCodeFactory = _levelToErrorCode[msg.level]; | |
| 63 var category = '${msg.runtimeType}'; | |
| 64 var errorCode = errorCodeFactory(category, msg.message); | |
| 65 var len = msg.end - msg.begin; | |
| 66 errors.add(new AnalysisError.con2(source, msg.begin, len, errorCode)); | |
| 67 }; | |
| 68 | |
| 69 for (Source librarySource in _context.getLibrariesContaining(source)) { | |
| 70 var resolved = _context.resolveCompilationUnit2(source, librarySource); | |
| 71 _checker.visitCompilationUnit(resolved); | |
| 72 } | |
| 73 _reporter._log = null; | |
| 74 return new AnalysisErrorInfoImpl(errors, _context.getLineInfo(source)); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 /// Maps a DDC log level to an analyzer ErrorCode subclass. | |
| 79 final _levelToErrorCode = <Level, _ErrorCodeFactory>{ | |
| 80 Level.SEVERE: (n, m) => new CompileTimeErrorCode(n, m), | |
| 81 Level.WARNING: (n, m) => new StaticTypeWarningCode(n, m), | |
| 82 Level.INFO: (n, m) => new HintCode(n, m) | |
| 83 }; | |
| 84 | |
| 85 class _ErrorReporter implements CheckerReporter { | |
| 86 _CheckerReporterLog _log; | |
| 87 void log(Message message) => _log(message); | |
| 88 } | |
| 89 | |
| 90 class StrongModeOptions { | |
| 91 | |
| 92 /// Whether to infer return types and field types from overriden members. | |
|
vsm
2015/06/10 23:31:17
overriden -> overridden
Jennifer Messerly
2015/06/11 14:46:48
Good catch. Done. Also fixed this typo in a few mo
| |
| 93 final bool inferFromOverrides; | |
| 94 static const inferFromOverridesDefault = true; | |
| 95 | |
| 96 /// Whether to infer types for consts and fields by looking at initializers on | |
| 97 /// the RHS. For example, in a constant declaration like: | |
| 98 /// | |
| 99 /// const A = B; | |
| 100 /// | |
| 101 /// We can infer the type of `A` based on the type of `B`. | |
| 102 /// | |
| 103 /// The inference algorithm determines what variables depend on others, and | |
| 104 /// computes types by visiting the variable dependency graph in topological | |
| 105 /// order. This ensures that the inferred type is deterministic when applying | |
| 106 /// inference on library cycles. | |
| 107 /// | |
| 108 /// When this feature is turned off, we don't use the type of `B` to infer the | |
| 109 /// type of `A`, even if `B` has a declared type. | |
| 110 final bool inferTransitively; | |
| 111 static const inferTransitivelyDefault = true; | |
| 112 | |
| 113 /// Restrict inference of fields and top-levels to those that are final and | |
| 114 /// const. | |
| 115 final bool onlyInferConstsAndFinalFields; | |
| 116 static const onlyInferConstAndFinalFieldsDefault = false; | |
| 117 | |
| 118 /// Whether to infer types downwards from local context | |
| 119 final bool inferDownwards; | |
| 120 static const inferDownwardsDefault = true; | |
| 121 | |
| 122 /// Whether to inject casts between Dart assignable types. | |
| 123 final bool relaxedCasts; | |
| 124 | |
| 125 /// A list of non-nullable type names (e.g., 'int') | |
| 126 final List<String> nonnullableTypes; | |
| 127 static const List<String> NONNULLABLE_TYPES = const <String>[]; | |
| 128 | |
| 129 /// Whether to include hints about dynamic invokes and runtime checks. | |
| 130 // TODO(jmesserly): this option is not used yet by DDC server mode or batch | |
| 131 // compile to JS. | |
| 132 final bool hints; | |
| 133 | |
| 134 const StrongModeOptions({this.hints: false, | |
| 135 this.inferFromOverrides: inferFromOverridesDefault, | |
| 136 this.inferTransitively: inferTransitivelyDefault, | |
| 137 this.onlyInferConstsAndFinalFields: onlyInferConstAndFinalFieldsDefault, | |
| 138 this.inferDownwards: inferDownwardsDefault, this.relaxedCasts: true, | |
| 139 this.nonnullableTypes: StrongModeOptions.NONNULLABLE_TYPES}); | |
| 140 | |
| 141 StrongModeOptions.fromArguments(ArgResults args, {String prefix: ''}) | |
| 142 : relaxedCasts = args[prefix + 'relaxed-casts'], | |
| 143 inferDownwards = args[prefix + 'infer-downwards'], | |
| 144 inferFromOverrides = args[prefix + 'infer-from-overrides'], | |
| 145 inferTransitively = args[prefix + 'infer-transitively'], | |
| 146 onlyInferConstsAndFinalFields = args[prefix + 'infer-only-finals'], | |
| 147 nonnullableTypes = _optionsToList(args[prefix + 'nonnullable'], | |
| 148 defaultValue: StrongModeOptions.NONNULLABLE_TYPES), | |
| 149 hints = args[prefix + 'hints']; | |
| 150 | |
| 151 static ArgParser addArguments(ArgParser parser, | |
| 152 {String prefix: '', bool hide: false}) { | |
| 153 return parser | |
| 154 ..addFlag(prefix + 'hints', | |
| 155 help: 'Display hints about dynamic casts and dispatch operations', | |
| 156 defaultsTo: false, | |
| 157 hide: hide) | |
| 158 ..addFlag(prefix + 'relaxed-casts', | |
| 159 help: 'Cast between Dart assignable types', | |
| 160 defaultsTo: true, | |
| 161 hide: hide) | |
| 162 ..addOption(prefix + 'nonnullable', | |
| 163 abbr: prefix == '' ? 'n' : null, | |
| 164 help: 'Comma separated string of non-nullable types', | |
| 165 defaultsTo: null, | |
| 166 hide: hide) | |
| 167 ..addFlag(prefix + 'infer-downwards', | |
| 168 help: 'Infer types downwards from local context', | |
| 169 defaultsTo: inferDownwardsDefault, | |
| 170 hide: hide) | |
| 171 ..addFlag(prefix + 'infer-from-overrides', | |
| 172 help: 'Infer unspecified types of fields and return types from\n' | |
| 173 'definitions in supertypes', | |
| 174 defaultsTo: inferFromOverridesDefault, | |
| 175 hide: hide) | |
| 176 ..addFlag(prefix + 'infer-transitively', | |
| 177 help: 'Infer consts/fields from definitions in other libraries', | |
| 178 defaultsTo: inferTransitivelyDefault, | |
| 179 hide: hide) | |
| 180 ..addFlag(prefix + 'infer-only-finals', | |
| 181 help: 'Do not infer non-const or non-final fields', | |
| 182 defaultsTo: onlyInferConstAndFinalFieldsDefault, | |
| 183 hide: hide); | |
| 184 } | |
| 185 | |
| 186 bool operator ==(Object other) { | |
| 187 if (other is! StrongModeOptions) return false; | |
| 188 StrongModeOptions s = other; | |
| 189 return inferFromOverrides == s.inferFromOverrides && | |
| 190 inferTransitively == s.inferTransitively && | |
| 191 onlyInferConstsAndFinalFields == s.onlyInferConstsAndFinalFields && | |
| 192 inferDownwards == s.inferDownwards && | |
| 193 relaxedCasts == s.relaxedCasts && | |
| 194 nonnullableTypes.length == s.nonnullableTypes.length && | |
| 195 new Set.from(nonnullableTypes).containsAll(s.nonnullableTypes); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 typedef void _CheckerReporterLog(Message message); | |
| 200 typedef ErrorCode _ErrorCodeFactory(String name, String message); | |
| 201 | |
| 202 List<String> _optionsToList(String option, | |
| 203 {List<String> defaultValue: const <String>[]}) { | |
| 204 if (option == null) { | |
| 205 return defaultValue; | |
| 206 } else if (option.isEmpty) { | |
| 207 return <String>[]; | |
| 208 } else { | |
| 209 return option.split(','); | |
| 210 } | |
| 211 } | |
| OLD | NEW |