Skip to content

Commit 6aea6ce

Browse files
authored
Merge pull request #79469 from rintaro/astgen-custom-type-attr
[ASTGen] Generate CustomAttr on types
2 parents 2f7e606 + d109a86 commit 6aea6ce

File tree

8 files changed

+107
-72
lines changed

8 files changed

+107
-72
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,15 +2351,39 @@ void BridgedStmt_dump(BridgedStmt statement);
23512351
// MARK: TypeAttributes
23522352
//===----------------------------------------------------------------------===//
23532353

2354-
#ifdef USED_IN_CPP_SOURCE
2355-
namespace swift {
2356-
class TypeAttributes {
2354+
class BridgedTypeOrCustomAttr {
2355+
public:
2356+
enum Kind : uint8_t {
2357+
TypeAttr,
2358+
CustomAttr,
2359+
} kind;
2360+
2361+
private:
2362+
intptr_t opaque;
2363+
2364+
void *_Nonnull getPointer() const {
2365+
return reinterpret_cast<void *>(opaque & ~0x7);
2366+
}
2367+
2368+
BRIDGED_INLINE BridgedTypeOrCustomAttr(void *_Nonnull pointer, Kind kind);
2369+
23572370
public:
2358-
SmallVector<TypeOrCustomAttr> attrs;
2359-
TypeAttributes() {}
2371+
SWIFT_NAME("typeAttr(_:)")
2372+
static BridgedTypeOrCustomAttr createTypeAttr(BridgedTypeAttribute typeAttr) {
2373+
return BridgedTypeOrCustomAttr(typeAttr.unbridged(), Kind::TypeAttr);
2374+
}
2375+
SWIFT_NAME("customAttr(_:)")
2376+
static BridgedTypeOrCustomAttr
2377+
createCust0kAttr(BridgedCustomAttr customAttr) {
2378+
return BridgedTypeOrCustomAttr(customAttr.unbridged(), Kind::CustomAttr);
2379+
}
2380+
2381+
Kind getKind() const { return static_cast<Kind>(opaque & 0x7); }
2382+
2383+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedTypeAttribute
2384+
castToTypeAttr() const;
2385+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCustomAttr castToCustomAttr() const;
23602386
};
2361-
} // namespace swift
2362-
#endif
23632387

23642388
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
23652389
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind {
@@ -2371,19 +2395,6 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind {
23712395
SWIFT_NAME("BridgedTypeAttrKind.init(from:)")
23722396
BridgedTypeAttrKind BridgedTypeAttrKind_fromString(BridgedStringRef cStr);
23732397

2374-
SWIFT_NAME("BridgedTypeAttributes.new()")
2375-
BridgedTypeAttributes BridgedTypeAttributes_create();
2376-
2377-
SWIFT_NAME("BridgedTypeAttributes.delete(self:)")
2378-
void BridgedTypeAttributes_delete(BridgedTypeAttributes cAttributes);
2379-
2380-
SWIFT_NAME("BridgedTypeAttributes.add(self:_:)")
2381-
void BridgedTypeAttributes_add(BridgedTypeAttributes cAttributes,
2382-
BridgedTypeAttribute cAttribute);
2383-
2384-
SWIFT_NAME("getter:BridgedTypeAttributes.isEmpty(self:)")
2385-
bool BridgedTypeAttributes_isEmpty(BridgedTypeAttributes cAttributes);
2386-
23872398
SWIFT_NAME("BridgedTypeAttribute.createSimple(_:kind:atLoc:nameLoc:)")
23882399
BridgedTypeAttribute BridgedTypeAttribute_createSimple(
23892400
BridgedASTContext cContext, BridgedTypeAttrKind cKind,
@@ -2465,12 +2476,11 @@ BridgedArrayTypeRepr BridgedArrayTypeRepr_createParsed(
24652476
BridgedASTContext cContext, BridgedTypeRepr base,
24662477
BridgedSourceLoc cLSquareLoc, BridgedSourceLoc cRSquareLoc);
24672478

2468-
SWIFT_NAME(
2469-
"BridgedAttributedTypeRepr.createParsed(_:base:consumingAttributes:)")
2479+
SWIFT_NAME("BridgedAttributedTypeRepr.createParsed(_:base:attributes:)")
24702480
BridgedAttributedTypeRepr
24712481
BridgedAttributedTypeRepr_createParsed(BridgedASTContext cContext,
24722482
BridgedTypeRepr base,
2473-
BridgedTypeAttributes cAttributes);
2483+
BridgedArrayRef cAttributes);
24742484

24752485
SWIFT_NAME("BridgedCompositionTypeRepr.createEmpty(_:anyKeywordLoc:)")
24762486
BridgedCompositionTypeRepr

include/swift/AST/ASTBridgingImpl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,26 @@ void BridgedIntegerLiteralExpr_setNegative(BridgedIntegerLiteralExpr cExpr,
662662
cExpr.unbridged()->setNegative(cLoc.unbridged());
663663
}
664664

665+
//===----------------------------------------------------------------------===//
666+
// MARK: BridgedTypeOrCustomAttr
667+
//===----------------------------------------------------------------------===//
668+
669+
BridgedTypeOrCustomAttr::BridgedTypeOrCustomAttr(void *_Nonnull pointer,
670+
Kind kind)
671+
: opaque(intptr_t(pointer) | kind) {
672+
assert(getPointer() == pointer && getKind() == kind);
673+
}
674+
675+
BridgedTypeAttribute BridgedTypeOrCustomAttr::castToTypeAttr() const {
676+
assert(getKind() == Kind::TypeAttr);
677+
return static_cast<swift::TypeAttribute *>(getPointer());
678+
}
679+
680+
BridgedCustomAttr BridgedTypeOrCustomAttr::castToCustomAttr() const {
681+
assert(getKind() == Kind::CustomAttr);
682+
return static_cast<swift::CustomAttr *>(getPointer());
683+
}
684+
665685
SWIFT_END_NULLABILITY_ANNOTATIONS
666686

667687
#endif // SWIFT_AST_ASTBRIDGINGIMPL_H

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ AST_BRIDGING_WRAPPER_NULLABLE(ParameterList)
110110
AST_BRIDGING_WRAPPER_NULLABLE(PatternBindingInitializer)
111111
AST_BRIDGING_WRAPPER_NULLABLE(DefaultArgumentInitializer)
112112
AST_BRIDGING_WRAPPER_NULLABLE(CustomAttributeInitializer)
113-
AST_BRIDGING_WRAPPER_NONNULL(TypeAttributes)
114113
AST_BRIDGING_WRAPPER_NONNULL(CustomAttribute)
115114
AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
116115
AST_BRIDGING_WRAPPER_NULLABLE(AvailabilitySpec)

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,6 @@ static std::optional<TypeAttrKind> unbridged(BridgedTypeAttrKind kind) {
5757
llvm_unreachable("unhandled enum value");
5858
}
5959

60-
BridgedTypeAttributes BridgedTypeAttributes_create() {
61-
return new TypeAttributes();
62-
}
63-
64-
void BridgedTypeAttributes_delete(BridgedTypeAttributes cAttributes) {
65-
delete cAttributes.unbridged();
66-
}
67-
68-
void BridgedTypeAttributes_add(BridgedTypeAttributes cAttributes,
69-
BridgedTypeAttribute cAttribute) {
70-
cAttributes.unbridged()->attrs.push_back(cAttribute.unbridged());
71-
}
72-
73-
bool BridgedTypeAttributes_isEmpty(BridgedTypeAttributes cAttributes) {
74-
TypeAttributes *typeAttributes = cAttributes.unbridged();
75-
return typeAttributes->attrs.empty();
76-
}
77-
7860
BridgedTypeAttribute BridgedTypeAttribute_createSimple(
7961
BridgedASTContext cContext, BridgedTypeAttrKind cKind,
8062
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc) {

lib/AST/Bridging/TypeReprBridging.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,22 @@ BridgedPackExpansionTypeRepr_createParsed(BridgedASTContext cContext,
143143
BridgedAttributedTypeRepr
144144
BridgedAttributedTypeRepr_createParsed(BridgedASTContext cContext,
145145
BridgedTypeRepr base,
146-
BridgedTypeAttributes cAttributes) {
147-
TypeAttributes *typeAttributes = cAttributes.unbridged();
148-
assert(!typeAttributes->attrs.empty());
149-
150-
auto attributedType = AttributedTypeRepr::create(
151-
cContext.unbridged(), typeAttributes->attrs, base.unbridged());
152-
delete typeAttributes;
153-
return attributedType;
146+
BridgedArrayRef cAttributes) {
147+
SmallVector<TypeOrCustomAttr, 2> attrs;
148+
for (auto elem : cAttributes.unbridged<BridgedTypeOrCustomAttr>()) {
149+
switch (elem.getKind()) {
150+
case BridgedTypeOrCustomAttr::TypeAttr:
151+
attrs.push_back(elem.castToTypeAttr().unbridged());
152+
break;
153+
case BridgedTypeOrCustomAttr::CustomAttr:
154+
attrs.push_back(elem.castToCustomAttr().unbridged());
155+
break;
156+
}
157+
}
158+
assert(!attrs.empty());
159+
160+
return AttributedTypeRepr::create(cContext.unbridged(), attrs,
161+
base.unbridged());
154162
}
155163

156164
BridgedSpecifierTypeRepr BridgedSpecifierTypeRepr_createParsed(

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ import SwiftDiagnostics
1616
@_spi(ExperimentalLanguageFeatures) @_spi(RawSyntax) import SwiftSyntax
1717

1818
extension ASTGenVisitor {
19-
func generateTypeAttributes(_ node: some WithAttributesSyntax) -> BridgedTypeAttributes? {
20-
guard !node.attributes.isEmpty else {
21-
return nil
22-
}
23-
24-
let attrs = BridgedTypeAttributes.new()
19+
func generateTypeAttributes(_ node: some WithAttributesSyntax) -> [BridgedTypeOrCustomAttr] {
20+
var attrs: [BridgedTypeOrCustomAttr] = []
2521
visitIfConfigElements(node.attributes, of: AttributeSyntax.self) { element in
2622
switch element {
2723
case .ifConfigDecl(let ifConfigDecl):
@@ -31,19 +27,13 @@ extension ASTGenVisitor {
3127
}
3228
} body: { attribute in
3329
if let attr = self.generateTypeAttribute(attribute: attribute) {
34-
attrs.add(attr)
30+
attrs.append(attr)
3531
}
3632
}
37-
38-
guard !attrs.isEmpty else {
39-
attrs.delete()
40-
return nil
41-
}
42-
4333
return attrs
4434
}
4535

46-
func generateTypeAttribute(attribute node: AttributeSyntax) -> BridgedTypeAttribute? {
36+
func generateTypeAttribute(attribute node: AttributeSyntax) -> BridgedTypeOrCustomAttr? {
4737
if let identTy = node.attributeName.as(IdentifierTypeSyntax.self) {
4838
let attrName = identTy.name.rawText
4939
let attrKind = BridgedTypeAttrKind(from: attrName.bridged)
@@ -76,17 +66,22 @@ extension ASTGenVisitor {
7666
.thick,
7767
.unimplementable:
7868
return self.generateSimpleTypeAttr(attribute: node, kind: attrKind)
69+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
7970

8071
case .convention:
81-
return self.generateConventionTypeAttr(attribute: node)?.asTypeAttribute
72+
return (self.generateConventionTypeAttr(attribute: node)?.asTypeAttribute)
73+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
8274
case .differentiable:
8375
fatalError("unimplemented")
8476
case .execution:
85-
return self.generateExecutionTypeAttr(attribute: node)?.asTypeAttribute
77+
return (self.generateExecutionTypeAttr(attribute: node)?.asTypeAttribute)
78+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
8679
case .opaqueReturnTypeOf:
87-
return self.generateOpaqueReturnTypeOfTypeAttr(attribute: node)?.asTypeAttribute
80+
return (self.generateOpaqueReturnTypeOfTypeAttr(attribute: node)?.asTypeAttribute)
81+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
8882
case .isolated:
89-
return self.generateIsolatedTypeAttr(attribute: node)?.asTypeAttribute
83+
return (self.generateIsolatedTypeAttr(attribute: node)?.asTypeAttribute)
84+
.map(BridgedTypeOrCustomAttr.typeAttr(_:))
9085

9186
// SIL type attributes are not supported.
9287
case .autoreleased,
@@ -120,15 +115,19 @@ extension ASTGenVisitor {
120115
.silSending,
121116
.silImplicitLeadingParam,
122117
.unownedInnerPointer:
118+
// TODO: Diagnose or fallback to CustomAttr?
119+
fatalError("SIL type attributes are not supported")
123120
break;
124121

125-
// Not a type attribute.
126122
case .none:
123+
// Not a builtin type attribute. Fall back to CustomAttr
127124
break;
128125
}
129126
}
130127

131-
// TODO: Diagnose.
128+
if let customAttr = self.generateCustomAttr(attribute: node) {
129+
return .customAttr(customAttr)
130+
}
132131
return nil
133132
}
134133

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,13 @@ extension ASTGenVisitor {
330330
}
331331

332332
// Handle type attributes.
333-
if let typeAttributes = self.generateTypeAttributes(node) {
333+
let typeAttributes = self.generateTypeAttributes(node)
334+
if !typeAttributes.isEmpty {
334335
type =
335336
BridgedAttributedTypeRepr.createParsed(
336337
self.ctx,
337338
base: type,
338-
consumingAttributes: typeAttributes
339+
attributes: typeAttributes.lazy.bridgedArray(in: self)
339340
).asTypeRepr
340341
}
341342

test/ASTGen/types.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend-dump-parse -enable-experimental-feature ParserASTGen \
4+
// RUN: | %sanitize-address > %t/astgen.ast
5+
// RUN: %target-swift-frontend-dump-parse \
6+
// RUN: | %sanitize-address > %t/cpp-parser.ast
7+
8+
// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast
9+
110
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ParserASTGen
211

312
// REQUIRES: swift_feature_ParserASTGen
@@ -52,3 +61,10 @@ struct FileDescriptor: ~Copyable {
5261

5362
// FIXME: warning for 'class'
5463
protocol ClassOnly: class {}
64+
65+
actor SomeActor { }
66+
@globalActor
67+
struct SomeGlobalActor {
68+
static let shared = SomeActor()
69+
}
70+
typealias SomeGlobalActorIsolated = @SomeGlobalActor () -> Void

0 commit comments

Comments
 (0)