@@ -308,6 +308,41 @@ ExternalMacroDefinitionRequest::evaluate(
308
308
return ExternalMacroDefinition{nullptr};
309
309
}
310
310
311
+ bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
312
+ Decl *decl) const {
313
+ auto *parentDecl = decl->getDeclContext()->getAsDecl();
314
+ if (!parentDecl)
315
+ return false;
316
+
317
+ bool addedAttributes = false;
318
+ auto parentAttrs = parentDecl->getSemanticAttrs();
319
+ for (auto customAttrConst: parentAttrs.getAttributes<CustomAttr>()) {
320
+ auto customAttr = const_cast<CustomAttr *>(customAttrConst);
321
+ auto customAttrDecl = evaluateOrDefault(
322
+ evaluator,
323
+ CustomAttrDeclRequest{
324
+ customAttr,
325
+ parentDecl->getInnermostDeclContext()
326
+ },
327
+ nullptr);
328
+
329
+ if (!customAttrDecl)
330
+ continue;
331
+
332
+ auto macroDecl = customAttrDecl.dyn_cast<MacroDecl *>();
333
+ if (!macroDecl)
334
+ continue;
335
+
336
+ if (!macroDecl->getMacroRoles().contains(MacroRole::MemberAttribute))
337
+ continue;
338
+
339
+ addedAttributes |= expandAttributes(customAttr, macroDecl, decl);
340
+ }
341
+
342
+ return addedAttributes;
343
+ }
344
+
345
+
311
346
/// Determine whether the given source file is from an expansion of the given
312
347
/// macro.
313
348
static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro) {
@@ -856,8 +891,7 @@ void swift::expandAccessors(
856
891
// FIXME: Almost entirely duplicated code from `expandAccessors`.
857
892
// Factor this out into an `expandAttachedMacro` function, with
858
893
// arguments for the PrettyStackTrace string, 'attachedTo' decl, etc.
859
- void swift::expandAttributes (CustomAttr *attr, MacroDecl *macro, Decl *member,
860
- SemanticDeclAttributes &result) {
894
+ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
861
895
auto *dc = member->getInnermostDeclContext();
862
896
ASTContext &ctx = dc->getASTContext();
863
897
SourceManager &sourceMgr = ctx.SourceMgr;
@@ -867,43 +901,43 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
867
901
auto attrSourceFile =
868
902
moduleDecl->getSourceFileContainingLocation(attr->AtLoc);
869
903
if (!attrSourceFile)
870
- return ;
904
+ return false ;
871
905
872
906
auto declSourceFile =
873
907
moduleDecl->getSourceFileContainingLocation(member->getStartLoc());
874
908
if (!declSourceFile)
875
- return ;
909
+ return false ;
876
910
877
911
Decl *parentDecl = member->getDeclContext()->getAsDecl();
878
912
if (!parentDecl)
879
- return ;
913
+ return false ;
880
914
881
915
auto parentDeclSourceFile =
882
916
moduleDecl->getSourceFileContainingLocation(parentDecl->getLoc());
883
917
if (!parentDeclSourceFile)
884
- return ;
918
+ return false ;
885
919
886
920
// Evaluate the macro.
887
921
NullTerminatedStringRef evaluatedSource;
888
922
889
923
if (isFromExpansionOfMacro(attrSourceFile, macro) ||
890
924
isFromExpansionOfMacro(declSourceFile, macro)) {
891
925
member->diagnose(diag::macro_recursive, macro->getName());
892
- return ;
926
+ return false ;
893
927
}
894
928
895
929
auto macroDef = macro->getDefinition();
896
930
switch (macroDef.kind) {
897
931
case MacroDefinition::Kind::Undefined:
898
932
case MacroDefinition::Kind::Invalid:
899
933
// Already diagnosed as an error elsewhere.
900
- return ;
934
+ return false ;
901
935
902
936
case MacroDefinition::Kind::Builtin: {
903
937
switch (macroDef.getBuiltinKind()) {
904
938
case BuiltinMacroKind::ExternalMacro:
905
939
// FIXME: Error here.
906
- return ;
940
+ return false ;
907
941
}
908
942
}
909
943
@@ -923,29 +957,29 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
923
957
macro->getName()
924
958
);
925
959
macro->diagnose(diag::decl_declared_here, macro->getName());
926
- return ;
960
+ return false ;
927
961
}
928
962
929
963
// Make sure macros are enabled before we expand.
930
964
if (!ctx.LangOpts.hasFeature(Feature::Macros)) {
931
965
member->diagnose(diag::macro_experimental);
932
- return ;
966
+ return false ;
933
967
}
934
968
935
969
#if SWIFT_SWIFT_PARSER
936
970
PrettyStackTraceDecl debugStack("expanding attribute macro", member);
937
971
938
972
auto astGenAttrSourceFile = attrSourceFile->exportedSourceFile;
939
973
if (!astGenAttrSourceFile)
940
- return ;
974
+ return false ;
941
975
942
976
auto astGenDeclSourceFile = declSourceFile->exportedSourceFile;
943
977
if (!astGenDeclSourceFile)
944
- return ;
978
+ return false ;
945
979
946
980
auto astGenParentDeclSourceFile = parentDeclSourceFile->exportedSourceFile;
947
981
if (!astGenParentDeclSourceFile)
948
- return ;
982
+ return false ;
949
983
950
984
Decl *searchDecl = member;
951
985
if (auto *var = dyn_cast<VarDecl>(member))
@@ -961,13 +995,13 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
961
995
astGenParentDeclSourceFile, parentDecl->getStartLoc().getOpaquePointerValue(),
962
996
&evaluatedSourceAddress, &evaluatedSourceLength);
963
997
if (!evaluatedSourceAddress)
964
- return ;
998
+ return false ;
965
999
evaluatedSource = NullTerminatedStringRef(evaluatedSourceAddress,
966
1000
(size_t)evaluatedSourceLength);
967
1001
break;
968
1002
#else
969
1003
member->diagnose(diag::macro_unsupported);
970
- return ;
1004
+ return false ;
971
1005
#endif
972
1006
}
973
1007
}
@@ -1029,6 +1063,7 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
1029
1063
PrettyStackTraceDecl debugStack(
1030
1064
"type checking expanded declaration macro", member);
1031
1065
1066
+ bool addedAttributes = false;
1032
1067
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
1033
1068
for (auto decl : topLevelDecls) {
1034
1069
// FIXME: We want to type check decl attributes applied to
@@ -1040,7 +1075,10 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
1040
1075
1041
1076
// Add the new attributes to the semantic attribute list.
1042
1077
for (auto *attr : decl->getAttrs()) {
1043
- result.add (attr);
1078
+ addedAttributes = true;
1079
+ member->getAttrs().add(attr);
1044
1080
}
1045
1081
}
1082
+
1083
+ return addedAttributes;
1046
1084
}
0 commit comments