Skip to content

Commit bca2aa7

Browse files
committed
[AST] InitAccessors: Introduce new @storageRestrictions(initializes:accesses:) attribute
This is the replacement for `initializes`, `accesses` attributes. (cherry picked from commit 9719cd0)
1 parent 7f95ad6 commit bca2aa7

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

include/swift/AST/Attr.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,45 @@ class AccessesAttr final
15941594
}
15951595
};
15961596

1597+
class StorageRestrictionsAttr final
1598+
: public DeclAttribute,
1599+
private llvm::TrailingObjects<StorageRestrictionsAttr, Identifier> {
1600+
friend TrailingObjects;
1601+
1602+
size_t NumInitializes;
1603+
size_t NumAccesses;
1604+
1605+
size_t numTrailingObjects(OverloadToken<Identifier>) const {
1606+
return NumInitializes + NumAccesses;
1607+
}
1608+
1609+
public:
1610+
StorageRestrictionsAttr(SourceLoc AtLoc, SourceRange Range,
1611+
ArrayRef<Identifier> initializes,
1612+
ArrayRef<Identifier> accesses, bool Implicit);
1613+
1614+
unsigned getNumInitializesProperties() const { return NumInitializes; }
1615+
1616+
unsigned getNumAccessesProperties() const { return NumAccesses; }
1617+
1618+
ArrayRef<Identifier> getInitializesNames() const {
1619+
return {getTrailingObjects<Identifier>(), NumInitializes};
1620+
}
1621+
1622+
ArrayRef<Identifier> getAccessesNames() const {
1623+
return {getTrailingObjects<Identifier>() + NumInitializes, NumAccesses};
1624+
}
1625+
1626+
static StorageRestrictionsAttr *create(ASTContext &ctx, SourceLoc atLoc,
1627+
SourceRange range,
1628+
ArrayRef<Identifier> initializes,
1629+
ArrayRef<Identifier> accesses);
1630+
1631+
static bool classof(const DeclAttribute *DA) {
1632+
return DA->getKind() == DAK_StorageRestrictions;
1633+
}
1634+
};
1635+
15971636
/// The @_implements attribute, which treats a decl as the implementation for
15981637
/// some named protocol requirement (but otherwise not-visible by that name).
15991638
class ImplementsAttr : public DeclAttribute {

lib/AST/Attr.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,8 @@ StringRef DeclAttribute::getAttrName() const {
15941594
return "initializes";
15951595
case DAK_Accesses:
15961596
return "accesses";
1597+
case DAK_StorageRestrictions:
1598+
return "storageRestrictions";
15971599
case DAK_Implements:
15981600
return "_implements";
15991601
case DAK_ClangImporterSynthesizedType:
@@ -2397,6 +2399,29 @@ AccessesAttr::create(ASTContext &ctx, SourceLoc atLoc, SourceRange range,
23972399
return new (mem) AccessesAttr(atLoc, range, properties);
23982400
}
23992401

2402+
StorageRestrictionsAttr::StorageRestrictionsAttr(
2403+
SourceLoc AtLoc, SourceRange Range, ArrayRef<Identifier> initializes,
2404+
ArrayRef<Identifier> accesses, bool Implicit)
2405+
: DeclAttribute(DAK_StorageRestrictions, AtLoc, Range, Implicit),
2406+
NumInitializes(initializes.size()),
2407+
NumAccesses(accesses.size()) {
2408+
std::uninitialized_copy(initializes.begin(), initializes.end(),
2409+
getTrailingObjects<Identifier>());
2410+
std::uninitialized_copy(accesses.begin(), accesses.end(),
2411+
getTrailingObjects<Identifier>() + NumInitializes);
2412+
}
2413+
2414+
StorageRestrictionsAttr *
2415+
StorageRestrictionsAttr::create(
2416+
ASTContext &ctx, SourceLoc atLoc, SourceRange range,
2417+
ArrayRef<Identifier> initializes, ArrayRef<Identifier> accesses) {
2418+
unsigned size =
2419+
totalSizeToAlloc<Identifier>(initializes.size() + accesses.size());
2420+
void *mem = ctx.Allocate(size, alignof(StorageRestrictionsAttr));
2421+
return new (mem) StorageRestrictionsAttr(atLoc, range, initializes, accesses,
2422+
/*implicit=*/false);
2423+
}
2424+
24002425
ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
24012426
TypeExpr *ProtocolType,
24022427
DeclName MemberName,

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,6 +3371,10 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
33713371
llvm_unreachable("AccessesAttr not yet implemented");
33723372
}
33733373

3374+
case DAK_StorageRestrictions: {
3375+
llvm_unreachable("StorageRestrictionsAttr not yet implemented");
3376+
}
3377+
33743378
case DAK_Implements: {
33753379
ParserResult<ImplementsAttr> Attr = parseImplementsAttribute(AtLoc, Loc);
33763380
if (Attr.isNonNull()) {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
294294
void visitTypeEraserAttr(TypeEraserAttr *attr);
295295
void visitInitializesAttr(InitializesAttr *attr);
296296
void visitAccessesAttr(AccessesAttr *attr);
297+
void visitStorageRestrictionsAttr(StorageRestrictionsAttr *attr);
297298
void visitImplementsAttr(ImplementsAttr *attr);
298299
void visitNoMetadataAttr(NoMetadataAttr *attr);
299300

@@ -3551,6 +3552,9 @@ void AttributeChecker::visitAccessesAttr(AccessesAttr *attr) {
35513552
}
35523553
}
35533554

3555+
void AttributeChecker::visitStorageRestrictionsAttr(StorageRestrictionsAttr *attr) {
3556+
}
3557+
35543558
void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
35553559
DeclContext *DC = D->getDeclContext();
35563560

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ namespace {
15811581
UNINTERESTING_ATTR(RestatedObjCConformance)
15821582
UNINTERESTING_ATTR(Initializes)
15831583
UNINTERESTING_ATTR(Accesses)
1584+
UNINTERESTING_ATTR(StorageRestrictions)
15841585
UNINTERESTING_ATTR(Implements)
15851586
UNINTERESTING_ATTR(StaticInitializeObjCMetadata)
15861587
UNINTERESTING_ATTR(ClangImporterSynthesizedType)

utils/gyb_syntax_support/AttributeKinds.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ def __init__(self, name, swift_name=None):
930930
ABIStableToAdd, ABIStableToRemove, APIStableToAdd, APIBreakingToRemove, # noqa: E501
931931
code=142),
932932
DeclAttributeAlias('freestanding', 'MacroRole'),
933+
DeclAttribute('storageRestrictions', 'StorageRestrictions',
934+
OnAccessor,
935+
ABIStableToAdd, ABIStableToRemove,
936+
APIBreakingToAdd, APIBreakingToRemove,
937+
code=145),
933938
]
934939

935940
DEPRECATED_MODIFIER_KINDS = [

0 commit comments

Comments
 (0)