Skip to content

Commit dddda9d

Browse files
CodaFiDougGregor
authored andcommitted
Wrap ASTGen Lookup
1 parent 032e8d5 commit dddda9d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/Sema/TypeCheckMacros.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,41 @@
2525

2626
using namespace swift;
2727

28+
extern "C" void *swift_ASTGen_lookupMacro(const char *macroName);
29+
30+
extern "C" void swift_ASTGen_destroyMacro(void *macro);
31+
32+
extern "C" void swift_ASTGen_getMacroTypeSignature(
33+
void *sourceFile, void *declContext, void *astContext, void *macro,
34+
void **genericSignature,
35+
void **signature);
36+
2837
extern "C" ptrdiff_t swift_ASTGen_evaluateMacro(
2938
void *sourceFile, const void *sourceLocation,
3039
const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
3140

3241
#if SWIFT_SWIFT_PARSER
42+
43+
llvm::Optional<ASTGenMacroRAII> ASTGenMacroRAII::lookup(StringRef macroName,
44+
void *sourceFile,
45+
DeclContext *DC,
46+
ASTContext &ctx) {
47+
auto *macro = swift_ASTGen_lookupMacro(macroName.str().c_str());
48+
if (!macro)
49+
return None;
50+
51+
void *genericParamList = nullptr;
52+
void *signatureAST = nullptr;
53+
swift_ASTGen_getMacroTypeSignature(sourceFile, (void *)DC, (void *)&ctx,
54+
macro, &genericParamList,
55+
&signatureAST);
56+
57+
return ASTGenMacroRAII{macro, (TypeRepr *)signatureAST,
58+
(GenericParamList *)genericParamList};
59+
}
60+
61+
ASTGenMacroRAII::~ASTGenMacroRAII() { /*swift_ASTGen_destroyMacro(opaqueMacro);*/ }
62+
3363
Expr *swift::expandMacroExpr(
3464
DeclContext *dc, Expr *expr, StringRef macroName, Type expandedType
3565
) {

lib/Sema/TypeCheckMacros.h

+29-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,39 @@
1717
#define SWIFT_SEMA_TYPECHECKMACROS_H
1818

1919
#include "swift/AST/Type.h"
20+
#include "llvm/ADT/StringRef.h"
2021

2122
namespace swift {
2223

2324
class Expr;
24-
25+
class TypeRepr;
26+
27+
#if SWIFT_SWIFT_PARSER
28+
29+
struct ASTGenMacroRAII {
30+
private:
31+
void *opaqueMacro;
32+
TypeRepr *signature;
33+
GenericParamList *genericSignature;
34+
35+
ASTGenMacroRAII(void *macro, TypeRepr *sig, GenericParamList *genericSig)
36+
: opaqueMacro(macro), signature(sig),
37+
genericSignature(genericSig) {}
38+
39+
public:
40+
static llvm::Optional<ASTGenMacroRAII> lookup(StringRef macroName,
41+
void *sourceFile,
42+
DeclContext *DC,
43+
ASTContext &ctx);
44+
45+
TypeRepr *getSignature() const { return signature; }
46+
GenericParamList *getGenericSignature() const { return genericSignature; }
47+
48+
~ASTGenMacroRAII();
49+
};
50+
51+
#endif
52+
2553
/// Expands the given macro expression and type-check the result with
2654
/// the given expanded type.
2755
///

0 commit comments

Comments
 (0)