Skip to content

Commit d109a86

Browse files
committed
[ASTGen] Generate CustomAttr on types
Rework TypeAttribute bridging facilities. Eliminate the _fake_ `swift::TypeAttributes`, but just use the normal `BridgedArrayRef` for briding the attribute list. Introduce `BridgedTypeOrCustomAttr` tagged pointer which resembles `swift::TypeOrCustomAttr`.
1 parent bdf2294 commit d109a86

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
@@ -2340,15 +2340,39 @@ void BridgedStmt_dump(BridgedStmt statement);
23402340
// MARK: TypeAttributes
23412341
//===----------------------------------------------------------------------===//
23422342

2343-
#ifdef USED_IN_CPP_SOURCE
2344-
namespace swift {
2345-
class TypeAttributes {
2343+
class BridgedTypeOrCustomAttr {
2344+
public:
2345+
enum Kind : uint8_t {
2346+
TypeAttr,
2347+
CustomAttr,
2348+
} kind;
2349+
2350+
private:
2351+
intptr_t opaque;
2352+
2353+
void *_Nonnull getPointer() const {
2354+
return reinterpret_cast<void *>(opaque & ~0x7);
2355+
}
2356+
2357+
BRIDGED_INLINE BridgedTypeOrCustomAttr(void *_Nonnull pointer, Kind kind);
2358+
23462359
public:
2347-
SmallVector<TypeOrCustomAttr> attrs;
2348-
TypeAttributes() {}
2360+
SWIFT_NAME("typeAttr(_:)")
2361+
static BridgedTypeOrCustomAttr createTypeAttr(BridgedTypeAttribute typeAttr) {
2362+
return BridgedTypeOrCustomAttr(typeAttr.unbridged(), Kind::TypeAttr);
2363+
}
2364+
SWIFT_NAME("customAttr(_:)")
2365+
static BridgedTypeOrCustomAttr
2366+
createCust0kAttr(BridgedCustomAttr customAttr) {
2367+
return BridgedTypeOrCustomAttr(customAttr.unbridged(), Kind::CustomAttr);
2368+
}
2369+
2370+
Kind getKind() const { return static_cast<Kind>(opaque & 0x7); }
2371+
2372+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedTypeAttribute
2373+
castToTypeAttr() const;
2374+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCustomAttr castToCustomAttr() const;
23492375
};
2350-
} // namespace swift
2351-
#endif
23522376

23532377
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
23542378
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind {
@@ -2360,19 +2384,6 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind {
23602384
SWIFT_NAME("BridgedTypeAttrKind.init(from:)")
23612385
BridgedTypeAttrKind BridgedTypeAttrKind_fromString(BridgedStringRef cStr);
23622386

2363-
SWIFT_NAME("BridgedTypeAttributes.new()")
2364-
BridgedTypeAttributes BridgedTypeAttributes_create();
2365-
2366-
SWIFT_NAME("BridgedTypeAttributes.delete(self:)")
2367-
void BridgedTypeAttributes_delete(BridgedTypeAttributes cAttributes);
2368-
2369-
SWIFT_NAME("BridgedTypeAttributes.add(self:_:)")
2370-
void BridgedTypeAttributes_add(BridgedTypeAttributes cAttributes,
2371-
BridgedTypeAttribute cAttribute);
2372-
2373-
SWIFT_NAME("getter:BridgedTypeAttributes.isEmpty(self:)")
2374-
bool BridgedTypeAttributes_isEmpty(BridgedTypeAttributes cAttributes);
2375-
23762387
SWIFT_NAME("BridgedTypeAttribute.createSimple(_:kind:atLoc:nameLoc:)")
23772388
BridgedTypeAttribute BridgedTypeAttribute_createSimple(
23782389
BridgedASTContext cContext, BridgedTypeAttrKind cKind,
@@ -2454,12 +2465,11 @@ BridgedArrayTypeRepr BridgedArrayTypeRepr_createParsed(
24542465
BridgedASTContext cContext, BridgedTypeRepr base,
24552466
BridgedSourceLoc cLSquareLoc, BridgedSourceLoc cRSquareLoc);
24562467

2457-
SWIFT_NAME(
2458-
"BridgedAttributedTypeRepr.createParsed(_:base:consumingAttributes:)")
2468+
SWIFT_NAME("BridgedAttributedTypeRepr.createParsed(_:base:attributes:)")
24592469
BridgedAttributedTypeRepr
24602470
BridgedAttributedTypeRepr_createParsed(BridgedASTContext cContext,
24612471
BridgedTypeRepr base,
2462-
BridgedTypeAttributes cAttributes);
2472+
BridgedArrayRef cAttributes);
24632473

24642474
SWIFT_NAME("BridgedCompositionTypeRepr.createEmpty(_:anyKeywordLoc:)")
24652475
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
@@ -109,7 +109,6 @@ AST_BRIDGING_WRAPPER_NULLABLE(TrailingWhereClause)
109109
AST_BRIDGING_WRAPPER_NULLABLE(ParameterList)
110110
AST_BRIDGING_WRAPPER_NULLABLE(PatternBindingInitializer)
111111
AST_BRIDGING_WRAPPER_NULLABLE(CustomAttributeInitializer)
112-
AST_BRIDGING_WRAPPER_NONNULL(TypeAttributes)
113112
AST_BRIDGING_WRAPPER_NONNULL(CustomAttribute)
114113
AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
115114
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)