Skip to content

Commit b3b7d35

Browse files
committed
[ASTGen] Rename macro related @_cdecl functions to swift_Macros_*
Rename @_cdecl name of functions moved to swiftMacros module. Also move those function decls to dedicated 'Macros.h' header.
1 parent 134c0a1 commit b3b7d35

File tree

6 files changed

+103
-71
lines changed

6 files changed

+103
-71
lines changed

include/swift/Bridging/ASTGen.h

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#ifndef SWIFT_BRIDGING_ASTGEN_H
14+
#define SWIFT_BRIDGING_ASTGEN_H
15+
1316
#include "swift/AST/ASTBridging.h"
1417
#include "swift/Parse/ParseBridging.h"
1518

@@ -61,51 +64,6 @@ void swift_ASTGen_buildTopLevelASTNodes(
6164

6265
void swift_ASTGen_freeBridgedString(BridgedStringRef);
6366

64-
void *_Nonnull swift_ASTGen_resolveExternalMacro(
65-
const char *_Nonnull moduleName, const char *_Nonnull typeName,
66-
void *_Nonnull opaquePluginHandle);
67-
void swift_ASTGen_destroyExternalMacro(void *_Nonnull macro);
68-
69-
bool swift_ASTGen_checkDefaultArgumentMacroExpression(
70-
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
71-
const void *_Nonnull macroSourceLocation);
72-
73-
ptrdiff_t swift_ASTGen_checkMacroDefinition(
74-
void *_Nonnull diagEngine, BridgedStringRef sourceFileBuffer,
75-
BridgedStringRef macroDeclText,
76-
BridgedStringRef *_Nonnull expansionSourceOutPtr,
77-
ptrdiff_t *_Nullable *_Nonnull replacementsPtr,
78-
ptrdiff_t *_Nonnull numReplacements,
79-
ptrdiff_t *_Nullable *_Nonnull genericReplacementsPtr,
80-
ptrdiff_t *_Nonnull numGenericReplacements);
81-
void swift_ASTGen_freeExpansionReplacements(
82-
ptrdiff_t *_Nullable replacementsPtr, ptrdiff_t numReplacements);
83-
84-
ptrdiff_t swift_ASTGen_expandFreestandingMacro(
85-
void *_Nonnull diagEngine, const void *_Nonnull macro,
86-
const char *_Nonnull discriminator, uint8_t rawMacroRole,
87-
void *_Nonnull sourceFile, const void *_Nullable sourceLocation,
88-
BridgedStringRef *_Nonnull evaluatedSourceOut);
89-
90-
ptrdiff_t swift_ASTGen_expandAttachedMacro(
91-
void *_Nonnull diagEngine, const void *_Nonnull macro,
92-
const char *_Nonnull discriminator, const char *_Nonnull qualifiedType,
93-
const char *_Nonnull conformances, uint8_t rawMacroRole,
94-
void *_Nonnull customAttrSourceFile,
95-
const void *_Nullable customAttrSourceLocation,
96-
void *_Nonnull declarationSourceFile,
97-
const void *_Nullable declarationSourceLocation,
98-
void *_Nullable parentDeclSourceFile,
99-
const void *_Nullable parentDeclSourceLocation,
100-
BridgedStringRef *_Nonnull evaluatedSourceOut);
101-
102-
bool swift_ASTGen_initializePlugin(void *_Nonnull handle,
103-
void *_Nullable diagEngine);
104-
void swift_ASTGen_deinitializePlugin(void *_Nonnull handle);
105-
bool swift_ASTGen_pluginServerLoadLibraryPlugin(
106-
void *_Nonnull handle, const char *_Nonnull libraryPath,
107-
const char *_Nonnull moduleName, BridgedStringRef *_Nullable errorOut);
108-
10967
/// Build a TypeRepr for AST node for the type at the given source location in
11068
/// the specified file.
11169
swift::TypeRepr *_Nullable swift_ASTGen_buildTypeRepr(
@@ -162,3 +120,5 @@ void swift_ASTGen_freeConfiguredRegions(
162120
#ifdef __cplusplus
163121
}
164122
#endif
123+
124+
#endif // SWIFT_BRIDGING_ASTGEN_H

include/swift/Bridging/Macros.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===--- Macros.h -----------------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BRIDGING_MACROS_H
14+
#define SWIFT_BRIDGING_MACROS_H
15+
16+
#include "swift/Basic/BasicBridging.h"
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
void *_Nonnull swift_Macros_resolveExternalMacro(
23+
const char *_Nonnull moduleName, const char *_Nonnull typeName,
24+
void *_Nonnull opaquePluginHandle);
25+
void swift_Macros_destroyExternalMacro(void *_Nonnull macro);
26+
27+
bool swift_Macros_checkDefaultArgumentMacroExpression(
28+
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
29+
const void *_Nonnull macroSourceLocation);
30+
31+
ptrdiff_t swift_Macros_checkMacroDefinition(
32+
void *_Nonnull diagEngine, BridgedStringRef sourceFileBuffer,
33+
BridgedStringRef macroDeclText,
34+
BridgedStringRef *_Nonnull expansionSourceOutPtr,
35+
ptrdiff_t *_Nullable *_Nonnull replacementsPtr,
36+
ptrdiff_t *_Nonnull numReplacements,
37+
ptrdiff_t *_Nullable *_Nonnull genericReplacementsPtr,
38+
ptrdiff_t *_Nonnull numGenericReplacements);
39+
void swift_Macros_freeExpansionReplacements(
40+
ptrdiff_t *_Nullable replacementsPtr, ptrdiff_t numReplacements);
41+
42+
ptrdiff_t swift_Macros_expandFreestandingMacro(
43+
void *_Nonnull diagEngine, const void *_Nonnull macro,
44+
const char *_Nonnull discriminator, uint8_t rawMacroRole,
45+
void *_Nonnull sourceFile, const void *_Nullable sourceLocation,
46+
BridgedStringRef *_Nonnull evaluatedSourceOut);
47+
48+
ptrdiff_t swift_Macros_expandAttachedMacro(
49+
void *_Nonnull diagEngine, const void *_Nonnull macro,
50+
const char *_Nonnull discriminator, const char *_Nonnull qualifiedType,
51+
const char *_Nonnull conformances, uint8_t rawMacroRole,
52+
void *_Nonnull customAttrSourceFile,
53+
const void *_Nullable customAttrSourceLocation,
54+
void *_Nonnull declarationSourceFile,
55+
const void *_Nullable declarationSourceLocation,
56+
void *_Nullable parentDeclSourceFile,
57+
const void *_Nullable parentDeclSourceLocation,
58+
BridgedStringRef *_Nonnull evaluatedSourceOut);
59+
60+
bool swift_Macros_initializePlugin(void *_Nonnull handle,
61+
void *_Nullable diagEngine);
62+
void swift_Macros_deinitializePlugin(void *_Nonnull handle);
63+
bool swift_Macros_pluginServerLoadLibraryPlugin(
64+
void *_Nonnull handle, const char *_Nonnull libraryPath,
65+
const char *_Nonnull moduleName, BridgedStringRef *_Nullable errorOut);
66+
67+
#ifdef __cplusplus
68+
}
69+
#endif
70+
71+
#endif // SWIFT_BRIDGING_MACROS_H

lib/ASTGen/Sources/Macros/Macros.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension MacroRole {
4545
}
4646
}
4747

48-
@_cdecl("swift_ASTGen_resolveExternalMacro")
48+
@_cdecl("swift_Macros_resolveExternalMacro")
4949
public func resolveExternalMacro(
5050
moduleName: UnsafePointer<CChar>,
5151
typeName: UnsafePointer<CChar>,
@@ -65,7 +65,7 @@ public func resolveExternalMacro(
6565
return UnsafeRawPointer(exportedPtr)
6666
}
6767

68-
@_cdecl("swift_ASTGen_destroyExternalMacro")
68+
@_cdecl("swift_Macros_destroyExternalMacro")
6969
public func destroyExternalMacro(
7070
macroPtr: UnsafeMutableRawPointer
7171
) {
@@ -139,7 +139,7 @@ fileprivate func identifierFromStringLiteral(_ node: ExprSyntax) -> String? {
139139
///
140140
/// - Returns: `true` if all restrictions are satisfied, `false` if diagnostics
141141
/// are emitted.
142-
@_cdecl("swift_ASTGen_checkDefaultArgumentMacroExpression")
142+
@_cdecl("swift_Macros_checkDefaultArgumentMacroExpression")
143143
func checkDefaultArgumentMacroExpression(
144144
diagEnginePtr: UnsafeMutableRawPointer,
145145
sourceFilePtr: UnsafeRawPointer,
@@ -196,7 +196,7 @@ func checkDefaultArgumentMacroExpression(
196196
/// (start offset, end offset, parameter index): the [start offset, end offset)
197197
/// range in the macro expansion expression should be replaced with the
198198
/// argument matching the corresponding parameter.
199-
@_cdecl("swift_ASTGen_checkMacroDefinition")
199+
@_cdecl("swift_Macros_checkMacroDefinition")
200200
func checkMacroDefinition(
201201
diagEnginePtr: UnsafeMutableRawPointer,
202202
sourceFileBuffer: BridgedStringRef,
@@ -399,7 +399,7 @@ func checkMacroDefinition(
399399
}
400400
}
401401

402-
@_cdecl("swift_ASTGen_freeExpansionReplacements")
402+
@_cdecl("swift_Macros_freeExpansionReplacements")
403403
public func freeExpansionReplacements(
404404
pointer: UnsafeMutablePointer<Int>?,
405405
numReplacements: Int
@@ -420,7 +420,7 @@ func makeExpansionOutputResult(
420420
return 0
421421
}
422422

423-
@_cdecl("swift_ASTGen_expandFreestandingMacro")
423+
@_cdecl("swift_Macros_expandFreestandingMacro")
424424
@usableFromInline
425425
func expandFreestandingMacro(
426426
diagEnginePtr: UnsafeMutableRawPointer,
@@ -556,7 +556,7 @@ func expandFreestandingMacroImpl(
556556
}
557557
}
558558

559-
@_cdecl("swift_ASTGen_expandAttachedMacro")
559+
@_cdecl("swift_Macros_expandAttachedMacro")
560560
@usableFromInline
561561
func expandAttachedMacro(
562562
diagEnginePtr: UnsafeMutableRawPointer,

lib/ASTGen/Sources/Macros/PluginHost.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ enum PluginError: String, Error, CustomStringConvertible {
2525
var description: String { rawValue }
2626
}
2727

28-
@_cdecl("swift_ASTGen_initializePlugin")
28+
@_cdecl("swift_Macros_initializePlugin")
2929
public func _initializePlugin(
3030
opaqueHandle: UnsafeMutableRawPointer,
3131
cxxDiagnosticEngine: UnsafeMutableRawPointer?
@@ -42,7 +42,7 @@ public func _initializePlugin(
4242
}
4343
}
4444

45-
@_cdecl("swift_ASTGen_deinitializePlugin")
45+
@_cdecl("swift_Macros_deinitializePlugin")
4646
public func _deinitializePlugin(
4747
opaqueHandle: UnsafeMutableRawPointer
4848
) {
@@ -52,8 +52,8 @@ public func _deinitializePlugin(
5252

5353
/// Load the library plugin in the plugin server.
5454
/// This should be called inside lock.
55-
@_cdecl("swift_ASTGen_pluginServerLoadLibraryPlugin")
56-
func swift_ASTGen_pluginServerLoadLibraryPlugin(
55+
@_cdecl("swift_Macros_pluginServerLoadLibraryPlugin")
56+
func swift_Macros_pluginServerLoadLibraryPlugin(
5757
opaqueHandle: UnsafeMutableRawPointer,
5858
libraryPath: UnsafePointer<CChar>,
5959
moduleName: UnsafePointer<CChar>,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#include "swift/Basic/Assertions.h"
5757
#include "swift/Basic/Defer.h"
5858
#include "swift/Basic/Statistic.h"
59-
#include "swift/Bridging/ASTGen.h"
59+
#include "swift/Bridging/Macros.h"
6060
#include "swift/Parse/Lexer.h"
6161
#include "swift/Parse/Parser.h"
6262
#include "swift/Serialization/SerializedModuleLoader.h"
@@ -1153,7 +1153,7 @@ static bool checkExpressionMacroDefaultValueRestrictions(ParamDecl *param) {
11531153
#if SWIFT_BUILD_SWIFT_SYNTAX
11541154
auto *DC = param->getInnermostDeclContext();
11551155
const SourceFile *SF = DC->getParentSourceFile();
1156-
return swift_ASTGen_checkDefaultArgumentMacroExpression(
1156+
return swift_Macros_checkDefaultArgumentMacroExpression(
11571157
&ctx.Diags, SF->getExportedSourceFile(),
11581158
initExpr->getLoc().getOpaquePointerValue());
11591159
#else

lib/Sema/TypeCheckMacros.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/Basic/SourceManager.h"
4141
#include "swift/Basic/StringExtras.h"
4242
#include "swift/Bridging/ASTGen.h"
43+
#include "swift/Bridging/Macros.h"
4344
#include "swift/Demangling/Demangler.h"
4445
#include "swift/Demangling/ManglingMacros.h"
4546
#include "swift/Parse/Lexer.h"
@@ -121,7 +122,7 @@ MacroDefinition MacroDefinitionRequest::evaluate(
121122
ptrdiff_t *genericReplacements = nullptr;
122123
ptrdiff_t numGenericReplacements = 0;
123124

124-
// Parse 'macro' decl in swift_ASTGen_checkMacroDefinition.
125+
// Parse 'macro' decl in swift_Macros_checkMacroDefinition.
125126
// NOTE: We don't use source->getExportedSourceFile() because it parses the
126127
// entire source buffer, which we don't want. Usually 'macro' decl is not in
127128
// the same file as the expansion, so we only want to parse only the decl.
@@ -133,16 +134,16 @@ MacroDefinition MacroDefinitionRequest::evaluate(
133134
SM.extractText(Lexer::getCharSourceRangeFromSourceRange(
134135
SM, macro->getSourceRangeIncludingAttrs()));
135136

136-
auto checkResult = swift_ASTGen_checkMacroDefinition(
137+
auto checkResult = swift_Macros_checkMacroDefinition(
137138
&ctx.Diags, sourceFileText, macroDeclText, &externalMacroName,
138139
&replacements, &numReplacements, &genericReplacements,
139140
&numGenericReplacements);
140141

141142
// Clean up after the call.
142143
SWIFT_DEFER {
143144
swift_ASTGen_freeBridgedString(externalMacroName);
144-
swift_ASTGen_freeExpansionReplacements(replacements, numReplacements);
145-
// swift_ASTGen_freeExpansionGenericReplacements(genericReplacements, numGenericReplacements); // FIXME: !!!!!!
145+
swift_Macros_freeExpansionReplacements(replacements, numReplacements);
146+
// swift_Macros_freeExpansionGenericReplacements(genericReplacements, numGenericReplacements); // FIXME: !!!!!!
146147
};
147148

148149
if (checkResult < 0 && ctx.CompletionCallback) {
@@ -247,20 +248,20 @@ initializePlugin(ASTContext &ctx, CompilerPlugin *plugin, StringRef libraryPath,
247248
// But plugin loading is in libAST and it can't link ASTGen symbols.
248249
if (!plugin->isInitialized()) {
249250
#if SWIFT_BUILD_SWIFT_SYNTAX
250-
if (!swift_ASTGen_initializePlugin(plugin, &ctx.Diags)) {
251+
if (!swift_Macros_initializePlugin(plugin, &ctx.Diags)) {
251252
return llvm::createStringError(llvm::inconvertibleErrorCode(),
252253
"'%s' produced malformed response",
253254
plugin->getPath().data());
254255
}
255256

256257
// Resend the compiler capability on reconnect.
257258
auto *callback = new std::function<void(void)>([plugin]() {
258-
(void)swift_ASTGen_initializePlugin(plugin, /*diags=*/nullptr);
259+
(void)swift_Macros_initializePlugin(plugin, /*diags=*/nullptr);
259260
});
260261
plugin->addOnReconnect(callback);
261262

262263
plugin->setCleanup([plugin, callback] {
263-
swift_ASTGen_deinitializePlugin(plugin);
264+
swift_Macros_deinitializePlugin(plugin);
264265
delete callback;
265266
});
266267
#endif
@@ -281,7 +282,7 @@ initializePlugin(ASTContext &ctx, CompilerPlugin *plugin, StringRef libraryPath,
281282
std::string moduleNameStr(moduleName.str());
282283

283284
BridgedStringRef bridgedErrorOut{nullptr, 0};
284-
bool loaded = swift_ASTGen_pluginServerLoadLibraryPlugin(
285+
bool loaded = swift_Macros_pluginServerLoadLibraryPlugin(
285286
plugin, resolvedLibraryPathStr.c_str(), moduleNameStr.c_str(),
286287
&bridgedErrorOut);
287288

@@ -300,7 +301,7 @@ initializePlugin(ASTContext &ctx, CompilerPlugin *plugin, StringRef libraryPath,
300301
// Set a callback to load the library again on reconnections.
301302
auto *callback = new std::function<void(void)>(
302303
[plugin, resolvedLibraryPathStr, moduleNameStr]() {
303-
(void)swift_ASTGen_pluginServerLoadLibraryPlugin(
304+
(void)swift_Macros_pluginServerLoadLibraryPlugin(
304305
plugin, resolvedLibraryPathStr.c_str(), moduleNameStr.c_str(),
305306
/*errorOut=*/nullptr);
306307
});
@@ -385,10 +386,10 @@ static ExternalMacroDefinition resolveExternalMacro(ASTContext &ctx,
385386
Identifier moduleName,
386387
Identifier typeName) {
387388
#if SWIFT_BUILD_SWIFT_SYNTAX
388-
if (auto *macro = swift_ASTGen_resolveExternalMacro(moduleName.get(),
389+
if (auto *macro = swift_Macros_resolveExternalMacro(moduleName.get(),
389390
typeName.get(), plugin)) {
390391
// Make sure we clean up after the macro.
391-
ctx.addCleanup([macro]() { swift_ASTGen_destroyExternalMacro(macro); });
392+
ctx.addCleanup([macro]() { swift_Macros_destroyExternalMacro(macro); });
392393
return ExternalMacroDefinition::success(macro);
393394
}
394395
// NOTE: this is not reachable because executable macro resolution always
@@ -1163,7 +1164,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
11631164

11641165
BridgedStringRef evaluatedSourceOut{nullptr, 0};
11651166
assert(!externalDef.isError());
1166-
swift_ASTGen_expandFreestandingMacro(
1167+
swift_Macros_expandFreestandingMacro(
11671168
&ctx.Diags, externalDef.get(), discriminator->c_str(),
11681169
getRawMacroRole(macroRole), astGenSourceFile,
11691170
expansion->getSourceRange().Start.getOpaquePointerValue(),
@@ -1487,7 +1488,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
14871488

14881489
BridgedStringRef evaluatedSourceOut{nullptr, 0};
14891490
assert(!externalDef.isError());
1490-
swift_ASTGen_expandAttachedMacro(
1491+
swift_Macros_expandAttachedMacro(
14911492
&ctx.Diags, externalDef.get(), discriminator->c_str(),
14921493
extendedType.c_str(), conformanceList.c_str(), getRawMacroRole(role),
14931494
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),

0 commit comments

Comments
 (0)