Skip to content

[Macros] Top-level freestanding macros #63553

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 4 commits into from
Mar 6, 2023
Merged

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Feb 9, 2023

Allow freestanding macros to be used at top-level.

  • Parse top-level #… as MacroExpansionDecl when we are not in scripting mode.
  • Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
  • Experimental support for script mode and brace-level declaration macro expansions: When type-checking a MacroExpansionExpr, assign it a substitute MacroExpansionDecl if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.

@DougGregor
Copy link
Member

@swift-ci please smoke test

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.

This is looking good! A few comments, but no major structural concerns. I'd like us to centralize the logic for "walk the flattened list" and use it everywhere, because there's a lot of ad hoc recursion over rewritten macros, and we're likely to need to expand that logic... that said, I'd be okay landing like this and addressing comments in subsequent pull requests, since this is a big chunk of work that moves freestanding macros forward considerably.

// Note: Don't look into macro expansions as it would be a circular dependency
// to have macro arguments' default literal types depend on macro expansions.
auto lookup = TypeChecker::lookupUnqualified(
dc->getModuleScopeContext(), nameRef, SourceLoc(),
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we could conjure up a source location at the beginning of the current source file, so we get to rely on location-based lookup.

SmallVector<ASTNode, 4> expansion(topLevelItems.begin(),
topLevelItems.end());
for (auto &node : expansion)
if (auto *expr = node.dyn_cast<Expr *>())
Copy link
Member

Choose a reason for hiding this comment

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

We should handle Stmt * here as well, right?

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'll handle this in the next patch when I added support for codeItem.

@DougGregor
Copy link
Member

@swift-ci please smoke test

@xedin xedin removed their request for review February 13, 2023 21:09
@rxwei rxwei force-pushed the top-level-macro branch 4 times, most recently from 9dc87ba to e0b597b Compare February 20, 2023 21:24
@rxwei
Copy link
Contributor Author

rxwei commented Feb 20, 2023

@swift-ci please smoke test

@rxwei rxwei force-pushed the top-level-macro branch 2 times, most recently from a67dc47 to 1619b55 Compare February 21, 2023 16:32
@rxwei rxwei force-pushed the top-level-macro branch 3 times, most recently from f716fbc to 9cc0e02 Compare February 22, 2023 13:45
@rxwei rxwei requested a review from hborla February 23, 2023 13:17
@rxwei rxwei force-pushed the top-level-macro branch 2 times, most recently from f4f3e6a to d71be1e Compare February 27, 2023 15:06
@rxwei rxwei force-pushed the top-level-macro branch 4 times, most recently from 2c217d7 to 58c85d0 Compare March 5, 2023 17:48

for (const auto *macro : found->second) {
macro->getIntroducedNames(MacroRole::Declaration,
/*attachedTo*/ nullptr,
Copy link
Member

@hborla hborla Mar 5, 2023

Choose a reason for hiding this comment

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

Nit: the dyn_cast<ValueDecl>(decl) above is already tolerant to MacroExpansionDecl, because the dynamic case will just return nullptr. I had intended for the code above to instead conditionalize the macro role and macro reference, instead of the entire if body.

Totally fine with a follow-up cleanup here, just wanted to mention it before I forget!

@rxwei rxwei force-pushed the top-level-macro branch 2 times, most recently from a864d42 to d24303c Compare March 6, 2023 05:34
@rxwei rxwei requested a review from AnthonyLatsis as a code owner March 6, 2023 05:34
@rxwei rxwei force-pushed the top-level-macro branch from d24303c to b56a62e Compare March 6, 2023 05:37
@rxwei rxwei force-pushed the top-level-macro branch from b56a62e to 6cf75cd Compare March 6, 2023 05:50
@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please smoke test

@rxwei rxwei force-pushed the top-level-macro branch from 6cf75cd to 24e204a Compare March 6, 2023 05:52
@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please smoke test

@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please build toolchain

@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please smoke test

@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please build toolchain macos

@rxwei
Copy link
Contributor Author

rxwei commented Mar 6, 2023

@swift-ci please smoke test macos

@rxwei rxwei merged commit 833338f into swiftlang:main Mar 6, 2023
@rxwei rxwei deleted the top-level-macro branch March 6, 2023 15:16
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