|
| 1 | +// Copyright (c) 2025, 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:_fe_analyzer_shared/src/messages/severity.dart' show Severity; |
| 6 | +import 'package:kernel/ast.dart' show Annotatable, Library, Version; |
| 7 | +import 'package:kernel/reference_from_index.dart'; |
| 8 | + |
| 9 | +import '../api_prototype/experimental_flags.dart'; |
| 10 | +import '../base/combinator.dart' show CombinatorBuilder; |
| 11 | +import '../base/export.dart' show Export; |
| 12 | +import '../base/loader.dart' show Loader; |
| 13 | +import '../base/messages.dart' |
| 14 | + show LocatedMessage, Message, ProblemReporting, noLength; |
| 15 | +import '../base/name_space.dart'; |
| 16 | +import '../base/scope.dart'; |
| 17 | +import '../fragment/fragment.dart'; |
| 18 | +import '../kernel/body_builder_context.dart'; |
| 19 | +import '../source/name_space_builder.dart'; |
| 20 | +import '../source/offset_map.dart'; |
| 21 | +import '../source/outline_builder.dart'; |
| 22 | +import '../source/source_class_builder.dart'; |
| 23 | +import '../source/source_library_builder.dart'; |
| 24 | +import '../source/source_loader.dart'; |
| 25 | +import 'builder.dart'; |
| 26 | +import 'declaration_builders.dart'; |
| 27 | +import 'library_builder.dart'; |
| 28 | +import 'metadata_builder.dart'; |
| 29 | +import 'prefix_builder.dart'; |
| 30 | +import 'type_builder.dart'; |
| 31 | + |
| 32 | +sealed class CompilationUnit { |
| 33 | + /// Returns the import uri for the compilation unit. |
| 34 | + /// |
| 35 | + /// This is the canonical uri for the compilation unit, for instance |
| 36 | + /// 'dart:core'. |
| 37 | + Uri get importUri; |
| 38 | + |
| 39 | + Uri get fileUri; |
| 40 | + |
| 41 | + bool get isSynthetic; |
| 42 | + |
| 43 | + /// If true, the library is not supported through the 'dart.library.*' value |
| 44 | + /// used in conditional imports and `bool.fromEnvironment` constants. |
| 45 | + bool get isUnsupported; |
| 46 | + |
| 47 | + Loader get loader; |
| 48 | + |
| 49 | + /// The [LibraryBuilder] for the library that this compilation unit belongs |
| 50 | + /// to. |
| 51 | + /// |
| 52 | + /// This is only valid after `SourceLoader.resolveParts` has be called. |
| 53 | + LibraryBuilder get libraryBuilder; |
| 54 | + |
| 55 | + bool get isPart; |
| 56 | + |
| 57 | + bool get isAugmenting; |
| 58 | + |
| 59 | + LibraryBuilder? get partOfLibrary; |
| 60 | + |
| 61 | + /// Returns the [Uri]s for the libraries that this library depend upon, either |
| 62 | + /// through import or export. |
| 63 | + Iterable<Uri> get dependencies; |
| 64 | + |
| 65 | + void recordAccess( |
| 66 | + CompilationUnit accessor, int charOffset, int length, Uri fileUri); |
| 67 | + |
| 68 | + List<Export> get exporters; |
| 69 | + |
| 70 | + void addExporter(SourceCompilationUnit exporter, |
| 71 | + List<CombinatorBuilder>? combinators, int charOffset); |
| 72 | + |
| 73 | + /// Add a problem with a severity determined by the severity of the message. |
| 74 | + /// |
| 75 | + /// If [fileUri] is null, it defaults to `this.fileUri`. |
| 76 | + /// |
| 77 | + /// See `Loader.addMessage` for an explanation of the |
| 78 | + /// arguments passed to this method. |
| 79 | + void addProblem(Message message, int charOffset, int length, Uri? fileUri, |
| 80 | + {bool wasHandled = false, |
| 81 | + List<LocatedMessage>? context, |
| 82 | + Severity? severity, |
| 83 | + bool problemOnLibrary = false}); |
| 84 | +} |
| 85 | + |
| 86 | +abstract class DillCompilationUnit implements CompilationUnit {} |
| 87 | + |
| 88 | +abstract class SourceCompilationUnit |
| 89 | + implements CompilationUnit, LibraryFragment { |
| 90 | + OutlineBuilder createOutlineBuilder(); |
| 91 | + |
| 92 | + /// Creates a [SourceLibraryBuilder] for with this [SourceCompilationUnit] as |
| 93 | + /// the main compilation unit. |
| 94 | + SourceLibraryBuilder createLibrary([Library? library]); |
| 95 | + |
| 96 | + @override |
| 97 | + SourceLoader get loader; |
| 98 | + |
| 99 | + OffsetMap get offsetMap; |
| 100 | + |
| 101 | + /// The language version of this compilation unit as defined by the language |
| 102 | + /// version of the package it belongs to, if present, or the current language |
| 103 | + /// version otherwise. |
| 104 | + /// |
| 105 | + /// This language version will be used as the language version for the |
| 106 | + /// compilation unit if the compilation unit does not contain an explicit |
| 107 | + /// `@dart=` annotation. |
| 108 | + LanguageVersion get packageLanguageVersion; |
| 109 | + |
| 110 | + /// Set the language version to an explicit major and minor version. |
| 111 | + /// |
| 112 | + /// The default language version specified by the `package_config.json` file |
| 113 | + /// is passed to the constructor, but the library can have source code that |
| 114 | + /// specifies another one which should be supported. |
| 115 | + /// |
| 116 | + /// Only the first registered language version is used. |
| 117 | + /// |
| 118 | + /// [offset] and [length] refers to the offset and length of the source code |
| 119 | + /// specifying the language version. |
| 120 | + void registerExplicitLanguageVersion(Version version, |
| 121 | + {int offset = 0, int length = noLength}); |
| 122 | + |
| 123 | + // TODO(johnniwinther): Remove this. |
| 124 | + bool get forAugmentationLibrary; |
| 125 | + |
| 126 | + // TODO(johnniwinther): Remove this. |
| 127 | + bool get forPatchLibrary; |
| 128 | + |
| 129 | + /// If this is an compilation unit for an augmentation library, returns the |
| 130 | + /// import uri for the origin library. Otherwise the [importUri] for the |
| 131 | + /// compilation unit itself. |
| 132 | + Uri get originImportUri; |
| 133 | + |
| 134 | + @override |
| 135 | + SourceLibraryBuilder get libraryBuilder; |
| 136 | + |
| 137 | + /// The parent compilation unit. |
| 138 | + /// |
| 139 | + /// This is the compilation unit that included this compilation unit as a |
| 140 | + /// part or `null` if this is the root compilation unit of a library. |
| 141 | + SourceCompilationUnit? get parentCompilationUnit; |
| 142 | + |
| 143 | + LibraryFeatures get libraryFeatures; |
| 144 | + |
| 145 | + /// Returns `true` if the compilation unit is part of a `dart:` library. |
| 146 | + bool get isDartLibrary; |
| 147 | + |
| 148 | + LanguageVersion get languageVersion; |
| 149 | + |
| 150 | + String? get name; |
| 151 | + |
| 152 | + int finishNativeMethods(); |
| 153 | + |
| 154 | + String? get partOfName; |
| 155 | + |
| 156 | + Uri? get partOfUri; |
| 157 | + |
| 158 | + List<MetadataBuilder>? get metadata; |
| 159 | + |
| 160 | + /// The scope of this compilation unit. |
| 161 | + /// |
| 162 | + /// This is the enclosing scope for all declarations within the compilation |
| 163 | + /// unit. |
| 164 | + LookupScope get compilationUnitScope; |
| 165 | + |
| 166 | + /// The prefix scope of this compilation unit. |
| 167 | + /// |
| 168 | + /// This contains all imports with prefixes declared in this compilation unit. |
| 169 | + LookupScope get prefixScope; |
| 170 | + |
| 171 | + NameSpace get prefixNameSpace; |
| 172 | + |
| 173 | + bool get mayImplementRestrictedTypes; |
| 174 | + |
| 175 | + void takeMixinApplications( |
| 176 | + Map<SourceClassBuilder, TypeBuilder> mixinApplications); |
| 177 | + |
| 178 | + void addDependencies(Library library, Set<SourceCompilationUnit> seen); |
| 179 | + |
| 180 | + /// Runs through all part directives in this compilation unit and adds the |
| 181 | + /// compilation unit for the parts to the [libraryBuilder] by adding them |
| 182 | + /// to [includedParts] |
| 183 | + /// |
| 184 | + /// [usedParts] is used to ensure that a compilation unit is only included in |
| 185 | + /// one library. If the compilation unit is part of two libraries, it is only |
| 186 | + /// included in the first and reported as an error on the second. |
| 187 | + /// |
| 188 | + /// This should only be called on the main compilation unit for |
| 189 | + /// [libraryBuilder]. Inclusion of nested parts is from within this method, |
| 190 | + /// using [becomePart] for each individual subpart. |
| 191 | + void includeParts( |
| 192 | + List<SourceCompilationUnit> includedParts, Set<Uri> usedParts); |
| 193 | + |
| 194 | + /// Includes this compilation unit as a part of [libraryBuilder] with |
| 195 | + /// [parentCompilationUnit] as the parent compilation unit. |
| 196 | + /// |
| 197 | + /// The parent compilation unit is used to define the compilation unit |
| 198 | + /// scope of this compilation unit. |
| 199 | + /// |
| 200 | + /// All fragment in this compilation unit will be added to |
| 201 | + /// [libraryNameSpaceBuilder]. |
| 202 | + /// |
| 203 | + /// If parts with parts is supported (through the enhanced parts feature), |
| 204 | + /// the compilation units of the part directives in this compilation unit |
| 205 | + /// will be added [libraryBuilder] recursively. |
| 206 | + void becomePart( |
| 207 | + SourceLibraryBuilder libraryBuilder, |
| 208 | + LibraryNameSpaceBuilder libraryNameSpaceBuilder, |
| 209 | + SourceCompilationUnit parentCompilationUnit, |
| 210 | + List<SourceCompilationUnit> includedParts, |
| 211 | + Set<Uri> usedParts, |
| 212 | + {required bool allowPartInParts}); |
| 213 | + |
| 214 | + void buildOutlineExpressions( |
| 215 | + {required Annotatable annotatable, |
| 216 | + required Uri annotatableFileUri, |
| 217 | + required BodyBuilderContext bodyBuilderContext}); |
| 218 | + |
| 219 | + /// Reports that [feature] is not enabled, using [charOffset] and |
| 220 | + /// [length] for the location of the message. |
| 221 | + /// |
| 222 | + /// Return the primary message. |
| 223 | + Message reportFeatureNotEnabled( |
| 224 | + LibraryFeature feature, Uri fileUri, int charOffset, int length); |
| 225 | + |
| 226 | + /// Registers that [augmentation] is a part of the library for which this is |
| 227 | + /// the main compilation unit. |
| 228 | + void registerAugmentation(CompilationUnit augmentation); |
| 229 | + |
| 230 | + /// Reports [message] on all compilation units that access this compilation |
| 231 | + /// unit. |
| 232 | + void addProblemAtAccessors(Message message); |
| 233 | + |
| 234 | + Iterable<LibraryAccess> get accessors; |
| 235 | + |
| 236 | + /// Non-null if this library causes an error upon access, that is, there was |
| 237 | + /// an error reading its source. |
| 238 | + abstract Message? accessProblem; |
| 239 | + |
| 240 | + /// Add a problem that might not be reported immediately. |
| 241 | + /// |
| 242 | + /// Problems will be issued after source information has been added. |
| 243 | + /// Once the problems has been issued, adding a new "postponed" problem will |
| 244 | + /// be issued immediately. |
| 245 | + void addPostponedProblem( |
| 246 | + Message message, int charOffset, int length, Uri fileUri); |
| 247 | + |
| 248 | + void issuePostponedProblems(); |
| 249 | + |
| 250 | + void markLanguageVersionFinal(); |
| 251 | + |
| 252 | + /// Index of the library we use references for. |
| 253 | + IndexedLibrary? get indexedLibrary; |
| 254 | + |
| 255 | + void addSyntheticImport( |
| 256 | + {required Uri importUri, |
| 257 | + required String? prefix, |
| 258 | + required List<CombinatorBuilder>? combinators, |
| 259 | + required bool deferred}); |
| 260 | + |
| 261 | + void addImportedBuilderToScope( |
| 262 | + {required String name, |
| 263 | + required NamedBuilder builder, |
| 264 | + required int charOffset}); |
| 265 | + |
| 266 | + void addImportsToScope(); |
| 267 | + |
| 268 | + void buildOutlineNode(Library library); |
| 269 | + |
| 270 | + int finishDeferredLoadTearOffs(Library library); |
| 271 | + |
| 272 | + /// This method instantiates type parameters to their bounds in some cases |
| 273 | + /// where they were omitted by the programmer and not provided by the type |
| 274 | + /// inference. The method returns the number of distinct type parameters |
| 275 | + /// that were instantiated in this library. |
| 276 | + int computeDefaultTypes(TypeBuilder dynamicType, TypeBuilder nullType, |
| 277 | + TypeBuilder bottomType, ClassBuilder objectClass); |
| 278 | + |
| 279 | + /// Computes variances of type parameters on typedefs. |
| 280 | + /// |
| 281 | + /// The variance property of type parameters on typedefs is computed from the |
| 282 | + /// use of the parameters in the right-hand side of the typedef definition. |
| 283 | + int computeVariances(); |
| 284 | + |
| 285 | + /// Adds all unbound nominal parameters to [nominalParameters] and unbound |
| 286 | + /// structural parameters to [structuralParameters], mapping them to |
| 287 | + /// [libraryBuilder]. |
| 288 | + /// |
| 289 | + /// This is used to compute the bounds of type parameters while taking the |
| 290 | + /// bound dependencies, which might span multiple libraries, into account. |
| 291 | + void collectUnboundTypeParameters( |
| 292 | + SourceLibraryBuilder libraryBuilder, |
| 293 | + Map<NominalParameterBuilder, SourceLibraryBuilder> nominalParameters, |
| 294 | + Map<StructuralParameterBuilder, SourceLibraryBuilder> |
| 295 | + structuralParameters); |
| 296 | + |
| 297 | + /// Adds [prefixFragment] to library name space. |
| 298 | + /// |
| 299 | + /// Returns `true` if the prefix name was new to the name space. Otherwise the |
| 300 | + /// prefix was merged with an existing prefix of the same name. |
| 301 | + // TODO(johnniwinther): Remove this. |
| 302 | + bool addPrefixFragment( |
| 303 | + String name, PrefixFragment prefixFragment, int charOffset); |
| 304 | + |
| 305 | + int resolveTypes(ProblemReporting problemReporting); |
| 306 | +} |
0 commit comments