Skip to content

Commit 052a77e

Browse files
authored
Merge pull request #61783 from DougGregor/macro-type-checking
2 parents 1d76e24 + d6d2318 commit 052a77e

23 files changed

+883
-47
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_C_AST_ASTBRIDGING_H
1515

1616
#include <inttypes.h>
17+
#include "swift/Basic/Compiler.h"
1718

1819
#if __clang__
1920
// Provide macros to temporarily suppress warning about the use of
@@ -48,6 +49,43 @@ typedef struct {
4849

4950
typedef void *BridgedIdentifier;
5051

52+
typedef struct {
53+
BridgedIdentifier _Nullable Name;
54+
void *_Nullable NameLoc;
55+
BridgedIdentifier _Nullable SecondName;
56+
void *_Nullable SecondNameLoc;
57+
void *_Nullable UnderscoreLoc;
58+
void *_Nullable ColonLoc;
59+
void *Type;
60+
void *_Nullable TrailingCommaLoc;
61+
} BridgedTupleTypeElement;
62+
63+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : long {
64+
/// A type bound T : P, where T is a type that depends on a generic
65+
/// parameter and P is some type that should bound T, either as a concrete
66+
/// supertype or a protocol to which T must conform.
67+
BridgedRequirementReprKindTypeConstraint,
68+
69+
/// A same-type requirement T == U, where T and U are types that shall be
70+
/// equivalent.
71+
BridgedRequirementReprKindSameType,
72+
73+
/// A layout bound T : L, where T is a type that depends on a generic
74+
/// parameter and L is some layout specification that should bound T.
75+
BridgedRequirementReprKindLayoutConstraint,
76+
77+
// Note: there is code that packs this enum in a 2-bit bitfield. Audit users
78+
// when adding enumerators.
79+
} BridgedRequirementReprKind;
80+
81+
typedef struct {
82+
void *_Nullable SeparatorLoc;
83+
BridgedRequirementReprKind Kind;
84+
void *FirstType;
85+
void *SecondType;
86+
// FIXME: Handle Layout Requirements
87+
} BridgedRequirementRepr;
88+
5189
#ifdef __cplusplus
5290
extern "C" {
5391

@@ -123,10 +161,33 @@ struct DeclContextAndDecl {
123161
};
124162

125163
struct DeclContextAndDecl StructDecl_create(
126-
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);
164+
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *_Nullable genericParams, void *dc);
127165
struct DeclContextAndDecl ClassDecl_create(
128166
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);
129167

168+
void *ArrayTypeRepr_create(void *ctx, void *base, void *lsquareLoc, void *rsquareLoc);
169+
void *DictionaryTypeRepr_create(void *ctx, void *keyType, void *valueType, void *lsquareLoc, void *colonloc, void *rsquareLoc);
170+
void *OptionalTypeRepr_create(void *ctx, void *base, void *questionLoc);
171+
void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base, void *exclamationLoc);
172+
void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc);
173+
void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc);
174+
void *PackExpansionTypeRepr_create(void *ctx, void *base, void *ellipsisLoc);
175+
void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc, void *rParenLoc);
176+
void *IdentTypeRepr_create(void *ctx, BridgedArrayRef components);
177+
void *GenericIdentTypeRepr_create(void *ctx, BridgedIdentifier name, void *nameLoc, BridgedArrayRef genericArgs, void *lAngle, void *rAngle);
178+
void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types, void *firstTypeLoc);
179+
void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc, void *_Nullable throwsLoc, void *arrowLoc, void *returnType);
180+
void *NamedOpaqueReturnTypeRepr_create(void *ctx, void *baseTy);
181+
void *OpaqueReturnTypeRepr_create(void *ctx, void *opaqueLoc, void *baseTy);
182+
void *ExistentialTypeRepr_create(void *ctx, void *anyLoc, void *baseTy);
183+
void *GenericParamList_create(void *ctx, void *lAngleLoc, BridgedArrayRef params, void *_Nullable whereLoc, BridgedArrayRef reqs, void *rAngleLoc);
184+
void *GenericTypeParamDecl_create(void *ctx, void *declContext, BridgedIdentifier name, void *nameLoc, void *_Nullable ellipsisLoc, long index, _Bool isParameterPack);
185+
void GenericTypeParamDecl_setInheritedType(void *ctx, void *Param, void *ty);
186+
187+
struct DeclContextAndDecl TypeAliasDecl_create(void *ctx, void *declContext, void *aliasLoc, void *equalLoc, BridgedIdentifier name, void *nameLoc, void *_Nullable genericParams);
188+
void TypeAliasDecl_setUnderlyingTypeRepr(void *decl, void *underlyingType);
189+
190+
130191
void TopLevelCodeDecl_dump(void *);
131192
void Expr_dump(void *);
132193
void Decl_dump(void *);

include/swift/AST/Expr.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,20 +6013,23 @@ class TypeJoinExpr final : public Expr,
60136013

60146014
class MacroExpansionExpr final : public Expr {
60156015
private:
6016-
Expr *Macro;
6017-
Expr *Rewritten;
6018-
ArgumentList *ArgList;
60196016
SourceLoc PoundLoc;
6017+
DeclNameRef MacroName;
6018+
DeclNameLoc MacroNameLoc;
6019+
ArgumentList *ArgList;
6020+
Expr *Rewritten;
60206021

60216022
public:
6022-
explicit MacroExpansionExpr(SourceLoc poundLoc, Expr *macro,
6023+
explicit MacroExpansionExpr(SourceLoc poundLoc, DeclNameRef macroName,
6024+
DeclNameLoc macroNameLoc,
60236025
ArgumentList *argList, bool isImplicit = false,
60246026
Type ty = Type())
6025-
: Expr(ExprKind::MacroExpansion, isImplicit, ty), Macro(macro),
6026-
Rewritten(nullptr), ArgList(argList), PoundLoc(poundLoc) {}
6027+
: Expr(ExprKind::MacroExpansion, isImplicit, ty), PoundLoc(poundLoc),
6028+
MacroName(macroName), MacroNameLoc(macroNameLoc), ArgList(argList),
6029+
Rewritten(nullptr) { }
60276030

6028-
Expr *getMacro() const { return Macro; }
6029-
void setMacro(Expr *macro) { Macro = macro; }
6031+
DeclNameRef getMacroName() const { return MacroName; }
6032+
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
60306033

60316034
Expr *getRewritten() const { return Rewritten; }
60326035
void setRewritten(Expr *rewritten) { Rewritten = rewritten; }
@@ -6038,7 +6041,7 @@ class MacroExpansionExpr final : public Expr {
60386041

60396042
SourceRange getSourceRange() const {
60406043
return SourceRange(
6041-
PoundLoc, ArgList ? ArgList->getEndLoc() : Macro->getEndLoc());
6044+
PoundLoc, ArgList ? ArgList->getEndLoc() : MacroNameLoc.getEndLoc());
60426045
}
60436046

60446047
static bool classof(const Expr *E) {

include/swift/AST/TypeCheckRequests.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,40 @@ class SynthesizeHasSymbolQueryRequest
37543754
bool isCached() const { return true; }
37553755
};
37563756

3757+
3758+
/// Retrieves the evaluation context of a macro with the given name.
3759+
///
3760+
/// The macro evaluation context is a user-defined generic signature and return
3761+
/// type that serves as the "interface type" of references to the macro. The
3762+
/// current implementation takes those pieces of syntax from the macro itself,
3763+
/// then inserts them into a Swift struct that looks like
3764+
///
3765+
/// \code
3766+
/// struct __MacroEvaluationContext\(macro.genericSignature) {
3767+
/// typealias SignatureType = \(macro.signature)
3768+
/// }
3769+
/// \endcode
3770+
///
3771+
/// So that we can use all of Swift's native name lookup and type resolution
3772+
/// facilities to map the parsed signature type back into a semantic \c Type
3773+
/// AST and a set of requiremnets.
3774+
class MacroContextRequest
3775+
: public SimpleRequest<MacroContextRequest,
3776+
StructDecl *(std::string, ModuleDecl *),
3777+
RequestFlags::Cached> {
3778+
public:
3779+
using SimpleRequest::SimpleRequest;
3780+
3781+
private:
3782+
friend SimpleRequest;
3783+
3784+
StructDecl *evaluate(Evaluator &evaluator,
3785+
std::string macroName, ModuleDecl *mod) const;
3786+
3787+
public:
3788+
bool isCached() const { return true; }
3789+
};
3790+
37573791
void simple_display(llvm::raw_ostream &out, ASTNode node);
37583792
void simple_display(llvm::raw_ostream &out, Type value);
37593793
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,6 @@ SWIFT_REQUEST(TypeChecker, GetTypeWrapperInitializer,
443443
SWIFT_REQUEST(TypeChecker, SynthesizeHasSymbolQueryRequest,
444444
FuncDecl *(ValueDecl *),
445445
Cached, NoLocationInfo)
446+
SWIFT_REQUEST(TypeChecker, MacroContextRequest,
447+
StructDecl *(std::string, ModuleDecl *),
448+
Cached, NoLocationInfo)

include/swift/Sema/ConstraintSystem.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,6 +4906,14 @@ class ConstraintSystem {
49064906
ConstraintLocator *locator,
49074907
OpenedTypeMap *replacements = nullptr);
49084908

4909+
#if SWIFT_SWIFT_PARSER
4910+
/// Retrieve the opened type of a macro with the given name.
4911+
///
4912+
/// \returns The opened type of the macro with this name, or the null \c Type
4913+
/// if no such macro exists.
4914+
Type getTypeOfMacroReference(StringRef macro, Expr *anchor);
4915+
#endif
4916+
49094917
/// Retrieve a list of generic parameter types solver has "opened" (replaced
49104918
/// with a type variable) at the given location.
49114919
ArrayRef<OpenedType> getOpenedTypes(ConstraintLocator *locator) const {

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,8 +2976,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
29762976

29772977
void visitMacroExpansionExpr(MacroExpansionExpr *E) {
29782978
printCommon(E, "macro_expansion_expr");
2979-
OS << '\n';
2980-
printRec(E->getMacro());
2979+
PrintWithColorRAII(OS, IdentifierColor) << " name=" << E->getMacroName();
29812980
if (E->getArgs()) {
29822981
OS << '\n';
29832982
printArgumentList(E->getArgs());

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
12471247
}
12481248

12491249
Expr *visitMacroExpansionExpr(MacroExpansionExpr *E) {
1250-
auto *macro = doIt(E->getMacro());
1251-
if (!macro) return nullptr;
12521250
Expr *rewritten = nullptr;
12531251
if (E->getRewritten()) {
12541252
rewritten = doIt(E->getRewritten());
@@ -1259,7 +1257,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
12591257
args = doIt(E->getArgs());
12601258
if (!args) return nullptr;
12611259
}
1262-
E->setMacro(macro);
12631260
E->setRewritten(rewritten);
12641261
E->setArgs(args);
12651262
return E;

0 commit comments

Comments
 (0)