Skip to content

Commit 271ad96

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe] Create Extension nodes for extensions
Adds Extension node to kernel/ast, currently with serialization. This is needed to support resolution of extension methods through dill boundaries. Adds (Source)ExtensionBuilder for build Extension nodes and DeclarationBuilder as common superclass of ClassBuilder and ExtensionBuilder. Change-Id: Id945a40452f2729f9a4f390477dd6c76f9030eaa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112256 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Dmitry Stefantsov <[email protected]>
1 parent cd42e2b commit 271ad96

File tree

56 files changed

+1075
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1075
-295
lines changed

pkg/front_end/lib/src/fasta/builder/class_builder.dart

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ import 'builder.dart'
6363
Scope,
6464
ScopeBuilder,
6565
TypeBuilder,
66-
TypeDeclarationBuilder,
6766
TypeVariableBuilder;
6867

68+
import 'declaration_builder.dart';
69+
6970
import '../fasta_codes.dart'
7071
show
7172
LocatedMessage,
@@ -137,7 +138,7 @@ import '../source/source_library_builder.dart' show SourceLibraryBuilder;
137138

138139
import '../type_inference/type_schema.dart' show UnknownType;
139140

140-
abstract class ClassBuilder extends TypeDeclarationBuilder {
141+
abstract class ClassBuilder extends DeclarationBuilder {
141142
/// The type variables declared on a class, extension or mixin declaration.
142143
List<TypeVariableBuilder> typeVariables;
143144

@@ -153,12 +154,8 @@ abstract class ClassBuilder extends TypeDeclarationBuilder {
153154
/// The types in the `on` clause of an extension or mixin declaration.
154155
List<TypeBuilder> onTypes;
155156

156-
final Scope scope;
157-
158157
final Scope constructors;
159158

160-
final ScopeBuilder scopeBuilder;
161-
162159
final ScopeBuilder constructorScopeBuilder;
163160

164161
Map<String, ConstructorRedirection> redirectingConstructors;
@@ -173,13 +170,12 @@ abstract class ClassBuilder extends TypeDeclarationBuilder {
173170
this.supertype,
174171
this.interfaces,
175172
this.onTypes,
176-
this.scope,
173+
Scope scope,
177174
this.constructors,
178175
LibraryBuilder parent,
179176
int charOffset)
180-
: scopeBuilder = new ScopeBuilder(scope),
181-
constructorScopeBuilder = new ScopeBuilder(constructors),
182-
super(metadata, modifiers, name, parent, charOffset);
177+
: constructorScopeBuilder = new ScopeBuilder(constructors),
178+
super(metadata, modifiers, name, parent, charOffset, scope);
183179

184180
String get debugName => "ClassBuilder";
185181

@@ -198,11 +194,6 @@ abstract class ClassBuilder extends TypeDeclarationBuilder {
198194

199195
List<ConstructorReferenceBuilder> get constructorReferences => null;
200196

201-
LibraryBuilder get library {
202-
LibraryBuilder library = parent;
203-
return library.partOfLibrary ?? library;
204-
}
205-
206197
void buildOutlineExpressions(LibraryBuilder library) {
207198
void build(String ignore, Builder declaration) {
208199
MemberBuilder member = declaration;
@@ -369,12 +360,6 @@ abstract class ClassBuilder extends TypeDeclarationBuilder {
369360
null);
370361
}
371362

372-
void addProblem(Message message, int charOffset, int length,
373-
{bool wasHandled: false, List<LocatedMessage> context}) {
374-
library.addProblem(message, charOffset, length, fileUri,
375-
wasHandled: wasHandled, context: context);
376-
}
377-
378363
/// Find the first member of this class with [name]. This method isn't
379364
/// suitable for scope lookups as it will throw an error if the name isn't
380365
/// declared. The [scope] should be used for that. This method is used to

pkg/front_end/lib/src/fasta/builder/declaration.dart

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,140 @@ abstract class Builder {
4040

4141
bool get isGetter => false;
4242

43-
bool get isInstanceMember => false;
43+
/// Returns `true` if this builder is an extension declaration.
44+
///
45+
/// For instance `B` in:
46+
///
47+
/// class A {}
48+
/// extension B on A {}
49+
///
50+
bool get isExtension => false;
51+
52+
/// Returns `true` if this builder is a member of a class, mixin, or extension
53+
/// declaration.
54+
///
55+
/// For instance `A.constructor`, `method1a`, `method1b`, `method2a`,
56+
/// `method2b`, `method3a`, and `method3b` in:
57+
///
58+
/// class A {
59+
/// A.constructor();
60+
/// method1a() {}
61+
/// static method1b() {}
62+
/// }
63+
/// mixin B {
64+
/// method2a() {}
65+
/// static method2b() {}
66+
/// }
67+
/// extends C on A {
68+
/// method3a() {}
69+
/// static method3b() {}
70+
/// }
71+
///
72+
bool get isDeclarationMember => false;
73+
74+
/// Returns `true` if this builder is a member of a class or mixin declaration.
75+
///
76+
/// For instance `A.constructor`, `method1a`, `method1b`, `method2a` and
77+
/// `method2b` in:
78+
///
79+
/// class A {
80+
/// A.constructor();
81+
/// method1a() {}
82+
/// static method1b() {}
83+
/// }
84+
/// mixin B {
85+
/// method2a() {}
86+
/// static method2b() {}
87+
/// }
88+
/// extends C on A {
89+
/// method3a() {} // Not a class member.
90+
/// static method3b() {} // Not a class member.
91+
/// }
92+
///
93+
bool get isClassMember => false;
94+
95+
/// Returns `true` if this builder is a member of an extension declaration.
96+
///
97+
/// For instance `method3a` and `method3b` in:
98+
///
99+
/// class A {
100+
/// A.constructor(); // Not an extension member.
101+
/// method1a() {} // Not an extension member.
102+
/// static method1b() {} // Not an extension member.
103+
/// }
104+
/// mixin B {
105+
/// method2a() {} // Not an extension member.
106+
/// static method2b() {} // Not an extension member.
107+
/// }
108+
/// extends C on A {
109+
/// method3a() {}
110+
/// static method3b() {}
111+
/// }
112+
///
113+
bool get isExtensionMember => false;
114+
115+
/// Returns `true` if this builder is an instance member of a class, mixin, or
116+
/// extension declaration.
117+
///
118+
/// For instance `method1a`, `method2a`, and `method3a` in:
119+
///
120+
/// class A {
121+
/// A.constructor(); // Not a declaration instance member.
122+
/// method1a() {}
123+
/// static method1b() {} // Not a declaration instance member.
124+
/// }
125+
/// mixin B {
126+
/// method2a() {}
127+
/// static method2b() {} // Not a declaration instance member.
128+
/// }
129+
/// extends C on A {
130+
/// method3a() {}
131+
/// static method3b() {} // Not a declaration instance member.
132+
/// }
133+
///
134+
bool get isDeclarationInstanceMember => false;
135+
136+
/// Returns `true` if this builder is an instance member of a class or mixin
137+
/// extension declaration.
138+
///
139+
/// For instance `method1a` and `method2a` in:
140+
///
141+
/// class A {
142+
/// A.constructor(); // Not a class instance member.
143+
/// method1a() {}
144+
/// static method1b() {} // Not a class instance member.
145+
/// }
146+
/// mixin B {
147+
/// method2a() {}
148+
/// static method2b() {} // Not a class instance member.
149+
/// }
150+
/// extends C on A {
151+
/// method3a() {} // Not a class instance member.
152+
/// static method3b() {} // Not a class instance member.
153+
/// }
154+
///
155+
bool get isClassInstanceMember => false;
156+
157+
/// Returns `true` if this builder is an instance member of an extension
158+
/// declaration.
159+
///
160+
/// For instance `method3a` in:
161+
///
162+
/// class A {
163+
/// A.constructor(); // Not an extension instance member.
164+
/// method1a() {} // Not an extension instance member.
165+
/// static method1b() {} // Not an extension instance member.
166+
/// }
167+
/// mixin B {
168+
/// method2a() {} // Not an extension instance member.
169+
/// static method2b() {} // Not an extension instance member.
170+
/// }
171+
/// extends C on A {
172+
/// method3a() {}
173+
/// static method3b() {} // Not an extension instance member.
174+
/// }
175+
///
176+
bool get isExtensionInstanceMember => false;
44177

45178
bool get isLocal => false;
46179

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2019, 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+
import '../messages.dart';
6+
import '../scope.dart';
7+
8+
import 'library_builder.dart';
9+
import 'metadata_builder.dart';
10+
import 'type_declaration_builder.dart';
11+
12+
abstract class DeclarationBuilder extends TypeDeclarationBuilder {
13+
final Scope scope;
14+
15+
final ScopeBuilder scopeBuilder;
16+
17+
DeclarationBuilder(List<MetadataBuilder> metadata, int modifiers, String name,
18+
LibraryBuilder parent, int charOffset, this.scope)
19+
: scopeBuilder = new ScopeBuilder(scope),
20+
super(metadata, modifiers, name, parent, charOffset);
21+
22+
LibraryBuilder get library {
23+
LibraryBuilder library = parent;
24+
return library.partOfLibrary ?? library;
25+
}
26+
27+
void addProblem(Message message, int charOffset, int length,
28+
{bool wasHandled: false, List<LocatedMessage> context}) {
29+
library.addProblem(message, charOffset, length, fileUri,
30+
wasHandled: wasHandled, context: context);
31+
}
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2019, 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+
import 'dart:core' hide MapEntry;
6+
7+
import 'package:kernel/ast.dart';
8+
9+
import 'declaration_builder.dart';
10+
import 'library_builder.dart';
11+
import 'metadata_builder.dart';
12+
import 'type_builder.dart';
13+
import 'type_variable_builder.dart';
14+
import '../scope.dart';
15+
16+
abstract class ExtensionBuilder extends DeclarationBuilder {
17+
final List<TypeVariableBuilder> typeParameters;
18+
final TypeBuilder onType;
19+
20+
ExtensionBuilder(
21+
List<MetadataBuilder> metadata,
22+
int modifiers,
23+
String name,
24+
LibraryBuilder parent,
25+
int charOffset,
26+
Scope scope,
27+
this.typeParameters,
28+
this.onType)
29+
: super(metadata, modifiers, name, parent, charOffset, scope);
30+
31+
@override
32+
DartType buildType(LibraryBuilder library, List<TypeBuilder> arguments) {
33+
throw new UnsupportedError("ExtensionBuilder.buildType is not supported.");
34+
}
35+
36+
@override
37+
DartType buildTypesWithBuiltArguments(
38+
LibraryBuilder library, List<DartType> arguments) {
39+
throw new UnsupportedError("ExtensionBuilder.buildTypesWithBuiltArguments "
40+
"is not supported.");
41+
}
42+
43+
@override
44+
bool get isExtension => true;
45+
46+
@override
47+
String get debugName => "ExtensionBuilder";
48+
}

pkg/front_end/lib/src/fasta/builder/field_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class FieldBuilder extends MemberBuilder {
8080
bool get isEligibleForInference {
8181
return !library.legacyMode &&
8282
type == null &&
83-
(hasInitializer || isInstanceMember);
83+
(hasInitializer || isClassInstanceMember);
8484
}
8585

8686
Field build(SourceLibraryBuilder library) {

pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class FormalParameterBuilder extends ModifierBuilder {
149149
// needed to generated noSuchMethod forwarders.
150150
final bool isConstConstructorParameter =
151151
(parent is ConstructorBuilder && parent.target.isConst);
152-
if ((isConstConstructorParameter || parent.isInstanceMember) &&
152+
if ((isConstConstructorParameter || parent.isClassInstanceMember) &&
153153
initializerToken != null) {
154154
final ClassBuilder classBuilder = parent.parent;
155155
Scope scope = classBuilder.scope;

pkg/front_end/lib/src/fasta/builder/member_builder.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,42 @@ import '../problems.dart' show unsupported;
99
import 'builder.dart'
1010
show ClassBuilder, Builder, LibraryBuilder, ModifierBuilder;
1111

12+
import 'declaration_builder.dart';
13+
import 'extension_builder.dart';
14+
1215
abstract class MemberBuilder extends ModifierBuilder {
1316
/// For top-level members, the parent is set correctly during
1417
/// construction. However, for class members, the parent is initially the
1518
/// library and updated later.
19+
@override
1620
Builder parent;
1721

22+
@override
1823
String get name;
1924

2025
MemberBuilder(this.parent, int charOffset) : super(parent, charOffset);
2126

22-
bool get isInstanceMember => isClassMember && !isStatic;
27+
bool get isDeclarationInstanceMember => isDeclarationMember && !isStatic;
28+
29+
@override
30+
bool get isClassInstanceMember => isClassMember && !isStatic;
31+
32+
@override
33+
bool get isExtensionInstanceMember => isExtensionMember && !isStatic;
34+
35+
@override
36+
bool get isDeclarationMember => parent is DeclarationBuilder;
2337

38+
@override
2439
bool get isClassMember => parent is ClassBuilder;
2540

26-
bool get isTopLevel => !isClassMember;
41+
@override
42+
bool get isExtensionMember => parent is ExtensionBuilder;
2743

44+
@override
45+
bool get isTopLevel => !isDeclarationMember;
46+
47+
@override
2848
bool get isNative => false;
2949

3050
bool get isRedirectingGenerativeConstructor => false;

pkg/front_end/lib/src/fasta/builder/modifier_builder.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import '../modifier.dart'
99
abstractMask,
1010
constMask,
1111
covariantMask,
12-
extensionDeclarationMask,
1312
externalMask,
1413
finalMask,
1514
hasConstConstructorMask,
@@ -57,10 +56,6 @@ abstract class ModifierBuilder extends Builder {
5756

5857
bool get isMixin => (modifiers & mixinDeclarationMask) != 0;
5958

60-
bool get isExtension => (modifiers & extensionDeclarationMask) != 0;
61-
62-
bool get isClassMember => false;
63-
6459
String get name;
6560

6661
bool get isNative => false;

0 commit comments

Comments
 (0)