@@ -10490,9 +10490,50 @@ ArrayRef<CustomAttr *> Decl::getRuntimeDiscoverableAttrs() const {
1049010490 nullptr );
1049110491}
1049210492
10493+ // / Adjust the declaration context to find a point in the context hierarchy
10494+ // / that the macro can be anchored on.
10495+ DeclContext *
10496+ MacroDiscriminatorContext::getInnermostMacroContext (DeclContext *dc) {
10497+ switch (dc->getContextKind ()) {
10498+ case DeclContextKind::SubscriptDecl:
10499+ case DeclContextKind::EnumElementDecl:
10500+ case DeclContextKind::AbstractFunctionDecl:
10501+ case DeclContextKind::SerializedLocal:
10502+ case DeclContextKind::Package:
10503+ case DeclContextKind::Module:
10504+ case DeclContextKind::FileUnit:
10505+ case DeclContextKind::GenericTypeDecl:
10506+ case DeclContextKind::ExtensionDecl:
10507+ case DeclContextKind::MacroDecl:
10508+ // These contexts are always fine
10509+ return dc;
10510+
10511+ case DeclContextKind::TopLevelCodeDecl:
10512+ // For top-level code, use the enclosing source file as the context.
10513+ return getInnermostMacroContext (dc->getParent ());
10514+
10515+ case DeclContextKind::AbstractClosureExpr: {
10516+ // For closures, we can mangle the closure if we're in a context we can
10517+ // mangle. Check that context.
10518+ auto adjustedParentDC = getInnermostMacroContext (dc->getParent ());
10519+ if (adjustedParentDC == dc->getParent ())
10520+ return dc;
10521+
10522+ return adjustedParentDC;
10523+ }
10524+
10525+ case DeclContextKind::Initializer:
10526+ // Initializers can be part of inferring types for variables, so we need
10527+ // their context.
10528+ return getInnermostMacroContext (dc->getParent ());
10529+ }
10530+ }
10531+
1049310532// / Retrieve the parent discriminator context for the given macro.
1049410533MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf (
1049510534 SourceLoc loc, DeclContext *origDC) {
10535+ origDC = getInnermostMacroContext (origDC);
10536+
1049610537 if (loc.isInvalid ())
1049710538 return origDC;
1049810539
0 commit comments