Skip to content

[Macros] Freestanding declaration macros #62934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2023

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Jan 9, 2023

Add support for freestanding declaration macros.

  • Parse @declaration attribute.
  • Type check and expand MacroExpansionDecl.

Known issues:

  • Generic macros are not yet handled.
  • Expansion does not work when the parent decl context is BraceStmt. Need to parse freestanding declaration macro expansions in BraceStmt as MacroExpansionDecl, and add expanded decls to name lookup.

@rxwei rxwei force-pushed the freestanding-declaration-macros branch 9 times, most recently from 6105347 to d1f28a2 Compare January 10, 2023 00:01
@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please test

@rxwei rxwei force-pushed the freestanding-declaration-macros branch from d1f28a2 to a5b99bf Compare January 10, 2023 02:12
@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please test linux

@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please test macos

@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please clean test windows

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of questions, but this is pretty awesome. Thank you!

llvm_unreachable("FIXME: macro expansion decl not handled in DeclChecker");
auto expanded = evaluateOrDefault(
Ctx.evaluator, ExpandMacroExpansionDeclRequest{MED}, {});
// FIXME: Handle this in name lookup instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely agreed that this should be handled in name lookup. This won't work across multiple source files.

// Call-like macros need to be resolved.
else {
using namespace constraints;
ConstraintSystem cs(dc, ConstraintSystemOptions());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a whole lot like CSGen for MacroExpansionExpr. Can we unify the implementations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can pull it out to TypeCheckMacros.h. I can do it in a separate patch.

@@ -498,3 +498,165 @@ Expr *swift::expandMacroExpr(
"Type checking changed the result type?");
return expandedExpr;
}

/// Expands the given macro expansion declaration and type-check the result.
bool swift::expandFreestandingDeclarationMacro(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, lots of duplication with the expandMacro for expressions. Can we unify them somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can definitely pull out the buffer creation, source info creation and the call to ASTGen in a follow-up patch.

@xedin xedin removed their request for review January 10, 2023 20:46
@rxwei rxwei force-pushed the freestanding-declaration-macros branch from a5b99bf to dc9260d Compare January 10, 2023 21:04
@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please smoke test

@rxwei rxwei force-pushed the freestanding-declaration-macros branch from dc9260d to a6788e8 Compare January 10, 2023 23:02
@rxwei
Copy link
Contributor Author

rxwei commented Jan 10, 2023

@swift-ci please smoke test and merge

Add support for freestanding declaration macros.

- Parse `@declaration` attribute.
- Type check and expand `MacroExpansionDecl`.

Known issues:
- Generic macros are not yet handled.
- Expansion does not work when the parent decl context is `BraceStmt`. Need to parse freestanding declaration macro expansions in `BraceStmt` as `MacroExpansionDecl`, and add expanded decls to name lookup.
@rxwei rxwei force-pushed the freestanding-declaration-macros branch from a6788e8 to f17b7c4 Compare January 11, 2023 03:09
@rxwei
Copy link
Contributor Author

rxwei commented Jan 11, 2023

@swift-ci please smoke test and merge


DeclAttribute('declaration', 'Declaration',
OnMacro, AllowMultipleAttributes,
ABIStableToAdd, ABIStableToRemove, APIStableToAdd, APIBreakingToRemove, # noqa: E501
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL what # noqa: E501 is for... Why doesn't it run on linux!?

@swift-ci swift-ci merged commit a82e17b into swiftlang:main Jan 11, 2023
@rxwei
Copy link
Contributor Author

rxwei commented Jan 11, 2023

Sorry for the CI breakage. I didn't know "test and merge" would bypass Windows CI. @shahmishal @compnerd

@rxwei rxwei deleted the freestanding-declaration-macros branch January 11, 2023 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants