Skip to content

Commit

Permalink
Augment. Support multiple augmentations of the same element in the sa…
Browse files Browse the repository at this point in the history
…me container.

The same mechanism is now also used to handle (not allowed) duplicate
declarations.

Change-Id: I814abcb15b7f2e7248701f754b7af1fdcb74f31a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328900
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Oct 4, 2023
1 parent 1aa8232 commit bb1a7ad
Show file tree
Hide file tree
Showing 11 changed files with 1,175 additions and 242 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import 'package:analyzer/src/utilities/uri_cache.dart';
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
class AnalysisDriver implements AnalysisDriverGeneric {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 310;
static const int DATA_VERSION = 311;

/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/src/dart/element/display_string_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class ElementDisplayStringBuilder {
}

void writeVariableElement(VariableElement element) {
switch (element) {
case FieldElement(isAugmentation: true):
case TopLevelVariableElement(isAugmentation: true):
_write('augment ');
}

_writeType(element.type);
_write(' ');
_write(element.displayName);
Expand Down
47 changes: 25 additions & 22 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6375,6 +6375,19 @@ abstract class PropertyInducingElementImpl
setModifier(Modifier.SHOULD_USE_TYPE_FOR_INITIALIZER_INFERENCE, true);
}

/// Return `true` if this variable needs the setter.
bool get hasSetter {
if (isConst) {
return false;
}

if (isLate) {
return !isFinal || !hasInitializer;
}

return !isFinal;
}

@override
bool get isConstantEvaluated => true;

Expand Down Expand Up @@ -6450,36 +6463,26 @@ abstract class PropertyInducingElementImpl
return _type!;
}

/// Return `true` if this variable needs the setter.
bool get _hasSetter {
if (isConst) {
return false;
}

if (isLate) {
return !isFinal || !hasInitializer;
}

return !isFinal;
}

void bindReference(Reference reference) {
this.reference = reference;
reference.element = this;
}

void createImplicitAccessors(Reference enclosingRef, String name) {
getter = PropertyAccessorElementImpl_ImplicitGetter(
PropertyAccessorElementImpl createImplicitGetter(Reference reference) {
assert(getter == null);
return getter = PropertyAccessorElementImpl_ImplicitGetter(
this,
reference: enclosingRef.getChild('@getter').getChild(name),
reference: reference,
);
}

if (_hasSetter) {
setter = PropertyAccessorElementImpl_ImplicitSetter(
this,
reference: enclosingRef.getChild('@setter').getChild(name),
);
}
PropertyAccessorElementImpl createImplicitSetter(Reference reference) {
assert(hasSetter);
assert(setter == null);
return setter = PropertyAccessorElementImpl_ImplicitSetter(
this,
reference: reference,
);
}

void setLinkedData(Reference reference, ElementLinkedData linkedData) {
Expand Down
57 changes: 41 additions & 16 deletions pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ class LibraryReader {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();

final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var element = ClassElementImpl(name, -1);

Expand Down Expand Up @@ -790,7 +790,7 @@ class LibraryReader {
return _reader.readTypedList(() {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name.ifEqualThen('new', '');
final name = reference.elementName.ifEqualThen('new', '');
var element = ConstructorElementImpl(name, -1);
var linkedData = ConstructorElementLinkedData(
reference: reference,
Expand Down Expand Up @@ -895,7 +895,7 @@ class LibraryReader {
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var element = EnumElementImpl(name, -1);

Expand Down Expand Up @@ -980,7 +980,7 @@ class LibraryReader {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();

final reference = _readReference();
final name = _reader.readBool() ? reference.name : null;
final name = _reader.readBool() ? reference.elementName : null;

var element = ExtensionElementImpl(name, -1);
element.setLinkedData(
Expand Down Expand Up @@ -1023,7 +1023,7 @@ class LibraryReader {
) {
final resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

final element = ExtensionTypeElementImpl(name, -1);
element.setLinkedData(
Expand Down Expand Up @@ -1073,8 +1073,12 @@ class LibraryReader {
Reference classReference,
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();

final reference = _readReference();
final name = reference.name;
final getterReference = _readOptionalReference();
final setterReference = _readOptionalReference();

final name = reference.elementName;
var isConstElement = _reader.readBool();

FieldElementImpl element;
Expand All @@ -1096,7 +1100,12 @@ class LibraryReader {
element.typeInferenceError = _readTopLevelInferenceError();

if (!element.isAugmentation) {
element.createImplicitAccessors(classReference, name);
if (getterReference != null) {
element.createImplicitGetter(getterReference);
}
if (element.hasSetter && setterReference != null) {
element.createImplicitSetter(setterReference);
}
}

return element;
Expand Down Expand Up @@ -1136,7 +1145,7 @@ class LibraryReader {
unitElement.functions = _reader.readTypedList(() {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var element = FunctionElementImpl(name, -1);

Expand Down Expand Up @@ -1258,7 +1267,7 @@ class LibraryReader {
return _reader.readTypedList(() {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;
var element = MethodElementImpl(name, -1);
var linkedData = MethodElementLinkedData(
reference: reference,
Expand All @@ -1281,7 +1290,7 @@ class LibraryReader {
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var element = MixinElementImpl(name, -1);

Expand Down Expand Up @@ -1335,18 +1344,24 @@ class LibraryReader {
}
}

/// Read the reference of a non-local element.
Reference? _readOptionalReference() {
return _reader.readOptionalObject(
(reader) => _readReference(),
);
}

/// TODO(scheglov) Deduplicate parameter reading implementation.
List<ParameterElementImpl> _readParameters(
ElementImpl enclosingElement,
Reference enclosingReference,
) {
var containerRef = enclosingReference.getChild('@parameter');
return _reader.readTypedList(() {
var name = _reader.readStringReference();
var isDefault = _reader.readBool();
var isInitializingFormal = _reader.readBool();
var isSuperFormal = _reader.readBool();
var reference = containerRef.getChild(name);
var reference = _readReference();

var kindIndex = _reader.readByte();
var kind = ResolutionReader._formalParameterKind(kindIndex);
Expand Down Expand Up @@ -1445,7 +1460,7 @@ class LibraryReader {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();

final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var element = PropertyAccessorElementImpl(name, -1);
PropertyAccessorElementFlags.read(_reader, element);
Expand Down Expand Up @@ -1557,8 +1572,12 @@ class LibraryReader {
Reference unitReference,
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();

final reference = _readReference();
final name = reference.name;
final getterReference = _readOptionalReference();
final setterReference = _readOptionalReference();

final name = reference.elementName;
var isConst = _reader.readBool();

TopLevelVariableElementImpl element;
Expand All @@ -1579,7 +1598,13 @@ class LibraryReader {
element.isConst = isConst;
TopLevelVariableElementFlags.read(_reader, element);
element.typeInferenceError = _readTopLevelInferenceError();
element.createImplicitAccessors(unitReference, name);

if (getterReference != null) {
element.createImplicitGetter(getterReference);
}
if (element.hasSetter && setterReference != null) {
element.createImplicitSetter(setterReference);
}

return element;
}
Expand Down Expand Up @@ -1613,7 +1638,7 @@ class LibraryReader {
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
final reference = _readReference();
final name = reference.name;
final name = reference.elementName;

var isFunctionTypeAliasBased = _reader.readBool();

Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/lib/src/summary2/bundle_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ class BundleWriter {
void _writeFieldElement(FieldElementImpl element) {
_sink.writeUInt30(_resolutionSink.offset);
_writeReference(element);
_writeOptionalReference(element.getter);
_writeOptionalReference(element.setter);
_sink.writeBool(element is ConstFieldElementImpl);
FieldElementFlags.write(_sink, element);
_sink._writeTopLevelInferenceError(element.typeInferenceError);
Expand Down Expand Up @@ -559,13 +561,21 @@ class BundleWriter {
}
}

/// Write the reference of a non-local element.
void _writeOptionalReference(ElementImpl? element) {
_sink.writeOptionalObject(element, (element) {
_writeReference(element);
});
}

/// TODO(scheglov) Deduplicate parameter writing implementation.
void _writeParameterElement(ParameterElement element) {
element as ParameterElementImpl;
_sink._writeStringReference(element.name);
_sink.writeBool(element is ConstVariableElement);
_sink.writeBool(element.isInitializingFormal);
_sink.writeBool(element.isSuperFormal);
_writeReference(element);
_sink._writeFormalParameterKind(element);
ParameterElementFlags.write(_sink, element);

Expand Down Expand Up @@ -623,6 +633,8 @@ class BundleWriter {
void _writeTopLevelVariableElement(TopLevelVariableElementImpl element) {
_sink.writeUInt30(_resolutionSink.offset);
_writeReference(element);
_writeOptionalReference(element.getter);
_writeOptionalReference(element.setter);
_sink.writeBool(element.isConst);
TopLevelVariableElementFlags.write(_sink, element);
_sink._writeTopLevelInferenceError(element.typeInferenceError);
Expand Down
Loading

0 comments on commit bb1a7ad

Please sign in to comment.