Skip to content

Commit 0f1b650

Browse files
committed
[AST] MacroRoleAttr accept any 'Expr *' as the conformances arguments
1 parent 093d0df commit 0f1b650

File tree

8 files changed

+53
-121
lines changed

8 files changed

+53
-121
lines changed

include/swift/AST/Attr.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,7 +2495,7 @@ class NonisolatedAttr final : public DeclAttribute {
24952495
class MacroRoleAttr final
24962496
: public DeclAttribute,
24972497
private llvm::TrailingObjects<MacroRoleAttr, MacroIntroducedDeclName,
2498-
TypeExpr *> {
2498+
Expr *> {
24992499
friend TrailingObjects;
25002500

25012501
MacroSyntax syntax;
@@ -2507,22 +2507,22 @@ class MacroRoleAttr final
25072507
MacroRoleAttr(SourceLoc atLoc, SourceRange range, MacroSyntax syntax,
25082508
SourceLoc lParenLoc, MacroRole role,
25092509
ArrayRef<MacroIntroducedDeclName> names,
2510-
ArrayRef<TypeExpr *> conformances,
2511-
SourceLoc rParenLoc, bool implicit);
2510+
ArrayRef<Expr *> conformances, SourceLoc rParenLoc,
2511+
bool implicit);
25122512

25132513
public:
25142514
static MacroRoleAttr *create(ASTContext &ctx, SourceLoc atLoc,
25152515
SourceRange range, MacroSyntax syntax,
25162516
SourceLoc lParenLoc, MacroRole role,
25172517
ArrayRef<MacroIntroducedDeclName> names,
2518-
ArrayRef<TypeExpr *> conformances,
2518+
ArrayRef<Expr *> conformances,
25192519
SourceLoc rParenLoc, bool implicit);
25202520

25212521
size_t numTrailingObjects(OverloadToken<MacroIntroducedDeclName>) const {
25222522
return numNames;
25232523
}
25242524

2525-
size_t numTrailingObjects(OverloadToken<TypeExpr *>) const {
2525+
size_t numTrailingObjects(OverloadToken<Expr *>) const {
25262526
return numConformances;
25272527
}
25282528

@@ -2532,7 +2532,8 @@ class MacroRoleAttr final
25322532
MacroSyntax getMacroSyntax() const { return syntax; }
25332533
MacroRole getMacroRole() const { return role; }
25342534
ArrayRef<MacroIntroducedDeclName> getNames() const;
2535-
ArrayRef<TypeExpr *> getConformances() const;
2535+
ArrayRef<Expr *> getConformances() const;
2536+
MutableArrayRef<Expr *> getConformances();
25362537
bool hasNameKind(MacroIntroducedDeclNameKind kind) const;
25372538

25382539
static bool classof(const DeclAttribute *DA) {

lib/AST/Attr.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,29 +2960,28 @@ MacroRoleAttr::MacroRoleAttr(SourceLoc atLoc, SourceRange range,
29602960
MacroSyntax syntax, SourceLoc lParenLoc,
29612961
MacroRole role,
29622962
ArrayRef<MacroIntroducedDeclName> names,
2963-
ArrayRef<TypeExpr *> conformances,
2964-
SourceLoc rParenLoc, bool implicit)
2963+
ArrayRef<Expr *> conformances, SourceLoc rParenLoc,
2964+
bool implicit)
29652965
: DeclAttribute(DeclAttrKind::MacroRole, atLoc, range, implicit),
29662966
syntax(syntax), role(role), numNames(names.size()),
29672967
numConformances(conformances.size()), lParenLoc(lParenLoc),
29682968
rParenLoc(rParenLoc) {
29692969
auto *trailingNamesBuffer = getTrailingObjects<MacroIntroducedDeclName>();
29702970
std::uninitialized_copy(names.begin(), names.end(), trailingNamesBuffer);
29712971

2972-
auto *trailingConformancesBuffer = getTrailingObjects<TypeExpr *>();
2972+
auto *trailingConformancesBuffer = getTrailingObjects<Expr *>();
29732973
std::uninitialized_copy(conformances.begin(), conformances.end(),
29742974
trailingConformancesBuffer);
29752975
}
29762976

2977-
MacroRoleAttr *
2978-
MacroRoleAttr::create(ASTContext &ctx, SourceLoc atLoc, SourceRange range,
2979-
MacroSyntax syntax, SourceLoc lParenLoc, MacroRole role,
2980-
ArrayRef<MacroIntroducedDeclName> names,
2981-
ArrayRef<TypeExpr *> conformances,
2982-
SourceLoc rParenLoc, bool implicit) {
2983-
unsigned size =
2984-
totalSizeToAlloc<MacroIntroducedDeclName, TypeExpr *>(
2985-
names.size(), conformances.size());
2977+
MacroRoleAttr *MacroRoleAttr::create(ASTContext &ctx, SourceLoc atLoc,
2978+
SourceRange range, MacroSyntax syntax,
2979+
SourceLoc lParenLoc, MacroRole role,
2980+
ArrayRef<MacroIntroducedDeclName> names,
2981+
ArrayRef<Expr *> conformances,
2982+
SourceLoc rParenLoc, bool implicit) {
2983+
unsigned size = totalSizeToAlloc<MacroIntroducedDeclName, Expr *>(
2984+
names.size(), conformances.size());
29862985
auto *mem = ctx.Allocate(size, alignof(MacroRoleAttr));
29872986
return new (mem) MacroRoleAttr(atLoc, range, syntax, lParenLoc, role, names,
29882987
conformances, rParenLoc, implicit);
@@ -2995,11 +2994,12 @@ ArrayRef<MacroIntroducedDeclName> MacroRoleAttr::getNames() const {
29952994
};
29962995
}
29972996

2998-
ArrayRef<TypeExpr *> MacroRoleAttr::getConformances() const {
2999-
return {
3000-
getTrailingObjects<TypeExpr *>(),
3001-
numConformances
3002-
};
2997+
ArrayRef<Expr *> MacroRoleAttr::getConformances() const {
2998+
return {getTrailingObjects<Expr *>(), numConformances};
2999+
}
3000+
3001+
MutableArrayRef<Expr *> MacroRoleAttr::getConformances() {
3002+
return {getTrailingObjects<Expr *>(), numConformances};
30033003
}
30043004

30053005
bool MacroRoleAttr::hasNameKind(MacroIntroducedDeclNameKind kind) const {

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ BridgedMacroRoleAttr BridgedMacroRoleAttr_createParsed(
294294
for (auto &n : cNames.unbridged<BridgedMacroIntroducedDeclName>())
295295
names.push_back(n.unbridged());
296296

297-
SmallVector<TypeExpr *, 2> conformances;
298-
for (auto &t : cConformances.unbridged<BridgedTypeExpr>())
297+
SmallVector<Expr *, 2> conformances;
298+
for (auto &t : cConformances.unbridged<BridgedExpr>())
299299
conformances.push_back(t.unbridged());
300300

301301
return MacroRoleAttr::create(

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -753,86 +753,6 @@ extension ASTGenVisitor {
753753
return BridgedMacroIntroducedDeclName(kind: kind, name: name)
754754
}
755755

756-
struct GeneratedGenericArguments {
757-
var arguments: BridgedArrayRef = .init()
758-
var range: BridgedSourceRange = .init()
759-
}
760-
761-
/// Generate 'TypeRepr' from a expression, because 'conformances' arguments in
762-
/// macro role attributes are parsed as normal expressions.
763-
func generateMacroIntroducedConformance(
764-
expr: ExprSyntax,
765-
genericArgs: GeneratedGenericArguments = GeneratedGenericArguments()
766-
) -> BridgedTypeRepr? {
767-
switch expr.as(ExprSyntaxEnum.self) {
768-
case .typeExpr(let node):
769-
return self.generate(type: node.type)
770-
771-
case .declReferenceExpr(let node):
772-
guard node.argumentNames == nil else {
773-
// 'Foo.bar(_:baz:)'
774-
break
775-
}
776-
let name = self.generateIdentifierAndSourceLoc(node.baseName)
777-
return BridgedUnqualifiedIdentTypeRepr .createParsed(
778-
self.ctx,
779-
name: name.identifier,
780-
nameLoc: name.sourceLoc,
781-
genericArgs: genericArgs.arguments,
782-
leftAngleLoc: genericArgs.range.start,
783-
rightAngleLoc: genericArgs.range.end
784-
).asTypeRepr
785-
786-
case .memberAccessExpr(let node):
787-
guard let parsedBase = node.base else {
788-
// Implicit member expressions. E.g. '.Foo'
789-
break
790-
}
791-
guard let base = self.generateMacroIntroducedConformance(expr: parsedBase) else {
792-
// Unsupported base expr. E.g. 'foo().bar'
793-
return nil
794-
}
795-
guard node.declName.argumentNames == nil else {
796-
// Function name. E.g. 'Foo.bar(_:baz:)'
797-
break
798-
}
799-
let name = self.generateIdentifierAndSourceLoc(node.declName.baseName)
800-
return BridgedDeclRefTypeRepr.createParsed(
801-
self.ctx,
802-
base: base,
803-
name: name.identifier,
804-
nameLoc: name.sourceLoc,
805-
genericArguments: genericArgs.arguments,
806-
angleRange: genericArgs.range
807-
).asTypeRepr
808-
809-
case .genericSpecializationExpr(let node):
810-
guard node.expression.is(MemberAccessExprSyntax.self) || node.expression.is(DeclReferenceExprSyntax.self) else {
811-
break
812-
}
813-
let args = node.genericArgumentClause.arguments.lazy.map {
814-
self.generate(genericArgument: $0.argument)
815-
}
816-
return self.generateMacroIntroducedConformance(
817-
expr: node.expression,
818-
genericArgs: GeneratedGenericArguments(
819-
arguments: args.bridgedArray(in: self),
820-
range: self.generateSourceRange(node.genericArgumentClause)
821-
)
822-
)
823-
824-
case .sequenceExpr:
825-
// TODO: Support protocol composition.
826-
break
827-
828-
default:
829-
break
830-
}
831-
832-
// TODO: Diagnose invalid expression for a conformance.
833-
return nil
834-
}
835-
836756
func generateMacroRoleAttr(attribute node: AttributeSyntax, attrName: SyntaxText) -> BridgedMacroRoleAttr? {
837757
// '@freestanding' or '@attached'.
838758
assert(attrName == "freestanding" || attrName == "attached")
@@ -865,7 +785,7 @@ extension ASTGenVisitor {
865785
}
866786

867787
var names: [BridgedMacroIntroducedDeclName] = []
868-
var conformances: [BridgedTypeExpr] = []
788+
var conformances: [BridgedExpr] = []
869789

870790
enum ArgState {
871791
case inNames
@@ -913,9 +833,7 @@ extension ASTGenVisitor {
913833
names.append(name)
914834
}
915835
case .inConformances:
916-
if let conformance = self.generateMacroIntroducedConformance(expr: arg.expression) {
917-
conformances.append(BridgedTypeExpr.createParsed(self.ctx, type: conformance))
918-
}
836+
conformances.append(self.generate(expr: arg.expression))
919837
case .inInvalid:
920838
// Ignore the value.
921839
break

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,7 @@ Parser::parseMacroRoleAttribute(
24292429
bool sawConformances = false;
24302430
bool sawNames = false;
24312431
SmallVector<MacroIntroducedDeclName, 2> names;
2432-
SmallVector<TypeExpr *, 2> conformances;
2432+
SmallVector<Expr *, 2> conformances;
24332433
auto argumentsStatus = parseList(
24342434
tok::r_paren, lParenLoc, rParenLoc,
24352435
/*AllowSepAfterLast=*/false, diag::expected_rparen_expr_list, [&] {
@@ -2525,9 +2525,9 @@ Parser::parseMacroRoleAttribute(
25252525
sawConformances = true;
25262526

25272527
// Parse the introduced conformances
2528-
auto type = parseType();
2529-
auto *typeExpr = new (Context) TypeExpr(type.get());
2530-
conformances.push_back(typeExpr);
2528+
auto expr = parseExpr(diag::expected_type);
2529+
if (expr.isNonNull())
2530+
conformances.push_back(expr.get());
25312531

25322532
return status;
25332533
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
#include "swift/Basic/Lazy.h"
4141
#include "swift/Basic/SourceManager.h"
4242
#include "swift/Basic/StringExtras.h"
43-
#include "swift/ClangImporter/ClangModule.h"
4443
#include "swift/Bridging/ASTGen.h"
4544
#include "swift/Bridging/MacroEvaluation.h"
45+
#include "swift/ClangImporter/ClangModule.h"
4646
#include "swift/Demangling/Demangler.h"
4747
#include "swift/Demangling/ManglingMacros.h"
4848
#include "swift/Parse/Lexer.h"
49+
#include "swift/Sema/ConstraintSystem.h"
4950
#include "swift/Sema/IDETypeChecking.h"
5051
#include "swift/Subsystems.h"
5152
#include "llvm/Config/config.h"
@@ -2091,7 +2092,19 @@ ResolveMacroConformances::evaluate(Evaluator &evaluator,
20912092
auto &ctx = dc->getASTContext();
20922093

20932094
SmallVector<Type, 2> protocols;
2094-
for (auto *typeExpr : attr->getConformances()) {
2095+
for (Expr *&expr : const_cast<MacroRoleAttr *>(attr)->getConformances()) {
2096+
using namespace constraints;
2097+
auto target = SyntacticElementTarget(expr, dc, CTP_Unused, Type(),
2098+
/*isDiscarded=*/true);
2099+
if (ConstraintSystem::preCheckTarget(target))
2100+
continue;
2101+
auto *typeExpr = dyn_cast<TypeExpr>(target.getAsExpr());
2102+
if (!typeExpr) {
2103+
ctx.Diags.diagnose(expr->getStartLoc(), diag::expected_type);
2104+
continue;
2105+
}
2106+
expr = typeExpr;
2107+
20952108
if (auto *typeRepr = typeExpr->getTypeRepr()) {
20962109
auto resolved =
20972110
TypeResolution::forInterface(

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6360,7 +6360,7 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
63606360
}
63616361

63626362
introducedDeclNames = introducedDeclNames.slice(numNames);
6363-
SmallVector<TypeExpr *, 1> conformances;
6363+
SmallVector<Expr *, 1> conformances;
63646364
for (TypeID conformanceID : introducedDeclNames) {
63656365
auto conformance = MF.getTypeChecked(conformanceID);
63666366
if (!conformance) {

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,13 +3399,13 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
33993399

34003400
unsigned numNames = introducedDeclNames.size();
34013401

3402-
(void)evaluateOrDefault(S.getASTContext().evaluator,
3403-
ResolveMacroConformances{theAttr, D}, {});
3402+
auto conformances =
3403+
evaluateOrDefault(S.getASTContext().evaluator,
3404+
ResolveMacroConformances{theAttr, D}, {});
34043405

34053406
unsigned numConformances = 0;
3406-
for (auto conformance : theAttr->getConformances()) {
3407-
introducedDeclNames.push_back(
3408-
S.addTypeRef(conformance->getInstanceType()));
3407+
for (auto conformance : conformances) {
3408+
introducedDeclNames.push_back(S.addTypeRef(conformance));
34093409
++numConformances;
34103410
}
34113411

0 commit comments

Comments
 (0)