Skip to content

Commit 4306e07

Browse files
johnniwintherCommit Queue
authored and
Commit Queue
committed
[cfe] Add (Source)ViewBuilder
Includes refactoring to share code with (Source)ExtensionBuilder. Change-Id: If3cc66fa9f868d23ef756f80bc2f447cb534c5d1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268763 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent e1ea341 commit 4306e07

20 files changed

+949
-386
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright (c) 2022, 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 'package:kernel/ast.dart';
6+
import 'package:kernel/class_hierarchy.dart';
7+
8+
import '../messages.dart';
9+
import '../problems.dart';
10+
import '../scope.dart';
11+
import '../source/source_library_builder.dart';
12+
import 'builder.dart';
13+
import 'declaration_builder.dart';
14+
import 'field_builder.dart';
15+
import 'library_builder.dart';
16+
import 'member_builder.dart';
17+
import 'nullability_builder.dart';
18+
import 'type_builder.dart';
19+
import 'type_variable_builder.dart';
20+
21+
/// Shared implementation between extension and view builders.
22+
mixin DeclarationBuilderMixin implements DeclarationBuilder {
23+
/// Type parameters declared.
24+
///
25+
/// This is `null` if the declaration is not generic.
26+
List<TypeVariableBuilder>? get typeParameters;
27+
28+
/// Lookup a static member of this declaration.
29+
@override
30+
Builder? findStaticBuilder(
31+
String name, int charOffset, Uri fileUri, LibraryBuilder accessingLibrary,
32+
{bool isSetter = false}) {
33+
if (accessingLibrary.nameOriginBuilder.origin !=
34+
libraryBuilder.nameOriginBuilder.origin &&
35+
name.startsWith("_")) {
36+
return null;
37+
}
38+
Builder? declaration = isSetter
39+
? scope.lookupSetter(name, charOffset, fileUri, isInstanceScope: false)
40+
: scope.lookup(name, charOffset, fileUri, isInstanceScope: false);
41+
// TODO(johnniwinther): Handle patched extensions/views.
42+
return declaration;
43+
}
44+
45+
@override
46+
DartType buildAliasedType(
47+
LibraryBuilder library,
48+
NullabilityBuilder nullabilityBuilder,
49+
List<TypeBuilder>? arguments,
50+
TypeUse typeUse,
51+
Uri fileUri,
52+
int charOffset,
53+
ClassHierarchyBase? hierarchy,
54+
{required bool hasExplicitTypeArguments}) {
55+
return buildAliasedTypeWithBuiltArguments(
56+
library,
57+
nullabilityBuilder.build(library),
58+
_buildAliasedTypeArguments(library, arguments, hierarchy),
59+
typeUse,
60+
fileUri,
61+
charOffset,
62+
hasExplicitTypeArguments: hasExplicitTypeArguments);
63+
}
64+
65+
@override
66+
int get typeVariablesCount => typeParameters?.length ?? 0;
67+
68+
List<DartType> _buildAliasedTypeArguments(LibraryBuilder library,
69+
List<TypeBuilder>? arguments, ClassHierarchyBase? hierarchy) {
70+
if (arguments == null && typeParameters == null) {
71+
return <DartType>[];
72+
}
73+
74+
if (arguments == null && typeParameters != null) {
75+
List<DartType> result =
76+
new List<DartType>.generate(typeParameters!.length, (int i) {
77+
return typeParameters![i].defaultType!.buildAliased(
78+
library, TypeUse.defaultTypeAsTypeArgument, hierarchy);
79+
}, growable: true);
80+
if (library is SourceLibraryBuilder) {
81+
library.inferredTypes.addAll(result);
82+
}
83+
return result;
84+
}
85+
86+
if (arguments != null && arguments.length != typeVariablesCount) {
87+
// That should be caught and reported as a compile-time error earlier.
88+
return unhandled(
89+
templateTypeArgumentMismatch
90+
.withArguments(typeVariablesCount)
91+
.problemMessage,
92+
"buildTypeArguments",
93+
-1,
94+
null);
95+
}
96+
97+
assert(arguments!.length == typeVariablesCount);
98+
List<DartType> result =
99+
new List<DartType>.generate(arguments!.length, (int i) {
100+
return arguments[i]
101+
.buildAliased(library, TypeUse.typeArgument, hierarchy);
102+
}, growable: true);
103+
return result;
104+
}
105+
106+
void forEach(void f(String name, Builder builder)) {
107+
scope
108+
.filteredNameIterator(
109+
includeDuplicates: false, includeAugmentations: false)
110+
.forEach(f);
111+
}
112+
113+
@override
114+
InterfaceType? get thisType => null;
115+
116+
@override
117+
Builder? lookupLocalMember(String name,
118+
{bool setter = false, bool required = false}) {
119+
// TODO(johnniwinther): Support patching on extensions/views.
120+
Builder? builder = scope.lookupLocalMember(name, setter: setter);
121+
if (required && builder == null) {
122+
internalProblem(
123+
templateInternalProblemNotFoundIn.withArguments(
124+
name, fullNameForErrors),
125+
-1,
126+
null);
127+
}
128+
return builder;
129+
}
130+
131+
Builder? lookupLocalMemberByName(Name name,
132+
{bool setter = false, bool required = false}) {
133+
Builder? builder =
134+
lookupLocalMember(name.text, setter: setter, required: required);
135+
if (builder == null && setter) {
136+
// When looking up setters, we include assignable fields.
137+
builder = lookupLocalMember(name.text, setter: false, required: required);
138+
if (builder is! FieldBuilder || !builder.isAssignable) {
139+
builder = null;
140+
}
141+
}
142+
if (builder != null) {
143+
if (name.isPrivate && libraryBuilder.library != name.library) {
144+
builder = null;
145+
} else if (builder is FieldBuilder &&
146+
!builder.isStatic &&
147+
!builder.isExternal) {
148+
// Non-external extension instance fields are invalid.
149+
builder = null;
150+
} else if (builder.isDuplicate) {
151+
// Duplicates are not visible in the instance scope.
152+
builder = null;
153+
} else if (builder is MemberBuilder && builder.isConflictingSetter) {
154+
// Conflicting setters are not visible in the instance scope.
155+
// TODO(johnniwinther): Should we return an [AmbiguousBuilder] here and
156+
// above?
157+
builder = null;
158+
}
159+
}
160+
return builder;
161+
}
162+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:kernel/ast.dart';
66

77
import '../messages.dart';
88
import '../scope.dart';
9-
109
import 'builder.dart';
1110
import 'library_builder.dart';
1211
import 'metadata_builder.dart';

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

Lines changed: 2 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:kernel/ast.dart';
6-
import 'package:kernel/class_hierarchy.dart';
7-
8-
import '../fasta_codes.dart'
9-
show templateInternalProblemNotFoundIn, templateTypeArgumentMismatch;
10-
import '../problems.dart';
116
import '../scope.dart';
127
import '../source/source_library_builder.dart';
138
import 'builder.dart';
9+
import 'builder_mixins.dart';
1410
import 'declaration_builder.dart';
15-
import 'field_builder.dart';
1611
import 'library_builder.dart';
17-
import 'member_builder.dart';
1812
import 'metadata_builder.dart';
19-
import 'nullability_builder.dart';
2013
import 'type_builder.dart';
2114
import 'type_variable_builder.dart';
2215

@@ -50,54 +43,12 @@ abstract class ExtensionBuilder implements DeclarationBuilder {
5043
}
5144

5245
abstract class ExtensionBuilderImpl extends DeclarationBuilderImpl
46+
with DeclarationBuilderMixin
5347
implements ExtensionBuilder {
5448
ExtensionBuilderImpl(List<MetadataBuilder>? metadata, int modifiers,
5549
String name, LibraryBuilder parent, int charOffset, Scope scope)
5650
: super(metadata, modifiers, name, parent, charOffset, scope);
5751

58-
/// Lookup a static member of this declaration.
59-
@override
60-
Builder? findStaticBuilder(
61-
String name, int charOffset, Uri fileUri, LibraryBuilder accessingLibrary,
62-
{bool isSetter = false}) {
63-
if (accessingLibrary.nameOriginBuilder.origin !=
64-
libraryBuilder.nameOriginBuilder.origin &&
65-
name.startsWith("_")) {
66-
return null;
67-
}
68-
Builder? declaration = isSetter
69-
? scope.lookupSetter(name, charOffset, fileUri, isInstanceScope: false)
70-
: scope.lookup(name, charOffset, fileUri, isInstanceScope: false);
71-
// TODO(johnniwinther): Handle patched extensions.
72-
return declaration;
73-
}
74-
75-
@override
76-
DartType buildAliasedType(
77-
LibraryBuilder library,
78-
NullabilityBuilder nullabilityBuilder,
79-
List<TypeBuilder>? arguments,
80-
TypeUse typeUse,
81-
Uri fileUri,
82-
int charOffset,
83-
ClassHierarchyBase? hierarchy,
84-
{required bool hasExplicitTypeArguments}) {
85-
if (library is SourceLibraryBuilder &&
86-
library.libraryFeatures.extensionTypes.isEnabled) {
87-
return buildAliasedTypeWithBuiltArguments(
88-
library,
89-
nullabilityBuilder.build(library),
90-
_buildAliasedTypeArguments(library, arguments, hierarchy),
91-
typeUse,
92-
fileUri,
93-
charOffset,
94-
hasExplicitTypeArguments: hasExplicitTypeArguments);
95-
} else {
96-
throw new UnsupportedError("ExtensionBuilder.buildType is not supported"
97-
"in library '${library.importUri}'.");
98-
}
99-
}
100-
10152
@override
10253
DartType buildAliasedTypeWithBuiltArguments(
10354
LibraryBuilder library,
@@ -117,109 +68,9 @@ abstract class ExtensionBuilderImpl extends DeclarationBuilderImpl
11768
}
11869
}
11970

120-
@override
121-
int get typeVariablesCount => typeParameters?.length ?? 0;
122-
123-
List<DartType> _buildAliasedTypeArguments(LibraryBuilder library,
124-
List<TypeBuilder>? arguments, ClassHierarchyBase? hierarchy) {
125-
if (arguments == null && typeParameters == null) {
126-
return <DartType>[];
127-
}
128-
129-
if (arguments == null && typeParameters != null) {
130-
List<DartType> result =
131-
new List<DartType>.generate(typeParameters!.length, (int i) {
132-
return typeParameters![i].defaultType!.buildAliased(
133-
library, TypeUse.defaultTypeAsTypeArgument, hierarchy);
134-
}, growable: true);
135-
if (library is SourceLibraryBuilder) {
136-
library.inferredTypes.addAll(result);
137-
}
138-
return result;
139-
}
140-
141-
if (arguments != null && arguments.length != typeVariablesCount) {
142-
// That should be caught and reported as a compile-time error earlier.
143-
return unhandled(
144-
templateTypeArgumentMismatch
145-
.withArguments(typeVariablesCount)
146-
.problemMessage,
147-
"buildTypeArguments",
148-
-1,
149-
null);
150-
}
151-
152-
assert(arguments!.length == typeVariablesCount);
153-
List<DartType> result =
154-
new List<DartType>.generate(arguments!.length, (int i) {
155-
return arguments[i]
156-
.buildAliased(library, TypeUse.typeArgument, hierarchy);
157-
}, growable: true);
158-
return result;
159-
}
160-
161-
@override
162-
void forEach(void f(String name, Builder builder)) {
163-
scope
164-
.filteredNameIterator(
165-
includeDuplicates: false, includeAugmentations: false)
166-
.forEach(f);
167-
}
168-
16971
@override
17072
bool get isExtension => true;
17173

172-
@override
173-
InterfaceType? get thisType => null;
174-
175-
@override
176-
Builder? lookupLocalMember(String name,
177-
{bool setter = false, bool required = false}) {
178-
// TODO(johnniwinther): Support patching on extensions.
179-
Builder? builder = scope.lookupLocalMember(name, setter: setter);
180-
if (required && builder == null) {
181-
internalProblem(
182-
templateInternalProblemNotFoundIn.withArguments(
183-
name, fullNameForErrors),
184-
-1,
185-
null);
186-
}
187-
return builder;
188-
}
189-
190-
@override
191-
Builder? lookupLocalMemberByName(Name name,
192-
{bool setter = false, bool required = false}) {
193-
Builder? builder =
194-
lookupLocalMember(name.text, setter: setter, required: required);
195-
if (builder == null && setter) {
196-
// When looking up setters, we include assignable fields.
197-
builder = lookupLocalMember(name.text, setter: false, required: required);
198-
if (builder is! FieldBuilder || !builder.isAssignable) {
199-
builder = null;
200-
}
201-
}
202-
if (builder != null) {
203-
if (name.isPrivate && libraryBuilder.library != name.library) {
204-
builder = null;
205-
} else if (builder is FieldBuilder &&
206-
!builder.isStatic &&
207-
!builder.isExternal) {
208-
// Non-external extension instance fields are invalid.
209-
builder = null;
210-
} else if (builder.isDuplicate) {
211-
// Duplicates are not visible in the instance scope.
212-
builder = null;
213-
} else if (builder is MemberBuilder && builder.isConflictingSetter) {
214-
// Conflicting setters are not visible in the instance scope.
215-
// TODO(johnniwinther): Should we return an [AmbiguousBuilder] here and
216-
// above?
217-
builder = null;
218-
}
219-
}
220-
return builder;
221-
}
222-
22374
@override
22475
String get debugName => "ExtensionBuilder";
22576
}

0 commit comments

Comments
 (0)