Skip to content

Commit b01701b

Browse files
committed
[AST] MacroRoleAttr accept any 'Expr *' as the conformances arguments
1 parent f8f3651 commit b01701b

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
@@ -745,86 +745,6 @@ extension ASTGenVisitor {
745745
return BridgedMacroIntroducedDeclName(kind: kind, name: name)
746746
}
747747

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

859779
var names: [BridgedMacroIntroducedDeclName] = []
860-
var conformances: [BridgedTypeExpr] = []
780+
var conformances: [BridgedExpr] = []
861781

862782
enum ArgState {
863783
case inNames
@@ -905,9 +825,7 @@ extension ASTGenVisitor {
905825
names.append(name)
906826
}
907827
case .inConformances:
908-
if let conformance = self.generateMacroIntroducedConformance(expr: arg.expression) {
909-
conformances.append(BridgedTypeExpr.createParsed(self.ctx, type: conformance))
910-
}
828+
conformances.append(self.generate(expr: arg.expression))
911829
case .inInvalid:
912830
// Ignore the value.
913831
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"
@@ -2088,7 +2089,19 @@ ResolveMacroConformances::evaluate(Evaluator &evaluator,
20882089
auto &ctx = dc->getASTContext();
20892090

20902091
SmallVector<Type, 2> protocols;
2091-
for (auto *typeExpr : attr->getConformances()) {
2092+
for (Expr *&expr : const_cast<MacroRoleAttr *>(attr)->getConformances()) {
2093+
using namespace constraints;
2094+
auto target = SyntacticElementTarget(expr, dc, CTP_Unused, Type(),
2095+
/*isDiscarded=*/true);
2096+
if (ConstraintSystem::preCheckTarget(target))
2097+
continue;
2098+
auto *typeExpr = dyn_cast<TypeExpr>(target.getAsExpr());
2099+
if (!typeExpr) {
2100+
ctx.Diags.diagnose(expr->getStartLoc(), diag::expected_type);
2101+
continue;
2102+
}
2103+
expr = typeExpr;
2104+
20922105
if (auto *typeRepr = typeExpr->getTypeRepr()) {
20932106
auto resolved =
20942107
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)