Skip to content

Commit 7c867ca

Browse files
committed
[NFC] Split DeclAttrOptions into two types
We’re running out of bits in DeclAttrOptions, so split it in two: DeclAttrRequirements contains all the `On*` options that describe the declarations allowed to have the attribute, while the other options are now DeclAttrBehaviors. This commit also sorts the entries in DeclAttr.def by serialization code and improves the formatting of the file.
1 parent 1a08018 commit 7c867ca

File tree

8 files changed

+650
-320
lines changed

8 files changed

+650
-320
lines changed

include/swift/AST/Attr.h

+57-47
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "swift/Basic/SourceLoc.h"
4343
#include "swift/Basic/UUID.h"
4444
#include "swift/Basic/Version.h"
45+
#include "llvm/ADT/bit.h"
4546
#include "llvm/ADT/DenseMapInfo.h"
4647
#include "llvm/ADT/SmallVector.h"
4748
#include "llvm/ADT/StringRef.h"
@@ -264,7 +265,7 @@ class DeclAttribute : public AttributeBase {
264265
};
265266

266267
public:
267-
enum DeclAttrOptions : uint64_t {
268+
enum DeclAttrRequirements : uint64_t {
268269
// There is one entry for each DeclKind, and some higher level buckets
269270
// below. These are used in Attr.def to control which kinds of declarations
270271
// an attribute can be attached to.
@@ -303,71 +304,80 @@ class DeclAttribute : public AttributeBase {
303304
#include "swift/AST/DeclNodes.def"
304305
,
305306

307+
/// Whether this attribute is valid on additional decls in ClangImporter.
308+
OnAnyClangDecl = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 1),
309+
};
310+
311+
static_assert(
312+
(unsigned(DeclKindIndex::Last_Decl) + 1) < 64,
313+
"Overflow decl attr requirement bitfields");
314+
315+
enum DeclAttrBehaviors : uint64_t {
316+
/// Whether this attribute is only valid when concurrency is enabled.
317+
ConcurrencyOnly = 1ull << 0,
318+
306319
/// True if multiple instances of this attribute are allowed on a single
307320
/// declaration.
308-
AllowMultipleAttributes = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 1),
321+
AllowMultipleAttributes = 1ull << 1,
309322

310323
/// True if this is a decl modifier - i.e., that it should not be spelled
311324
/// with an @.
312-
DeclModifier = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 2),
325+
DeclModifier = 1ull << 2,
313326

314327
/// True if this is a long attribute that should be printed on its own line.
315328
///
316329
/// Currently has no effect on DeclModifier attributes.
317-
LongAttribute = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 3),
330+
LongAttribute = 1ull << 3,
318331

319332
/// True if this shouldn't be serialized.
320-
NotSerialized = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 4),
333+
NotSerialized = 1ull << 4,
321334

322335
/// True if this attribute is only valid when parsing a .sil file.
323-
SILOnly = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 5),
336+
SILOnly = 1ull << 5,
324337

325338
/// The attribute should be reported by parser as unknown.
326-
RejectByParser = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 6),
339+
RejectByParser = 1ull << 6,
327340

328-
/// Whether client code cannot use the attribute.
329-
UserInaccessible = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 7),
341+
/// Whether client code cannot use the attribute. Hides it in code completion.
342+
UserInaccessible = 1ull << 7,
330343

331344
/// Whether adding this attribute can break API
332-
APIBreakingToAdd = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 8),
345+
APIBreakingToAdd = 1ull << 8,
333346

334347
/// Whether removing this attribute can break API
335-
APIBreakingToRemove = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 9),
348+
APIBreakingToRemove = 1ull << 9,
336349

337350
/// Whether adding this attribute can break ABI
338-
ABIBreakingToAdd = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 10),
351+
ABIBreakingToAdd = 1ull << 10,
339352

340353
/// Whether removing this attribute can break ABI
341-
ABIBreakingToRemove = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 11),
354+
ABIBreakingToRemove = 1ull << 11,
342355

343356
/// The opposite of APIBreakingToAdd
344-
APIStableToAdd = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 12),
357+
APIStableToAdd = 1ull << 12,
345358

346359
/// The opposite of APIBreakingToRemove
347-
APIStableToRemove = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 13),
360+
APIStableToRemove = 1ull << 13,
348361

349362
/// The opposite of ABIBreakingToAdd
350-
ABIStableToAdd = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 14),
363+
ABIStableToAdd = 1ull << 14,
351364

352365
/// The opposite of ABIBreakingToRemove
353-
ABIStableToRemove = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 15),
354-
355-
/// Whether this attribute is only valid when concurrency is enabled.
356-
ConcurrencyOnly = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 16),
357-
358-
/// Whether this attribute is valid on additional decls in ClangImporter.
359-
OnAnyClangDecl = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 17),
366+
ABIStableToRemove = 1ull << 15,
360367
};
361368

362-
static_assert(
363-
(unsigned(DeclKindIndex::Last_Decl) + 17) < 64,
364-
"Overflow decl attr options bitfields");
369+
LLVM_READNONE
370+
static uint64_t getRequirements(DeclAttrKind DK);
371+
372+
uint64_t getRequirements() const {
373+
return getRequirements(getKind());
374+
}
365375

366376
LLVM_READNONE
367-
static uint64_t getOptions(DeclAttrKind DK);
377+
static uint64_t getBehaviors(DeclAttrKind DK);
368378

369-
uint64_t getOptions() const {
370-
return getOptions(getKind());
379+
uint64_t getBehaviors() const {
380+
return getBehaviors(getKind());
371381
}
372382

373383
/// Prints this attribute (if applicable), returning `true` if anything was
@@ -433,74 +443,74 @@ class DeclAttribute : public AttributeBase {
433443
/// Returns true if multiple instances of an attribute kind
434444
/// can appear on a declaration.
435445
static bool allowMultipleAttributes(DeclAttrKind DK) {
436-
return getOptions(DK) & AllowMultipleAttributes;
446+
return getBehaviors(DK) & AllowMultipleAttributes;
437447
}
438448

439449
bool isLongAttribute() const {
440450
return isLongAttribute(getKind());
441451
}
442452
static bool isLongAttribute(DeclAttrKind DK) {
443-
return getOptions(DK) & LongAttribute;
453+
return getBehaviors(DK) & LongAttribute;
444454
}
445455

446456
static bool shouldBeRejectedByParser(DeclAttrKind DK) {
447-
return getOptions(DK) & RejectByParser;
457+
return getBehaviors(DK) & RejectByParser;
448458
}
449459

450460
static bool isSilOnly(DeclAttrKind DK) {
451-
return getOptions(DK) & SILOnly;
461+
return getBehaviors(DK) & SILOnly;
452462
}
453463

454464
static bool isConcurrencyOnly(DeclAttrKind DK) {
455-
return getOptions(DK) & ConcurrencyOnly;
465+
return getBehaviors(DK) & ConcurrencyOnly;
456466
}
457467

458468
static bool isUserInaccessible(DeclAttrKind DK) {
459-
return getOptions(DK) & UserInaccessible;
469+
return getBehaviors(DK) & UserInaccessible;
460470
}
461471

462472
static bool isAddingBreakingABI(DeclAttrKind DK) {
463-
return getOptions(DK) & ABIBreakingToAdd;
473+
return getBehaviors(DK) & ABIBreakingToAdd;
464474
}
465475

466-
#define DECL_ATTR(_, CLASS, OPTIONS, ...) \
467-
static constexpr bool isOptionSetFor##CLASS(DeclAttrOptions Bit) { \
468-
return (OPTIONS) & Bit; \
476+
#define DECL_ATTR(_, CLASS, REQUIREMENTS, BEHAVIORS, ...) \
477+
static constexpr bool hasOneBehaviorFor##CLASS(uint64_t Mask) { \
478+
return llvm::has_single_bit((BEHAVIORS) & Mask); \
469479
}
470480
#include "swift/AST/DeclAttr.def"
471481

472482
static bool isAddingBreakingAPI(DeclAttrKind DK) {
473-
return getOptions(DK) & APIBreakingToAdd;
483+
return getBehaviors(DK) & APIBreakingToAdd;
474484
}
475485

476486
static bool isRemovingBreakingABI(DeclAttrKind DK) {
477-
return getOptions(DK) & ABIBreakingToRemove;
487+
return getBehaviors(DK) & ABIBreakingToRemove;
478488
}
479489
static bool isRemovingBreakingAPI(DeclAttrKind DK) {
480-
return getOptions(DK) & APIBreakingToRemove;
490+
return getBehaviors(DK) & APIBreakingToRemove;
481491
}
482492

483493
bool isDeclModifier() const {
484494
return isDeclModifier(getKind());
485495
}
486496
static bool isDeclModifier(DeclAttrKind DK) {
487-
return getOptions(DK) & DeclModifier;
497+
return getBehaviors(DK) & DeclModifier;
488498
}
489499

490500
static bool isOnParam(DeclAttrKind DK) {
491-
return getOptions(DK) & OnParam;
501+
return getRequirements(DK) & OnParam;
492502
}
493503

494504
static bool isOnFunc(DeclAttrKind DK) {
495-
return getOptions(DK) & OnFunc;
505+
return getRequirements(DK) & OnFunc;
496506
}
497507

498508
static bool isOnClass(DeclAttrKind DK) {
499-
return getOptions(DK) & OnClass;
509+
return getRequirements(DK) & OnClass;
500510
}
501511

502512
static bool isNotSerialized(DeclAttrKind DK) {
503-
return getOptions(DK) & NotSerialized;
513+
return getBehaviors(DK) & NotSerialized;
504514
}
505515
bool isNotSerialized() const {
506516
return isNotSerialized(getKind());

0 commit comments

Comments
 (0)