Skip to content

Commit 07ec69f

Browse files
authored
Merge pull request #5570 from slavapestov/add-fragile-attribute
Add @_inlineable attribute
2 parents b8a342a + 4780b0b commit 07ec69f

File tree

17 files changed

+154
-70
lines changed

17 files changed

+154
-70
lines changed

include/swift/AST/Attr.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ SIMPLE_DECL_ATTR(prefix , Prefix , OnFunc | OnOperator | DeclModifier, 24)
152152
SIMPLE_DECL_ATTR(postfix, Postfix, OnFunc | OnOperator | DeclModifier, 25)
153153

154154
SIMPLE_DECL_ATTR(_transparent, Transparent,
155-
OnFunc|OnConstructor|OnVar|OnExtension|UserInaccessible, 26)
155+
OnFunc|OnConstructor|OnVar|UserInaccessible, 26)
156156
SIMPLE_DECL_ATTR(requires_stored_property_inits, RequiresStoredPropertyInits,
157157
OnClass, 27)
158158
DECL_ATTR(autoclosure, AutoClosure, OnParam, 28)
@@ -164,9 +164,13 @@ SIMPLE_DECL_ATTR(nonobjc, NonObjC,
164164
SIMPLE_DECL_ATTR(_fixed_layout, FixedLayout,
165165
OnVar | OnClass | OnStruct | OnEnum | UserInaccessible, 31)
166166

167+
SIMPLE_DECL_ATTR(_inlineable, Inlineable,
168+
OnVar | OnSubscript | OnFunc | OnConstructor | OnDestructor |
169+
UserInaccessible, 32)
170+
167171
DECL_ATTR(_specialize, Specialize,
168172
OnConstructor | OnFunc | AllowMultipleAttributes | LongAttribute
169-
| UserInaccessible, 32)
173+
| UserInaccessible, 33)
170174

171175
// Non-serialized attributes.
172176

include/swift/AST/Decl.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,6 @@ class alignas(1 << DeclAlignInBits) Decl {
733733

734734
bool walk(ASTWalker &walker);
735735

736-
/// \brief Should this declaration be treated as if annotated with transparent
737-
/// attribute.
738-
bool isTransparent() const;
739-
740736
/// \brief Return whether this declaration has been determined invalid.
741737
bool isInvalid() const { return DeclBits.Invalid; }
742738

@@ -3784,6 +3780,10 @@ class AbstractStorageDecl : public ValueDecl {
37843780
}
37853781
public:
37863782

3783+
/// \brief Should this declaration be treated as if annotated with transparent
3784+
/// attribute.
3785+
bool isTransparent() const;
3786+
37873787
/// \brief Determine whether this storage is a static member, if it
37883788
/// is a member. Currently only variables can be static.
37893789
inline bool isStatic() const; // defined in this header
@@ -4595,8 +4595,13 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
45954595
}
45964596

45974597
void setGenericParams(GenericParamList *GenericParams);
4598-
4598+
45994599
public:
4600+
4601+
/// \brief Should this declaration be treated as if annotated with transparent
4602+
/// attribute.
4603+
bool isTransparent() const;
4604+
46004605
void setGenericSignature(GenericSignature *GenericSig) {
46014606
assert(!this->GenericSig && "already have signature?");
46024607
this->GenericSig = GenericSig;

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,6 @@ ERROR(static_functions_not_mutating,none,
930930

931931
ERROR(transparent_stored_property,none,
932932
"@_transparent cannot be applied to stored properties", ())
933-
ERROR(transparent_on_invalid_extension,none,
934-
"@_transparent is only supported on struct and enum extensions", ())
935933
ERROR(transparent_in_protocols_not_supported,none,
936934
"@_transparent is not supported on declarations within protocols", ())
937935
ERROR(transparent_in_classes_not_supported,none,

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 281; // Last change: complete witnesses
57+
const uint16_t VERSION_MINOR = 282; // Last change: @_inlineable
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;

lib/AST/Decl.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,15 @@ case DeclKind::ID: return cast<ID##Decl>(this)->getLoc();
348348
llvm_unreachable("Unknown decl kind");
349349
}
350350

351-
bool Decl::isTransparent() const {
351+
bool AbstractStorageDecl::isTransparent() const {
352+
return getAttrs().hasAttribute<TransparentAttr>();
353+
}
354+
355+
bool AbstractFunctionDecl::isTransparent() const {
352356
// Check if the declaration had the attribute.
353357
if (getAttrs().hasAttribute<TransparentAttr>())
354358
return true;
355359

356-
// Check if this is a function declaration which is within a transparent
357-
// extension.
358-
if (const AbstractFunctionDecl *FD = dyn_cast<AbstractFunctionDecl>(this)) {
359-
if (const ExtensionDecl *ED = dyn_cast<ExtensionDecl>(FD->getParent()))
360-
if (ED->isTransparent())
361-
return true;
362-
}
363-
364360
// If this is an accessor, check if the transparent attribute was set
365361
// on the value decl.
366362
if (const FuncDecl *FD = dyn_cast<FuncDecl>(this)) {

lib/AST/DeclContext.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,21 +503,31 @@ bool DeclContext::isValidGenericContext() const {
503503
/// are used.
504504
ResilienceExpansion DeclContext::getResilienceExpansion() const {
505505
for (const auto *dc = this; dc->isLocalContext(); dc = dc->getParent()) {
506-
if (auto *func = dyn_cast<AbstractFunctionDecl>(dc)) {
506+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(dc)) {
507507
// If the function is not externally visible, we will not be serializing
508508
// its body.
509-
if (!func->getDeclContext()->isLocalContext() &&
510-
func->getEffectiveAccess() < Accessibility::Public)
509+
if (!AFD->getDeclContext()->isLocalContext() &&
510+
AFD->getEffectiveAccess() < Accessibility::Public)
511511
break;
512512

513513
// Bodies of public transparent and always-inline functions are
514514
// serialized, so use conservative access patterns.
515-
if (func->isTransparent())
515+
if (AFD->isTransparent())
516516
return ResilienceExpansion::Minimal;
517517

518-
if (auto attr = func->getAttrs().getAttribute<InlineAttr>())
518+
if (AFD->getAttrs().hasAttribute<InlineableAttr>())
519+
return ResilienceExpansion::Minimal;
520+
521+
if (auto attr = AFD->getAttrs().getAttribute<InlineAttr>())
519522
if (attr->getKind() == InlineKind::Always)
520523
return ResilienceExpansion::Minimal;
524+
525+
// If a property or subscript is @_fragile, the accessors are
526+
// @_fragile also.
527+
if (auto FD = dyn_cast<FuncDecl>(AFD))
528+
if (auto *ASD = FD->getAccessorStorageDecl())
529+
if (ASD->getAttrs().getAttribute<InlineableAttr>())
530+
return ResilienceExpansion::Minimal;
521531
}
522532
}
523533

lib/SIL/SILDeclRef.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,15 @@ bool SILDeclRef::isTransparent() const {
482482
if (hasAutoClosureExpr())
483483
return true;
484484

485-
return hasDecl() ? getDecl()->isTransparent() : false;
485+
if (hasDecl()) {
486+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))
487+
return AFD->isTransparent();
488+
489+
if (auto *ASD = dyn_cast<AbstractStorageDecl>(getDecl()))
490+
return ASD->isTransparent();
491+
}
492+
493+
return false;
486494
}
487495

488496
/// \brief True if the function should have its body serialized.

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
5757
IGNORED_ATTR(FixedLayout)
5858
IGNORED_ATTR(Infix)
5959
IGNORED_ATTR(Inline)
60+
IGNORED_ATTR(Inlineable)
6061
IGNORED_ATTR(NSApplicationMain)
6162
IGNORED_ATTR(NSCopying)
6263
IGNORED_ATTR(NonObjC)
@@ -219,15 +220,6 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
219220
} // end anonymous namespace
220221

221222
void AttributeEarlyChecker::visitTransparentAttr(TransparentAttr *attr) {
222-
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
223-
CanType ExtendedTy = ED->getExtendedType()->getCanonicalType();
224-
const NominalTypeDecl *ExtendedNominal = ExtendedTy->getAnyNominal();
225-
// Only Struct and Enum extensions can be transparent.
226-
if (!isa<StructDecl>(ExtendedNominal) && !isa<EnumDecl>(ExtendedNominal))
227-
return diagnoseAndRemoveAttr(attr,diag::transparent_on_invalid_extension);
228-
return;
229-
}
230-
231223
DeclContext *Ctx = D->getDeclContext();
232224
// Protocol declarations cannot be transparent.
233225
if (isa<ProtocolDecl>(Ctx))
@@ -697,18 +689,18 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
697689

698690
IGNORED_ATTR(AutoClosure)
699691
IGNORED_ATTR(Alignment)
700-
IGNORED_ATTR(SILGenName)
692+
IGNORED_ATTR(Convenience)
701693
IGNORED_ATTR(Dynamic)
694+
IGNORED_ATTR(Effects)
702695
IGNORED_ATTR(Exported)
703-
IGNORED_ATTR(Convenience)
696+
IGNORED_ATTR(FixedLayout)
704697
IGNORED_ATTR(GKInspectable)
705698
IGNORED_ATTR(IBDesignable)
706699
IGNORED_ATTR(IBInspectable)
707700
IGNORED_ATTR(IBOutlet) // checked early.
708701
IGNORED_ATTR(Indirect)
709702
IGNORED_ATTR(Inline)
710-
IGNORED_ATTR(Effects)
711-
IGNORED_ATTR(FixedLayout)
703+
IGNORED_ATTR(Inlineable)
712704
IGNORED_ATTR(Lazy) // checked early.
713705
IGNORED_ATTR(LLDBDebuggerFunction)
714706
IGNORED_ATTR(Mutating)
@@ -725,6 +717,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
725717
IGNORED_ATTR(Override)
726718
IGNORED_ATTR(RawDocComment)
727719
IGNORED_ATTR(Semantics)
720+
IGNORED_ATTR(SILGenName)
728721
IGNORED_ATTR(Transparent)
729722
IGNORED_ATTR(SynthesizedProtocol)
730723
IGNORED_ATTR(RequiresStoredPropertyInits)

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5864,6 +5864,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
58645864
UNINTERESTING_ATTR(IBOutlet)
58655865
UNINTERESTING_ATTR(Indirect)
58665866
UNINTERESTING_ATTR(Inline)
5867+
UNINTERESTING_ATTR(Inlineable)
58675868
UNINTERESTING_ATTR(Effects)
58685869
UNINTERESTING_ATTR(FixedLayout)
58695870
UNINTERESTING_ATTR(Lazy)

0 commit comments

Comments
 (0)