Skip to content

[5.9] Macro refactorings and fixes #66426

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 7 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/Basic/Mangler.h"
#include "swift/AST/Types.h"
#include "swift/AST/Decl.h"
#include "swift/AST/FreestandingMacroExpansion.h"
#include "swift/Basic/TaggedUnion.h"

namespace clang {
Expand Down Expand Up @@ -367,8 +368,7 @@ class ASTMangler : public Mangler {
mangleRuntimeAttributeGeneratorEntity(const ValueDecl *decl, CustomAttr *attr,
SymbolKind SKind = SymbolKind::Default);

std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
std::string mangleMacroExpansion(const FreestandingMacroExpansion *expansion);
std::string mangleAttachedMacroExpansion(
const Decl *decl, CustomAttr *attr, MacroRole role);

Expand Down
81 changes: 27 additions & 54 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "swift/AST/DefaultArgumentKind.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/FreestandingMacroExpansion.h"
#include "swift/AST/GenericParamKey.h"
#include "swift/AST/IfConfigClause.h"
#include "swift/AST/LayoutConstraint.h"
Expand Down Expand Up @@ -912,7 +913,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
///
/// Auxiliary declarations can be property wrapper backing variables,
/// backing variables for 'lazy' vars, or peer macro expansions.
void visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const;
///
/// When \p visitFreestandingExpanded is true (the default), this will also
/// visit the declarations produced by a freestanding macro expansion.
void visitAuxiliaryDecls(
AuxiliaryDeclCallback callback,
bool visitFreestandingExpanded = true
) const;

using MacroCallback = llvm::function_ref<void(CustomAttr *, MacroDecl *)>;

Expand Down Expand Up @@ -8596,6 +8603,9 @@ class MacroDecl : public GenericContext, public ValueDecl {
/// Retrieve the definition of this macro.
MacroDefinition getDefinition() const;

/// Set the definition of this macro
void setDefinition(MacroDefinition definition);

/// Retrieve the parameter list of this macro.
ParameterList *getParameterList() const { return parameterList; }

Expand All @@ -8618,69 +8628,29 @@ class MacroDecl : public GenericContext, public ValueDecl {
using Decl::getASTContext;
};

/// Information about a macro expansion that is common between macro
/// expansion declarations and expressions.
///
/// Instances of these types will be shared among paired macro expansion
/// declaration/expression nodes.
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
SourceLoc SigilLoc;
DeclNameRef MacroName;
DeclNameLoc MacroNameLoc;
SourceLoc LeftAngleLoc, RightAngleLoc;
ArrayRef<TypeRepr *> GenericArgs;
ArgumentList *ArgList;

/// The referenced macro.
ConcreteDeclRef macroRef;

MacroExpansionInfo(SourceLoc sigilLoc,
DeclNameRef macroName,
DeclNameLoc macroNameLoc,
SourceLoc leftAngleLoc, SourceLoc rightAngleLoc,
ArrayRef<TypeRepr *> genericArgs,
ArgumentList *argList)
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
GenericArgs(genericArgs), ArgList(argList) { }
};

class MacroExpansionDecl : public Decl {
MacroExpansionInfo *info;
class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {

public:
enum : unsigned { InvalidDiscriminator = 0xFFFF };

MacroExpansionDecl(DeclContext *dc, MacroExpansionInfo *info);

MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
DeclNameLoc macroLoc,
SourceLoc leftAngleLoc,
ArrayRef<TypeRepr *> genericArgs,
SourceLoc rightAngleLoc,
ArgumentList *args);
static MacroExpansionDecl *create(DeclContext *dc, SourceLoc poundLoc,
DeclNameRef macro, DeclNameLoc macroLoc,
SourceLoc leftAngleLoc,
ArrayRef<TypeRepr *> genericArgs,
SourceLoc rightAngleLoc,
ArgumentList *args);

ArrayRef<TypeRepr *> getGenericArgs() const {
return info->GenericArgs;
}
DeclContext *getDeclContext() const { return Decl::getDeclContext(); }

SourceRange getGenericArgsRange() const {
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
SourceRange getSourceRange() const {
return getExpansionInfo()->getSourceRange();
}
SourceLoc getLocFromSource() const { return getExpansionInfo()->SigilLoc; }

SourceRange getSourceRange() const;
SourceLoc getLocFromSource() const { return info->SigilLoc; }
SourceLoc getPoundLoc() const { return info->SigilLoc; }
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
DeclNameRef getMacroName() const { return info->MacroName; }
ArgumentList *getArgs() const { return info->ArgList; }
void setArgs(ArgumentList *args) { info->ArgList = args; }
using ExprOrStmtExpansionCallback = llvm::function_ref<void(ASTNode)>;
void forEachExpandedExprOrStmt(ExprOrStmtExpansionCallback) const;
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }

MacroExpansionInfo *getExpansionInfo() const { return info; }
/// Enumerate the nodes produced by expanding this macro expansion.
void forEachExpandedNode(llvm::function_ref<void(ASTNode)> callback) const;

/// Returns a discriminator which determines this macro expansion's index
/// in the sequence of macro expansions within the current function.
Expand All @@ -8703,6 +8673,9 @@ class MacroExpansionDecl : public Decl {
static bool classof(const Decl *D) {
return D->getKind() == DeclKind::MacroExpansion;
}
static bool classof(const FreestandingMacroExpansion *expansion) {
return expansion->getFreestandingMacroKind() == FreestandingMacroKind::Decl;
}
};

/// Find references to the given generic parameter in the type signature of the
Expand Down
54 changes: 21 additions & 33 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/AST/Decl.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/DeclNameLoc.h"
#include "swift/AST/FreestandingMacroExpansion.h"
#include "swift/AST/FunctionRefKind.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/TypeAlignments.h"
Expand Down Expand Up @@ -6214,10 +6215,10 @@ class TypeJoinExpr final : public Expr,

/// An invocation of a macro expansion, spelled with `#` for freestanding
/// macros or `@` for attached macros.
class MacroExpansionExpr final : public Expr {
class MacroExpansionExpr final : public Expr,
public FreestandingMacroExpansion {
private:
DeclContext *DC;
MacroExpansionInfo *info;
Expr *Rewritten;
MacroRoles Roles;
MacroExpansionDecl *SubstituteDecl;
Expand All @@ -6226,47 +6227,31 @@ class MacroExpansionExpr final : public Expr {
enum : unsigned { InvalidDiscriminator = 0xFFFF };

explicit MacroExpansionExpr(DeclContext *dc, MacroExpansionInfo *info,
MacroRoles roles,
bool isImplicit = false,
MacroRoles roles, bool isImplicit = false,
Type ty = Type())
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
DC(dc), info(info), Rewritten(nullptr), Roles(roles),
SubstituteDecl(nullptr) {
FreestandingMacroExpansion(FreestandingMacroKind::Expr, info), DC(dc),
Rewritten(nullptr), Roles(roles), SubstituteDecl(nullptr) {
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
}

explicit MacroExpansionExpr(DeclContext *dc,
SourceLoc sigilLoc, DeclNameRef macroName,
DeclNameLoc macroNameLoc,
SourceLoc leftAngleLoc,
ArrayRef<TypeRepr *> genericArgs,
SourceLoc rightAngleLoc,
ArgumentList *argList,
MacroRoles roles,
bool isImplicit = false,
Type ty = Type());

DeclNameRef getMacroName() const { return info->MacroName; }
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
static MacroExpansionExpr *
create(DeclContext *dc, SourceLoc sigilLoc, DeclNameRef macroName,
DeclNameLoc macroNameLoc, SourceLoc leftAngleLoc,
ArrayRef<TypeRepr *> genericArgs, SourceLoc rightAngleLoc,
ArgumentList *argList, MacroRoles roles, bool isImplicit = false,
Type ty = Type());

Expr *getRewritten() const { return Rewritten; }
void setRewritten(Expr *rewritten) { Rewritten = rewritten; }

ArrayRef<TypeRepr *> getGenericArgs() const { return info->GenericArgs; }

SourceRange getGenericArgsRange() const {
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
ArgumentList *getArgs() const {
return FreestandingMacroExpansion::getArgs();
}

ArgumentList *getArgs() const { return info->ArgList; }
void setArgs(ArgumentList *newArgs) { info->ArgList = newArgs; }

MacroRoles getMacroRoles() const { return Roles; }

SourceLoc getLoc() const { return info->SigilLoc; }

ConcreteDeclRef getMacroRef() const { return info->macroRef; }
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
SourceLoc getLoc() const { return getPoundLoc(); }

DeclContext *getDeclContext() const { return DC; }
void setDeclContext(DeclContext *dc) { DC = dc; }
Expand All @@ -6289,16 +6274,19 @@ class MacroExpansionExpr final : public Expr {
Bits.MacroExpansionExpr.Discriminator = discriminator;
}

MacroExpansionInfo *getExpansionInfo() const { return info; }

SourceRange getSourceRange() const;
SourceRange getSourceRange() const {
return getExpansionInfo()->getSourceRange();
}

MacroExpansionDecl *createSubstituteDecl();
MacroExpansionDecl *getSubstituteDecl() const;

static bool classof(const Expr *E) {
return E->getKind() == ExprKind::MacroExpansion;
}
static bool classof(const FreestandingMacroExpansion *expansion) {
return expansion->getFreestandingMacroKind() == FreestandingMacroKind::Expr;
}
};

inline bool Expr::isInfixOperator() const {
Expand Down
113 changes: 113 additions & 0 deletions include/swift/AST/FreestandingMacroExpansion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//===--- FreestandingMacroExpansion.h ------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H
#define SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H

#include "swift/AST/ASTAllocated.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/ConcreteDeclRef.h"
#include "swift/AST/DeclNameLoc.h"
#include "swift/AST/Identifier.h"
#include "swift/Basic/SourceLoc.h"

namespace swift {
class MacroExpansionDecl;
class MacroExpansionExpr;
class Expr;
class Decl;
class ArgumentList;

/// Information about a macro expansion that is common between macro
/// expansion declarations and expressions.
///
/// Instances of these types will be shared among paired macro expansion
/// declaration/expression nodes.
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
SourceLoc SigilLoc;
DeclNameRef MacroName;
DeclNameLoc MacroNameLoc;
SourceLoc LeftAngleLoc, RightAngleLoc;
llvm::ArrayRef<TypeRepr *> GenericArgs;
ArgumentList *ArgList;

/// The referenced macro.
ConcreteDeclRef macroRef;

MacroExpansionInfo(SourceLoc sigilLoc, DeclNameRef macroName,
DeclNameLoc macroNameLoc, SourceLoc leftAngleLoc,
SourceLoc rightAngleLoc, ArrayRef<TypeRepr *> genericArgs,
ArgumentList *argList)
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
GenericArgs(genericArgs), ArgList(argList) {}

SourceLoc getLoc() const { return SigilLoc; }
SourceRange getGenericArgsRange() const {
return {LeftAngleLoc, RightAngleLoc};
}
SourceRange getSourceRange() const;
};

enum class FreestandingMacroKind {
Expr, // MacroExpansionExpr.
Decl, // MacroExpansionDecl.
};

/// A base class of either 'MacroExpansionExpr' or 'MacroExpansionDecl'.
class FreestandingMacroExpansion {
llvm::PointerIntPair<MacroExpansionInfo *, 1, FreestandingMacroKind>
infoAndKind;

protected:
FreestandingMacroExpansion(FreestandingMacroKind kind,
MacroExpansionInfo *info)
: infoAndKind(info, kind) {}

public:
MacroExpansionInfo *getExpansionInfo() const {
return infoAndKind.getPointer();
}
FreestandingMacroKind getFreestandingMacroKind() const {
return infoAndKind.getInt();
}

ASTNode getASTNode();

SourceLoc getPoundLoc() const { return getExpansionInfo()->SigilLoc; }

DeclNameLoc getMacroNameLoc() const {
return getExpansionInfo()->MacroNameLoc;
}
DeclNameRef getMacroName() const { return getExpansionInfo()->MacroName; }

ArrayRef<TypeRepr *> getGenericArgs() const {
return getExpansionInfo()->GenericArgs;
}
SourceRange getGenericArgsRange() const {
return getExpansionInfo()->getGenericArgsRange();
}

ArgumentList *getArgs() const { return getExpansionInfo()->ArgList; }
void setArgs(ArgumentList *args) { getExpansionInfo()->ArgList = args; }

ConcreteDeclRef getMacroRef() const { return getExpansionInfo()->macroRef; }
void setMacroRef(ConcreteDeclRef ref) { getExpansionInfo()->macroRef = ref; }

DeclContext *getDeclContext() const;
SourceRange getSourceRange() const;
unsigned getDiscriminator() const;
};

} // namespace swift

#endif // SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H
6 changes: 2 additions & 4 deletions include/swift/AST/MacroDiscriminatorContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ namespace swift {
/// Describes the context of a macro expansion for the purpose of
/// computing macro expansion discriminators.
struct MacroDiscriminatorContext
: public llvm::PointerUnion<DeclContext *, MacroExpansionExpr *,
MacroExpansionDecl *> {
: public llvm::PointerUnion<DeclContext *, FreestandingMacroExpansion *> {
using PointerUnion::PointerUnion;

static MacroDiscriminatorContext getParentOf(MacroExpansionExpr *expansion);
static MacroDiscriminatorContext getParentOf(MacroExpansionDecl *expansion);
static MacroDiscriminatorContext getParentOf(FreestandingMacroExpansion *expansion);
static MacroDiscriminatorContext getParentOf(
SourceLoc loc, DeclContext *origDC
);
Expand Down
Loading