22
22
#include " swift/AST/Macro.h"
23
23
#include " swift/AST/SourceFile.h"
24
24
#include " swift/AST/TypeCheckRequests.h"
25
+ #include " swift/Basic/Defer.h"
25
26
#include " swift/Basic/SourceManager.h"
26
27
#include " swift/Basic/StringExtras.h"
27
28
#include " swift/Parse/Lexer.h"
@@ -43,13 +44,17 @@ extern "C" ptrdiff_t swift_ASTGen_evaluateMacro(
43
44
void *sourceFile, const void *sourceLocation,
44
45
const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
45
46
47
+ extern " C" void
48
+ swift_ASTGen_getMacroGenericSignature (void *macro,
49
+ const char **genericSignaturePtr,
50
+ ptrdiff_t *genericSignatureLengthPtr);
51
+
46
52
extern " C" void
47
53
swift_ASTGen_getMacroTypeSignature (void *macro,
48
- const char **evaluationContextPtr ,
49
- ptrdiff_t *evaluationContextLengthPtr );
54
+ const char **signaturePtr ,
55
+ ptrdiff_t *signatureLengthPtr );
50
56
51
- // / Compute the macro signature given the the source containing macro signature
52
- // / source.
57
+ // / Create a new macro signature context buffer describing the macro signature.
53
58
// /
54
59
// / The macro signature is a user-defined generic signature and return
55
60
// / type that serves as the "interface type" of references to the macro. The
@@ -66,14 +71,38 @@ swift_ASTGen_getMacroTypeSignature(void *macro,
66
71
// / facilities to map the parsed signature type back into a semantic
67
72
// / \c GenericSignature and \c Type ASTs. The macro signature source above is
68
73
// / provided via \c signatureSource.
74
+ static NullTerminatedStringRef
75
+ getMacroSignatureContextBuffer (
76
+ ASTContext &ctx, Optional<StringRef> genericSignature,
77
+ StringRef typeSignature
78
+ ) {
79
+ std::string source;
80
+ llvm::raw_string_ostream out (source);
81
+ out << " struct __MacroEvaluationContext"
82
+ << (genericSignature ? *genericSignature : " " ) << " {\n "
83
+ << " typealias SignatureType = " << typeSignature << " \n "
84
+ << " }" ;
85
+ auto len = source.length ();
86
+ auto *buffer = (char *)malloc (len + 1 );
87
+ memcpy (buffer, source.data (), len + 1 );
88
+ return {buffer, len};
89
+ }
90
+
91
+ // / Compute the macro signature for a macro given the source code for its
92
+ // / generic signature and type signature.
69
93
static Optional<std::pair<GenericSignature, Type>>
70
94
getMacroSignature (
71
95
ModuleDecl *mod, Identifier macroName,
72
- NullTerminatedStringRef signatureSource
96
+ Optional<StringRef> genericSignature,
97
+ StringRef typeSignature
73
98
) {
99
+ // Form a buffer containing the macro signature context.
100
+ ASTContext &ctx = mod->getASTContext ();
101
+ StringRef signatureSource = getMacroSignatureContextBuffer (
102
+ ctx, genericSignature, typeSignature);
103
+
74
104
// Create a new source buffer with the contents of the macro's
75
105
// signature.
76
- ASTContext &ctx = mod->getASTContext ();
77
106
SourceManager &sourceMgr = ctx.SourceMgr ;
78
107
std::string bufferName;
79
108
{
@@ -115,20 +144,16 @@ getMacroSignature(
115
144
signature->getGenericSignature (), signature->getUnderlyingType ());
116
145
}
117
146
118
- // / Create a builtin macro.
119
- static Macro *createBuiltinMacro (
120
- ModuleDecl *mod, Identifier macroName, void *opaqueHandle) {
121
- // Form the macro type signature.
122
- const char *evaluatedSourcePtr;
123
- ptrdiff_t evaluatedSourceLength;
124
- swift_ASTGen_getMacroTypeSignature (opaqueHandle, &evaluatedSourcePtr,
125
- &evaluatedSourceLength);
126
-
147
+ // / Create a macro.
148
+ static Macro *createMacro (
149
+ ModuleDecl *mod, Identifier macroName,
150
+ Macro::ImplementationKind implKind,
151
+ Optional<StringRef> genericSignature, StringRef typeSignature,
152
+ void * opaqueHandle
153
+ ) {
127
154
// Get the type signature of the macro.
128
155
auto signature = getMacroSignature (
129
- mod, macroName,
130
- NullTerminatedStringRef (
131
- evaluatedSourcePtr,(size_t )evaluatedSourceLength));
156
+ mod, macroName, genericSignature, typeSignature);
132
157
if (!signature) {
133
158
// FIXME: Swap in ErrorTypes, perhaps?
134
159
return nullptr ;
@@ -137,45 +162,61 @@ static Macro *createBuiltinMacro(
137
162
// FIXME: All macros are expression macros right now
138
163
ASTContext &ctx = mod->getASTContext ();
139
164
return new (ctx) Macro (
140
- Macro::Expression, Macro::ImplementationKind::Builtin , macroName,
141
- signature->first , signature->second , /* FIXME:Documentation */ StringRef (),
165
+ Macro::Expression, implKind , macroName,
166
+ signature->first , signature->second ,
142
167
/* FIXME:owningModule*/ mod, /* FIXME:supplementalImportModules*/ {},
143
168
opaqueHandle);
144
169
}
145
170
146
- static NullTerminatedStringRef
147
- getPluginMacroTypeSignature (CompilerPlugin *plugin, ASTContext &ctx) {
148
- auto genSig = plugin->invokeGenericSignature ();
149
- auto typeSig = plugin->invokeTypeSignature ();
150
- std::string source;
151
- llvm::raw_string_ostream out (source);
152
- out << " struct __MacroEvaluationContext" << (genSig ? *genSig : " " ) << " {\n "
153
- << " typealias SignatureType = " << typeSig << " \n "
154
- << " }" ;
155
- auto len = source.length ();
156
- auto *buffer = (char *)malloc (len + 1 );
157
- memcpy (buffer, source.data (), len + 1 );
158
- return {buffer, len};
171
+ // / Create a builtin macro.
172
+ static Macro *createBuiltinMacro (
173
+ ModuleDecl *mod, Identifier macroName, void *opaqueHandle) {
174
+ // Form the macro generic signature.
175
+ const char *genericSignaturePtr;
176
+ ptrdiff_t genericSignatureLength;
177
+ swift_ASTGen_getMacroGenericSignature (opaqueHandle, &genericSignaturePtr,
178
+ &genericSignatureLength);
179
+ SWIFT_DEFER {
180
+ free ((void *)genericSignaturePtr);
181
+ };
182
+
183
+ Optional<StringRef> genericSignature;
184
+ if (genericSignaturePtr && genericSignatureLength)
185
+ genericSignature = StringRef (genericSignaturePtr, genericSignatureLength);
186
+
187
+ // Form the macro type signature.
188
+ const char *typeSignaturePtr;
189
+ ptrdiff_t typeSignatureLength;
190
+ swift_ASTGen_getMacroTypeSignature (opaqueHandle, &typeSignaturePtr,
191
+ &typeSignatureLength);
192
+ SWIFT_DEFER {
193
+ free ((void *)typeSignaturePtr);
194
+ };
195
+ StringRef typeSignature = StringRef (typeSignaturePtr, typeSignatureLength);
196
+
197
+ return createMacro (
198
+ mod, macroName, Macro::ImplementationKind::Builtin,
199
+ genericSignature, typeSignature,
200
+ opaqueHandle);
159
201
}
160
202
161
203
// / Create a plugin-based macro.
162
204
static Macro *createPluginMacro (
163
205
ModuleDecl *mod, Identifier macroName, CompilerPlugin *plugin) {
164
- // Get the type signature of the macro.
165
- ASTContext &ctx = mod->getASTContext ();
166
- auto signature = getMacroSignature (
167
- mod, macroName, getPluginMacroTypeSignature (plugin, ctx));
168
- if (!signature) {
169
- // FIXME: Swap in ErrorTypes, perhaps?
170
- return nullptr ;
171
- }
206
+ auto genSignature = plugin->invokeGenericSignature ();
207
+ SWIFT_DEFER {
208
+ if (genSignature)
209
+ free ((void *)genSignature->data ());
210
+ };
172
211
173
- // FIXME: All macros are expression macros right now
174
- return new (ctx) Macro (
175
- Macro::Expression, Macro::ImplementationKind::Plugin, macroName,
176
- signature->first , signature->second , /* FIXME:Documentation*/ StringRef (),
177
- /* FIXME:owningModule*/ mod, /* FIXME:supplementalImportModules*/ {},
178
- plugin);
212
+ auto typeSignature = plugin->invokeTypeSignature ();
213
+ SWIFT_DEFER {
214
+ free ((void *)typeSignature.data ());
215
+ };
216
+
217
+ return createMacro (
218
+ mod, macroName, Macro::ImplementationKind::Plugin, genSignature,
219
+ typeSignature, plugin);
179
220
}
180
221
181
222
ArrayRef<Macro *> MacroLookupRequest::evaluate (
0 commit comments