Skip to content

Commit 44a5dd1

Browse files
authored
Merge pull request swiftlang#76833 from rintaro/swiftmacros-module
[ASTGen] Move macros related code to a new dedicated module
2 parents bf41473 + b3b7d35 commit 44a5dd1

File tree

13 files changed

+349
-295
lines changed

13 files changed

+349
-295
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/CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ add_pure_swift_host_library(swiftASTGen STATIC
3131
Sources/ASTGen/Generics.swift
3232
Sources/ASTGen/LegacyParse.swift
3333
Sources/ASTGen/Literals.swift
34-
Sources/ASTGen/Macros.swift
3534
Sources/ASTGen/ParameterClause.swift
3635
Sources/ASTGen/Patterns.swift
37-
Sources/ASTGen/PluginHost.swift
3836
Sources/ASTGen/Regex.swift
3937
Sources/ASTGen/SourceFile.swift
40-
Sources/ASTGen/SourceManager.swift
4138
Sources/ASTGen/Stmts.swift
4239
Sources/ASTGen/TypeAttrs.swift
4340
Sources/ASTGen/Types.swift
@@ -51,19 +48,34 @@ add_pure_swift_host_library(swiftASTGen STATIC
5148
_CompilerSwiftSyntaxBuilder
5249
_CompilerSwiftParser
5350
_CompilerSwiftParserDiagnostics
54-
_CompilerSwiftCompilerPluginMessageHandling
55-
_CompilerSwiftSyntaxMacroExpansion
5651
_CompilerSwiftDiagnostics
57-
_CompilerSwiftIDEUtils
5852
${ASTGen_Swift_dependencies}
5953
)
6054

55+
add_pure_swift_host_library(swiftMacros STATIC
56+
Sources/Macros/Macros.swift
57+
Sources/Macros/PluginHost.swift
58+
Sources/Macros/SourceManager.swift
59+
60+
DEPENDENCIES
61+
swiftAST
62+
SWIFT_DEPENDENCIES
63+
_CompilerSwiftCompilerPluginMessageHandling
64+
_CompilerSwiftDiagnostics
65+
_CompilerSwiftOperators
66+
_CompilerSwiftParser
67+
_CompilerSwiftSyntax
68+
_CompilerSwiftSyntaxMacroExpansion
69+
swiftASTGen
70+
)
71+
6172
add_pure_swift_host_library(swiftIDEUtilsBridging
6273
Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift
6374

6475
DEPENDENCIES
6576
swiftAST
6677
SWIFT_DEPENDENCIES
78+
_CompilerSwiftIDEUtils
6779
_CompilerSwiftSyntax
6880
swiftASTGen
6981
)
@@ -111,7 +123,7 @@ else()
111123
endif()
112124

113125
if(SWIFT_BUILD_SWIFT_SYNTAX)
114-
foreach(target swiftASTGen swiftIDEUtilsBridging)
126+
foreach(target swiftASTGen swiftIDEUtilsBridging swiftMacros)
115127
target_compile_options(${target} PRIVATE ${compile_options})
116128
endforeach()
117129
endif()

lib/ASTGen/Package.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ let package = Package(
5454
.target(
5555
name: "swiftASTGen",
5656
dependencies: [
57-
.product(name: "_SwiftCompilerPluginMessageHandling", package: "swift-syntax"),
5857
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
5958
.product(name: "SwiftIfConfig", package: "swift-syntax"),
6059
.product(name: "SwiftOperators", package: "swift-syntax"),
@@ -68,6 +67,20 @@ let package = Package(
6867
path: "Sources/ASTGen",
6968
swiftSettings: swiftSetttings
7069
),
70+
.target(
71+
name: "swiftMacros",
72+
dependencies: [
73+
"swiftASTGen",
74+
.product(name: "_SwiftCompilerPluginMessageHandling", package: "swift-syntax"),
75+
.product(name: "SwiftSyntax", package: "swift-syntax"),
76+
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
77+
.product(name: "SwiftParser", package: "swift-syntax"),
78+
.product(name: "SwiftOperators", package: "swift-syntax"),
79+
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
80+
],
81+
path: "Sources/Macros",
82+
swiftSettings: swiftSetttings
83+
),
7184
.target(
7285
name: "swiftIDEUtilsBridging",
7386
dependencies: [

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import ASTBridging
1414
import BasicBridging
1515
@_spi(RawSyntax) import SwiftSyntax
1616

17-
protocol BridgedNullable: ExpressibleByNilLiteral {
17+
public protocol BridgedNullable: ExpressibleByNilLiteral {
1818
associatedtype RawPtr
1919
init(raw: RawPtr?)
2020
}
@@ -105,7 +105,7 @@ extension String {
105105
)
106106
}
107107

108-
mutating func withBridgedString<R>(_ body: (BridgedStringRef) throws -> R) rethrows -> R {
108+
public mutating func withBridgedString<R>(_ body: (BridgedStringRef) throws -> R) rethrows -> R {
109109
try withUTF8 { buffer in
110110
try body(BridgedStringRef(data: buffer.baseAddress, count: buffer.count))
111111
}
@@ -119,7 +119,7 @@ extension SyntaxText {
119119
}
120120

121121
/// Allocate a copy of the given string as a null-terminated UTF-8 string.
122-
func allocateBridgedString(
122+
public func allocateBridgedString(
123123
_ string: String
124124
) -> BridgedStringRef {
125125
var string = string
@@ -143,12 +143,6 @@ public func freeBridgedString(bridged: BridgedStringRef) {
143143
bridged.data?.deallocate()
144144
}
145145

146-
extension BridgedStringRef {
147-
var isEmptyInitialized: Bool {
148-
return self.data == nil && self.count == 0
149-
}
150-
}
151-
152146
extension BridgedStringRef: /*@retroactive*/ Swift.ExpressibleByStringLiteral {
153147
public init(stringLiteral str: StaticString) {
154148
self.init(data: str.utf8Start, count: str.utf8CodeUnitCount)

0 commit comments

Comments
 (0)