Skip to content

Commit

Permalink
[CFE] Indicate NNBD compilation mode on all libraries
Browse files Browse the repository at this point in the history
Change-Id: I01236158b593d0e4bd7cf67017459f05fb9520a8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136128
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
jensjoha authored and commit-bot@chromium.org committed Feb 18, 2020
1 parent ef71564 commit a314cff
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 17 deletions.
12 changes: 12 additions & 0 deletions pkg/front_end/lib/src/fasta/source/source_library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import 'package:kernel/ast.dart'
Member,
Name,
NeverType,
NonNullableByDefaultCompiledMode,
Nullability,
Procedure,
ProcedureKind,
Expand Down Expand Up @@ -310,6 +311,11 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
super(
fileUri, libraryDeclaration.toScope(importScope), new Scope.top()) {
library.isNonNullableByDefault = isNonNullableByDefault;
library.nonNullableByDefaultCompiledMode = loader.target.enableNonNullable
? (loader.nnbdStrongMode
? NonNullableByDefaultCompiledMode.Strong
: NonNullableByDefaultCompiledMode.Weak)
: NonNullableByDefaultCompiledMode.Disabled;
}

SourceLibraryBuilder(
Expand Down Expand Up @@ -958,6 +964,12 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
}

library.isNonNullableByDefault = isNonNullableByDefault;
// TODO(CFE Team): Is this really needed in two places?
library.nonNullableByDefaultCompiledMode = loader.target.enableNonNullable
? (loader.nnbdStrongMode
? NonNullableByDefaultCompiledMode.Strong
: NonNullableByDefaultCompiledMode.Weak)
: NonNullableByDefaultCompiledMode.Disabled;

return library;
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/front_end/test/spell_checking_list_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ acov
across
affecting
afterwards
agnostic
agree
ahe
ai
Expand Down Expand Up @@ -97,6 +98,8 @@ bin
binder
binders
binds
bit1
bit2
bj
blob
blocking
Expand Down
5 changes: 3 additions & 2 deletions pkg/kernel/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type CanonicalName {

type ComponentFile {
UInt32 magic = 0x90ABCDEF;
UInt32 formatVersion = 39;
UInt32 formatVersion = 40;
List<String> problemsAsJson; // Described in problems.md.
Library[] libraries;
UriSource sourceMap;
Expand Down Expand Up @@ -228,7 +228,8 @@ type Name {
}

type Library {
Byte flags (_unused_, isSynthetic, isNonNullableByDefault);
Byte flags (_unused_, isSynthetic, isNonNullableByDefault,
nnbdModeBit1, nnbdModeBit2);
UInt languageVersionMajor;
UInt languageVersionMinor;
CanonicalNameReference canonicalName;
Expand Down
36 changes: 36 additions & 0 deletions pkg/kernel/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ class Reference {
// LIBRARIES and CLASSES
// ------------------------------------------------------------------------

enum NonNullableByDefaultCompiledMode { Disabled, Weak, Strong, Agnostic }

class Library extends NamedNode
implements Annotatable, Comparable<Library>, FileUriNode {
/// An import path to this library.
Expand Down Expand Up @@ -347,6 +349,8 @@ class Library extends NamedNode

static const int SyntheticFlag = 1 << 1;
static const int NonNullableByDefaultFlag = 1 << 2;
static const int NonNullableByDefaultModeBit1Weak = 1 << 3;
static const int NonNullableByDefaultModeBit2Strong = 1 << 4;

int flags = 0;

Expand All @@ -364,6 +368,38 @@ class Library extends NamedNode
: (flags & ~NonNullableByDefaultFlag);
}

NonNullableByDefaultCompiledMode get nonNullableByDefaultCompiledMode {
bool weak = (flags & NonNullableByDefaultModeBit1Weak) != 0;
bool strong = (flags & NonNullableByDefaultModeBit2Strong) != 0;

if (weak && strong) return NonNullableByDefaultCompiledMode.Agnostic;
if (strong) return NonNullableByDefaultCompiledMode.Strong;
if (weak) return NonNullableByDefaultCompiledMode.Weak;
return NonNullableByDefaultCompiledMode.Disabled;
}

void set nonNullableByDefaultCompiledMode(
NonNullableByDefaultCompiledMode mode) {
switch (mode) {
case NonNullableByDefaultCompiledMode.Disabled:
flags = (flags & ~NonNullableByDefaultModeBit1Weak) &
~NonNullableByDefaultModeBit2Strong;
break;
case NonNullableByDefaultCompiledMode.Weak:
flags = (flags | NonNullableByDefaultModeBit1Weak) &
~NonNullableByDefaultModeBit2Strong;
break;
case NonNullableByDefaultCompiledMode.Strong:
flags = (flags & ~NonNullableByDefaultModeBit1Weak) |
NonNullableByDefaultModeBit2Strong;
break;
case NonNullableByDefaultCompiledMode.Agnostic:
flags = (flags | NonNullableByDefaultModeBit1Weak) |
NonNullableByDefaultModeBit2Strong;
break;
}
}

String name;

/// Problems in this [Library] encoded as json objects.
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/lib/binary/tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Tag {
/// Internal version of kernel binary format.
/// Bump it when making incompatible changes in kernel binaries.
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
static const int BinaryFormatVersion = 39;
static const int BinaryFormatVersion = 40;
}

abstract class ConstantTag {
Expand Down
80 changes: 67 additions & 13 deletions pkg/kernel/test/binary/library_flags_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,72 @@

import 'utils.dart';

/// Test that library flags (external, synthetic)
/// are serialized and read correctly.
/// Test that library flags are serialized and read correctly.
main() {
Library lib = new Library(Uri.parse("foo://bar.dart"));
lib.isSynthetic = false;
Library lib2 = libRoundTrip(lib);
if (lib2.isSynthetic != false)
throw "Serialized and re-read library had change in synthetic flag.";

lib = new Library(Uri.parse("foo://bar.dart"));
lib.isSynthetic = true;
lib2 = libRoundTrip(lib);
if (lib2.isSynthetic != true)
throw "Serialized and re-read library had change in synthetic flag.";
setSynthetic(Library lib, bool isSynthetic) {
lib.isSynthetic = isSynthetic;
}

verifySynthetic(Library lib, bool isSynthetic) {
if (lib.isSynthetic != isSynthetic) {
throw "Serialized and re-read library had change in synthetic flag.";
}
}

setNonNullableByDefault(Library lib, bool isNonNullableByDefault) {
lib.isNonNullableByDefault = isNonNullableByDefault;
}

verifyNonNullableByDefault(Library lib, bool isNonNullableByDefault) {
if (lib.isNonNullableByDefault != isNonNullableByDefault) {
throw "Serialized and re-read library had change in "
"isNonNullableByDefault flag.";
}
}

setNonNullableByDefaultCompiledMode(Library lib,
NonNullableByDefaultCompiledMode nonNullableByDefaultCompiledMode) {
lib.nonNullableByDefaultCompiledMode = nonNullableByDefaultCompiledMode;
}

verifyNonNullableByDefaultCompiledMode(Library lib,
NonNullableByDefaultCompiledMode nonNullableByDefaultCompiledMode) {
if (lib.nonNullableByDefaultCompiledMode !=
nonNullableByDefaultCompiledMode) {
throw "Serialized and re-read library had change in "
"nonNullableByDefaultCompiledMode flag.";
}
}

int combination = 0;
for (bool isSynthetic in [true, false]) {
for (bool isNonNullableByDefault in [true, false]) {
for (NonNullableByDefaultCompiledMode nonNullableByDefaultCompiledMode
in [
NonNullableByDefaultCompiledMode.Disabled,
NonNullableByDefaultCompiledMode.Weak,
NonNullableByDefaultCompiledMode.Strong,
NonNullableByDefaultCompiledMode.Agnostic,
]) {
combination++;
print("Checking combination #$combination ("
"isSynthetic: $isSynthetic; "
"isNonNullableByDefault: $isNonNullableByDefault; "
"nonNullableByDefaultCompiledMode:"
" $nonNullableByDefaultCompiledMode");
Library lib = new Library(Uri.parse("foo://bar.dart"));
setSynthetic(lib, isSynthetic);
setNonNullableByDefault(lib, isNonNullableByDefault);
setNonNullableByDefaultCompiledMode(
lib, nonNullableByDefaultCompiledMode);
Library lib2 = libRoundTrip(lib);
verifySynthetic(lib2, isSynthetic);
verifyNonNullableByDefault(lib2, isNonNullableByDefault);
verifyNonNullableByDefaultCompiledMode(
lib2, nonNullableByDefaultCompiledMode);
}
}
}

print("Done: Everything looks good.");
}
17 changes: 17 additions & 0 deletions runtime/vm/compiler/frontend/kernel_translation_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@ class LibraryHelper {
kExternal = 1 << 0,
kSynthetic = 1 << 1,
kIsNonNullableByDefault = 1 << 2,
kNonNullableByDefaultCompiledModeBit1Weak = 1 << 3,
kNonNullableByDefaultCompiledModeBit2Strong = 1 << 4,
};

enum NonNullableByDefaultCompiledMode {
kDisabled,
kWeak,
kStrong,
kAgnostic
};

explicit LibraryHelper(KernelReaderHelper* helper, uint32_t binary_version)
Expand All @@ -782,6 +791,14 @@ class LibraryHelper {
bool IsNonNullableByDefault() const {
return (flags_ & kIsNonNullableByDefault) != 0;
}
NonNullableByDefaultCompiledMode GetNonNullableByDefaultCompiledMode() const {
bool weak = (flags_ & kNonNullableByDefaultCompiledModeBit1Weak) != 0;
bool strong = (flags_ & kNonNullableByDefaultCompiledModeBit2Strong) != 0;
if (weak && strong) return kAgnostic;
if (strong) return kStrong;
if (weak) return kWeak;
return kDisabled;
}

uint8_t flags_ = 0;
NameIndex canonical_name_;
Expand Down
2 changes: 1 addition & 1 deletion runtime/vm/kernel_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static const uint32_t kMagicProgramFile = 0x90ABCDEFu;

// Both version numbers are inclusive.
static const uint32_t kMinSupportedKernelFormatVersion = 29;
static const uint32_t kMaxSupportedKernelFormatVersion = 39;
static const uint32_t kMaxSupportedKernelFormatVersion = 40;

// Keep in sync with package:kernel/lib/binary/tag.dart
#define KERNEL_TAG_LIST(V) \
Expand Down

0 comments on commit a314cff

Please sign in to comment.