31
31
import com .google .devtools .build .lib .packages .Package .Declarations ;
32
32
import com .google .devtools .build .lib .packages .Package .Metadata ;
33
33
import com .google .devtools .build .lib .packages .TargetRecorder .MacroNamespaceViolationException ;
34
- import com .google .devtools .build .lib .util .HashCodes ;
35
34
import com .google .devtools .build .lib .vfs .RootedPath ;
36
35
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
37
- import java .util .Objects ;
38
36
import java .util .Optional ;
39
37
import java .util .concurrent .Semaphore ;
40
38
import javax .annotation .Nullable ;
55
53
// another class of package piece obtained by evaluating a set of macros.
56
54
public abstract sealed class PackagePiece extends Packageoid
57
55
permits PackagePiece .ForBuildFile , PackagePiece .ForMacro {
58
- protected final Identifier identifier ;
59
-
60
- public Identifier getIdentifier () {
61
- return identifier ;
62
- }
56
+ public abstract PackagePieceIdentifier getIdentifier ();
63
57
64
58
/**
65
59
* Returns the {@link PackagePiece} corresponding to the evaluation of the BUILD file for this
@@ -150,100 +144,20 @@ public String getShortDescription() {
150
144
return "package piece " + getIdentifier ();
151
145
}
152
146
153
- private PackagePiece (Identifier identifier ) {
154
- this .identifier = identifier ;
155
- }
156
-
157
- /** A unique identifier for a {@link PackagePiece}. */
158
- public static sealed class Identifier permits PackagePiece .ForMacro .Identifier {
159
- protected final PackageIdentifier packageIdentifier ;
160
- // BUILD file label for a {@link PackagePiece.ForBuildFile}, or the label of the macro class
161
- // definition for a {@link PackagePiece.ForMacro}.
162
- protected final Label definingLabel ;
163
-
164
- /**
165
- * The canonical form of the package name if this is an identifier for a {@link
166
- * PackagePiece.ForBuildFile}, or the canonical form of the macro instance name if this is an
167
- * identifier for a {@link PackagePiece.ForMacro}.
168
- *
169
- * <p>This string is not unique, since multiple macro instances can have the same name. Intended
170
- * to be used in combination with {@link #getCanonicalFormDefinedBy}, or with {@link
171
- * #getDefiningLabel} + {@link #getDefiningSymbol} pair.
172
- */
173
- public String getCanonicalFormName () {
174
- String pkgIdString = packageIdentifier .getCanonicalForm ();
175
- return getInstanceName () != null
176
- ? String .format ("%s:%s" , pkgIdString , getInstanceName ())
177
- : pkgIdString ;
178
- }
179
-
180
- public String getCanonicalFormDefinedBy () {
181
- String definingLabelString = definingLabel .getCanonicalForm ();
182
- return getDefiningSymbol () != null
183
- ? String .format ("%s%%%s" , definingLabelString , getDefiningSymbol ())
184
- : definingLabelString ;
185
- }
186
-
187
- @ Nullable
188
- protected String getInstanceName () {
189
- return null ;
190
- }
191
-
192
- /**
193
- * BUILD file label for a {@link PackagePiece.ForBuildFile}, or the label of the macro class
194
- * definition for a {@link PackagePiece.ForMacro}.
195
- */
196
- public Label getDefiningLabel () {
197
- return definingLabel ;
198
- }
199
-
200
- @ Nullable
201
- public String getDefiningSymbol () {
202
- return null ;
203
- }
204
-
205
- @ Override
206
- public String toString () {
207
- return String .format ("%s defined by %s" , getCanonicalFormName (), getCanonicalFormDefinedBy ());
208
- }
209
-
210
- @ Override
211
- public boolean equals (Object other ) {
212
- if (this == other ) {
213
- return true ;
214
- }
215
- if (!(other instanceof PackagePiece .Identifier that )) {
216
- return false ;
217
- }
218
- return this .packageIdentifier .equals (that .packageIdentifier )
219
- && this .definingLabel .equals (that .definingLabel )
220
- // Valid because PackagePiece.ForMacro.Identifier requires defining symbol and instance
221
- // name to be non-null.
222
- && Objects .equals (this .getDefiningSymbol (), that .getDefiningSymbol ())
223
- && Objects .equals (this .getInstanceName (), that .getInstanceName ());
224
- }
225
-
226
- @ Override
227
- public int hashCode () {
228
- return HashCodes .hashObjects (
229
- packageIdentifier , definingLabel , getDefiningSymbol (), getInstanceName ());
230
- }
231
-
232
- @ VisibleForTesting
233
- Identifier (PackageIdentifier packageIdentifier , Label definingLabel ) {
234
- this .packageIdentifier = packageIdentifier ;
235
- this .definingLabel = definingLabel ;
236
- }
237
- }
238
-
239
147
/**
240
148
* A {@link PackagePiece} obtained by evaluating a BUILD file, without expanding any symbolic
241
149
* macros.
242
150
*/
243
151
public static final class ForBuildFile extends PackagePiece {
152
+ private final PackagePieceIdentifier .ForBuildFile identifier ;
244
153
private final Metadata metadata ;
245
154
private final Declarations declarations ;
246
155
156
+ @ Override
157
+ public PackagePieceIdentifier .ForBuildFile getIdentifier () {
158
+ return identifier ;
159
+ }
160
+
247
161
@ Override
248
162
public PackagePiece .ForBuildFile getPackagePieceForBuildFile () {
249
163
return this ;
@@ -266,7 +180,9 @@ public void checkMacroNamespaceCompliance(Target target) {
266
180
}
267
181
268
182
private ForBuildFile (Metadata metadata ) {
269
- super (new Identifier (metadata .packageIdentifier (), metadata .buildFileLabel ()));
183
+ this .identifier =
184
+ new PackagePieceIdentifier .ForBuildFile (
185
+ metadata .packageIdentifier (), metadata .buildFileLabel ());
270
186
this .metadata = metadata ;
271
187
this .declarations = new Declarations ();
272
188
}
@@ -394,11 +310,17 @@ private Builder(
394
310
395
311
/** A {@link PackagePiece} obtained by evaluating a symbolic macro instance. */
396
312
public static final class ForMacro extends PackagePiece {
313
+ private final PackagePieceIdentifier .ForMacro identifier ;
397
314
private final MacroInstance evaluatedMacro ;
398
315
private final PackagePiece .ForBuildFile pieceForBuildFile ;
399
316
// Null until the package piece is fully initialized by its builder's {@code finishBuild()}.
400
317
@ Nullable private ImmutableSet <String > macroNamespaceViolations = null ;
401
318
319
+ @ Override
320
+ public PackagePieceIdentifier .ForMacro getIdentifier () {
321
+ return identifier ;
322
+ }
323
+
402
324
@ Override
403
325
public PackagePiece .ForBuildFile getPackagePieceForBuildFile () {
404
326
return pieceForBuildFile ;
@@ -444,12 +366,12 @@ public void checkMacroNamespaceCompliance(Target target)
444
366
}
445
367
446
368
private ForMacro (MacroInstance evaluatedMacro , PackagePiece .ForBuildFile pieceForBuildFile ) {
447
- super (
448
- new PackagePiece .ForMacro . Identifier (
369
+ this . identifier =
370
+ new PackagePieceIdentifier .ForMacro (
449
371
pieceForBuildFile .getPackageIdentifier (),
450
372
evaluatedMacro .getMacroClass ().getDefiningBzlLabel (),
451
373
/* definingSymbol= */ evaluatedMacro .getMacroClass ().getName (),
452
- /* instanceName= */ evaluatedMacro .getName ())) ;
374
+ /* instanceName= */ evaluatedMacro .getName ());
453
375
this .evaluatedMacro = evaluatedMacro ;
454
376
this .pieceForBuildFile = pieceForBuildFile ;
455
377
}
@@ -480,38 +402,6 @@ public static Builder newBuilder(
480
402
trackFullMacroInformation );
481
403
}
482
404
483
- /**
484
- * A unique identifier for a {@link PackagePiece.ForMacro}.
485
- *
486
- * <p>Exists purely as a memory optimization to avoid allocating the defining symbol and
487
- * instance name for {@link PackagePiece.ForBuildFile} objects.
488
- */
489
- public static final class Identifier extends PackagePiece .Identifier {
490
- private final String definingSymbol ;
491
- private final String instanceName ;
492
-
493
- @ Override
494
- public String getDefiningSymbol () {
495
- return definingSymbol ;
496
- }
497
-
498
- @ Override
499
- public String getInstanceName () {
500
- return instanceName ;
501
- }
502
-
503
- @ VisibleForTesting
504
- Identifier (
505
- PackageIdentifier packageIdentifier ,
506
- Label definingLabel ,
507
- String definingSymbol ,
508
- String instanceName ) {
509
- super (packageIdentifier , definingLabel );
510
- this .definingSymbol = checkNotNull (definingSymbol );
511
- this .instanceName = checkNotNull (instanceName );
512
- }
513
- }
514
-
515
405
/** A builder for {@link PackagePieceForMacro} objects. */
516
406
public static class Builder extends TargetDefinitionContext {
517
407
0 commit comments