Skip to content

Commit

Permalink
Flag to ignore types during codegen
Browse files Browse the repository at this point in the history
R=jmesserly@google.com, leafp@google.com

Review URL: https://chromereviews.googleplex.com/161647013
  • Loading branch information
vsmenon committed Feb 21, 2015
1 parent 608906d commit ee887b1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
3 changes: 3 additions & 0 deletions pkg/dev_compiler/bin/devc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final ArgParser argParser = new ArgParser()
..addFlag('force-compile',
help: 'Compile code with static errors', defaultsTo: false)
..addFlag('help', abbr: 'h', help: 'Display this message')
..addFlag('ignore-types',
help: 'Ignore types during codegen', defaultsTo: false)
..addOption('log', abbr: 'l', help: 'Logging level', defaultsTo: 'severe')
..addFlag('mock-sdk',
abbr: 'm', help: 'Use a mock Dart SDK', defaultsTo: false)
Expand Down Expand Up @@ -109,6 +111,7 @@ void main(List<String> argv) {
dumpSrcDir: args['dump-src-to'],
forceCompile: args['force-compile'],
formatOutput: args['dart-gen-fmt'],
ignoreTypes: args['ignore-types'],
outputDart: args['dart-gen'],
outputDir: args['out'],
covariantGenerics: args['covariant-generics'],
Expand Down
54 changes: 35 additions & 19 deletions pkg/dev_compiler/lib/src/checker/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,10 @@ abstract class TypeRules {

DartType elementType(Element e);

/// Returns `true` if the target expression is dynamic.
bool isDynamicTarget(Expression expr) => getStaticType(expr).isDynamic;

/// Returns `true` if the expression is a dynamic property access or prefixed
/// identifier.
bool isDynamicGet(Expression expr) {
var t = getStaticType(expr);
// TODO(jmesserly): we should not allow all property gets on `Function`
return t.isDynamic || t.isDartCoreFunction;
}

/// Returns `true` if the expression is a dynamic function call or method
/// invocation.
bool isDynamicCall(Expression call) {
var t = getStaticType(call);
// TODO(jmesserly): fix handling of types with `call` methods. These are not
// FunctionType, but they also aren't dynamic calls.
return t.isDynamic || t.isDartCoreFunction || t is! FunctionType;
}
bool isDynamic(DartType t);
bool isDynamicTarget(Expression expr);
bool isDynamicGet(Expression expr);
bool isDynamicCall(Expression call);
}

class DartRules extends TypeRules {
Expand All @@ -80,6 +65,12 @@ class DartRules extends TypeRules {
DartType elementType(Element e) {
return (e as dynamic).type;
}

/// By default, all invocations are dynamic in Dart.
bool isDynamic(DartType t) => true;
bool isDynamicTarget(Expression expr) => true;
bool isDynamicGet(Expression expr) => true;
bool isDynamicCall(Expression call) => true;
}

class RestrictedRules extends TypeRules {
Expand Down Expand Up @@ -473,4 +464,29 @@ class RestrictedRules extends TypeRules {
DartType elementType(Element e) {
return (e as dynamic).type;
}

bool isDynamic(DartType t) => options.ignoreTypes || t.isDynamic;

/// Returns `true` if the target expression is dynamic.
bool isDynamicTarget(Expression expr) =>
options.ignoreTypes || getStaticType(expr).isDynamic;

/// Returns `true` if the expression is a dynamic property access or prefixed
/// identifier.
bool isDynamicGet(Expression expr) {
if (options.ignoreTypes) return true;
var t = getStaticType(expr);
// TODO(jmesserly): we should not allow all property gets on `Function`
return t.isDynamic || t.isDartCoreFunction;
}

/// Returns `true` if the expression is a dynamic function call or method
/// invocation.
bool isDynamicCall(Expression call) {
if (options.ignoreTypes) return true;
var t = getStaticType(call);
// TODO(jmesserly): fix handling of types with `call` methods. These are not
// FunctionType, but they also aren't dynamic calls.
return t.isDynamic || t.isDartCoreFunction || t is! FunctionType;
}
}
9 changes: 5 additions & 4 deletions pkg/dev_compiler/lib/src/codegen/js_codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1052,10 +1052,11 @@ $name.prototype[Symbol.iterator] = function() {
bool _isJSBuiltinType(DartType t) =>
rules.isNumType(t) || rules.isStringType(t) || rules.isBoolType(t);

bool typeIsPrimitiveInJS(DartType t) => rules.isIntType(t) ||
rules.isDoubleType(t) ||
rules.isBoolType(t) ||
rules.isNumType(t);
bool typeIsPrimitiveInJS(DartType t) => !rules.isDynamic(t) &&
(rules.isIntType(t) ||
rules.isDoubleType(t) ||
rules.isBoolType(t) ||
rules.isNumType(t));

bool typeIsNonNullablePrimitiveInJS(DartType t) =>
typeIsPrimitiveInJS(t) && rules.isNonNullableType(t);
Expand Down
21 changes: 15 additions & 6 deletions pkg/dev_compiler/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class RulesOptions extends TypeOptions {
/// Whether to inject casts between Dart assignable types.
final bool relaxedCasts;

RulesOptions({this.covariantGenerics: true, this.relaxedCasts: true});
/// Whether to use static types for code generation.
final bool ignoreTypes;

RulesOptions({this.covariantGenerics: true, this.relaxedCasts: true,
this.ignoreTypes: false});
}

/// General options used by the dev compiler.
Expand Down Expand Up @@ -140,13 +144,18 @@ class CompilerOptions implements RulesOptions, ResolverOptions {
@override
final List<String> nonnullableTypes;

/// Whether to use static types for code generation.
@override
final bool ignoreTypes;

CompilerOptions({this.checkSdk: false, this.dumpInfo: false,
this.dumpInfoFile, this.dumpSrcDir, this.forceCompile: false,
this.formatOutput: false, this.outputDir, this.outputDart: false,
this.useColors: true, this.covariantGenerics: true,
this.relaxedCasts: true, this.useMultiPackage: false,
this.packageRoot: 'packages/', this.packagePaths: const [],
this.inferFromOverrides: true, this.inferStaticsFromIdentifiers: false,
this.formatOutput: false, this.ignoreTypes: false, this.outputDir,
this.outputDart: false, this.useColors: true,
this.covariantGenerics: true, this.relaxedCasts: true,
this.useMultiPackage: false, this.packageRoot: 'packages/',
this.packagePaths: const [], this.inferFromOverrides: true,
this.inferStaticsFromIdentifiers: false,
this.inferInNonStableOrder: false,
this.onlyInferConstsAndFinalFields: false,
this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES});
Expand Down

0 comments on commit ee887b1

Please sign in to comment.